94 lines
2.8 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;
2014-05-23 16:25:09 +01:00
use League\OAuth2\Server\Entity\AuthCodeEntity;
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\AuthCodeInterface;
2014-06-23 08:20:34 +01:00
class AuthCodeStorage extends AbstractStorage implements AuthCodeInterface
2014-05-09 10:46:59 +01:00
{
/**
* {@inheritdoc}
*/
2014-05-23 16:25:09 +01:00
public function get($code)
{
2014-06-23 08:20:34 +01:00
$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 10:58:15 +01:00
$token->setId($result[0]['auth_code']);
2014-07-27 17:12:03 +01:00
$token->setRedirectUri($result[0]['client_redirect_uri']);
2014-12-02 11:28:55 +01:00
$token->setExpireTime($result[0]['expire_time']);
2014-11-08 18:26:12 +00:00
2014-06-23 08:20:34 +01:00
return $token;
}
2014-12-10 13:10:35 +00:00
return;
2014-06-23 08:20:34 +01:00
}
2014-07-27 17:12:03 +01:00
public function create($token, $expireTime, $sessionId, $redirectUri)
2014-06-23 08:20:34 +01:00
{
Capsule::table('oauth_auth_codes')
->insert([
'auth_code' => $token,
'client_redirect_uri' => $redirectUri,
'session_id' => $sessionId,
2014-11-08 18:26:12 +00:00
'expire_time' => $expireTime,
2014-06-23 08:20:34 +01:00
]);
2014-05-23 16:25:09 +01:00
}
/**
* {@inheritdoc}
*/
public function getScopes(AuthCodeEntity $token)
{
2014-07-27 17:12:03 +01:00
$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 18:26:12 +00:00
'description' => $row['description'],
2014-07-27 17:12:03 +01:00
]);
$response[] = $scope;
}
}
return $response;
2014-05-23 16:25:09 +01:00
}
/**
* {@inheritdoc}
*/
public function associateScope(AuthCodeEntity $token, ScopeEntity $scope)
{
2014-06-23 08:20:34 +01:00
Capsule::table('oauth_auth_code_scopes')
2014-07-27 17:12:03 +01:00
->insert([
'auth_code' => $token->getId(),
2014-11-08 18:26:12 +00:00
'scope' => $scope->getId(),
2014-07-27 17:12:03 +01:00
]);
2014-05-23 16:25:09 +01:00
}
/**
* {@inheritdoc}
*/
public function delete(AuthCodeEntity $token)
2014-05-09 10:46:59 +01:00
{
2014-07-27 17:12:03 +01:00
Capsule::table('oauth_auth_codes')
->where('auth_code', $token->getId())
->delete();
2014-05-09 10:46:59 +01:00
}
}