2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2019-08-23 13:58:04 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-11-27 03:13:42 +05:30
|
|
|
namespace api\components\OAuth2;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2019-12-06 21:01:04 +05:30
|
|
|
use Carbon\CarbonInterval;
|
2019-08-23 13:58:04 +05:30
|
|
|
use DateInterval;
|
2016-02-14 23:20:10 +05:30
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
2017-06-17 23:35:36 +05:30
|
|
|
use yii\base\Component as BaseComponent;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
class Component extends BaseComponent {
|
2016-02-14 23:20:10 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AuthorizationServer
|
|
|
|
*/
|
|
|
|
private $_authServer;
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
public function getAuthServer(): AuthorizationServer {
|
2016-02-14 23:20:10 +05:30
|
|
|
if ($this->_authServer === null) {
|
2019-12-09 22:01:54 +05:30
|
|
|
$this->_authServer = $this->createAuthServer();
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_authServer;
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
private function createAuthServer(): AuthorizationServer {
|
|
|
|
$clientsRepo = new Repositories\ClientRepository();
|
|
|
|
$accessTokensRepo = new Repositories\AccessTokenRepository();
|
|
|
|
$publicScopesRepo = new Repositories\PublicScopeRepository();
|
|
|
|
$internalScopesRepo = new Repositories\InternalScopeRepository();
|
|
|
|
$authCodesRepo = new Repositories\AuthCodeRepository();
|
|
|
|
$refreshTokensRepo = new Repositories\RefreshTokenRepository();
|
|
|
|
|
|
|
|
$accessTokenTTL = CarbonInterval::create(-1); // Set negative value to make tokens non expiring
|
|
|
|
|
|
|
|
$authServer = new AuthorizationServer(
|
|
|
|
$clientsRepo,
|
|
|
|
$accessTokensRepo,
|
|
|
|
new Repositories\EmptyScopeRepository(),
|
|
|
|
new Keys\EmptyKey(),
|
|
|
|
'', // Omit the key because we use our own encryption mechanism
|
|
|
|
new ResponseTypes\BearerTokenResponse()
|
|
|
|
);
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
$authCodeGrant = new Grants\AuthCodeGrant($authCodesRepo, $refreshTokensRepo, new DateInterval('PT10M'));
|
|
|
|
$authCodeGrant->disableRequireCodeChallengeForPublicClients();
|
|
|
|
$authServer->enableGrantType($authCodeGrant, $accessTokenTTL);
|
|
|
|
$authCodeGrant->setScopeRepository($publicScopesRepo); // Change repository after enabling
|
|
|
|
|
|
|
|
$refreshTokenGrant = new Grants\RefreshTokenGrant($refreshTokensRepo);
|
|
|
|
$authServer->enableGrantType($refreshTokenGrant, $accessTokenTTL);
|
|
|
|
$refreshTokenGrant->setScopeRepository($publicScopesRepo); // Change repository after enabling
|
|
|
|
|
|
|
|
$clientCredentialsGrant = new Grants\ClientCredentialsGrant();
|
|
|
|
$authServer->enableGrantType($clientCredentialsGrant, $accessTokenTTL);
|
|
|
|
$clientCredentialsGrant->setScopeRepository($internalScopesRepo); // Change repository after enabling
|
|
|
|
|
|
|
|
return $authServer;
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|