Porting interfaces to Mongo

This commit is contained in:
Josh Sherman 2014-10-19 12:01:04 -04:00
parent 8e88ffb440
commit 1f3650b98a
7 changed files with 51 additions and 67 deletions

View file

@ -37,7 +37,7 @@ class Mongo extends Object
// Instantiates our Mongo client
$instance = new \MongoClient($mongo['server']);
$instance->selectDB($mongo['database']);
$instance = $instance->$mongo['database'];
// Caches the instance for possible reuse later
self::$instances['Mongo'] = $instance;

View file

@ -30,6 +30,10 @@ class AccessTokenStorage extends StorageAdapter implements AccessTokenInterface
public function getScopes(AbstractTokenEntity $token)
{
$response = [];
/*
@todo Port to Mongo
$sql = 'SELECT oauth_scopes.id, oauth_scopes.description'
. ' FROM oauth_access_token_scopes'
. ' INNER JOIN oauth_scopes'
@ -37,7 +41,6 @@ class AccessTokenStorage extends StorageAdapter implements AccessTokenInterface
. ' WHERE oauth_access_token_scopes.access_token_id = ?;';
$results = $this->db->fetch($sql, [$token->getId()]);
$response = [];
if (count($results) > 0)
{
@ -49,18 +52,18 @@ class AccessTokenStorage extends StorageAdapter implements AccessTokenInterface
]);
}
}
*/
return $response;
}
public function create($token, $expiration, $session_id)
{
$sql = 'INSERT INTO oauth_access_tokens'
. ' (access_token, session_id, expires_at)'
. ' VALUES'
. ' (?, ?, ?);';
$this->db->execute($sql, [$token, $session_id, $expiration]);
return $this->mongo->oauth_access_tokens->insert([
'access_token' => $token,
'session_id' => $session_id, // @todo Store as MongoId?
'expires_at' => $expiration,
]);
}
public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope)

View file

@ -11,40 +11,27 @@ class ClientStorage extends StorageAdapter implements ClientInterface
{
public function get($client_id, $client_secret = null, $redirect_uri = null, $grant_type = null)
{
$sql = 'SELECT oauth_clients.*';
$criteria = ['_id' => new \MongoId($client_id)];
if ($redirect_uri)
{
$sql .= ', oauth_client_redirect_uris.*'
. ' INNER JOIN oauth_redirect_uris'
. ' ON oauth_clients.id = oauth_redirect_uris.client_id';
// @todo join / query oauth_client_redirect_uris
}
$sql .= ' FROM oauth_clients WHERE oauth_clients.id = ?';
$parameters = [$client_id];
if ($client_secret)
{
$sql .= ' AND oauth_clients.secret = ?';
$parameters[] = $client_secret;
$criteria['secret'] = $client_secret;
}
if ($redirect_uri)
{
$sql .= 'AND oauth_redirect_uris.redirect_uri = ?';
$parameters[] = $redirect_uri;
}
$results = $this->mongo->oauth_clients->findOne($criteria);
$results = $this->db->fetch($sql, $parameters);
if (count($results) === 1)
if ($results)
{
$client = new ClientEntity($this->server);
$client->hydrate([
'id' => $results[0]['id'],
'name' => $results[0]['name']
'id' => $results['_id']->{'$id'},
'name' => $results['name']
]);
return $client;

View file

@ -29,20 +29,17 @@ class RefreshTokenStorage extends StorageAdapter implements RefreshTokenInterfac
public function create($token, $expiration, $access_token)
{
$sql = 'SELECT id FROM oauth_access_tokens WHERE access_token = ?;';
$results = $this->db->fetch($sql, [$access_token]);
$token_id = $results[0]['id'];
$results = $this->mongo->oauth_access_tokens->findOne([
'access_token' => $access_token,
]);
$sql = 'INSERT INTO oauth_refresh_tokens'
. ' (refresh_token, access_token_id, expires_at, client_id)'
. ' VALUES'
. ' (?, ?, ?, ?);';
$token_id = $results['_id']->{'$id'};
$this->db->execute($sql, [
$token,
$token_id,
$expiration,
$this->server->getRequest()->request->get('client_id', null),
return $this->mongo->oauth_refresh_tokens->insert([
'refresh_token' => $token,
'access_token_id' => $token_id,
'expires_at' => $expiration,
'client_id' => $this->server->getRequest()->request->get('client_id', null),
]);
}

View file

@ -6,7 +6,6 @@ use \League\OAuth2\Exception\OAuthException;
use \League\OAuth2\Server\AuthorizationServer;
use \League\OAuth2\Server\Grant\PasswordGrant;
use \League\OAuth2\Server\Grant\RefreshTokenGrant;
use \Pickles\App\Models\User;
use \Pickles\Config;
class Resource extends \Pickles\Resource
@ -79,14 +78,8 @@ class Resource extends \Pickles\Resource
$grant->setVerifyCredentialsCallback(function ($username, $password)
{
$user = new User([
'conditions' => [
'email' => $username,
],
]);
return $user->count()
&& password_verify($password, $user->record['password']);
$user = $this->mongo->user->findOne(['email' => $username]);
return $user && password_verify($password, $user['password']);
});
break;

View file

@ -61,16 +61,18 @@ class SessionStorage extends StorageAdapter implements SessionInterface
public function getScopes(SessionEntity $session)
{
$sql = 'SELECT oauth_sessions.*'
. ' FROM oauth_sessions'
. ' INNER JOIN oauth_access_token_scopes'
. ' ON oauth_sessions.id = oauth_access_token_scopes.access_token_id'
. ' INNER JOIN oauth_scopes'
. ' ON oauth_scopes.id = oauth_access_token_scopes.scope_id'
. ' WHERE oauth_sessions.id = ?;';
/*
// @todo
// INNER JOIN oauth_access_token_scopes
// ON oauth_sessions.id = oauth_access_token_scopes.access_token_id
// INNER JOIN oauth_scopes
// ON oauth_scopes.id = oauth_access_token_scopes.scope_id
$results = $this->db->fetch($sql, [$session->getId()]);
$scopes = [];
$results = $this->mongo->oauth_sessions->findOne([
'_id' => new \MongoId($session->getId())
]);
$scopes = [];
foreach ($results as $scope)
{
@ -81,16 +83,18 @@ class SessionStorage extends StorageAdapter implements SessionInterface
}
return $scopes;
*/
return [];
}
public function create($owner_type, $owner_id, $client_id, $client_redirect_uri = null)
{
$sql = 'INSERT INTO oauth_sessions'
. ' (owner_type, owner_id, client_id)'
. ' VALUES'
. ' (?, ?, ?);';
return $this->db->execute($sql, [$owner_type, $owner_id, $client_id]);
return $this->mongo->oauth_sessions->insert([
'owner_type' => $owner_type,
'owner_id' => $owner_id,
'client_id' => $client_id,
]);
}
public function associateScope(SessionEntity $session, ScopeEntity $scope)

View file

@ -4,17 +4,17 @@ namespace Pickles\OAuth2;
use \League\OAuth2\Server\Storage\Adapter;
use \Pickles\Config;
use \Pickles\Database;
use \Pickles\Mongo;
class StorageAdapter extends Adapter
{
protected $config;
protected $db;
protected $mongo;
public function __construct()
{
$this->config = Config::getInstance();
$this->db = Database::getInstance();
$this->mongo = Mongo::getInstance();
}
}