Implemented device code grant

This commit is contained in:
ErickSkrauch
2024-12-08 16:54:45 +01:00
parent c7d192d14e
commit 2cc27d34ad
28 changed files with 665 additions and 171 deletions

View File

@@ -6,9 +6,9 @@ namespace common\components\OAuth2;
use Carbon\CarbonInterval;
use DateInterval;
use League\OAuth2\Server\AuthorizationServer;
use yii\base\Component as BaseComponent;
use Yii;
final class AuthorizationServerFactory extends BaseComponent {
final class AuthorizationServerFactory {
public static function build(): AuthorizationServer {
$clientsRepo = new Repositories\ClientRepository();
@@ -17,6 +17,7 @@ final class AuthorizationServerFactory extends BaseComponent {
$internalScopesRepo = new Repositories\InternalScopeRepository();
$authCodesRepo = new Repositories\AuthCodeRepository();
$refreshTokensRepo = new Repositories\RefreshTokenRepository();
$deviceCodesRepo = new Repositories\DeviceCodeRepository();
$accessTokenTTL = CarbonInterval::create(-1); // Set negative value to make tokens non expiring
@@ -42,6 +43,12 @@ final class AuthorizationServerFactory extends BaseComponent {
$authServer->enableGrantType($clientCredentialsGrant, $accessTokenTTL);
$clientCredentialsGrant->setScopeRepository($internalScopesRepo); // Change repository after enabling
$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
return $authServer;
}