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-29 04:27:58 +05:30
|
|
|
use api\components\OAuth2\Entities\RefreshTokenEntity;
|
2016-11-27 20:11:39 +05:30
|
|
|
use common\components\Redis\Key;
|
2016-11-30 04:49:14 +05:30
|
|
|
use common\components\Redis\Set;
|
|
|
|
use common\models\OauthSession;
|
|
|
|
use ErrorException;
|
2016-11-29 04:27:58 +05:30
|
|
|
use League\OAuth2\Server\Entity\RefreshTokenEntity as OriginalRefreshTokenEntity;
|
2016-02-14 23:20:10 +05:30
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
use League\OAuth2\Server\Storage\RefreshTokenInterface;
|
2016-11-30 04:49:14 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\helpers\Json;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
class RefreshTokenStorage extends AbstractStorage implements RefreshTokenInterface {
|
|
|
|
|
|
|
|
public $dataTable = 'oauth_refresh_tokens';
|
|
|
|
|
|
|
|
public function get($token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$result = Json::decode((new Key($this->dataTable, $token))->getValue());
|
2017-02-05 17:56:44 +05:30
|
|
|
if ($result === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
$entity = new RefreshTokenEntity($this->server);
|
|
|
|
$entity->setId($result['id']);
|
|
|
|
$entity->setAccessTokenId($result['access_token_id']);
|
2016-11-30 04:49:14 +05:30
|
|
|
$entity->setSessionId($result['session_id']);
|
2016-11-27 03:13:42 +05:30
|
|
|
|
|
|
|
return $entity;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function create($token, $expireTime, $accessToken) {
|
2016-11-30 04:49:14 +05:30
|
|
|
$sessionId = $this->server->getAccessTokenStorage()->get($accessToken)->getSession()->getId();
|
|
|
|
$payload = Json::encode([
|
2016-02-14 23:20:10 +05:30
|
|
|
'id' => $token,
|
|
|
|
'access_token_id' => $accessToken,
|
2016-11-30 04:49:14 +05:30
|
|
|
'session_id' => $sessionId,
|
|
|
|
]);
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
$this->key($token)->setValue($payload);
|
|
|
|
$this->sessionHash($sessionId)->add($token);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2016-11-29 04:27:58 +05:30
|
|
|
public function delete(OriginalRefreshTokenEntity $token) {
|
2016-11-30 04:49:14 +05:30
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sessionHash(string $sessionId) : Set {
|
|
|
|
$tableName = Yii::$app->db->getSchema()->getRawTableName(OauthSession::tableName());
|
|
|
|
return new Set($tableName, $sessionId, 'refresh_tokens');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function key(string $token) : Key {
|
|
|
|
return new Key($this->dataTable, $token);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|