2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2019-08-23 13:58:04 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-06 06:04:09 +05:30
|
|
|
namespace common\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;
|
2024-12-08 21:24:45 +05:30
|
|
|
use Yii;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2024-12-08 21:24:45 +05:30
|
|
|
final class AuthorizationServerFactory {
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2024-12-06 06:04:09 +05:30
|
|
|
public static function build(): AuthorizationServer {
|
2019-12-09 22:01:54 +05:30
|
|
|
$clientsRepo = new Repositories\ClientRepository();
|
|
|
|
$accessTokensRepo = new Repositories\AccessTokenRepository();
|
|
|
|
$publicScopesRepo = new Repositories\PublicScopeRepository();
|
|
|
|
$internalScopesRepo = new Repositories\InternalScopeRepository();
|
|
|
|
$authCodesRepo = new Repositories\AuthCodeRepository();
|
|
|
|
$refreshTokensRepo = new Repositories\RefreshTokenRepository();
|
2024-12-08 21:24:45 +05:30
|
|
|
$deviceCodesRepo = new Repositories\DeviceCodeRepository();
|
2019-12-09 22:01:54 +05:30
|
|
|
|
|
|
|
$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
|
2024-12-02 15:40:55 +05:30
|
|
|
new ResponseTypes\BearerTokenResponse(),
|
2019-12-09 22:01:54 +05:30
|
|
|
);
|
|
|
|
/** @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
|
|
|
|
|
2024-12-08 21:24:45 +05:30
|
|
|
$verificationUri = Yii::$app->request->getHostInfo() . '/code';
|
|
|
|
$deviceCodeGrant = new Grants\DeviceCodeGrant($deviceCodesRepo, $refreshTokensRepo, new DateInterval('PT10M'), $verificationUri);
|
|
|
|
$deviceCodeGrant->setIntervalVisibility(true);
|
|
|
|
$authServer->enableGrantType($deviceCodeGrant, $accessTokenTTL);
|
|
|
|
$deviceCodeGrant->setScopeRepository($publicScopesRepo); // Change repository after enabling
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
return $authServer;
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|