From 5636c66fc29f907a6c994c4aeea5adecbb4cefdb Mon Sep 17 00:00:00 2001 From: Daniel Horrigan Date: Tue, 29 Jan 2013 11:41:01 -0500 Subject: [PATCH] Fixing a whole bunch of formatting and bugs. Still a lot to do. --- src/OAuth/Client/IDP.php | 58 +++++++------------ src/OAuth/Client/Provider.php | 7 ++- .../{Token.php => Token/AbstractToken.php} | 31 +++++----- src/OAuth/Client/Token/Access.php | 21 +++---- src/OAuth/Client/Token/Authorize.php | 14 ++--- 5 files changed, 56 insertions(+), 75 deletions(-) rename src/OAuth/Client/{Token.php => Token/AbstractToken.php} (61%) mode change 100755 => 100644 diff --git a/src/OAuth/Client/IDP.php b/src/OAuth/Client/IDP.php index 092da0e..e4b9ced 100644 --- a/src/OAuth/Client/IDP.php +++ b/src/OAuth/Client/IDP.php @@ -3,6 +3,8 @@ namespace OAuth2\Client; use Guzzle\Service\Client as GuzzleClient; +use OAuth2\Client\Token\Access as AccessToken; +use OAuth2\Client\Token\Authorize as AuthorizeToken; class IDPException extends \Exception { @@ -110,12 +112,12 @@ abstract class IDP { setcookie($this->name.'_authorize_state', $state); $params = array( - 'client_id' => $this->clientId, - 'redirect_uri' => $this->redirectUri, - 'state' => $state, - 'scope' => is_array($this->scope) ? implode($this->scopeSeperator, $this->scope) : $this->scope, - 'response_type' => isset($options['response_type']) ? $options['response_type'] : 'code', - 'approval_prompt' => 'force' // - google force-recheck + 'client_id' => $this->clientId, + 'redirect_uri' => $this->redirectUri, + 'state' => $state, + 'scope' => is_array($this->scope) ? implode($this->scopeSeperator, $this->scope) : $this->scope, + 'response_type' => isset($options['response_type']) ? $options['response_type'] : 'code', + 'approval_prompt' => 'force' // - google force-recheck ); header('Location: ' . $this->urlAuthorize().'?'.http_build_query($params)); @@ -124,7 +126,7 @@ abstract class IDP { public function getAccessToken($code = NULL, $options = array()) { - if ($code === NULL) { + if (is_null($code)) { throw new \BadMethodCallException('Missing authorization code'); } @@ -135,58 +137,40 @@ abstract class IDP { ); switch ($params['grant_type']) { - case 'authorization_code': $params['code'] = $code; $params['redirect_uri'] = isset($options['redirectUri']) ? $options['redirectUri'] : $this->redirectUri; - break; - + break; case 'refresh_token': $params['refresh_token'] = $code; - break; - + break; } try { - switch ($this->method) { - case 'get': - $client = new GuzzleClient($this->urlAccessToken() . '?' . http_build_query($params)); $request = $client->send(); $response = $request->getBody(); - break; - case 'post': - $client = new GuzzleClient($this->urlAccessToken()); $request = $client->post(null, null, $params)->send(); $response = $request->getBody(); - break; - } - - } - - catch (\Guzzle\Http\Exception\BadResponseException $e) - { + } catch (\Guzzle\Http\Exception\BadResponseException $e) { $raw_response = explode("\n", $e->getResponse()); $response = end($raw_response); } switch ($this->responseType) { - case 'json': $result = json_decode($response, true); - break; - + break; case 'string': parse_str($response, $result); - break; - + break; } if (isset($result['error']) && ! empty($result['error'])) { @@ -196,19 +180,17 @@ abstract class IDP { } switch ($params['grant_type']) { - case 'authorization_code': - return \OAuth2\Client\Token::factory('access', $result); - break; - - case 'refresh_token': - return \OAuth2\Client\Token::factory('refresh', $result); - break; + return new AccessToken($result); + // TODO: implement refresh_token + // case 'refresh_token': + // return new RefreshToken($result); + // break; } } - public function getUserDetails(\OAuth2\Client\Token\Access $token) + public function getUserDetails(AccessToken $token) { $url = $this->urlUserDetails($token); diff --git a/src/OAuth/Client/Provider.php b/src/OAuth/Client/Provider.php index d50aee3..e4d04fe 100755 --- a/src/OAuth/Client/Provider.php +++ b/src/OAuth/Client/Provider.php @@ -2,16 +2,17 @@ namespace OAuth2\Client; +use InvalidArgumentException; + class Provider { private function __constuct() {} public static function factory($name, array $options = null) { + $name = 'OAuth2\\Client\\Provider\\'.ucfirst($name); if ( ! class_exists($name)) { - - throw new OAuth2\Client\Exception('There is no identity provider called: '.$name); - + throw new InvalidArgumentException('There is no identity provider called: '.$name); } return new $name($options); diff --git a/src/OAuth/Client/Token.php b/src/OAuth/Client/Token/AbstractToken.php old mode 100755 new mode 100644 similarity index 61% rename from src/OAuth/Client/Token.php rename to src/OAuth/Client/Token/AbstractToken.php index 1186e9e..26f1ce1 --- a/src/OAuth/Client/Token.php +++ b/src/OAuth/Client/Token/AbstractToken.php @@ -2,9 +2,10 @@ namespace OAuth2\Client; -abstract class Token -{ +use InvalidArgumentException; +abstract class AbstractToken +{ /** * Create a new token object. * @@ -14,24 +15,14 @@ abstract class Token */ public static function factory($name = 'access', array $options = null) { - include_once 'Token/'.ucfirst(strtolower($name)).'.php'; - - $class = 'OAuth2\Client\Token\\'.ucfirst($name); + $class = 'OAuth2\\Client\\Token\\'.ucfirst($name); + if ( ! class_exists($name)) { + throw new InvalidArgumentException('Invalide token type: '.$name); + } return new $class($options); } - /** - * Return the value of any protected class variable. - * - * @param string variable name - * @return mixed - */ - public function __get($key) - { - return $this->$key; - } - /** * Return a boolean if the property is set * @@ -43,4 +34,10 @@ abstract class Token return isset($this->$key); } -} // End Token + /** + * Return the token string. + * + * @return string + */ + public function __toString(); +} diff --git a/src/OAuth/Client/Token/Access.php b/src/OAuth/Client/Token/Access.php index 13ba1b0..1e8c4ef 100755 --- a/src/OAuth/Client/Token/Access.php +++ b/src/OAuth/Client/Token/Access.php @@ -1,7 +1,4 @@ accessToken = $options['access_token']; @@ -76,4 +77,4 @@ class Access extends \OAuth2\Client\Token return (string) $this->accessToken; } -} \ No newline at end of file +} diff --git a/src/OAuth/Client/Token/Authorize.php b/src/OAuth/Client/Token/Authorize.php index e70e5a4..efa47bd 100755 --- a/src/OAuth/Client/Token/Authorize.php +++ b/src/OAuth/Client/Token/Authorize.php @@ -8,7 +8,11 @@ * @copyright (c) 2011 HappyNinjas Ltd */ -class Authorize extends \OAuth2\Client\Token +namespace OAuth2\Client\Token; + +use InvalidArgumentException; + +class Authorize extends AbstractToken { /** * @var string code @@ -29,13 +33,9 @@ class Authorize extends \OAuth2\Client\Token public function __construct(array $options) { if ( ! isset($options['code'])) { - - throw new Exception('Required option not passed: code'); - + throw new InvalidArgumentException('Required option not passed: code'); } elseif ( ! isset($options['redirect_uri'])) { - - throw new Exception('Required option not passed: redirect_uri'); - + throw new InvalidArgumentException('Required option not passed: redirect_uri'); } $this->code = $options['code'];