Epic rewriting to use Guzzle

Signed-off-by: Alex Bilbie <alex@alexbilbie.com>
This commit is contained in:
Alex Bilbie
2012-10-05 15:05:27 +01:00
parent bef7a212e3
commit a76fbcd23d

210
src/Oauth2/Client/IDP.php Executable file → Normal file
View File

@@ -1,143 +1,64 @@
<?php <?php
namespace Oauth2\Client; namespace OAuth2\Client;
/** use Guzzle\Service\Client;
* OAuth2 Provider
* class IDPException extends \Exception {}
* @package CodeIgniter/OAuth2
* @category Provider class IDP {
* @author Phil Sturgeon
* @copyright (c) 2012 HappyNinjas Ltd public $clientId = '';
* @license http://philsturgeon.co.uk/code/dbad-license
*/ public $clientSecret = '';
public $redirectUri = '';
abstract class IDP
{
/**
* @var string provider name
*/
public $name; public $name;
/**
* @var string uid key name
*/
public $uidKey = 'uid'; public $uidKey = 'uid';
/** public $scopes = array();
* @var string additional request parameters to be used for remote requests
*/
public $callback;
/** public $method = 'post';
* @var array additional request parameters to be used for remote requests
*/
protected $params = array();
/** public $scopeSeperator = ',';
* @var string the method to use when requesting tokens
*/
protected $method = 'GET';
/** public $responseType = 'json';
* @var string default scope (useful if a scope is required for user info)
*/
protected $scope;
/** public function __construct()
* @var string scope separator, most use "," but some like Google are spaces
*/
protected $scopeSeperator = ',';
/**
* Overloads default class properties from the options.
*
* Any of the provider options can be set here, such as app_id or secret.
*
* @param array provider options
* @return void
*/
public function __construct(array $options = array())
{ {
if ( ! $this->name) //$this->redirectUri = $_SERVER[]
{
// Attempt to guess the name from the class name
$this->name = strtolower(substr(get_class($this), strlen('OAuth2_Provider_')));
}
if (empty($options['id']))
{
throw new Exception('Required option not provided: id');
}
$this->clientId = $options['id'];
isset($options['callback']) and $this->callback = $options['callback'];
isset($options['secret']) and $this->client_secret = $options['secret'];
isset($options['scope']) and $this->scope = $options['scope'];
$this->redirectUri = site_url(get_instance()->uri->uri_string());
} }
/**
* Return the value of any protected class variable.
*
* // Get the provider signature
* $signature = $provider->signature;
*
* @param string variable name
* @return mixed
*/
public function __get($key) public function __get($key)
{ {
return $this->$key; return $this->$key;
} }
/**
* Returns the authorization URL for the provider.
*
* $url = $provider->urlAuthorize();
*
* @return string
*/
abstract public function urlAuthorize(); abstract public function urlAuthorize();
/**
* Returns the access token endpoint for the provider.
*
* $url = $provider->urlAccessToken();
*
* @return string
*/
abstract public function urlAccessToken(); abstract public function urlAccessToken();
/*
* Get an authorization code from Facebook. Redirects to Facebook, which this redirects back to the app using the redirect address you've set.
*/
public function authorize($options = array()) public function authorize($options = array())
{ {
$state = md5(uniqid(rand(), TRUE)); $state = md5(uniqid(rand(), TRUE));
get_instance()->session->set_userdata('state', $state); setcookie($this->name.'_authorize_state', $state);
$params = array( $params = array(
'client_id' => $this->client_id, 'client_id' => $this->clientId,
'redirect_uri' => isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri, 'redirect_uri' => $this->redirectUri,
'state' => $state, 'state' => $state,
'scope' => is_array($this->scope) ? implode($this->scopeSeperator, $this->scope) : $this->scope, 'scope' => is_array($this->scope) ? implode($this->scopeSeperator, $this->scope) : $this->scope,
'response_type' => 'code', 'response_type' => isset($options['response_type'] ? $options['response_type'] : 'code',
'approval_prompt' => 'force' // - google force-recheck 'approval_prompt' => 'force' // - google force-recheck
); );
redirect($this->urlAuthorize().'?'.http_build_query($params)); header('Location: ' . $this->urlAuthorize().'?'.http_build_query($params));
exit;
} }
/* public function getAccessToken()
* Get access to the API
*
* @param string The access code
* @return object Success or failure along with the response details
*/
public function access($code, $options = array())
{ {
$params = array( $params = array(
'client_id' => $this->clientId, 'client_id' => $this->clientId,
@@ -158,72 +79,49 @@ abstract class IDP
} }
$response = null;
$url = $this->urlAccessToken();
switch ($this->method) { switch ($this->method) {
case 'GET': case 'get':
$client = new Client($this->urlAccessToken() .= '?'.http_build_query($params));
// Need to switch to Request library, but need to test it on one that works $response = $client->get();
$url .= '?'.http_build_query($params); break;
$response = file_get_contents($url);
parse_str($response, $return);
break;
case 'POST':
/* $ci = get_instance();
$ci->load->spark('curl/1.2.1');
$ci->curl
->create($url)
->post($params, array('failonerror' => false));
$response = $ci->curl->execute();
*/
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($params),
)
);
$defaultOpts = stream_context_get_params(stream_context_get_default());
$context = stream_context_create(array_merge_recursive($defaultOpts['options'], $opts));
$response = file_get_contents($url, false, $context);
$return = json_decode($response, true);
break;
default: default:
throw new OutOfBoundsException("Method '{$this->method}' must be either GET or POST"); $client = new Client($this->urlAccessToken());
} $response = $client->{$this->method}(null, null, $params);
break;
if ( ! empty($return['error'])) {
throw new OAuth2_Exception($return);
} }
switch ($this->responseType) {
case 'json':
$result = json_decode($response, true);
break;
case 'string':
parse_str($response, $result);
break;
}
if (isset($result['error']) && ! empty($result['error'])) {
throw new Oauth2\Client\IDPException($result);
}
switch ($params['grant_type']) { switch ($params['grant_type']) {
case 'authorization_code': case 'authorization_code':
return OAuth2_Token::factory('access', $return); return Oauth2\Client\Token::factory('access', $result);
break; break;
case 'refresh_token': case 'refresh_token':
return OAuth2_Token::factory('refresh', $return); return Oauth2\Client\Token::factory('refresh', $result);
break; break;
} }
} }
} }