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

98 lines
2.9 KiB
PHP
Raw Normal View History

2014-05-09 15:16:59 +05:30
<?php
namespace RelationalExample\Storage;
use League\OAuth2\Server\Storage\AccessTokenInterface;
use League\OAuth2\Server\Storage\Adapter;
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Entity\AbstractTokenEntity;
use League\OAuth2\Server\Entity\RefreshTokenEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
use Illuminate\Database\Capsule\Manager as Capsule;
class AccessTokenStorage extends Adapter implements AccessTokenInterface
{
/**
* {@inheritdoc}
*/
public function get($token)
{
$result = Capsule::table('oauth_access_tokens')
->where('access_token', $token)
->where('expire_time', '>=', time())
->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'],
'description' => $row['description']
]);
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,
'expire_time' => $expireTime
]);
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(),
'scope' => $scope->getId()
]);
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
}
}