diff --git a/common/components/oauth/Component.php b/api/components/OAuth2/Component.php similarity index 57% rename from common/components/oauth/Component.php rename to api/components/OAuth2/Component.php index 6bd3091..940e29b 100644 --- a/common/components/oauth/Component.php +++ b/api/components/OAuth2/Component.php @@ -1,13 +1,13 @@ _authServer === null) { $authServer = new AuthorizationServer(); - $authServer - ->setAccessTokenStorage(new AccessTokenStorage()) - ->setClientStorage(new ClientStorage()) - ->setScopeStorage(new ScopeStorage()) - ->setSessionStorage(new SessionStorage()) - ->setAuthCodeStorage(new AuthCodeStorage()) - ->setRefreshTokenStorage(new RefreshTokenStorage()) - ->setScopeDelimiter(','); + $authServer->setAccessTokenStorage(new AccessTokenStorage()); + $authServer->setClientStorage(new ClientStorage()); + $authServer->setScopeStorage(new ScopeStorage()); + $authServer->setSessionStorage(new SessionStorage()); + $authServer->setAuthCodeStorage(new AuthCodeStorage()); + $authServer->setRefreshTokenStorage(new RefreshTokenStorage()); + $authServer->setScopeDelimiter(','); $this->_authServer = $authServer; foreach ($this->grantTypes as $grantType) { - if (!array_key_exists($grantType, $this->grantMap)) { + if (!isset($this->grantMap[$grantType])) { throw new InvalidConfigException('Invalid grant type'); } + /** @var Grant\GrantTypeInterface $grant */ $grant = new $this->grantMap[$grantType](); $this->_authServer->addGrantType($grant); } diff --git a/common/components/oauth/Entity/AccessTokenEntity.php b/api/components/OAuth2/Entities/AccessTokenEntity.php similarity index 82% rename from common/components/oauth/Entity/AccessTokenEntity.php rename to api/components/OAuth2/Entities/AccessTokenEntity.php index bd70930..3f92c5b 100644 --- a/common/components/oauth/Entity/AccessTokenEntity.php +++ b/api/components/OAuth2/Entities/AccessTokenEntity.php @@ -1,11 +1,9 @@ id = $id; + } + + public function setName(string $name) { + $this->name = $name; + } + + public function setSecret(string $secret) { + $this->secret = $secret; + } + + public function setRedirectUri(string $redirectUri) { + $this->redirectUri = $redirectUri; + } + +} diff --git a/api/components/OAuth2/Entities/ScopeEntity.php b/api/components/OAuth2/Entities/ScopeEntity.php new file mode 100644 index 0000000..7b9f3c0 --- /dev/null +++ b/api/components/OAuth2/Entities/ScopeEntity.php @@ -0,0 +1,10 @@ +id = $id; + } + +} diff --git a/common/components/oauth/Entity/SessionEntity.php b/api/components/OAuth2/Entities/SessionEntity.php similarity index 79% rename from common/components/oauth/Entity/SessionEntity.php rename to api/components/OAuth2/Entities/SessionEntity.php index 28fafb5..0d13361 100644 --- a/common/components/oauth/Entity/SessionEntity.php +++ b/api/components/OAuth2/Entities/SessionEntity.php @@ -1,5 +1,5 @@ clientId = $clientId; + } + } diff --git a/common/components/oauth/Exception/AcceptRequiredException.php b/api/components/OAuth2/Exception/AcceptRequiredException.php similarity index 89% rename from common/components/oauth/Exception/AcceptRequiredException.php rename to api/components/OAuth2/Exception/AcceptRequiredException.php index 36c5bf0..038be67 100644 --- a/common/components/oauth/Exception/AcceptRequiredException.php +++ b/api/components/OAuth2/Exception/AcceptRequiredException.php @@ -1,5 +1,5 @@ server))->hydrate([ - 'id' => $model->access_token, - 'expireTime' => $model->expire_time, - 'sessionId' => $model->session_id, - ]); + /** @var SessionStorage $sessionStorage */ + $sessionStorage = $this->server->getSessionStorage(); + + $token = new AccessTokenEntity($this->server); + $token->setId($model->access_token); + $token->setExpireTime($model->expire_time); + $token->setSession($sessionStorage->getById($model->session_id)); + + return $token; } /** diff --git a/common/components/oauth/Storage/Redis/AuthCodeStorage.php b/api/components/OAuth2/Storage/AuthCodeStorage.php similarity index 80% rename from common/components/oauth/Storage/Redis/AuthCodeStorage.php rename to api/components/OAuth2/Storage/AuthCodeStorage.php index f3bdbdc..e153260 100644 --- a/common/components/oauth/Storage/Redis/AuthCodeStorage.php +++ b/api/components/OAuth2/Storage/AuthCodeStorage.php @@ -1,7 +1,7 @@ server))->hydrate([ - 'id' => $result['id'], - 'redirectUri' => $result['client_redirect_uri'], - 'expireTime' => $result['expire_time'], - 'sessionId' => $result['session_id'], - ]); + /** @var SessionStorage $sessionStorage */ + $sessionStorage = $this->server->getSessionStorage(); + + $entity = new AuthCodeEntity($this->server); + $entity->setId($result['id']); + $entity->setRedirectUri($result['client_redirect_uri']); + $entity->setExpireTime($result['expire_time']); + $entity->setSession($sessionStorage->getById($result['session_id'])); + + return $entity; } /** diff --git a/common/components/oauth/Storage/Yii2/ClientStorage.php b/api/components/OAuth2/Storage/ClientStorage.php similarity index 67% rename from common/components/oauth/Storage/Yii2/ClientStorage.php rename to api/components/OAuth2/Storage/ClientStorage.php index 5e8808d..90d024b 100644 --- a/common/components/oauth/Storage/Yii2/ClientStorage.php +++ b/api/components/OAuth2/Storage/ClientStorage.php @@ -1,9 +1,9 @@ select(['id', 'name', 'secret', 'redirect_uri']) - ->where([OauthClient::tableName() . '.id' => $clientId]); - + $query = OauthClient::find()->andWhere(['id' => $clientId]); if ($clientSecret !== null) { $query->andWhere(['secret' => $clientSecret]); } - $model = $query->asArray()->one(); + /** @var OauthClient|null $model */ + $model = $query->one(); if ($model === null) { return null; } @@ -39,22 +37,17 @@ class ClientStorage extends AbstractStorage implements ClientInterface { * Короче это нужно учесть */ if ($redirectUri !== null) { - if ($redirectUri === self::REDIRECT_STATIC_PAGE || $redirectUri === self::REDIRECT_STATIC_PAGE_WITH_CODE) { + if (in_array($redirectUri, [self::REDIRECT_STATIC_PAGE, self::REDIRECT_STATIC_PAGE_WITH_CODE], true)) { // Тут, наверное, нужно проверить тип приложения } else { - if (!StringHelper::startsWith($redirectUri, $model['redirect_uri'], false)) { + if (!StringHelper::startsWith($redirectUri, $model->redirect_uri, false)) { return null; } } } - $entity = new ClientEntity($this->server); - $entity->hydrate([ - 'id' => $model['id'], - 'name' => $model['name'], - 'secret' => $model['secret'], - 'redirectUri' => $redirectUri, - ]); + $entity = $this->hydrate($model); + $entity->setRedirectUri($redirectUri); return $entity; } @@ -67,17 +60,23 @@ class ClientStorage extends AbstractStorage implements ClientInterface { throw new \ErrorException('This module assumes that $session typeof ' . SessionEntity::class); } - $model = OauthClient::find() - ->select(['id', 'name']) - ->andWhere(['id' => $session->getClientId()]) - ->asArray() - ->one(); - + /** @var OauthClient|null $model */ + $model = OauthClient::findOne($session->getClientId()); if ($model === null) { return null; } - return (new ClientEntity($this->server))->hydrate($model); + return $this->hydrate($model); + } + + private function hydrate(OauthClient $model) : ClientEntity { + $entity = new ClientEntity($this->server); + $entity->setId($model->id); + $entity->setName($model->name); + $entity->setSecret($model->secret); + $entity->setRedirectUri($model->redirect_uri); + + return $entity; } } diff --git a/common/components/oauth/Storage/Redis/RefreshTokenStorage.php b/api/components/OAuth2/Storage/RefreshTokenStorage.php similarity index 78% rename from common/components/oauth/Storage/Redis/RefreshTokenStorage.php rename to api/components/OAuth2/Storage/RefreshTokenStorage.php index f3ad9e0..037f252 100644 --- a/common/components/oauth/Storage/Redis/RefreshTokenStorage.php +++ b/api/components/OAuth2/Storage/RefreshTokenStorage.php @@ -1,5 +1,5 @@ server)) - ->setId($result['id']) - ->setExpireTime($result['expire_time']) - ->setAccessTokenId($result['access_token_id']); + $entity = new RefreshTokenEntity($this->server); + $entity->setId($result['id']); + $entity->setExpireTime($result['expire_time']); + $entity->setAccessTokenId($result['access_token_id']); + + return $entity; } /** diff --git a/common/components/oauth/Storage/Yii2/ScopeStorage.php b/api/components/OAuth2/Storage/ScopeStorage.php similarity index 69% rename from common/components/oauth/Storage/Yii2/ScopeStorage.php rename to api/components/OAuth2/Storage/ScopeStorage.php index 64fef0e..788f7e3 100644 --- a/common/components/oauth/Storage/Yii2/ScopeStorage.php +++ b/api/components/OAuth2/Storage/ScopeStorage.php @@ -1,8 +1,8 @@ andWhere(['id' => $scope])->asArray()->one(); + /** @var OauthScope|null $row */ + $row = OauthScope::findOne($scope); if ($row === null) { return null; } $entity = new ScopeEntity($this->server); - $entity->hydrate($row); + $entity->setId($row->id); return $entity; } diff --git a/common/components/oauth/Storage/Yii2/SessionStorage.php b/api/components/OAuth2/Storage/SessionStorage.php similarity index 77% rename from common/components/oauth/Storage/Yii2/SessionStorage.php rename to api/components/OAuth2/Storage/SessionStorage.php index 1542391..06e66ff 100644 --- a/common/components/oauth/Storage/Yii2/SessionStorage.php +++ b/api/components/OAuth2/Storage/SessionStorage.php @@ -1,8 +1,8 @@ cache[$sessionId])) { - $this->cache[$sessionId] = OauthSession::findOne($sessionId); - } - - return $this->cache[$sessionId]; - } - - private function hydrateEntity($sessionModel) { - if (!$sessionModel instanceof OauthSession) { - return null; - } - - return (new SessionEntity($this->server))->hydrate([ - 'id' => $sessionModel->id, - 'client_id' => $sessionModel->client_id, - ])->setOwner($sessionModel->owner_type, $sessionModel->owner_id); - } - /** * @param string $sessionId * @return SessionEntity|null */ - public function getSession($sessionId) { - return $this->hydrateEntity($this->getSessionModel($sessionId)); + public function getById($sessionId) { + return $this->hydrate($this->getSessionModel($sessionId)); } /** @@ -60,7 +37,7 @@ class SessionStorage extends AbstractStorage implements SessionInterface { }, ])->one(); - return $this->hydrateEntity($model); + return $this->hydrate($model); } /** @@ -71,7 +48,7 @@ class SessionStorage extends AbstractStorage implements SessionInterface { throw new ErrorException('This module assumes that $authCode typeof ' . AuthCodeEntity::class); } - return $this->getSession($authCode->getSessionId()); + return $this->getById($authCode->getSessionId()); } /** @@ -123,4 +100,21 @@ class SessionStorage extends AbstractStorage implements SessionInterface { $this->getSessionModel($session->getId())->getScopes()->add($scope->getId()); } + private function getSessionModel(string $sessionId) : OauthSession { + if (!isset($this->cache[$sessionId])) { + $this->cache[$sessionId] = OauthSession::findOne($sessionId); + } + + return $this->cache[$sessionId]; + } + + 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; + } + } diff --git a/api/components/OAuth2/Utils/KeyAlgorithm/UuidAlgorithm.php b/api/components/OAuth2/Utils/KeyAlgorithm/UuidAlgorithm.php new file mode 100644 index 0000000..54b4ba3 --- /dev/null +++ b/api/components/OAuth2/Utils/KeyAlgorithm/UuidAlgorithm.php @@ -0,0 +1,16 @@ +toString(); + } + +} diff --git a/api/config/config.php b/api/config/config.php index dfcd9c3..37bdd71 100644 --- a/api/config/config.php +++ b/api/config/config.php @@ -63,7 +63,7 @@ return [ 'format' => yii\web\Response::FORMAT_JSON, ], 'oauth' => [ - 'class' => common\components\oauth\Component::class, + 'class' => api\components\OAuth2\Component::class, 'grantTypes' => ['authorization_code'], ], 'errorHandler' => [ diff --git a/api/controllers/OauthController.php b/api/controllers/OauthController.php index 4f5db86..bed3b4a 100644 --- a/api/controllers/OauthController.php +++ b/api/controllers/OauthController.php @@ -2,8 +2,8 @@ namespace api\controllers; use api\filters\ActiveUserRule; -use common\components\oauth\Exception\AcceptRequiredException; -use common\components\oauth\Exception\AccessDeniedException; +use api\components\OAuth2\Exceptions\AcceptRequiredException; +use api\components\OAuth2\Exceptions\AccessDeniedException; use common\models\Account; use common\models\OauthClient; use common\models\OauthScope; @@ -186,7 +186,7 @@ class OauthController extends Controller { } $scopes = $codeModel->getScopes(); - if (array_search(OauthScope::OFFLINE_ACCESS, array_keys($scopes)) === false) { + if (array_search(OauthScope::OFFLINE_ACCESS, array_keys($scopes), true) === false) { return; } } elseif ($grantType === 'refresh_token') { diff --git a/autocompletion.php b/autocompletion.php index 24c5140..42643fa 100644 --- a/autocompletion.php +++ b/autocompletion.php @@ -29,10 +29,10 @@ abstract class BaseApplication extends yii\base\Application { * Class WebApplication * Include only Web application related components here * - * @property \api\components\User\Component $user User component. - * @property \api\components\ApiUser\Component $apiUser Api User component. + * @property \api\components\User\Component $user User component. + * @property \api\components\ApiUser\Component $apiUser Api User component. * @property \api\components\ReCaptcha\Component $reCaptcha - * @property \common\components\oauth\Component $oauth + * @property \api\components\OAuth2\Component $oauth * * @method \api\components\User\Component getUser() */ diff --git a/common/components/oauth/Util/KeyAlgorithm/UuidAlgorithm.php b/common/components/oauth/Util/KeyAlgorithm/UuidAlgorithm.php deleted file mode 100644 index e75580d..0000000 --- a/common/components/oauth/Util/KeyAlgorithm/UuidAlgorithm.php +++ /dev/null @@ -1,16 +0,0 @@ -toString(); - } - -}