vendor/trikoder/oauth2-bundle/Security/Authentication/Token/OAuth2Token.php line 11

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. final class OAuth2Token extends AbstractToken
  8. {
  9.     /**
  10.      * @var string
  11.      */
  12.     private $providerKey;
  13.     public function __construct(
  14.         ServerRequestInterface $serverRequest,
  15.         ?UserInterface $user,
  16.         string $rolePrefix,
  17.         string $providerKey
  18.     ) {
  19.         $this->setAttribute('server_request'$serverRequest);
  20.         $this->setAttribute('role_prefix'$rolePrefix);
  21.         $roles $this->buildRolesFromScopes();
  22.         if (null !== $user) {
  23.             // Merge the user's roles with the OAuth 2.0 scopes.
  24.             $roles array_merge($roles$user->getRoles());
  25.             $this->setUser($user);
  26.         }
  27.         parent::__construct(array_unique($roles));
  28.         $this->providerKey $providerKey;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function getCredentials()
  34.     {
  35.         return $this->getAttribute('server_request')->getAttribute('oauth_access_token_id');
  36.     }
  37.     public function getProviderKey(): string
  38.     {
  39.         return $this->providerKey;
  40.     }
  41.     public function __serialize(): array
  42.     {
  43.         return [$this->providerKeyparent::__serialize()];
  44.     }
  45.     public function __unserialize(array $data): void
  46.     {
  47.         [$this->providerKey$parentData] = $data;
  48.         parent::__unserialize($parentData);
  49.     }
  50.     private function buildRolesFromScopes(): array
  51.     {
  52.         $prefix $this->getAttribute('role_prefix');
  53.         $roles = [];
  54.         foreach ($this->getAttribute('server_request')->getAttribute('oauth_scopes', []) as $scope) {
  55.             $roles[] = strtoupper(trim($prefix $scope));
  56.         }
  57.         return $roles;
  58.     }
  59. }