From b1f6354fea7e52d185e87c939d4360d7dfe89797 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Wed, 25 Dec 2013 14:07:41 +0200 Subject: [PATCH] Auth clients for Facebook, GitHub, LinkedIn added. --- extensions/yii/authclient/clients/Facebook.php | 48 ++++++++ extensions/yii/authclient/clients/GitHub.php | 58 +++++++++ extensions/yii/authclient/clients/GoogleOAuth.php | 20 +++- extensions/yii/authclient/clients/GoogleOpenId.php | 19 ++- extensions/yii/authclient/clients/LinkedIn.php | 133 +++++++++++++++++++++ extensions/yii/authclient/clients/YandexOAuth.php | 72 +++++++++++ extensions/yii/authclient/clients/YandexOpenId.php | 19 ++- 7 files changed, 365 insertions(+), 4 deletions(-) create mode 100644 extensions/yii/authclient/clients/Facebook.php create mode 100644 extensions/yii/authclient/clients/GitHub.php create mode 100644 extensions/yii/authclient/clients/LinkedIn.php create mode 100644 extensions/yii/authclient/clients/YandexOAuth.php diff --git a/extensions/yii/authclient/clients/Facebook.php b/extensions/yii/authclient/clients/Facebook.php new file mode 100644 index 0000000..7c17c27 --- /dev/null +++ b/extensions/yii/authclient/clients/Facebook.php @@ -0,0 +1,48 @@ + + * @since 2.0 + */ +class Facebook extends OAuth2 +{ + /** + * @inheritdoc + */ + public $authUrl = 'https://www.facebook.com/dialog/oauth'; + /** + * @inheritdoc + */ + public $tokenUrl = 'https://graph.facebook.com/oauth/access_token'; + /** + * @inheritdoc + */ + public $apiBaseUrl = 'https://graph.facebook.com'; + /** + * @inheritdoc + */ + public $scope = 'email'; + + /** + * @inheritdoc + */ + protected function initUserAttributes() + { + return $this->api('me', 'GET'); + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/clients/GitHub.php b/extensions/yii/authclient/clients/GitHub.php new file mode 100644 index 0000000..796b86e --- /dev/null +++ b/extensions/yii/authclient/clients/GitHub.php @@ -0,0 +1,58 @@ + + * @since 2.0 + */ +class GitHub extends OAuth2 +{ + /** + * @inheritdoc + */ + public $authUrl = 'https://github.com/login/oauth/authorize'; + /** + * @inheritdoc + */ + public $tokenUrl = 'https://github.com/login/oauth/access_token'; + /** + * @inheritdoc + */ + public $apiBaseUrl = 'https://api.github.com'; + + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + if ($this->scope === null) { + $this->scope = implode(' ', [ + 'user', + 'user:email', + ]); + } + } + + /** + * @inheritdoc + */ + protected function initUserAttributes() + { + return $this->api('user', 'GET'); + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/clients/GoogleOAuth.php b/extensions/yii/authclient/clients/GoogleOAuth.php index 0d9ab8a..2fcd0c1 100644 --- a/extensions/yii/authclient/clients/GoogleOAuth.php +++ b/extensions/yii/authclient/clients/GoogleOAuth.php @@ -39,6 +39,7 @@ class GoogleOAuth extends OAuth2 */ public function init() { + parent::init(); if ($this->scope === null) { $this->scope = implode(' ', [ 'https://www.googleapis.com/auth/userinfo.profile', @@ -52,7 +53,22 @@ class GoogleOAuth extends OAuth2 */ protected function initUserAttributes() { - $attributes = $this->api('userinfo', 'GET'); - return $attributes; + return $this->api('userinfo', 'GET'); + } + + /** + * @inheritdoc + */ + protected function defaultName() + { + return 'google'; + } + + /** + * @inheritdoc + */ + protected function defaultTitle() + { + return 'Google'; } } \ No newline at end of file diff --git a/extensions/yii/authclient/clients/GoogleOpenId.php b/extensions/yii/authclient/clients/GoogleOpenId.php index 1f8ebb7..cf5a3b4 100644 --- a/extensions/yii/authclient/clients/GoogleOpenId.php +++ b/extensions/yii/authclient/clients/GoogleOpenId.php @@ -10,7 +10,8 @@ namespace yii\authclient\clients; use yii\authclient\OpenId; /** - * Class GoogleOpenId + * GoogleOpenId allows authentication via Google OpenId. + * Unlike Google OAuth you do not need to register your application anywhere in order to use Google OpenId. * * @author Paul Klimov * @since 2.0 @@ -54,4 +55,20 @@ class GoogleOpenId extends OpenId 'popupHeight' => 520, ]; } + + /** + * @inheritdoc + */ + protected function defaultName() + { + return 'google'; + } + + /** + * @inheritdoc + */ + protected function defaultTitle() + { + return 'Google'; + } } \ No newline at end of file diff --git a/extensions/yii/authclient/clients/LinkedIn.php b/extensions/yii/authclient/clients/LinkedIn.php new file mode 100644 index 0000000..c6de848 --- /dev/null +++ b/extensions/yii/authclient/clients/LinkedIn.php @@ -0,0 +1,133 @@ + + * @since 2.0 + */ +class LinkedIn extends OAuth2 +{ + /** + * @inheritdoc + */ + public $authUrl = 'https://www.linkedin.com/uas/oauth2/authorization'; + /** + * @inheritdoc + */ + public $tokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken'; + /** + * @inheritdoc + */ + public $apiBaseUrl = 'https://api.linkedin.com/v1'; + + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + if ($this->scope === null) { + $this->scope = implode(' ', [ + 'r_basicprofile', + 'r_emailaddress', + ]); + } + } + + /** + * @inheritdoc + */ + protected function defaultNormalizeUserAttributeMap() + { + return [ + 'email' => 'email-address', + 'first_name' => 'first-name', + 'last_name' => 'last-name', + ]; + } + + /** + * @inheritdoc + */ + protected function initUserAttributes() + { + $attributeNames = [ + 'id', + 'email-address', + 'first-name', + 'last-name', + 'public-profile-url', + ]; + return $this->api('people/~:(' . implode(',', $attributeNames) . ')', 'GET'); + } + + /** + * @inheritdoc + */ + public function buildAuthUrl(array $params = []) + { + $authState = $this->generateAuthState(); + $this->setState('authState', $authState); + $params['state'] = $authState; + return parent::buildAuthUrl($params); + } + + /** + * @inheritdoc + */ + public function fetchAccessToken($authCode, array $params = []) + { + $authState = $this->getState('authState'); + if (!isset($_REQUEST['state']) || empty($authState) || strcmp($_REQUEST['state'], $authState) !== 0) { + throw new HttpException(400, 'Invalid auth state parameter.'); + } else { + $this->removeState('authState'); + } + return parent::fetchAccessToken($authCode, $params); + } + + /** + * @inheritdoc + */ + protected function apiInternal($accessToken, $url, $method, array $params) + { + $params['oauth2_access_token'] = $accessToken->getToken(); + return $this->sendRequest($method, $url, $params); + } + + /** + * @inheritdoc + */ + protected function defaultReturnUrl() + { + $params = $_GET; + unset($params['code']); + unset($params['state']); + return Yii::$app->getUrlManager()->createAbsoluteUrl(Yii::$app->controller->getRoute(), $params); + } + + /** + * Generates the auth state value. + * @return string auth state value. + */ + protected function generateAuthState() { + return sha1(uniqid(get_class($this), true)); + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/clients/YandexOAuth.php b/extensions/yii/authclient/clients/YandexOAuth.php new file mode 100644 index 0000000..83e1072 --- /dev/null +++ b/extensions/yii/authclient/clients/YandexOAuth.php @@ -0,0 +1,72 @@ + + * @since 2.0 + */ +class YandexOAuth extends OAuth2 +{ + /** + * @inheritdoc + */ + public $authUrl = 'https://oauth.yandex.ru/authorize'; + /** + * @inheritdoc + */ + public $tokenUrl = 'https://oauth.yandex.ru/token'; + /** + * @inheritdoc + */ + public $apiBaseUrl = 'https://login.yandex.ru'; + + /** + * @inheritdoc + */ + protected function initUserAttributes() + { + return $this->api('info', 'GET'); + } + + /** + * @inheritdoc + */ + protected function apiInternal($accessToken, $url, $method, array $params) + { + if (!isset($params['format'])) { + $params['format'] = 'json'; + } + $params['oauth_token'] = $accessToken->getToken(); + return $this->sendRequest($method, $url, $params); + } + + /** + * @inheritdoc + */ + protected function defaultName() + { + return 'yandex'; + } + + /** + * @inheritdoc + */ + protected function defaultTitle() + { + return 'Yandex'; + } +} \ No newline at end of file diff --git a/extensions/yii/authclient/clients/YandexOpenId.php b/extensions/yii/authclient/clients/YandexOpenId.php index 0c848ba..9aedf2d 100644 --- a/extensions/yii/authclient/clients/YandexOpenId.php +++ b/extensions/yii/authclient/clients/YandexOpenId.php @@ -10,7 +10,8 @@ namespace yii\authclient\clients; use yii\authclient\OpenId; /** - * Class YandexOpenId + * YandexOpenId allows authentication via Yandex OpenId. + * Unlike Yandex OAuth you do not need to register your application anywhere in order to use Yandex OpenId. * * @author Paul Klimov * @since 2.0 @@ -50,4 +51,20 @@ class YandexOpenId extends OpenId 'popupHeight' => 550, ]; } + + /** + * @inheritdoc + */ + protected function defaultName() + { + return 'yandex'; + } + + /** + * @inheritdoc + */ + protected function defaultTitle() + { + return 'Yandex'; + } } \ No newline at end of file