From d6f35f07ae462c571f91058fb98e670b8bff9d31 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Mon, 23 Dec 2013 16:23:56 +0200 Subject: [PATCH] OAuth classes refactored. --- extensions/yii/authclient/BaseOAuth.php | 12 +++---- extensions/yii/authclient/OAuth1.php | 10 +++--- extensions/yii/authclient/OAuth2.php | 6 ++-- extensions/yii/authclient/clients/GoogleOAuth.php | 36 +++++++++++---------- extensions/yii/authclient/clients/Twitter.php | 39 ++++++++++++----------- 5 files changed, 55 insertions(+), 48 deletions(-) diff --git a/extensions/yii/authclient/BaseOAuth.php b/extensions/yii/authclient/BaseOAuth.php index c30ce1c..e23f36e 100644 --- a/extensions/yii/authclient/BaseOAuth.php +++ b/extensions/yii/authclient/BaseOAuth.php @@ -36,19 +36,19 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface * Note: this should be absolute URL (with http:// or https:// leading). * By default current URL will be used. */ - private $_returnUrl = ''; + private $_returnUrl; /** * @var string API base URL. */ - public $apiBaseUrl = ''; + public $apiBaseUrl; /** * @var string authorize URL. */ - public $authUrl = ''; + public $authUrl; /** * @var string auth request scope. */ - public $scope = ''; + public $scope; /** * @var array cURL request options. Option values from this field will overwrite corresponding * values from {@link defaultCurlOptions()}. @@ -57,7 +57,7 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface /** * @var OAuthToken|array access token instance or its array configuration. */ - private $_accessToken = null; + private $_accessToken; /** * @var signature\BaseMethod|array signature method instance or its array configuration. */ @@ -76,7 +76,7 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface */ public function getReturnUrl() { - if (empty($this->_returnUrl)) { + if ($this->_returnUrl === null) { $this->_returnUrl = $this->defaultReturnUrl(); } return $this->_returnUrl; diff --git a/extensions/yii/authclient/OAuth1.php b/extensions/yii/authclient/OAuth1.php index e68118d..3eaf82f 100644 --- a/extensions/yii/authclient/OAuth1.php +++ b/extensions/yii/authclient/OAuth1.php @@ -40,15 +40,15 @@ class OAuth1 extends BaseOAuth /** * @var string OAuth consumer key. */ - public $consumerKey = ''; + public $consumerKey; /** * @var string OAuth consumer secret. */ - public $consumerSecret = ''; + public $consumerSecret; /** * @var string OAuth request token URL. */ - public $requestTokenUrl = ''; + public $requestTokenUrl; /** * @var string request token HTTP method. */ @@ -56,7 +56,7 @@ class OAuth1 extends BaseOAuth /** * @var string OAuth access token URL. */ - public $accessTokenUrl = ''; + public $accessTokenUrl; /** * @var string access token HTTP method. */ @@ -179,7 +179,7 @@ class OAuth1 extends BaseOAuth $curlOptions[CURLOPT_POSTFIELDS] = $params; } $authorizationHeader = $this->composeAuthorizationHeader($params); - if (!empty($authorizationHeader)/* && $this->curlAuthHeader*/) { + if (!empty($authorizationHeader)) { $curlOptions[CURLOPT_HTTPHEADER] = ['Content-Type: application/atom+xml', $authorizationHeader]; } break; diff --git a/extensions/yii/authclient/OAuth2.php b/extensions/yii/authclient/OAuth2.php index b6e4368..09146b8 100644 --- a/extensions/yii/authclient/OAuth2.php +++ b/extensions/yii/authclient/OAuth2.php @@ -40,15 +40,15 @@ class OAuth2 extends BaseOAuth /** * @var string OAuth client ID. */ - public $clientId = ''; + public $clientId; /** * @var string OAuth client secret. */ - public $clientSecret = ''; + public $clientSecret; /** * @var string token request URL endpoint. */ - public $tokenUrl = ''; + public $tokenUrl; /** * Composes user authorization URL. diff --git a/extensions/yii/authclient/clients/GoogleOAuth.php b/extensions/yii/authclient/clients/GoogleOAuth.php index fa56b7f..0d9ab8a 100644 --- a/extensions/yii/authclient/clients/GoogleOAuth.php +++ b/extensions/yii/authclient/clients/GoogleOAuth.php @@ -24,23 +24,27 @@ class GoogleOAuth extends OAuth2 /** * @inheritdoc */ - public function __construct($config = []) + public $authUrl = 'https://accounts.google.com/o/oauth2/auth'; + /** + * @inheritdoc + */ + public $tokenUrl = 'https://accounts.google.com/o/oauth2/token'; + /** + * @inheritdoc + */ + public $apiBaseUrl = 'https://www.googleapis.com/oauth2/v1'; + + /** + * @inheritdoc + */ + public function init() { - $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); + if ($this->scope === null) { + $this->scope = implode(' ', [ + 'https://www.googleapis.com/auth/userinfo.profile', + 'https://www.googleapis.com/auth/userinfo.email', + ]); + } } /** diff --git a/extensions/yii/authclient/clients/Twitter.php b/extensions/yii/authclient/clients/Twitter.php index 4add039..839cfce 100644 --- a/extensions/yii/authclient/clients/Twitter.php +++ b/extensions/yii/authclient/clients/Twitter.php @@ -24,24 +24,27 @@ class Twitter extends OAuth1 /** * @inheritdoc */ - public function __construct($config = []) - { - $config = array_merge( - [ - 'consumerKey' => 'anonymous', - 'consumerSecret' => 'anonymous', - 'requestTokenUrl' => 'https://api.twitter.com/oauth/request_token', - 'requestTokenMethod' => 'POST', - 'accessTokenUrl' => 'https://api.twitter.com/oauth/access_token', - 'accessTokenMethod' => 'POST', - 'authUrl' => 'https://api.twitter.com/oauth/authorize', - 'scope' => '', - 'apiBaseUrl' => 'https://api.twitter.com/1.1', - ], - $config - ); - parent::__construct($config); - } + public $authUrl = 'https://api.twitter.com/oauth/authorize'; + /** + * @inheritdoc + */ + public $requestTokenUrl = 'https://api.twitter.com/oauth/request_token'; + /** + * @inheritdoc + */ + public $requestTokenMethod = 'POST'; + /** + * @inheritdoc + */ + public $accessTokenUrl = 'https://api.twitter.com/oauth/access_token'; + /** + * @inheritdoc + */ + public $accessTokenMethod = 'POST'; + /** + * @inheritdoc + */ + public $apiBaseUrl = 'https://api.twitter.com/1.1'; /** * @inheritdoc