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

94 lines
2.7 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;
use League\OAuth2\Server\Entity\AccessTokenEntity;
2014-05-09 15:16:59 +05:30
use League\OAuth2\Server\Entity\ScopeEntity;
use League\OAuth2\Server\Storage\AbstractStorage;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Storage\AccessTokenInterface;
2014-05-09 15:16:59 +05:30
class AccessTokenStorage extends AbstractStorage implements AccessTokenInterface
2014-05-09 15:16:59 +05:30
{
/**
* {@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;
}
2014-12-10 18:40:35 +05:30
return;
2014-05-09 15:16:59 +05:30
}
/**
* {@inheritdoc}
*/
2014-12-02 14:50:55 +05:30
public function getScopes(AccessTokenEntity $token)
2014-05-09 15:16:59 +05:30
{
$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}
*/
2014-12-02 14:50:55 +05:30
public function associateScope(AccessTokenEntity $token, ScopeEntity $scope)
2014-05-09 15:16:59 +05:30
{
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}
*/
2014-12-02 14:50:55 +05:30
public function delete(AccessTokenEntity $token)
2014-05-09 15:16:59 +05:30
{
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
}
}