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

93 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;
2014-05-23 20:55:09 +05:30
use League\OAuth2\Server\Entity\AuthCodeEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Storage\Adapter;
use League\OAuth2\Server\Storage\AuthCodeInterface;
2014-06-23 12:50:34 +05:30
2014-05-09 15:16:59 +05:30
class AuthCodeStorage extends Adapter implements AuthCodeInterface
{
/**
* {@inheritdoc}
*/
2014-05-23 20:55:09 +05:30
public function get($code)
{
2014-06-23 12:50:34 +05:30
$result = Capsule::table('oauth_auth_codes')
->where('auth_code', $code)
->where('expire_time', '>=', time())
->get();
if (count($result) === 1) {
$token = new AuthCodeEntity($this->server);
2014-07-22 15:28:15 +05:30
$token->setId($result[0]['auth_code']);
2014-07-27 21:42:03 +05:30
$token->setRedirectUri($result[0]['client_redirect_uri']);
2014-11-08 23:56:12 +05:30
2014-06-23 12:50:34 +05:30
return $token;
}
return null;
}
2014-07-27 21:42:03 +05:30
public function create($token, $expireTime, $sessionId, $redirectUri)
2014-06-23 12:50:34 +05:30
{
Capsule::table('oauth_auth_codes')
->insert([
'auth_code' => $token,
'client_redirect_uri' => $redirectUri,
'session_id' => $sessionId,
2014-11-08 23:56:12 +05:30
'expire_time' => $expireTime,
2014-06-23 12:50:34 +05:30
]);
2014-05-23 20:55:09 +05:30
}
/**
* {@inheritdoc}
*/
public function getScopes(AuthCodeEntity $token)
{
2014-07-27 21:42:03 +05:30
$result = Capsule::table('oauth_auth_code_scopes')
->select(['oauth_scopes.id', 'oauth_scopes.description'])
->join('oauth_scopes', 'oauth_auth_code_scopes.scope', '=', 'oauth_scopes.id')
->where('auth_code', $token->getId())
->get();
$response = [];
if (count($result) > 0) {
foreach ($result as $row) {
$scope = (new ScopeEntity($this->server))->hydrate([
'id' => $row['id'],
2014-11-08 23:56:12 +05:30
'description' => $row['description'],
2014-07-27 21:42:03 +05:30
]);
$response[] = $scope;
}
}
return $response;
2014-05-23 20:55:09 +05:30
}
/**
* {@inheritdoc}
*/
public function associateScope(AuthCodeEntity $token, ScopeEntity $scope)
{
2014-06-23 12:50:34 +05:30
Capsule::table('oauth_auth_code_scopes')
2014-07-27 21:42:03 +05:30
->insert([
'auth_code' => $token->getId(),
2014-11-08 23:56:12 +05:30
'scope' => $scope->getId(),
2014-07-27 21:42:03 +05:30
]);
2014-05-23 20:55:09 +05:30
}
/**
* {@inheritdoc}
*/
public function delete(AuthCodeEntity $token)
2014-05-09 15:16:59 +05:30
{
2014-07-27 21:42:03 +05:30
Capsule::table('oauth_auth_codes')
->where('auth_code', $token->getId())
->delete();
2014-05-09 15:16:59 +05:30
}
}