mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
api
components
ApiUser
OAuth2
Entities
Exception
Grants
Storage
AccessTokenStorage.php
AuthCodeStorage.php
ClientStorage.php
RefreshTokenStorage.php
ScopeStorage.php
SessionStorage.php
Utils
Component.php
ReCaptcha
User
ErrorHandler.php
config
controllers
exceptions
filters
mails
models
modules
runtime
traits
validators
web
common
console
data
docker
tests
.dockerignore
.env-dist
.gitignore
.gitlab-ci.yml
Dockerfile
Dockerfile-dev
README.md
autocompletion.php
composer.json
docker-compose.dev.yml
docker-compose.prod.yml
yii
Добавлен client_credentials grant для oAuth Рефакторинг структуры OauthScopes чтобы можно было разделить владельца прав на пользовательские и общие (машинные) Исправлена стилистика кода, внедряются фишки PHP 7.1
84 lines
2.9 KiB
PHP
84 lines
2.9 KiB
PHP
<?php
|
||
namespace api\components\OAuth2\Storage;
|
||
|
||
use api\components\OAuth2\Entities\ClientEntity;
|
||
use api\components\OAuth2\Entities\SessionEntity;
|
||
use common\models\OauthClient;
|
||
use League\OAuth2\Server\Entity\SessionEntity as OriginalSessionEntity;
|
||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||
use League\OAuth2\Server\Storage\ClientInterface;
|
||
use yii\helpers\StringHelper;
|
||
|
||
class ClientStorage extends AbstractStorage implements ClientInterface {
|
||
|
||
const REDIRECT_STATIC_PAGE = 'static_page';
|
||
const REDIRECT_STATIC_PAGE_WITH_CODE = 'static_page_with_code';
|
||
|
||
/**
|
||
* @inheritdoc
|
||
*/
|
||
public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null) {
|
||
$query = OauthClient::find()->andWhere(['id' => $clientId]);
|
||
if ($clientSecret !== null) {
|
||
$query->andWhere(['secret' => $clientSecret]);
|
||
}
|
||
|
||
/** @var OauthClient|null $model */
|
||
$model = $query->one();
|
||
if ($model === null) {
|
||
return null;
|
||
}
|
||
|
||
// TODO: нужно учитывать тип приложения
|
||
/*
|
||
* Для приложений типа "настольный" redirect_uri необязателем - он должен быть по умолчанию равен
|
||
* статичному редиректу на страницу сайта
|
||
* А для приложений типа "сайт" редирект должен быть всегда.
|
||
* Короче это нужно учесть
|
||
*/
|
||
if ($redirectUri !== null) {
|
||
if (in_array($redirectUri, [self::REDIRECT_STATIC_PAGE, self::REDIRECT_STATIC_PAGE_WITH_CODE], true)) {
|
||
// Тут, наверное, нужно проверить тип приложения
|
||
} else {
|
||
if (!StringHelper::startsWith($redirectUri, $model->redirect_uri, false)) {
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
|
||
$entity = $this->hydrate($model);
|
||
$entity->setRedirectUri($redirectUri);
|
||
|
||
return $entity;
|
||
}
|
||
|
||
/**
|
||
* @inheritdoc
|
||
*/
|
||
public function getBySession(OriginalSessionEntity $session) {
|
||
if (!$session instanceof SessionEntity) {
|
||
throw new \ErrorException('This module assumes that $session typeof ' . SessionEntity::class);
|
||
}
|
||
|
||
/** @var OauthClient|null $model */
|
||
$model = OauthClient::findOne($session->getClientId());
|
||
if ($model === null) {
|
||
return null;
|
||
}
|
||
|
||
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->setIsTrusted($model->is_trusted);
|
||
$entity->setRedirectUri($model->redirect_uri);
|
||
|
||
return $entity;
|
||
}
|
||
|
||
}
|