Fixing a whole bunch of formatting and bugs. Still a lot to do.

1.0
Daniel Horrigan 2013-01-29 11:41:01 -05:00
parent a1a13840e2
commit 5636c66fc2
5 changed files with 56 additions and 75 deletions

View File

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

View File

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

View File

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

View File

@ -1,7 +1,4 @@
<?php
namespace OAuth2\Client\Token;
/**
* OAuth2 Token
*
@ -11,27 +8,31 @@ namespace OAuth2\Client\Token;
* @copyright (c) 2011 HappyNinjas Ltd
*/
class Access extends \OAuth2\Client\Token
namespace OAuth2\Client\Token;
use InvalidArgumentException;
class Access extends AbstractToken
{
/**
* @var string accessToken
*/
protected $accessToken;
public $accessToken;
/**
* @var int expires
*/
protected $expires;
public $expires;
/**
* @var string refreshToken
*/
protected $refreshToken;
public $refreshToken;
/**
* @var string uid
*/
protected $uid;
public $uid;
/**
* Sets the token, expiry, etc values.
@ -42,7 +43,7 @@ class Access extends \OAuth2\Client\Token
public function __construct(array $options = null)
{
if ( ! isset($options['access_token'])) {
throw new \BadMethodCallException('Required option not passed: access_token'.PHP_EOL.print_r($options, true));
throw new InvalidArgumentException('Required option not passed: access_token'.PHP_EOL.print_r($options, true));
}
$this->accessToken = $options['access_token'];
@ -76,4 +77,4 @@ class Access extends \OAuth2\Client\Token
return (string) $this->accessToken;
}
}
}

View File

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