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

110 lines
3.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\AccessTokenEntity;
use League\OAuth2\Server\Entity\AuthCodeEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Entity\SessionEntity;
use League\OAuth2\Server\Storage\AbstractStorage;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Storage\SessionInterface;
2014-05-09 15:16:59 +05:30
class SessionStorage extends AbstractStorage implements SessionInterface
2014-05-09 15:16:59 +05:30
{
/**
* {@inheritdoc}
*/
public function getByAccessToken(AccessTokenEntity $accessToken)
{
$result = Capsule::table('oauth_sessions')
->select(['oauth_sessions.id', 'oauth_sessions.owner_type', 'oauth_sessions.owner_id', 'oauth_sessions.client_id', 'oauth_sessions.client_redirect_uri'])
->join('oauth_access_tokens', 'oauth_access_tokens.session_id', '=', 'oauth_sessions.id')
2014-07-27 21:42:03 +05:30
->where('oauth_access_tokens.access_token', $accessToken->getId())
2014-05-09 15:16:59 +05:30
->get();
if (count($result) === 1) {
$session = new SessionEntity($this->server);
$session->setId($result[0]['id']);
$session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
return $session;
}
return null;
}
/**
* {@inheritdoc}
*/
public function getByAuthCode(AuthCodeEntity $authCode)
{
2014-07-27 21:42:03 +05:30
$result = Capsule::table('oauth_sessions')
->select(['oauth_sessions.id', 'oauth_sessions.owner_type', 'oauth_sessions.owner_id', 'oauth_sessions.client_id', 'oauth_sessions.client_redirect_uri'])
->join('oauth_auth_codes', 'oauth_auth_codes.session_id', '=', 'oauth_sessions.id')
->where('oauth_auth_codes.auth_code', $authCode->getId())
->get();
if (count($result) === 1) {
$session = new SessionEntity($this->server);
$session->setId($result[0]['id']);
$session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
return $session;
}
return null;
2014-05-09 15:16:59 +05:30
}
/**
* {@inheritdoc}
*/
public function getScopes(SessionEntity $session)
{
2014-06-23 12:50:34 +05:30
$result = Capsule::table('oauth_sessions')
->select('oauth_scopes.*')
->join('oauth_session_scopes', 'oauth_sessions.id', '=', 'oauth_session_scopes.session_id')
->join('oauth_scopes', 'oauth_scopes.id', '=', 'oauth_session_scopes.scope')
->where('oauth_sessions.id', $session->getId())
->get();
$scopes = [];
foreach ($result as $scope) {
2014-07-27 21:42:03 +05:30
$scopes[] = (new ScopeEntity($this->server))->hydrate([
'id' => $scope['id'],
2014-11-08 23:56:12 +05:30
'description' => $scope['description'],
2014-07-27 21:42:03 +05:30
]);
2014-06-23 12:50:34 +05:30
}
return $scopes;
2014-05-09 15:16:59 +05:30
}
/**
* {@inheritdoc}
*/
public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null)
{
2014-06-23 12:50:34 +05:30
$id = Capsule::table('oauth_sessions')
->insertGetId([
2014-06-23 12:50:34 +05:30
'owner_type' => $ownerType,
'owner_id' => $ownerId,
2014-11-08 23:56:12 +05:30
'client_id' => $clientId,
2014-06-23 12:50:34 +05:30
]);
return $id;
2014-05-09 15:16:59 +05:30
}
/**
* {@inheritdoc}
*/
public function associateScope(SessionEntity $session, ScopeEntity $scope)
{
2014-07-27 21:42:03 +05:30
Capsule::table('oauth_session_scopes')
->insert([
'session_id' => $session->getId(),
2014-11-08 23:56:12 +05:30
'scope' => $scope->getId(),
2014-07-27 21:42:03 +05:30
]);
2014-05-09 15:16:59 +05:30
}
}