2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2016-11-27 03:13:42 +05:30
|
|
|
namespace api\components\OAuth2\Storage;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
use api\components\OAuth2\Entities\AccessTokenEntity;
|
2016-11-30 04:49:14 +05:30
|
|
|
use common\components\Redis\Key;
|
|
|
|
use common\components\Redis\Set;
|
2016-02-14 23:20:10 +05:30
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity as OriginalAccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity;
|
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
use League\OAuth2\Server\Storage\AccessTokenInterface;
|
2016-11-30 04:49:14 +05:30
|
|
|
use yii\helpers\Json;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
class AccessTokenStorage extends AbstractStorage implements AccessTokenInterface {
|
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
public $dataTable = 'oauth_access_tokens';
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
public function get($token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$result = Json::decode((new Key($this->dataTable, $token))->getValue());
|
2016-11-27 03:13:42 +05:30
|
|
|
|
|
|
|
$token = new AccessTokenEntity($this->server);
|
2016-11-30 04:49:14 +05:30
|
|
|
$token->setId($result['id']);
|
|
|
|
$token->setExpireTime($result['expire_time']);
|
|
|
|
$token->setSessionId($result['session_id']);
|
2016-11-27 03:13:42 +05:30
|
|
|
|
|
|
|
return $token;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getScopes(OriginalAccessTokenEntity $token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$scopes = $this->scopes($token->getId());
|
2016-02-14 23:20:10 +05:30
|
|
|
$entities = [];
|
2016-11-30 04:49:14 +05:30
|
|
|
foreach($scopes as $scope) {
|
|
|
|
if ($this->server->getScopeStorage()->get($scope) !== null) {
|
|
|
|
$entities[] = (new ScopeEntity($this->server))->hydrate(['id' => $scope]);
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($token, $expireTime, $sessionId) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$payload = Json::encode([
|
|
|
|
'id' => $token,
|
|
|
|
'expire_time' => $expireTime,
|
|
|
|
'session_id' => $sessionId,
|
|
|
|
]);
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
$this->key($token)->setValue($payload)->expireAt($expireTime);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function associateScope(OriginalAccessTokenEntity $token, ScopeEntity $scope) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$this->scopes($token->getId())->add($scope->getId())->expireAt($token->getExpireTime());
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(OriginalAccessTokenEntity $token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$this->key($token->getId())->delete();
|
|
|
|
$this->scopes($token->getId())->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function key(string $token) : Key {
|
|
|
|
return new Key($this->dataTable, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function scopes(string $token) : Set {
|
|
|
|
return new Set($this->dataTable, $token, 'scopes');
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|