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-27 03:13:42 +05:30
|
|
|
use api\components\OAuth2\Entities\AuthCodeEntity;
|
|
|
|
use api\components\OAuth2\Entities\SessionEntity;
|
2016-02-14 23:20:10 +05:30
|
|
|
use common\models\OauthSession;
|
2016-11-05 20:32:46 +05:30
|
|
|
use ErrorException;
|
2016-02-14 23:20:10 +05:30
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity as OriginalAccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\AuthCodeEntity as OriginalAuthCodeEntity;
|
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity;
|
|
|
|
use League\OAuth2\Server\Entity\SessionEntity as OriginalSessionEntity;
|
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
use League\OAuth2\Server\Storage\SessionInterface;
|
|
|
|
use yii\db\Exception;
|
|
|
|
|
|
|
|
class SessionStorage extends AbstractStorage implements SessionInterface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sessionId
|
|
|
|
* @return SessionEntity|null
|
|
|
|
*/
|
2016-11-27 03:13:42 +05:30
|
|
|
public function getById($sessionId) {
|
|
|
|
return $this->hydrate($this->getSessionModel($sessionId));
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getByAccessToken(OriginalAccessTokenEntity $accessToken) {
|
2016-11-30 04:49:14 +05:30
|
|
|
throw new ErrorException('This method is not implemented and should not be used');
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getByAuthCode(OriginalAuthCodeEntity $authCode) {
|
|
|
|
if (!$authCode instanceof AuthCodeEntity) {
|
2016-11-05 20:32:46 +05:30
|
|
|
throw new ErrorException('This module assumes that $authCode typeof ' . AuthCodeEntity::class);
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
return $this->getById($authCode->getSessionId());
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getScopes(OriginalSessionEntity $session) {
|
|
|
|
$result = [];
|
|
|
|
foreach ($this->getSessionModel($session->getId())->getScopes() as $scope) {
|
2016-11-30 04:49:14 +05:30
|
|
|
if ($this->server->getScopeStorage()->get($scope) !== null) {
|
|
|
|
$result[] = (new ScopeEntity($this->server))->hydrate(['id' => $scope]);
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null) {
|
|
|
|
$sessionId = OauthSession::find()
|
|
|
|
->select('id')
|
|
|
|
->andWhere([
|
|
|
|
'client_id' => $clientId,
|
|
|
|
'owner_type' => $ownerType,
|
|
|
|
'owner_id' => $ownerId,
|
|
|
|
])->scalar();
|
|
|
|
|
|
|
|
if ($sessionId === false) {
|
2016-11-05 20:32:46 +05:30
|
|
|
$model = new OauthSession();
|
|
|
|
$model->client_id = $clientId;
|
|
|
|
$model->owner_type = $ownerType;
|
|
|
|
$model->owner_id = $ownerId;
|
|
|
|
$model->client_redirect_uri = $clientRedirectUri;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
if (!$model->save()) {
|
|
|
|
throw new Exception('Cannot save ' . OauthSession::class . ' model.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$sessionId = $model->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function associateScope(OriginalSessionEntity $session, ScopeEntity $scope) {
|
|
|
|
$this->getSessionModel($session->getId())->getScopes()->add($scope->getId());
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
private function getSessionModel(string $sessionId) : OauthSession {
|
2016-11-30 04:49:14 +05:30
|
|
|
$session = OauthSession::findOne($sessionId);
|
|
|
|
if ($session === null) {
|
|
|
|
throw new ErrorException('Cannot find oauth session');
|
2016-11-27 03:13:42 +05:30
|
|
|
}
|
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
return $session;
|
2016-11-27 03:13:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private function hydrate(OauthSession $sessionModel) {
|
|
|
|
$entity = new SessionEntity($this->server);
|
|
|
|
$entity->setId($sessionModel->id);
|
|
|
|
$entity->setClientId($sessionModel->client_id);
|
|
|
|
$entity->setOwner($sessionModel->owner_type, $sessionModel->owner_id);
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|