From 3c8c85514574f4bdd3c658a6b3c983b40fd3c833 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Mon, 23 Dec 2013 12:07:33 +0200 Subject: [PATCH] 'GoogleOAuth' auth client added. --- extensions/yii/authclient/AuthAction.php | 23 +++++----- extensions/yii/authclient/clients/GoogleOAuth.php | 54 +++++++++++++++++++++++ extensions/yii/authclient/widgets/Choice.php | 2 +- 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 extensions/yii/authclient/clients/GoogleOAuth.php diff --git a/extensions/yii/authclient/AuthAction.php b/extensions/yii/authclient/AuthAction.php index d8af5e2..a7e14cf 100644 --- a/extensions/yii/authclient/AuthAction.php +++ b/extensions/yii/authclient/AuthAction.php @@ -29,7 +29,7 @@ class AuthAction extends Action /** * @var string name of the GET param, which is used to passed auth client id to this action. */ - public $clientIdGetParamName = 'client_id'; + public $clientIdGetParamName = 'authclient'; /** * @var callable PHP callback, which should be triggered in case of successful authentication. */ @@ -119,19 +119,20 @@ class AuthAction extends Action } /** - * @param mixed $provider - * @throws \yii\base\NotSupportedException + * @param mixed $client auth client instance. + * @return \yii\web\Response response instance. + * @throws \yii\base\NotSupportedException on invalid client. */ - protected function auth($provider) + protected function auth($client) { - if ($provider instanceof OpenId) { - return $this->authOpenId($provider); - } elseif ($provider instanceof OAuth2) { - return $this->authOAuth2($provider); - } elseif ($provider instanceof OAuth1) { - return $this->authOAuth1($provider); + if ($client instanceof OpenId) { + return $this->authOpenId($client); + } elseif ($client instanceof OAuth2) { + return $this->authOAuth2($client); + } elseif ($client instanceof OAuth1) { + return $this->authOAuth1($client); } else { - throw new NotSupportedException('Provider "' . get_class($provider) . '" is not supported.'); + throw new NotSupportedException('Provider "' . get_class($client) . '" is not supported.'); } } diff --git a/extensions/yii/authclient/clients/GoogleOAuth.php b/extensions/yii/authclient/clients/GoogleOAuth.php new file mode 100644 index 0000000..fa56b7f --- /dev/null +++ b/extensions/yii/authclient/clients/GoogleOAuth.php @@ -0,0 +1,54 @@ + + * @since 2.0 + */ +class GoogleOAuth extends OAuth2 +{ + /** + * @inheritdoc + */ + public function __construct($config = []) + { + $config = array_merge( + [ + 'clientId' => 'anonymous', + 'clientSecret' => 'anonymous', + 'authUrl' => 'https://accounts.google.com/o/oauth2/auth', + 'tokenUrl' => 'https://accounts.google.com/o/oauth2/token', + 'apiBaseUrl' => 'https://www.googleapis.com/oauth2/v1', + 'scope' => implode(' ', [ + 'https://www.googleapis.com/auth/userinfo.profile', + 'https://www.googleapis.com/auth/userinfo.email', + ]), + ], + $config + ); + parent::__construct($config); + } + + /** + * @inheritdoc + */ + protected function initUserAttributes() + { + $attributes = $this->api('userinfo', 'GET'); + return $attributes; + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/widgets/Choice.php b/extensions/yii/authclient/widgets/Choice.php index 501aa01..3cf2247 100644 --- a/extensions/yii/authclient/widgets/Choice.php +++ b/extensions/yii/authclient/widgets/Choice.php @@ -40,7 +40,7 @@ class Choice extends Widget * @var string name of the GET param , which should be used to passed auth client id to URL * defined by {@link baseAuthUrl}. */ - public $clientIdGetParamName = 'client_id'; + public $clientIdGetParamName = 'authclient'; /** * @var array the HTML attributes that should be rendered in the div HTML tag representing the container element. */