2016-02-14 20:50:10 +03:00
|
|
|
<?php
|
2016-11-27 00:43:42 +03:00
|
|
|
namespace api\components\OAuth2\Storage;
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2016-11-29 01:57:58 +03:00
|
|
|
use api\components\OAuth2\Entities\RefreshTokenEntity;
|
2016-11-27 17:41:39 +03:00
|
|
|
use common\components\Redis\Key;
|
2016-11-30 02:19:14 +03:00
|
|
|
use common\components\Redis\Set;
|
|
|
|
use common\models\OauthSession;
|
|
|
|
use ErrorException;
|
2016-11-29 01:57:58 +03:00
|
|
|
use League\OAuth2\Server\Entity\RefreshTokenEntity as OriginalRefreshTokenEntity;
|
2016-02-14 20:50:10 +03:00
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
use League\OAuth2\Server\Storage\RefreshTokenInterface;
|
2016-11-30 02:19:14 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\helpers\Json;
|
2016-02-14 20:50:10 +03:00
|
|
|
|
|
|
|
class RefreshTokenStorage extends AbstractStorage implements RefreshTokenInterface {
|
|
|
|
|
|
|
|
public $dataTable = 'oauth_refresh_tokens';
|
|
|
|
|
|
|
|
public function get($token) {
|
2016-11-30 02:19:14 +03:00
|
|
|
$result = Json::decode((new Key($this->dataTable, $token))->getValue());
|
2017-02-05 15:26:44 +03:00
|
|
|
if ($result === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2016-11-27 00:43:42 +03:00
|
|
|
$entity = new RefreshTokenEntity($this->server);
|
|
|
|
$entity->setId($result['id']);
|
|
|
|
$entity->setAccessTokenId($result['access_token_id']);
|
2016-11-30 02:19:14 +03:00
|
|
|
$entity->setSessionId($result['session_id']);
|
2016-11-27 00:43:42 +03:00
|
|
|
|
|
|
|
return $entity;
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create($token, $expireTime, $accessToken) {
|
2016-11-30 02:19:14 +03:00
|
|
|
$sessionId = $this->server->getAccessTokenStorage()->get($accessToken)->getSession()->getId();
|
|
|
|
$payload = Json::encode([
|
2016-02-14 20:50:10 +03:00
|
|
|
'id' => $token,
|
|
|
|
'access_token_id' => $accessToken,
|
2016-11-30 02:19:14 +03:00
|
|
|
'session_id' => $sessionId,
|
|
|
|
]);
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2016-11-30 02:19:14 +03:00
|
|
|
$this->key($token)->setValue($payload);
|
|
|
|
$this->sessionHash($sessionId)->add($token);
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
2016-11-29 01:57:58 +03:00
|
|
|
public function delete(OriginalRefreshTokenEntity $token) {
|
2016-11-30 02:19:14 +03:00
|
|
|
if (!$token instanceof RefreshTokenEntity) {
|
|
|
|
throw new ErrorException('Token must be instance of ' . RefreshTokenEntity::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->key($token->getId())->delete();
|
|
|
|
$this->sessionHash($token->getSessionId())->remove($token->getId());
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:47:25 +03:00
|
|
|
public function sessionHash(string $sessionId): Set {
|
2016-11-30 02:19:14 +03:00
|
|
|
$tableName = Yii::$app->db->getSchema()->getRawTableName(OauthSession::tableName());
|
|
|
|
return new Set($tableName, $sessionId, 'refresh_tokens');
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:47:25 +03:00
|
|
|
private function key(string $token): Key {
|
2016-11-30 02:19:14 +03:00
|
|
|
return new Key($this->dataTable, $token);
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|