2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2016-11-27 03:13:42 +05:30
|
|
|
namespace api\components\OAuth2;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
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\Utils\KeyAlgorithm\UuidAlgorithm;
|
2016-02-14 23:20:10 +05:30
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
2016-06-18 19:08:15 +05:30
|
|
|
use League\OAuth2\Server\Grant;
|
|
|
|
use League\OAuth2\Server\Util\SecureKey;
|
2016-02-14 23:20:10 +05:30
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property AuthorizationServer $authServer
|
|
|
|
*/
|
|
|
|
class Component extends \yii\base\Component {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AuthorizationServer
|
|
|
|
*/
|
|
|
|
private $_authServer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $grantTypes = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array grant type => class
|
|
|
|
*/
|
|
|
|
public $grantMap = [
|
2016-06-18 19:08:15 +05:30
|
|
|
'authorization_code' => Grant\AuthCodeGrant::class,
|
|
|
|
'client_credentials' => Grant\ClientCredentialsGrant::class,
|
|
|
|
'password' => Grant\PasswordGrant::class,
|
|
|
|
'refresh_token' => Grant\RefreshTokenGrant::class,
|
2016-02-14 23:20:10 +05:30
|
|
|
];
|
|
|
|
|
|
|
|
public function getAuthServer() {
|
|
|
|
if ($this->_authServer === null) {
|
|
|
|
$authServer = new AuthorizationServer();
|
2016-11-27 03:13:42 +05:30
|
|
|
$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(',');
|
2017-03-07 21:39:41 +05:30
|
|
|
$authServer->setAccessTokenTTL(86400); // 1d
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
$this->_authServer = $authServer;
|
|
|
|
|
|
|
|
foreach ($this->grantTypes as $grantType) {
|
2016-11-27 03:13:42 +05:30
|
|
|
if (!isset($this->grantMap[$grantType])) {
|
2016-02-14 23:20:10 +05:30
|
|
|
throw new InvalidConfigException('Invalid grant type');
|
|
|
|
}
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
/** @var Grant\GrantTypeInterface $grant */
|
2016-02-14 23:20:10 +05:30
|
|
|
$grant = new $this->grantMap[$grantType]();
|
|
|
|
$this->_authServer->addGrantType($grant);
|
|
|
|
}
|
2016-06-18 19:08:15 +05:30
|
|
|
|
|
|
|
SecureKey::setAlgorithm(new UuidAlgorithm());
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_authServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|