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

94 lines
2.7 KiB
PHP
Raw Normal View History

2014-05-09 10:46:59 +01:00
<?php
namespace RelationalExample\Storage;
2014-11-08 18:26:12 +00:00
use Illuminate\Database\Capsule\Manager as Capsule;
use League\OAuth2\Server\Entity\AccessTokenEntity;
2014-05-09 10:46:59 +01:00
use League\OAuth2\Server\Entity\ScopeEntity;
use League\OAuth2\Server\Storage\AbstractStorage;
2014-11-08 18:26:12 +00:00
use League\OAuth2\Server\Storage\AccessTokenInterface;
2014-05-09 10:46:59 +01:00
class AccessTokenStorage extends AbstractStorage implements AccessTokenInterface
2014-05-09 10:46:59 +01:00
{
/**
* {@inheritdoc}
*/
public function get($token)
{
$result = Capsule::table('oauth_access_tokens')
->where('access_token', $token)
->get();
if (count($result) === 1) {
2014-07-22 10:58:15 +01:00
$token = (new AccessTokenEntity($this->server))
->setId($result[0]['access_token'])
->setExpireTime($result[0]['expire_time']);
2014-05-09 10:46:59 +01:00
return $token;
}
2014-12-10 13:10:35 +00:00
return;
2014-05-09 10:46:59 +01:00
}
/**
* {@inheritdoc}
*/
2014-12-02 10:20:55 +01:00
public function getScopes(AccessTokenEntity $token)
2014-05-09 10:46:59 +01:00
{
$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 10:58:15 +01:00
->where('access_token', $token->getId())
2014-05-09 10:46:59 +01:00
->get();
$response = [];
if (count($result) > 0) {
foreach ($result as $row) {
2014-07-22 10:58:15 +01:00
$scope = (new ScopeEntity($this->server))->hydrate([
'id' => $row['id'],
2014-11-08 18:26:12 +00:00
'description' => $row['description'],
2014-07-22 10:58:15 +01:00
]);
2014-05-09 10:46:59 +01:00
$response[] = $scope;
}
}
return $response;
}
/**
* {@inheritdoc}
*/
public function create($token, $expireTime, $sessionId)
{
2014-07-27 17:12:03 +01:00
Capsule::table('oauth_access_tokens')
->insert([
'access_token' => $token,
'session_id' => $sessionId,
2014-11-08 18:26:12 +00:00
'expire_time' => $expireTime,
2014-07-27 17:12:03 +01:00
]);
2014-05-09 10:46:59 +01:00
}
/**
* {@inheritdoc}
*/
2014-12-02 10:20:55 +01:00
public function associateScope(AccessTokenEntity $token, ScopeEntity $scope)
2014-05-09 10:46:59 +01:00
{
2014-08-04 20:15:31 +01:00
Capsule::table('oauth_access_token_scopes')
->insert([
'access_token' => $token->getId(),
2014-11-08 18:26:12 +00:00
'scope' => $scope->getId(),
2014-08-04 20:15:31 +01:00
]);
2014-05-09 10:46:59 +01:00
}
/**
* {@inheritdoc}
*/
2014-12-02 10:20:55 +01:00
public function delete(AccessTokenEntity $token)
2014-05-09 10:46:59 +01:00
{
2014-08-06 08:42:15 +01:00
Capsule::table('oauth_access_token_scopes')
->where('access_token', $token->getId())
->delete();
2014-05-09 10:46:59 +01:00
}
}