Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Much cleaner API :)

  • Loading branch information...
commit 25f93670bd4ba76e6417dfc66d7ae7a01b67ffac 1 parent d7ed74e
@kiall kiall authored
View
44 classes/kohana/oauth2/consumer.php
@@ -27,36 +27,40 @@
protected $_provider;
- public static function factory($provider, $grant_type, $grant_type_options = array())
+ public static function factory($provider, $user_id = NULL)
{
- return new OAuth2_Consumer($provider, $grant_type, $grant_type_options);
+ return new OAuth2_Consumer($provider, $user_id);
}
-
/**
* Constructor
*/
- public function __construct($provider, $grant_type, $grant_type_options = array())
+ public function __construct($provider, $user_id = NULL)
{
$this->_config = Kohana::$config->load('oauth2.consumer');
$this->_provider = $provider;
- $this->_grant_type = OAuth2_Consumer_GrantType::factory($grant_type, $grant_type_options, $provider);
+ $this->_user_id = $user_id;
+
+ $grant_type = $this->_config[$provider]['grant_type'];
+
+ $this->_grant_type = OAuth2_Consumer_GrantType::factory($grant_type, $provider, $user_id);
}
/**
* Execute an API request
+ *
* @param Request $request
* @param string $user_id
*
* @return Response
*/
- public function execute(Request $request, $user_id = NULL)
+ public function execute(Request $request)
{
- $token = Model_OAuth2_User_Token::find_token($this->_provider, $user_id);
+ $token = Model_OAuth2_User_Token::find_token($this->_provider, $this->_user_id);
// Dont have a token? Lets ask for one..
if ( ! $token->loaded())
{
- $token = $this->_grant_type->request_token($user_id);
+ throw new OAuth2_Exception_InvalidToken('No token avail');
}
// Try to use the token
@@ -75,24 +79,22 @@ public function execute(Request $request, $user_id = NULL)
// Try to exchange a refresh token for an access token
try
{
- $refresh_grant_type = OAuth2_Consumer_GrantType::factory('refresh_token', array(
- 'refresh_token' => $token->refresh_token,
- ), $this->_provider);
+ $refresh_grant_type = OAuth2_Consumer_GrantType::factory('refresh_token', $this->_provider, $this->_user_id);
- $token = $refresh_grant_type->request_token($user_id);
+ $token = $refresh_grant_type->request_token($this->_user_id, array(
+ 'refresh_token' => $token->refresh_token,
+ ));
return $this->_execute($request, $token);
}
catch (OAuth2_Exception_InvalidGrant $e)
{
- // Failure .. Move on
+ throw new OAuth2_Exception_InvalidToken('No token avail');
}
}
// If we get here, our token and refresh token are both expired. Get another.
- $token = $this->_grant_type->request_token($user_id);
-
- return $this->_execute($request, $token);
+ throw new OAuth2_Exception_InvalidToken('No token avail');
}
protected function _execute($request, $token)
@@ -108,4 +110,14 @@ protected function _execute($request, $token)
return $response;
}
+
+ public function request_token($user_id, $grant_type_options = array())
+ {
+ $this->_grant_type->request_token($user_id, $grant_type_options);
+ }
+
+ public function get_grant_type()
+ {
+ return $this->_grant_type;
+ }
}
View
11 classes/kohana/oauth2/consumer/granttype.php
@@ -15,7 +15,7 @@
* @var Config
*/
protected $_config;
-
+
/**
* @var array Request Paramaters
*/
@@ -26,22 +26,21 @@
*/
protected $_provider;
- public static function factory($type, $options, $provider)
+ public static function factory($type, $provider)
{
$class = 'OAuth2_Consumer_GrantType_'.$type;
if ( ! class_exists($class))
throw new OAuth2_Exception_UnsupportedGrantType('Unknown or invalid grant_type');
- return new $class($options, $provider);
+ return new $class($provider);
}
- public function __construct($options, $provider)
+ public function __construct($provider)
{
$this->_config = Kohana::$config->load('oauth2.consumer');
- $this->_options = $options;
$this->_provider = $provider;
}
- abstract public function request_token($user_id = NULL);
+ abstract public function request_token($user_id = NULL, $grant_type_options = array());
}
View
21 classes/kohana/oauth2/consumer/granttype/authorization/code.php
@@ -11,25 +11,20 @@
*/
abstract class Kohana_OAuth2_Consumer_GrantType_Authorization_Code extends OAuth2_Consumer_GrantType {
- public function request_token($user_id = NULL)
+ public function request_token($user_id = NULL, $grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
->post(array(
'grant_type' => 'authorization_code',
- 'code' => $this->_options['code'],
+ 'code' => $grant_type_options['code'],
));
$response = $request->execute();
if ($response->status() != 200)
{
- $e = new OAuth2_Exception_InvalidGrant('Authorization Needed');
-
- // TODO...
- $e->setRedirectUri($this->_config[$this->_provider]['authorize_uri']);
-
- throw $e;
+ throw new OAuth2_Exception_InvalidGrant('Error! .. '.$response->body());
}
switch ($response->headers('content-type'))
@@ -52,4 +47,14 @@ public function request_token($user_id = NULL)
return $token;
}
+ public function get_redirect_uri($state, $response_type = OAuth2::RESPONSE_TYPE_CODE)
+ {
+ $query = http_build_query(array(
+ 'client_id' => $this->_config[$this->_provider]['client_id'],
+ 'redirect_uri' => $this->_config[$this->_provider]['redirect_uri'],
+ 'response_type' => $response_type,
+ ));
+
+ return $this->_config[$this->_provider]['redirect_uri'].'?'.$query;
+ }
}
View
2  classes/kohana/oauth2/consumer/granttype/client/credentials.php
@@ -13,7 +13,7 @@
*/
abstract class Kohana_OAuth2_Consumer_GrantType_Client_Credentials extends OAuth2_Consumer_GrantType {
- public function request_token($user_id = NULL)
+ public function request_token($user_id = NULL, $grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
View
7 classes/kohana/oauth2/consumer/granttype/password.php
@@ -13,7 +13,7 @@
*/
abstract class Kohana_OAuth2_Consumer_GrantType_Password extends OAuth2_Consumer_GrantType {
- public function request_token($user_id = NULL)
+ public function request_token($user_id = NULL, $grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
@@ -21,8 +21,8 @@ public function request_token($user_id = NULL)
'grant_type' => 'password',
'client_id' => $this->_config[$this->_provider]['client_id'],
'client_secret' => $this->_config[$this->_provider]['client_secret'],
- 'username' => $this->_options['username'],
- 'password' => $this->_options['password'],
+ 'username' => $grant_type_options['username'],
+ 'password' => $grant_type_options['password'],
));
$response = $request->execute();
@@ -51,5 +51,4 @@ public function request_token($user_id = NULL)
return $token;
}
-
}
View
4 classes/kohana/oauth2/consumer/granttype/refresh/token.php
@@ -11,7 +11,7 @@
*/
abstract class Kohana_OAuth2_Consumer_GrantType_Refresh_Token extends OAuth2_Consumer_GrantType {
- public function request_token($user_id = NULL)
+ public function request_token($user_id = NULL, $grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
@@ -19,7 +19,7 @@ public function request_token($user_id = NULL)
'grant_type' => 'refresh_token',
'client_id' => $this->_config[$this->_provider]['client_id'],
'client_secret' => $this->_config[$this->_provider]['client_secret'],
- 'refresh_token' => $this->_options['refresh_token'],
+ 'refresh_token' => $grant_type_options['refresh_token'],
));
$response = $request->execute();
View
13 classes/kohana/oauth2/exception/invalidgrant.php
@@ -11,17 +11,4 @@
*/
class Kohana_OAuth2_Exception_InvalidGrant extends OAuth2_Exception {
protected $_error = OAuth2::ERROR_INVALID_GRANT;
-
- protected $_redirect_uri;
-
- public function setRedirectUri($redirect_uri = NULL)
- {
- $this->_redirect_uri = $redirect_uri;
- }
-
- public function getRedirectUri($state = NULL)
- {
- // TODO: State
- return $this->_redirect_uri;
- }
}
Please sign in to comment.
Something went wrong with that request. Please try again.