oauth2-server/examples/relational/Storage/AccessTokenStorage.php

95 lines
2.8 KiB
PHP
Raw Normal View History

2014-05-09 15:16:59 +05:30
<?php
namespace RelationalExample\Storage;
2014-11-08 23:56:12 +05:30
use Illuminate\Database\Capsule\Manager as Capsule;
2014-05-09 15:16:59 +05:30
use League\OAuth2\Server\Entity\AbstractTokenEntity;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Entity\AccessTokenEntity;
2014-05-09 15:16:59 +05:30
use League\OAuth2\Server\Entity\ScopeEntity;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Storage\AccessTokenInterface;
use League\OAuth2\Server\Storage\Adapter;
2014-05-09 15:16:59 +05:30
class AccessTokenStorage extends Adapter implements AccessTokenInterface
{
/**
* {@inheritdoc}
*/
public function get($token)
{
$result = Capsule::table('oauth_access_tokens')
->where('access_token', $token)
->get();
if (count($result) === 1) {
2014-07-22 15:28:15 +05:30
$token = (new AccessTokenEntity($this->server))
->setId($result[0]['access_token'])
->setExpireTime($result[0]['expire_time']);
2014-05-09 15:16:59 +05:30
return $token;
}
return null;
}
/**
* {@inheritdoc}
*/
public function getScopes(AbstractTokenEntity $token)
{
$result = Capsule::table('oauth_access_token_scopes')
->select(['oauth_scopes.id', 'oauth_scopes.description'])
->join('oauth_scopes', 'oauth_access_token_scopes.scope', '=', 'oauth_scopes.id')
2014-07-22 15:28:15 +05:30
->where('access_token', $token->getId())
2014-05-09 15:16:59 +05:30
->get();
$response = [];
if (count($result) > 0) {
foreach ($result as $row) {
2014-07-22 15:28:15 +05:30
$scope = (new ScopeEntity($this->server))->hydrate([
'id' => $row['id'],
2014-11-08 23:56:12 +05:30
'description' => $row['description'],
2014-07-22 15:28:15 +05:30
]);
2014-05-09 15:16:59 +05:30
$response[] = $scope;
}
}
return $response;
}
/**
* {@inheritdoc}
*/
public function create($token, $expireTime, $sessionId)
{
2014-07-27 21:42:03 +05:30
Capsule::table('oauth_access_tokens')
->insert([
'access_token' => $token,
'session_id' => $sessionId,
2014-11-08 23:56:12 +05:30
'expire_time' => $expireTime,
2014-07-27 21:42:03 +05:30
]);
2014-05-09 15:16:59 +05:30
}
/**
* {@inheritdoc}
*/
public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope)
{
2014-08-05 00:45:31 +05:30
Capsule::table('oauth_access_token_scopes')
->insert([
'access_token' => $token->getId(),
2014-11-08 23:56:12 +05:30
'scope' => $scope->getId(),
2014-08-05 00:45:31 +05:30
]);
2014-05-09 15:16:59 +05:30
}
/**
* {@inheritdoc}
*/
public function delete(AbstractTokenEntity $token)
{
2014-08-06 13:12:15 +05:30
Capsule::table('oauth_access_token_scopes')
->where('access_token', $token->getId())
->delete();
2014-05-09 15:16:59 +05:30
}
}