Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
d8079cc6
Commit
d8079cc6
authored
Dec 25, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Doc comments at "yii\authclient" updated.
parent
52de0405
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
30 deletions
+101
-30
AuthAction.php
extensions/yii/authclient/AuthAction.php
+94
-27
Collection.php
extensions/yii/authclient/Collection.php
+1
-1
Choice.php
extensions/yii/authclient/widgets/Choice.php
+6
-2
No files found.
extensions/yii/authclient/AuthAction.php
View file @
d8079cc6
...
...
@@ -9,13 +9,44 @@ namespace yii\authclient;
use
yii\base\Action
;
use
yii\base\Exception
;
use
yii\base\InvalidConfigException
;
use
yii\base\NotSupportedException
;
use
yii\web\Response
;
use
yii\web\HttpException
;
use
yii\web\NotFoundHttpException
;
use
Yii
;
/**
* Class AuthAction
* AuthAction performs authentication via different auth clients.
* It supports [[OpenId]], [[OAuth1] and [[OAuth2]] client types.
*
* Usage:
* ~~~
* class SiteController extends Controller
* {
* public function actions()
* {
* return [
* 'auth' => [
* 'class' => 'yii\authclient\AuthAction',
* 'successCallback' => [$this, 'successCallback'],
* ],
* ]
* }
*
* public function successCallback($client)
* {
* $atributes = $client->getUserAttributes();
* // user login or signup comes here
* }
* }
* ~~~
*
* Usually authentication via external services is performed inside the popup window.
* This action handles the redirection and closing of popup window correctly.
*
* @see Collection
* @see \yii\authclient\widgets\Choice
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
...
...
@@ -24,14 +55,30 @@ class AuthAction extends Action
{
/**
* @var string name of the auth client collection application component.
* It should point to [[Collection]] instance.
*/
public
$clientCollection
=
'auth'
;
public
$clientCollection
=
'auth
ClientCollection
'
;
/**
* @var string name of the GET param, which is used to passed auth client id to this action.
* Note: watch for the naming, make sure you do not choose name used in some auth protocol.
*/
public
$clientIdGetParamName
=
'authclient'
;
/**
* @var callable PHP callback, which should be triggered in case of successful authentication.
* This callback should accept [[ClientInterface]] instance as an argument.
* For example:
*
* ~~~
* public function onAuthSuccess($client)
* {
* $atributes = $client->getUserAttributes();
* // user login or signup comes here
* }
* ~~~
*
* If this callback returns [[Response]] instance, it will be used as action response,
* otherwise redirection to [[successUrl]] will be performed.
*
*/
public
$successCallback
;
/**
...
...
@@ -42,6 +89,11 @@ class AuthAction extends Action
* @var string the redirect url after unsuccessful authorization (e.g. user canceled).
*/
private
$_cancelUrl
=
''
;
/**
* @var string name or alias of the view file, which should be rendered in order to perform redirection.
* If not set default one will be used.
*/
public
$redirectView
;
/**
* @param string $url successful URL.
...
...
@@ -120,7 +172,7 @@ class AuthAction extends Action
/**
* @param mixed $client auth client instance.
* @return
\yii\web\
Response response instance.
* @return Response response instance.
* @throws \yii\base\NotSupportedException on invalid client.
*/
protected
function
auth
(
$client
)
...
...
@@ -137,12 +189,20 @@ class AuthAction extends Action
}
/**
* @param mixed $provider
* @return \yii\web\Response
* This method is invoked in case of successful authentication via auth client.
* @param ClientInterface $client auth client instance.
* @throws InvalidConfigException on invalid success callback.
* @return Response response instance.
*/
protected
function
authSuccess
(
$
provider
)
protected
function
authSuccess
(
$
client
)
{
call_user_func
(
$this
->
successCallback
,
$provider
);
if
(
!
is_callable
(
$this
->
successCallback
))
{
throw
new
InvalidConfigException
(
'"'
.
get_class
(
$this
)
.
'::successCallback" should be a valid callback.'
);
}
$response
=
call_user_func
(
$this
->
successCallback
,
$client
);
if
(
$response
instanceof
Response
)
{
return
$response
;
}
return
$this
->
redirectSuccess
();
}
...
...
@@ -154,12 +214,16 @@ class AuthAction extends Action
*/
public
function
redirect
(
$url
,
$enforceRedirect
=
true
)
{
$viewFile
=
$this
->
redirectView
;
if
(
$viewFile
===
null
)
{
$viewFile
=
__DIR__
.
DIRECTORY_SEPARATOR
.
'views'
.
DIRECTORY_SEPARATOR
.
'redirect.php'
;
}
else
{
$viewFile
=
Yii
::
getAlias
(
$viewFile
);
}
$viewData
=
[
'url'
=>
$url
,
'enforceRedirect'
=>
$enforceRedirect
,
];
$viewFile
=
__DIR__
.
DIRECTORY_SEPARATOR
.
'views'
.
DIRECTORY_SEPARATOR
.
'redirect.php'
;
$response
=
Yii
::
$app
->
getResponse
();
$response
->
content
=
Yii
::
$app
->
getView
()
->
renderFile
(
$viewFile
,
$viewData
);
return
$response
;
...
...
@@ -192,10 +256,11 @@ class AuthAction extends Action
}
/**
* @param OpenId $client provider instance.
* @return \yii\web\Response action response.
* @throws Exception on failure
* @throws \yii\web\HttpException
* Performs OpenID auth flow.
* @param OpenId $client auth client instance.
* @return Response action response.
* @throws Exception on failure.
* @throws HttpException on failure.
*/
protected
function
authOpenId
(
$client
)
{
...
...
@@ -223,10 +288,11 @@ class AuthAction extends Action
}
/**
* @param OAuth1 $provider
* @return \yii\web\Response
* Performs OAuth1 auth flow.
* @param OAuth1 $client auth client instance.
* @return Response action response.
*/
protected
function
authOAuth1
(
$
provider
)
protected
function
authOAuth1
(
$
client
)
{
// user denied error
if
(
isset
(
$_GET
[
'denied'
]))
{
...
...
@@ -239,24 +305,25 @@ class AuthAction extends Action
if
(
!
isset
(
$oauthToken
))
{
// Get request token.
$requestToken
=
$
provider
->
fetchRequestToken
();
$requestToken
=
$
client
->
fetchRequestToken
();
// Get authorization URL.
$url
=
$
provider
->
buildAuthUrl
(
$requestToken
);
$url
=
$
client
->
buildAuthUrl
(
$requestToken
);
// Redirect to authorization URL.
return
Yii
::
$app
->
getResponse
()
->
redirect
(
$url
);
}
else
{
// Upgrade to access token.
$accessToken
=
$
provider
->
fetchAccessToken
();
return
$this
->
authSuccess
(
$
provider
);
$accessToken
=
$
client
->
fetchAccessToken
();
return
$this
->
authSuccess
(
$
client
);
}
}
/**
* @param OAuth2 $provider
* @return \yii\web\Response
* @throws \yii\base\Exception
* Performs OAuth2 auth flow.
* @param OAuth2 $client auth client instance.
* @return Response action response.
* @throws \yii\base\Exception on failure.
*/
protected
function
authOAuth2
(
$
provider
)
protected
function
authOAuth2
(
$
client
)
{
if
(
isset
(
$_GET
[
'error'
]))
{
if
(
$_GET
[
'error'
]
==
'access_denied'
)
{
...
...
@@ -278,14 +345,14 @@ class AuthAction extends Action
// Get the access_token and save them to the session.
if
(
isset
(
$_GET
[
'code'
]))
{
$code
=
$_GET
[
'code'
];
$token
=
$
provider
->
fetchAccessToken
(
$code
);
$token
=
$
client
->
fetchAccessToken
(
$code
);
if
(
!
empty
(
$token
))
{
return
$this
->
authSuccess
(
$
provider
);
return
$this
->
authSuccess
(
$
client
);
}
else
{
return
$this
->
redirectCancel
();
}
}
else
{
$url
=
$
provider
->
buildAuthUrl
();
$url
=
$
client
->
buildAuthUrl
();
return
Yii
::
$app
->
getResponse
()
->
redirect
(
$url
);
}
}
...
...
extensions/yii/authclient/Collection.php
View file @
d8079cc6
...
...
@@ -18,7 +18,7 @@ use Yii;
*
* ~~~
* 'components' => [
* 'auth' => [
* 'auth
ClientCollection
' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'google' => [
...
...
extensions/yii/authclient/widgets/Choice.php
View file @
d8079cc6
...
...
@@ -29,7 +29,9 @@ use yii\authclient\ClientInterface;
* For example:
*
* ~~~
* <?php $authChoice = yii\authclient\Choice::beginWidget(); ?>
* <?php $authChoice = yii\authclient\Choice::beginWidget([
* 'baseAuthUrl' => ['site/auth']
* ]); ?>
* <ul>
* <?php foreach ($authChoice->getClients() as $client): ?>
* <li><?= $authChoice->clientLink($client); ?></li>
...
...
@@ -38,6 +40,8 @@ use yii\authclient\ClientInterface;
* <?php yii\authclient\Choice::endWidget(); ?>
* ~~~
*
* @see \yii\authclient\AuthAction
*
* @property ClientInterface[] $providers auth providers list.
* @property array $baseAuthUrl configuration for the external services base authentication URL.
*
...
...
@@ -54,7 +58,7 @@ class Choice extends Widget
* @var string name of the auth client collection application component.
* This component will be used to fetch {@link services} value if it is not set.
*/
public
$clientCollection
=
'auth'
;
public
$clientCollection
=
'auth
ClientCollection
'
;
/**
* @var array configuration for the external clients base authentication URL.
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment