2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2019-08-23 13:58:04 +05:30
|
|
|
namespace api\components\OAuth2\Repositories;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
use api\components\OAuth2\Entities\AuthCodeEntity;
|
2016-11-27 20:11:39 +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\AuthCodeEntity as OriginalAuthCodeEntity;
|
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity;
|
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
use League\OAuth2\Server\Storage\AuthCodeInterface;
|
2016-11-30 04:49:14 +05:30
|
|
|
use yii\helpers\Json;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
class AuthCodeStorage extends AbstractStorage implements AuthCodeInterface {
|
|
|
|
|
|
|
|
public $dataTable = 'oauth_auth_codes';
|
|
|
|
|
|
|
|
public function get($code) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$result = Json::decode((new Key($this->dataTable, $code))->getValue());
|
|
|
|
if ($result === null) {
|
2016-02-14 23:20:10 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
$entity = new AuthCodeEntity($this->server);
|
|
|
|
$entity->setId($result['id']);
|
|
|
|
$entity->setExpireTime($result['expire_time']);
|
2016-11-30 04:49:14 +05:30
|
|
|
$entity->setSessionId($result['session_id']);
|
|
|
|
$entity->setRedirectUri($result['client_redirect_uri']);
|
2016-11-27 03:13:42 +05:30
|
|
|
|
|
|
|
return $entity;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function create($token, $expireTime, $sessionId, $redirectUri) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$payload = Json::encode([
|
2016-02-14 23:20:10 +05:30
|
|
|
'id' => $token,
|
|
|
|
'expire_time' => $expireTime,
|
|
|
|
'session_id' => $sessionId,
|
|
|
|
'client_redirect_uri' => $redirectUri,
|
2016-11-30 04:49:14 +05:30
|
|
|
]);
|
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 getScopes(OriginalAuthCodeEntity $token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$scopes = $this->scopes($token->getId());
|
|
|
|
$scopesEntities = [];
|
|
|
|
foreach ($scopes as $scope) {
|
|
|
|
if ($this->server->getScopeStorage()->get($scope) !== null) {
|
|
|
|
$scopesEntities[] = (new ScopeEntity($this->server))->hydrate(['id' => $scope]);
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
return $scopesEntities;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function associateScope(OriginalAuthCodeEntity $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(OriginalAuthCodeEntity $token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$this->key($token->getId())->delete();
|
|
|
|
$this->scopes($token->getId())->delete();
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
private function key(string $token): Key {
|
2016-11-30 04:49:14 +05:30
|
|
|
return new Key($this->dataTable, $token);
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
private function scopes(string $token): Set {
|
2016-11-30 04:49:14 +05:30
|
|
|
return new Set($this->dataTable, $token, 'scopes');
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|