2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
|
|
|
namespace common\components\oauth\Storage\Yii2;
|
|
|
|
|
|
|
|
use common\components\oauth\Entity\AccessTokenEntity;
|
|
|
|
use common\models\OauthAccessToken;
|
|
|
|
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;
|
|
|
|
use yii\db\Exception;
|
|
|
|
|
|
|
|
class AccessTokenStorage extends AbstractStorage implements AccessTokenInterface {
|
|
|
|
|
|
|
|
private $cache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
* @return OauthAccessToken|null
|
|
|
|
*/
|
|
|
|
private function getTokenModel($token) {
|
2016-02-23 03:19:46 +05:30
|
|
|
if (!isset($this->cache[$token])) {
|
2016-02-14 23:20:10 +05:30
|
|
|
$this->cache[$token] = OauthAccessToken::findOne($token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->cache[$token];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function get($token) {
|
|
|
|
$model = $this->getTokenModel($token);
|
|
|
|
if ($model === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new AccessTokenEntity($this->server))->hydrate([
|
|
|
|
'id' => $model->access_token,
|
|
|
|
'expireTime' => $model->expire_time,
|
|
|
|
'sessionId' => $model->session_id,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getScopes(OriginalAccessTokenEntity $token) {
|
|
|
|
$entities = [];
|
|
|
|
foreach($this->getTokenModel($token->getId())->getScopes() as $scope) {
|
|
|
|
$entities[] = (new ScopeEntity($this->server))->hydrate(['id' => $scope]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function create($token, $expireTime, $sessionId) {
|
2016-11-05 20:32:46 +05:30
|
|
|
$model = new OauthAccessToken();
|
|
|
|
$model->access_token = $token;
|
|
|
|
$model->expire_time = $expireTime;
|
|
|
|
$model->session_id = $sessionId;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
if (!$model->save()) {
|
|
|
|
throw new Exception('Cannot save ' . OauthAccessToken::class . ' model.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function associateScope(OriginalAccessTokenEntity $token, ScopeEntity $scope) {
|
|
|
|
$this->getTokenModel($token->getId())->getScopes()->add($scope->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function delete(OriginalAccessTokenEntity $token) {
|
|
|
|
$this->getTokenModel($token->getId())->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|