Хранилище access_token вынесено в redis

Переписана логика связи моделей для oAuth процесса
This commit is contained in:
ErickSkrauch
2016-11-30 02:19:14 +03:00
parent 4f259a9dc7
commit 422d5c4fd4
12 changed files with 297 additions and 146 deletions

View File

@ -3,47 +3,58 @@ namespace api\components\OAuth2\Storage;
use api\components\OAuth2\Entities\RefreshTokenEntity;
use common\components\Redis\Key;
use common\components\Redis\Set;
use common\models\OauthSession;
use ErrorException;
use League\OAuth2\Server\Entity\RefreshTokenEntity as OriginalRefreshTokenEntity;
use League\OAuth2\Server\Storage\AbstractStorage;
use League\OAuth2\Server\Storage\RefreshTokenInterface;
use Yii;
use yii\helpers\Json;
class RefreshTokenStorage extends AbstractStorage implements RefreshTokenInterface {
public $dataTable = 'oauth_refresh_tokens';
/**
* @inheritdoc
*/
public function get($token) {
$result = json_decode((new Key($this->dataTable, $token))->getValue(), true);
if (!$result) {
return null;
}
$result = Json::decode((new Key($this->dataTable, $token))->getValue());
$entity = new RefreshTokenEntity($this->server);
$entity->setId($result['id']);
$entity->setAccessTokenId($result['access_token_id']);
$entity->setSessionId($result['session_id']);
return $entity;
}
/**
* @inheritdoc
*/
public function create($token, $expireTime, $accessToken) {
$payload = [
$sessionId = $this->server->getAccessTokenStorage()->get($accessToken)->getSession()->getId();
$payload = Json::encode([
'id' => $token,
'access_token_id' => $accessToken,
];
'session_id' => $sessionId,
]);
(new Key($this->dataTable, $token))->setValue($payload);
$this->key($token)->setValue($payload);
$this->sessionHash($sessionId)->add($token);
}
/**
* @inheritdoc
*/
public function delete(OriginalRefreshTokenEntity $token) {
(new Key($this->dataTable, $token->getId()))->delete();
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);
}
}