Skip to content
Browse files

Leave consumer token storage up to the application.

  • Loading branch information...
1 parent adee2a1 commit 7b5dbd8fcdff61118c2a9ecdd7c7b9fa21bf64d9 @kiall kiall committed Sep 12, 2011
View
107 classes/kohana/model/oauth2/user/token.php
@@ -1,107 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-/**
- * Model to handle oauth2 access tokens
- *
- * @package OAuth2
- * @category Model
- * @author Managed I.T.
- * @copyright (c) 2011 Managed I.T.
- * @license https://github.com/managedit/kohana-oauth2/blob/master/LICENSE.md
- */
-class Kohana_Model_OAuth2_User_Token
- extends Model_OAuth2
- implements Interface_Model_OAuth2_User_Token
-{
- protected $_table_name = 'oauth2_user_tokens';
-
- /**
- * @var array Array of field names
- */
- protected $_fields = array(
- 'id',
- 'provider',
- 'token_type',
- 'access_token',
- 'refresh_token',
- 'user_id',
- );
-
- /**
- * @var integer Token Lifetime in seconds
- */
- public static $lifetime = 900; // 5 Minutes
-
- /**
- * Find an access token
- *
- * @param string $access_token token to find
- * @param int $client_id client to match with
- *
- * @return stdClass
- */
- public static function find_token($provider, $user_id = NULL)
- {
- $query = db::select('*')->from('oauth2_user_tokens')
- ->where('provider', '=', $provider)
- ->order_by('id', 'DESC');
-
- if ($user_id !== NULL)
- {
- $query->where('user_id', '=', $user_id);
- }
-
- $result = $query->as_object('Model_OAuth2_User_Token', array(
- array('loaded' => TRUE, 'saved' => TRUE)
- ))->execute();
-
- if (count($result))
- {
- return $result->current();
- }
- else
- {
- return new Model_OAuth2_User_Token;
- }
- }
-
- /**
- * Create an access token
- *
- * @param int $client_id client id to create with
- * @param int $user_id user id to create with
- * @param int $scope scope to create with
- *
- * @return stdClass
- */
- public static function create_token($provider, $token_type, $access_token, $user_id = NULL, $refresh_token = NULL)
- {
- $token = new Model_OAuth2_User_Token(
- array(
- 'data' => array(
- 'provider' => $provider,
- 'token_type' => $token_type,
- 'access_token' => $access_token,
- 'refresh_token' => $refresh_token,
- 'user_id' => $user_id,
- )
- )
- );
-
- $token->save();
-
- return $token;
- }
-
- /**
- * Deletes an access token
- *
- * @param string $access_token the token to delete
- *
- * @return null
- */
- public static function delete_token($provider, $user_id = NULL)
- {
- return Model_OAuth2_User_Token::find_token($provider, $user_id)->delete();
- }
-}
View
59 classes/kohana/oauth2/consumer.php
@@ -26,86 +26,85 @@
*/
protected $_provider;
+ /**
+ * @var array
+ */
+ protected $_token;
+
- public static function factory($provider, $user_id = FALSE)
+ public static function factory($provider, $token = NULL)
{
- return new OAuth2_Consumer($provider, $user_id);
+ return new OAuth2_Consumer($provider, $token);
}
+
/**
* Constructor
*/
- public function __construct($provider, $user_id = FALSE)
+ public function __construct($provider, $token = NULL)
{
$this->_config = Kohana::$config->load('oauth2.consumer');
$this->_provider = $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);
+ $this->_token = $token;
+ $this->_grant_type = OAuth2_Consumer_GrantType::factory($this->_config[$provider]['grant_type'], $provider);
}
/**
* Execute an API request
*
* @param Request $request
- * @param string $user_id
*
* @return Response
*/
- public function execute(Request $request)
+ public function execute(Request $request, $token = NULL)
{
- $token = Model_OAuth2_User_Token::find_token($this->_provider, $this->_user_id);
+ if ($token != NULL)
+ {
+ $this->_token = $token;
+ }
// Dont have a token? Lets ask for one..
- if ( ! $token->loaded())
+ if ($this->_token === NULL OR ! isset($this->_token['access_token']))
{
- throw new OAuth2_Exception_InvalidToken('No token available for provider \':provider\' and user_id \':user_id\'', array(
- ':provider' => $this->_provider,
- ':user_id' => $this->_user_id,
- ));
+ throw new OAuth2_Exception_InvalidToken('No token available');
}
// Try to use the token
try
{
- return $this->_execute($request, $token);
+ return $this->_execute($request);
}
catch (OAuth2_Exception_InvalidToken $e)
{
// Failure .. Move on
}
// Do we have a refresh token?
- if ($token->refresh_token != NULL)
+ if (isset($token['refresh_token']))
{
// Try to exchange a refresh token for an access token
try
{
- $refresh_grant_type = OAuth2_Consumer_GrantType::factory('refresh_token', $this->_provider, $this->_user_id);
+ $refresh_grant_type = OAuth2_Consumer_GrantType::factory('refresh_token', $this->_provider);
- $token = $refresh_grant_type->request_token($this->_user_id, array(
- 'refresh_token' => $token->refresh_token,
+ $token = $refresh_grant_type->request_token(array(
+ 'refresh_token' => $token['refresh_token'],
));
- return $this->_execute($request, $token);
+ return $this->_execute($request);
}
catch (OAuth2_Exception_InvalidGrant $e)
{
- throw new OAuth2_Exception_InvalidToken('No token available for provider \':provider\' and user_id \':user_id\'', array(
- ':provider' => $this->_provider,
- ':user_id' => $this->_user_id,
- ));
+ throw new OAuth2_Exception_InvalidToken('No token available');
}
}
// If we get here, our token and refresh token are both expired. Get another.
throw new OAuth2_Exception_InvalidToken('No token avail');
}
- protected function _execute($request, $token)
+ protected function _execute($request)
{
- $request->headers('Authorization', $token->token_type.' '.$token->access_token);
+ $request->headers('Authorization', $this->_token['token_type'].' '.$this->_token['access_token']);
$response = $request->execute();
@@ -120,7 +119,9 @@ protected function _execute($request, $token)
public function request_token($grant_type_options = array())
{
- $this->_grant_type->request_token($this->_user_id, $grant_type_options);
+ $this->_token = $this->_grant_type->request_token($grant_type_options);
+
+ return $this->_token;
}
public function get_grant_type()
View
2 classes/kohana/oauth2/consumer/granttype.php
@@ -42,5 +42,5 @@ public function __construct($provider)
$this->_provider = $provider;
}
- abstract public function request_token($user_id = NULL, $grant_type_options = array());
+ abstract public function request_token($grant_type_options = array());
}
View
11 classes/kohana/oauth2/consumer/granttype/authorization/code.php
@@ -11,7 +11,7 @@
*/
abstract class Kohana_OAuth2_Consumer_GrantType_Authorization_Code extends OAuth2_Consumer_GrantType {
- public function request_token($user_id = NULL, $grant_type_options = array())
+ public function request_token($grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
@@ -40,14 +40,7 @@ public function request_token($user_id = NULL, $grant_type_options = array())
break;
}
- // Lazy..
- $token_type = @$x['token_type'];
- $access_token = @$x['access_token'];
- $refresh_token = @$x['refresh_token'];
-
- $token = Model_OAuth2_User_Token::create_token($this->_provider, $token_type, $access_token, $user_id, $refresh_token);
-
- return $token;
+ return $x;
}
public function get_redirect_uri($state = NULL, $response_type = OAuth2::RESPONSE_TYPE_CODE)
View
13 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, $grant_type_options = array())
+ public function request_token($grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
@@ -39,15 +39,8 @@ public function request_token($user_id = NULL, $grant_type_options = array())
parse_str($response->body(), $x);
break;
}
-
- // Lazy..
- $token_type = @$x['token_type'];
- $access_token = @$x['access_token'];
- $refresh_token = @$x['refresh_token'];
-
- $token = Model_OAuth2_User_Token::create_token($this->_provider, $token_type, $access_token, $user_id, $refresh_token);
-
- return $token;
+
+ return $x;
}
}
View
11 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, $grant_type_options = array())
+ public function request_token($grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
@@ -42,13 +42,6 @@ public function request_token($user_id = NULL, $grant_type_options = array())
break;
}
- // Lazy..
- $token_type = @$x['token_type'];
- $access_token = @$x['access_token'];
- $refresh_token = @$x['refresh_token'];
-
- $token = Model_OAuth2_User_Token::create_token($this->_provider, $token_type, $access_token, $user_id, $refresh_token);
-
- return $token;
+ return $x;
}
}
View
11 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, $grant_type_options = array())
+ public function request_token($grant_type_options = array())
{
$request = Request::factory($this->_config[$this->_provider]['token_uri'])
->method(Request::POST)
@@ -40,14 +40,7 @@ public function request_token($user_id = NULL, $grant_type_options = array())
break;
}
- // Lazy..
- $token_type = @$x['token_type'];
- $access_token = @$x['access_token'];
- $refresh_token = @$x['refresh_token'];
-
- $token = Model_OAuth2_User_Token::create_token($this->_provider, $token_type, $access_token, $user_id, $refresh_token);
-
- return $token;
+ return $x;
}
}
View
3 classes/model/oauth2/user/token.php
@@ -1,3 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-
-class Model_OAuth2_User_Token extends Kohana_Model_OAuth2_User_Token {}
View
34 migrations/oauth2/20110912110258_drop-consumer-tokens-table.php
@@ -0,0 +1,34 @@
+<?php defined('SYSPATH') or die('No direct script access.');/**
+ * Drop Consumer Tokens Table
+ */
+class Migration_Oauth2_20110912110258 extends Minion_Migration_Base {
+
+ /**
+ * Run queries needed to apply this migration
+ *
+ * @param Kohana_Database Database connection
+ */
+ public function up(Kohana_Database $db)
+ {
+ $db->query(NULL, 'DROP TABLE IF EXISTS `oauth2_user_tokens`');
+ }
+
+ /**
+ * Run queries needed to remove this migration
+ *
+ * @param Kohana_Database Database connection
+ */
+ public function down(Kohana_Database $db)
+ {
+ $db->query(NULL, 'CREATE TABLE IF NOT EXISTS `oauth2_user_tokens` (
+ `id` int(255) NOT NULL AUTO_INCREMENT,
+ `provider` varchar(255) NOT NULL,
+ `token_type` varchar(255) NOT NULL,
+ `access_token` varchar(255) NOT NULL,
+ `refresh_token` varchar(255) DEFAULT NULL,
+ `user_id` varchar(255) DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ KEY `client_id` (`refresh_token`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;');
+ }
+}

0 comments on commit 7b5dbd8

Please sign in to comment.
Something went wrong with that request. Please try again.