Все реализации Grant'ов для oAuth перенесены в проект. Форк league/oauth2-client больше не используется

This commit is contained in:
ErickSkrauch
2017-06-17 21:05:36 +03:00
parent 33148a5ac7
commit cb068b9dc0
8 changed files with 415 additions and 164 deletions

View File

@@ -1,67 +1,40 @@
<?php
namespace api\components\OAuth2;
use api\components\OAuth2\Storage\AuthCodeStorage;
use api\components\OAuth2\Storage\RefreshTokenStorage;
use api\components\OAuth2\Storage\AccessTokenStorage;
use api\components\OAuth2\Storage\ClientStorage;
use api\components\OAuth2\Storage\ScopeStorage;
use api\components\OAuth2\Storage\SessionStorage;
use api\components\OAuth2\Storage;
use api\components\OAuth2\Utils\KeyAlgorithm\UuidAlgorithm;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Grant;
use League\OAuth2\Server\Util\SecureKey;
use yii\base\InvalidConfigException;
use yii\base\Component as BaseComponent;
/**
* @property AuthorizationServer $authServer
*/
class Component extends \yii\base\Component {
class Component extends BaseComponent {
/**
* @var AuthorizationServer
*/
private $_authServer;
/**
* @var string[]
*/
public $grantTypes = [];
/**
* @var array grant type => class
*/
public $grantMap = [
'authorization_code' => Grant\AuthCodeGrant::class,
'client_credentials' => Grant\ClientCredentialsGrant::class,
'password' => Grant\PasswordGrant::class,
'refresh_token' => Grant\RefreshTokenGrant::class,
];
public function getAuthServer() {
public function getAuthServer(): AuthorizationServer {
if ($this->_authServer === null) {
$authServer = new AuthorizationServer();
$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->setAccessTokenStorage(new Storage\AccessTokenStorage());
$authServer->setClientStorage(new Storage\ClientStorage());
$authServer->setScopeStorage(new Storage\ScopeStorage());
$authServer->setSessionStorage(new Storage\SessionStorage());
$authServer->setAuthCodeStorage(new Storage\AuthCodeStorage());
$authServer->setRefreshTokenStorage(new Storage\RefreshTokenStorage());
$authServer->setScopeDelimiter(',');
$authServer->setAccessTokenTTL(86400); // 1d
$authServer->addGrantType(new Grants\AuthCodeGrant());
$authServer->addGrantType(new Grants\RefreshTokenGrant());
$authServer->addGrantType(new Grants\ClientCredentialsGrant());
$this->_authServer = $authServer;
foreach ($this->grantTypes as $grantType) {
if (!isset($this->grantMap[$grantType])) {
throw new InvalidConfigException('Invalid grant type');
}
/** @var Grant\GrantTypeInterface $grant */
$grant = new $this->grantMap[$grantType]();
$this->_authServer->addGrantType($grant);
}
SecureKey::setAlgorithm(new UuidAlgorithm());
}