Porting interfaces to Mongo
This commit is contained in:
parent
8e88ffb440
commit
1f3650b98a
7 changed files with 51 additions and 67 deletions
|
@ -37,7 +37,7 @@ class Mongo extends Object
|
||||||
|
|
||||||
// Instantiates our Mongo client
|
// Instantiates our Mongo client
|
||||||
$instance = new \MongoClient($mongo['server']);
|
$instance = new \MongoClient($mongo['server']);
|
||||||
$instance->selectDB($mongo['database']);
|
$instance = $instance->$mongo['database'];
|
||||||
|
|
||||||
// Caches the instance for possible reuse later
|
// Caches the instance for possible reuse later
|
||||||
self::$instances['Mongo'] = $instance;
|
self::$instances['Mongo'] = $instance;
|
||||||
|
|
|
@ -30,6 +30,10 @@ class AccessTokenStorage extends StorageAdapter implements AccessTokenInterface
|
||||||
|
|
||||||
public function getScopes(AbstractTokenEntity $token)
|
public function getScopes(AbstractTokenEntity $token)
|
||||||
{
|
{
|
||||||
|
$response = [];
|
||||||
|
|
||||||
|
/*
|
||||||
|
@todo Port to Mongo
|
||||||
$sql = 'SELECT oauth_scopes.id, oauth_scopes.description'
|
$sql = 'SELECT oauth_scopes.id, oauth_scopes.description'
|
||||||
. ' FROM oauth_access_token_scopes'
|
. ' FROM oauth_access_token_scopes'
|
||||||
. ' INNER JOIN oauth_scopes'
|
. ' INNER JOIN oauth_scopes'
|
||||||
|
@ -37,7 +41,6 @@ class AccessTokenStorage extends StorageAdapter implements AccessTokenInterface
|
||||||
. ' WHERE oauth_access_token_scopes.access_token_id = ?;';
|
. ' WHERE oauth_access_token_scopes.access_token_id = ?;';
|
||||||
|
|
||||||
$results = $this->db->fetch($sql, [$token->getId()]);
|
$results = $this->db->fetch($sql, [$token->getId()]);
|
||||||
$response = [];
|
|
||||||
|
|
||||||
if (count($results) > 0)
|
if (count($results) > 0)
|
||||||
{
|
{
|
||||||
|
@ -49,18 +52,18 @@ class AccessTokenStorage extends StorageAdapter implements AccessTokenInterface
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create($token, $expiration, $session_id)
|
public function create($token, $expiration, $session_id)
|
||||||
{
|
{
|
||||||
$sql = 'INSERT INTO oauth_access_tokens'
|
return $this->mongo->oauth_access_tokens->insert([
|
||||||
. ' (access_token, session_id, expires_at)'
|
'access_token' => $token,
|
||||||
. ' VALUES'
|
'session_id' => $session_id, // @todo Store as MongoId?
|
||||||
. ' (?, ?, ?);';
|
'expires_at' => $expiration,
|
||||||
|
]);
|
||||||
$this->db->execute($sql, [$token, $session_id, $expiration]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope)
|
public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope)
|
||||||
|
|
|
@ -11,40 +11,27 @@ class ClientStorage extends StorageAdapter implements ClientInterface
|
||||||
{
|
{
|
||||||
public function get($client_id, $client_secret = null, $redirect_uri = null, $grant_type = null)
|
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)
|
if ($redirect_uri)
|
||||||
{
|
{
|
||||||
$sql .= ', oauth_client_redirect_uris.*'
|
// @todo join / query oauth_client_redirect_uris
|
||||||
. ' INNER JOIN oauth_redirect_uris'
|
|
||||||
. ' ON oauth_clients.id = oauth_redirect_uris.client_id';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql .= ' FROM oauth_clients WHERE oauth_clients.id = ?';
|
|
||||||
|
|
||||||
$parameters = [$client_id];
|
|
||||||
|
|
||||||
if ($client_secret)
|
if ($client_secret)
|
||||||
{
|
{
|
||||||
$sql .= ' AND oauth_clients.secret = ?';
|
$criteria['secret'] = $client_secret;
|
||||||
$parameters[] = $client_secret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($redirect_uri)
|
$results = $this->mongo->oauth_clients->findOne($criteria);
|
||||||
{
|
|
||||||
$sql .= 'AND oauth_redirect_uris.redirect_uri = ?';
|
|
||||||
$parameters[] = $redirect_uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
$results = $this->db->fetch($sql, $parameters);
|
if ($results)
|
||||||
|
|
||||||
if (count($results) === 1)
|
|
||||||
{
|
{
|
||||||
$client = new ClientEntity($this->server);
|
$client = new ClientEntity($this->server);
|
||||||
|
|
||||||
$client->hydrate([
|
$client->hydrate([
|
||||||
'id' => $results[0]['id'],
|
'id' => $results['_id']->{'$id'},
|
||||||
'name' => $results[0]['name']
|
'name' => $results['name']
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return $client;
|
return $client;
|
||||||
|
|
|
@ -29,20 +29,17 @@ class RefreshTokenStorage extends StorageAdapter implements RefreshTokenInterfac
|
||||||
|
|
||||||
public function create($token, $expiration, $access_token)
|
public function create($token, $expiration, $access_token)
|
||||||
{
|
{
|
||||||
$sql = 'SELECT id FROM oauth_access_tokens WHERE access_token = ?;';
|
$results = $this->mongo->oauth_access_tokens->findOne([
|
||||||
$results = $this->db->fetch($sql, [$access_token]);
|
'access_token' => $access_token,
|
||||||
$token_id = $results[0]['id'];
|
]);
|
||||||
|
|
||||||
$sql = 'INSERT INTO oauth_refresh_tokens'
|
$token_id = $results['_id']->{'$id'};
|
||||||
. ' (refresh_token, access_token_id, expires_at, client_id)'
|
|
||||||
. ' VALUES'
|
|
||||||
. ' (?, ?, ?, ?);';
|
|
||||||
|
|
||||||
$this->db->execute($sql, [
|
return $this->mongo->oauth_refresh_tokens->insert([
|
||||||
$token,
|
'refresh_token' => $token,
|
||||||
$token_id,
|
'access_token_id' => $token_id,
|
||||||
$expiration,
|
'expires_at' => $expiration,
|
||||||
$this->server->getRequest()->request->get('client_id', null),
|
'client_id' => $this->server->getRequest()->request->get('client_id', null),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ use \League\OAuth2\Exception\OAuthException;
|
||||||
use \League\OAuth2\Server\AuthorizationServer;
|
use \League\OAuth2\Server\AuthorizationServer;
|
||||||
use \League\OAuth2\Server\Grant\PasswordGrant;
|
use \League\OAuth2\Server\Grant\PasswordGrant;
|
||||||
use \League\OAuth2\Server\Grant\RefreshTokenGrant;
|
use \League\OAuth2\Server\Grant\RefreshTokenGrant;
|
||||||
use \Pickles\App\Models\User;
|
|
||||||
use \Pickles\Config;
|
use \Pickles\Config;
|
||||||
|
|
||||||
class Resource extends \Pickles\Resource
|
class Resource extends \Pickles\Resource
|
||||||
|
@ -79,14 +78,8 @@ class Resource extends \Pickles\Resource
|
||||||
|
|
||||||
$grant->setVerifyCredentialsCallback(function ($username, $password)
|
$grant->setVerifyCredentialsCallback(function ($username, $password)
|
||||||
{
|
{
|
||||||
$user = new User([
|
$user = $this->mongo->user->findOne(['email' => $username]);
|
||||||
'conditions' => [
|
return $user && password_verify($password, $user['password']);
|
||||||
'email' => $username,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
return $user->count()
|
|
||||||
&& password_verify($password, $user->record['password']);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -61,15 +61,17 @@ class SessionStorage extends StorageAdapter implements SessionInterface
|
||||||
|
|
||||||
public function getScopes(SessionEntity $session)
|
public function getScopes(SessionEntity $session)
|
||||||
{
|
{
|
||||||
$sql = 'SELECT oauth_sessions.*'
|
/*
|
||||||
. ' FROM oauth_sessions'
|
// @todo
|
||||||
. ' INNER JOIN oauth_access_token_scopes'
|
// INNER JOIN oauth_access_token_scopes
|
||||||
. ' ON oauth_sessions.id = oauth_access_token_scopes.access_token_id'
|
// ON oauth_sessions.id = oauth_access_token_scopes.access_token_id
|
||||||
. ' INNER JOIN oauth_scopes'
|
// INNER JOIN oauth_scopes
|
||||||
. ' ON oauth_scopes.id = oauth_access_token_scopes.scope_id'
|
// ON oauth_scopes.id = oauth_access_token_scopes.scope_id
|
||||||
. ' WHERE oauth_sessions.id = ?;';
|
|
||||||
|
$results = $this->mongo->oauth_sessions->findOne([
|
||||||
|
'_id' => new \MongoId($session->getId())
|
||||||
|
]);
|
||||||
|
|
||||||
$results = $this->db->fetch($sql, [$session->getId()]);
|
|
||||||
$scopes = [];
|
$scopes = [];
|
||||||
|
|
||||||
foreach ($results as $scope)
|
foreach ($results as $scope)
|
||||||
|
@ -81,16 +83,18 @@ class SessionStorage extends StorageAdapter implements SessionInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
return $scopes;
|
return $scopes;
|
||||||
|
*/
|
||||||
|
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create($owner_type, $owner_id, $client_id, $client_redirect_uri = null)
|
public function create($owner_type, $owner_id, $client_id, $client_redirect_uri = null)
|
||||||
{
|
{
|
||||||
$sql = 'INSERT INTO oauth_sessions'
|
return $this->mongo->oauth_sessions->insert([
|
||||||
. ' (owner_type, owner_id, client_id)'
|
'owner_type' => $owner_type,
|
||||||
. ' VALUES'
|
'owner_id' => $owner_id,
|
||||||
. ' (?, ?, ?);';
|
'client_id' => $client_id,
|
||||||
|
]);
|
||||||
return $this->db->execute($sql, [$owner_type, $owner_id, $client_id]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function associateScope(SessionEntity $session, ScopeEntity $scope)
|
public function associateScope(SessionEntity $session, ScopeEntity $scope)
|
||||||
|
|
|
@ -4,17 +4,17 @@ namespace Pickles\OAuth2;
|
||||||
|
|
||||||
use \League\OAuth2\Server\Storage\Adapter;
|
use \League\OAuth2\Server\Storage\Adapter;
|
||||||
use \Pickles\Config;
|
use \Pickles\Config;
|
||||||
use \Pickles\Database;
|
use \Pickles\Mongo;
|
||||||
|
|
||||||
class StorageAdapter extends Adapter
|
class StorageAdapter extends Adapter
|
||||||
{
|
{
|
||||||
protected $config;
|
protected $config;
|
||||||
protected $db;
|
protected $mongo;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->config = Config::getInstance();
|
$this->config = Config::getInstance();
|
||||||
$this->db = Database::getInstance();
|
$this->mongo = Mongo::getInstance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue