Paul Klimov
11 years ago
7 changed files with 365 additions and 4 deletions
@ -0,0 +1,48 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\authclient\clients; |
||||
|
||||
use yii\authclient\OAuth2; |
||||
|
||||
/** |
||||
* Facebook allows authentication via Facebook OAuth. |
||||
* In order to use Facebook OAuth you must register your application at [[https://developers.facebook.com/apps]]. |
||||
* |
||||
* @see https://developers.facebook.com/apps |
||||
* @see http://developers.facebook.com/docs/reference/api |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @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'); |
||||
} |
||||
} |
@ -0,0 +1,58 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\authclient\clients; |
||||
|
||||
use yii\authclient\OAuth2; |
||||
|
||||
/** |
||||
* GitHub allows authentication via GitHub OAuth. |
||||
* In order to use GitHub OAuth you must register your application at [[https://github.com/settings/applications/new]]. |
||||
* |
||||
* @see http://developer.github.com/v3/oauth/ |
||||
* @see https://github.com/settings/applications/new |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @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'); |
||||
} |
||||
} |
@ -0,0 +1,133 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\authclient\clients; |
||||
|
||||
use yii\authclient\OAuth2; |
||||
use yii\web\HttpException; |
||||
use Yii; |
||||
|
||||
/** |
||||
* LinkedIn allows authentication via LinkedIn OAuth. |
||||
* In order to use linkedIn OAuth you must register your application at [[https://www.linkedin.com/secure/developer]]. |
||||
* |
||||
* @see http://developer.linkedin.com/documents/authentication |
||||
* @see https://www.linkedin.com/secure/developer |
||||
* @see http://developer.linkedin.com/apis |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @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)); |
||||
} |
||||
} |
@ -0,0 +1,72 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\authclient\clients; |
||||
|
||||
use yii\authclient\OAuth2; |
||||
|
||||
/** |
||||
* YandexOAuth allows authentication via Yandex OAuth. |
||||
* In order to use Yandex OAuth you must register your application at [[https://oauth.yandex.ru/client/new]]. |
||||
* |
||||
* @see https://oauth.yandex.ru/client/new |
||||
* @see http://api.yandex.ru/login/doc/dg/reference/response.xml |
||||
* |
||||
* @author Paul Klimov <klimov.paul@gmail.com> |
||||
* @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'; |
||||
} |
||||
} |
Loading…
Reference in new issue