Browse Source

Auth clients for Facebook, GitHub, LinkedIn added.

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
b1f6354fea
  1. 48
      extensions/yii/authclient/clients/Facebook.php
  2. 58
      extensions/yii/authclient/clients/GitHub.php
  3. 20
      extensions/yii/authclient/clients/GoogleOAuth.php
  4. 19
      extensions/yii/authclient/clients/GoogleOpenId.php
  5. 133
      extensions/yii/authclient/clients/LinkedIn.php
  6. 72
      extensions/yii/authclient/clients/YandexOAuth.php
  7. 19
      extensions/yii/authclient/clients/YandexOpenId.php

48
extensions/yii/authclient/clients/Facebook.php

@ -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');
}
}

58
extensions/yii/authclient/clients/GitHub.php

@ -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');
}
}

20
extensions/yii/authclient/clients/GoogleOAuth.php

@ -39,6 +39,7 @@ class GoogleOAuth extends OAuth2
*/ */
public function init() public function init()
{ {
parent::init();
if ($this->scope === null) { if ($this->scope === null) {
$this->scope = implode(' ', [ $this->scope = implode(' ', [
'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.profile',
@ -52,7 +53,22 @@ class GoogleOAuth extends OAuth2
*/ */
protected function initUserAttributes() protected function initUserAttributes()
{ {
$attributes = $this->api('userinfo', 'GET'); return $this->api('userinfo', 'GET');
return $attributes; }
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'google';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'Google';
} }
} }

19
extensions/yii/authclient/clients/GoogleOpenId.php

@ -10,7 +10,8 @@ namespace yii\authclient\clients;
use yii\authclient\OpenId; 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 <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
@ -54,4 +55,20 @@ class GoogleOpenId extends OpenId
'popupHeight' => 520, 'popupHeight' => 520,
]; ];
} }
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'google';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'Google';
}
} }

133
extensions/yii/authclient/clients/LinkedIn.php

@ -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));
}
}

72
extensions/yii/authclient/clients/YandexOAuth.php

@ -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';
}
}

19
extensions/yii/authclient/clients/YandexOpenId.php

@ -10,7 +10,8 @@ namespace yii\authclient\clients;
use yii\authclient\OpenId; 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 <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
@ -50,4 +51,20 @@ class YandexOpenId extends OpenId
'popupHeight' => 550, 'popupHeight' => 550,
]; ];
} }
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'yandex';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'Yandex';
}
} }
Loading…
Cancel
Save