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\ClientEntity;
|
|
|
|
|
use api\components\OAuth2\Entities\SessionEntity;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
use common\models\OauthClient;
|
|
|
|
|
use League\OAuth2\Server\Entity\SessionEntity as OriginalSessionEntity;
|
|
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage;
|
|
|
|
|
use League\OAuth2\Server\Storage\ClientInterface;
|
2016-03-20 20:09:09 +05:30
|
|
|
|
use yii\helpers\StringHelper;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
|
|
class ClientStorage extends AbstractStorage implements ClientInterface {
|
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
|
private const REDIRECT_STATIC_PAGE = 'static_page';
|
|
|
|
|
private const REDIRECT_STATIC_PAGE_WITH_CODE = 'static_page_with_code';
|
2016-03-20 20:09:09 +05:30
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null) {
|
2018-02-28 03:57:35 +05:30
|
|
|
|
$model = $this->findClient($clientId);
|
|
|
|
|
if ($model === null) {
|
|
|
|
|
return null;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
|
if ($clientSecret !== null && $clientSecret !== $model->secret) {
|
2016-02-14 23:20:10 +05:30
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 20:09:09 +05:30
|
|
|
|
// TODO: нужно учитывать тип приложения
|
|
|
|
|
/*
|
|
|
|
|
* Для приложений типа "настольный" redirect_uri необязателем - он должен быть по умолчанию равен
|
|
|
|
|
* статичному редиректу на страницу сайта
|
|
|
|
|
* А для приложений типа "сайт" редирект должен быть всегда.
|
|
|
|
|
* Короче это нужно учесть
|
|
|
|
|
*/
|
|
|
|
|
if ($redirectUri !== null) {
|
2016-11-27 03:13:42 +05:30
|
|
|
|
if (in_array($redirectUri, [self::REDIRECT_STATIC_PAGE, self::REDIRECT_STATIC_PAGE_WITH_CODE], true)) {
|
2016-03-20 20:09:09 +05:30
|
|
|
|
// Тут, наверное, нужно проверить тип приложения
|
|
|
|
|
} else {
|
2016-11-27 03:13:42 +05:30
|
|
|
|
if (!StringHelper::startsWith($redirectUri, $model->redirect_uri, false)) {
|
2016-03-20 20:09:09 +05:30
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
|
$entity = $this->hydrate($model);
|
|
|
|
|
$entity->setRedirectUri($redirectUri);
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function getBySession(OriginalSessionEntity $session) {
|
|
|
|
|
if (!$session instanceof SessionEntity) {
|
|
|
|
|
throw new \ErrorException('This module assumes that $session typeof ' . SessionEntity::class);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
|
$model = $this->findClient($session->getClientId());
|
2016-02-14 23:20:10 +05:30
|
|
|
|
if ($model === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
|
return $this->hydrate($model);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
|
private function hydrate(OauthClient $model): ClientEntity {
|
2016-11-27 03:13:42 +05:30
|
|
|
|
$entity = new ClientEntity($this->server);
|
|
|
|
|
$entity->setId($model->id);
|
|
|
|
|
$entity->setName($model->name);
|
|
|
|
|
$entity->setSecret($model->secret);
|
2016-12-18 04:50:53 +05:30
|
|
|
|
$entity->setIsTrusted($model->is_trusted);
|
2016-11-27 03:13:42 +05:30
|
|
|
|
$entity->setRedirectUri($model->redirect_uri);
|
|
|
|
|
|
|
|
|
|
return $entity;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
|
private function findClient(string $clientId): ?OauthClient {
|
|
|
|
|
return OauthClient::findOne($clientId);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
|
}
|