1.0
Alex Bilbie 2013-02-26 11:33:00 +00:00
parent f5b23e09d4
commit 2a989d9576
8 changed files with 102 additions and 148 deletions

View File

@ -0,0 +1,26 @@
<?php
namespace OAuth2\Client\Grant;
use OAuth2\Client\Token\AccessToken as AccessToken;
class Authorizationcode implements GrantInterface {
public function __toString()
{
return 'authorization_code';
}
public function prepRequestParams($defaultParams, $params)
{
if ( ! isset($params['code']) || empty($params['code'])) {
throw new \BadMethodCallException('Missing authorization code');
}
return array_merge($defaultParams, $params);
}
public function handleResponse($response = array())
{
return new AccessToken($response);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace OAuth2\Client\Grant;
interface GrantInterface {
public function __toString();
public function handleResponse($response = array());
public function prepRequestParams($defaultParams, $params);
}

View File

@ -3,7 +3,7 @@
namespace OAuth2\Client;
use Guzzle\Service\Client as GuzzleClient;
use OAuth2\Client\Token\Access as AccessToken;
use OAuth2\Client\Token\AccessToken as AccessToken;
use OAuth2\Client\Token\Authorize as AuthorizeToken;
abstract class IdentityProvider {
@ -26,7 +26,7 @@ abstract class IdentityProvider {
public $responseType = 'json';
public function __construct($options)
public function __construct($options = array())
{
foreach ($options as $option => $value) {
if (isset($this->{$option})) {
@ -39,9 +39,9 @@ abstract class IdentityProvider {
abstract public function urlAccessToken();
abstract public function urlUserDetails(\OAuth2\Client\Token\Access $token);
abstract public function urlUserDetails(\OAuth2\Client\Token\AccessToken $token);
abstract public function userDetails($response, \OAuth2\Client\Token\Access $token);
abstract public function userDetails($response, \OAuth2\Client\Token\AccessToken $token);
public function authorize($options = array())
{
@ -49,11 +49,11 @@ abstract class IdentityProvider {
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',
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUri,
'state' => $state,
'scope' => is_array($this->scopes) ? implode($this->scopeSeperator, $this->scopes) : $this->scopes,
'response_type' => isset($options['response_type']) ? $options['response_type'] : 'code',
'approval_prompt' => 'force' // - google force-recheck
);
@ -61,38 +61,37 @@ abstract class IdentityProvider {
exit;
}
public function getAccessToken($code = null, $options = array())
public function getAccessToken($grant = 'authorization_code', $params = array())
{
if (is_null($code)) {
throw new \BadMethodCallException('Missing authorization code');
if (is_string($grant)) {
$grant = 'OAuth2\\Client\\Grant\\'.ucfirst(str_replace('_', '', $grant));
if ( ! class_exists($grant)) {
throw new \InvalidArgumentException('Unknown grant "'.$grant.'"');
}
$grant = new $grant;
} elseif ( ! $grant instanceof Grant\GrantInterface) {
throw new \InvalidArgumentException($grant.' is not an instance of \OAuth2\Client\Grant\GrantInterface');
}
$params = array(
$defaultParams = array(
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => isset($options['grantType']) ? $options['grantType'] : 'authorization_code',
'redirect_uri' => $this->redirectUri,
'grant_type' => $grant,
);
switch ($params['grant_type']) {
case 'authorization_code':
$params['code'] = $code;
$params['redirect_uri'] = isset($options['redirectUri']) ? $options['redirectUri'] : $this->redirectUri;
break;
case 'refresh_token':
$params['refresh_token'] = $code;
break;
}
$requestParams = $grant->prepRequestParams($defaultParams, $params);
try {
switch ($this->method) {
case 'get':
$client = new GuzzleClient($this->urlAccessToken() . '?' . http_build_query($params));
$client = new GuzzleClient($this->urlAccessToken() . '?' . http_build_query($requestParams));
$request = $client->send();
$response = $request->getBody();
break;
case 'post':
$client = new GuzzleClient($this->urlAccessToken());
$request = $client->post(null, null, $params)->send();
$request = $client->post(null, null, $requestParams)->send();
$response = $request->getBody();
break;
}
@ -111,20 +110,10 @@ abstract class IdentityProvider {
}
if (isset($result['error']) && ! empty($result['error'])) {
throw new \OAuth2\Client\IDPException($result);
throw new \OAuth2\Client\Exception\IDPException($result);
}
switch ($params['grant_type']) {
case 'authorization_code':
return new AccessToken($result);
// TODO: implement refresh_token
// case 'refresh_token':
// return new RefreshToken($result);
// break;
}
return $grant->handleResponse($result);
}
public function getUserDetails(AccessToken $token)
@ -141,7 +130,7 @@ abstract class IdentityProvider {
} catch (\Guzzle\Http\Exception\BadResponseException $e) {
$raw_response = explode("\n", $e->getResponse());
throw new \OAuth2\Client\IDPException(end($raw_response));
throw new \OAuth2\Client\Exception\IDPException(end($raw_response));
}
}

View File

@ -8,7 +8,7 @@ class Provider
{
private function __constuct() {}
public static function factory($name, array $options = null)
public static function factory($name, array $options = array())
{
$name = 'OAuth2\\Client\\Provider\\'.ucfirst($name);
if ( ! class_exists($name)) {

View File

@ -5,4 +5,26 @@ use OAuth2\Client;
class UniLincoln extends Client\IdentityProvider {
public $scopes = array('public');
public function urlAuthorize()
{
return 'https://ssotest.online.lincoln.ac.uk/oauth';
}
public function urlAccessToken()
{
return 'https://ssotest.online.lincoln.ac.uk/access_token';
}
public function urlUserDetails(\OAuth2\Client\Token\AccessToken $token)
{
return 'https://n2.online.lincoln.ac.uk/people/me?access_token='.$token;
}
public function userDetails($response, \OAuth2\Client\Token\AccessToken $token)
{
die(var_dump($response));
}
}

View File

@ -1,43 +0,0 @@
<?php
namespace OAuth2\Client;
use InvalidArgumentException;
abstract class AbstractToken
{
/**
* Create a new token object.
*
* @param string token type
* @param array token options
* @return Token
*/
public static function factory($name = 'access', array $options = null)
{
$class = 'OAuth2\\Client\\Token\\'.ucfirst($name);
if ( ! class_exists($name)) {
throw new InvalidArgumentException('Invalide token type: '.$name);
}
return new $class($options);
}
/**
* Return a boolean if the property is set
*
* @param string variable name
* @return bool
*/
public function __isset($key)
{
return isset($this->$key);
}
/**
* Return the token string.
*
* @return string
*/
public function __toString();
}

View File

@ -1,18 +1,9 @@
<?php
/**
* OAuth2 Token
*
* @package OAuth2
* @category Token
* @author Phil Sturgeon
* @copyright (c) 2011 HappyNinjas Ltd
*/
namespace OAuth2\Client\Token;
use InvalidArgumentException;
class Access extends AbstractToken
class AccessToken
{
/**
* @var string accessToken
@ -43,7 +34,8 @@ class Access extends AbstractToken
public function __construct(array $options = null)
{
if ( ! isset($options['access_token'])) {
throw new InvalidArgumentException('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'];
@ -77,4 +69,14 @@ class Access extends AbstractToken
return (string) $this->accessToken;
}
/**
* Return a boolean if the property is set
*
* @param string variable name
* @return bool
*/
public function __isset($key)
{
return isset($this->$key);
}
}

View File

@ -1,55 +0,0 @@
<?php
/**
* OAuth2 Token
*
* @package OAuth2
* @category Token
* @author Phil Sturgeon
* @copyright (c) 2011 HappyNinjas Ltd
*/
namespace OAuth2\Client\Token;
use InvalidArgumentException;
class Authorize extends AbstractToken
{
/**
* @var string code
*/
protected $code;
/**
* @var string redirect_uri
*/
protected $redirectUri;
/**
* Sets the token, expiry, etc values.
*
* @param array token options
* @return void
*/
public function __construct(array $options)
{
if ( ! isset($options['code'])) {
throw new InvalidArgumentException('Required option not passed: code');
} elseif ( ! isset($options['redirect_uri'])) {
throw new InvalidArgumentException('Required option not passed: redirect_uri');
}
$this->code = $options['code'];
$this->redirectUri = $options['redirect_uri'];
}
/**
* Returns the token key.
*
* @return string
*/
public function __toString()
{
return (string) $this->code;
}
}