|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\User; |
| 13 | + |
| 14 | +/** |
| 15 | + * UserInterface implementation used by the access-token security workflow with an OIDC server. |
| 16 | + */ |
| 17 | +class OAuth2User implements UserInterface |
| 18 | +{ |
| 19 | + public readonly array $additionalClaims; |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + private array $roles = ['ROLE_USER'], |
| 23 | + // Standard Claims (https://datatracker.ietf.org/doc/html/rfc7662#section-2.2) |
| 24 | + public readonly ?string $scope = null, |
| 25 | + public readonly ?string $clientId = null, |
| 26 | + public readonly ?string $username = null, |
| 27 | + public readonly ?string $tokenType = null, |
| 28 | + public readonly ?int $exp = null, |
| 29 | + public readonly ?int $iat = null, |
| 30 | + public readonly ?int $nbf = null, |
| 31 | + public readonly ?string $sub = null, |
| 32 | + public readonly ?string $aud = null, |
| 33 | + public readonly ?string $iss = null, |
| 34 | + public readonly ?string $jti = null, |
| 35 | + |
| 36 | + // Additional Claims (" |
| 37 | + // Specific implementations MAY extend this structure with |
| 38 | + // their own service-specific response names as top-level members |
| 39 | + // of this JSON object. |
| 40 | + // ") |
| 41 | + ...$additionalClaims, |
| 42 | + ) { |
| 43 | + if ((null === $sub || '' === $sub) && (null === $username || '' === $username)) { |
| 44 | + throw new \InvalidArgumentException('The claim "sub" or "username" must be provided.'); |
| 45 | + } |
| 46 | + |
| 47 | + $this->additionalClaims = $additionalClaims['additionalClaims'] ?? $additionalClaims; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * OIDC or OAuth specs don't have any "role" notion. |
| 52 | + * |
| 53 | + * If you want to implement "roles" from your OIDC server, |
| 54 | + * send a "roles" constructor argument to this object |
| 55 | + * (e.g.: using a custom UserProvider). |
| 56 | + */ |
| 57 | + public function getRoles(): array |
| 58 | + { |
| 59 | + return $this->roles; |
| 60 | + } |
| 61 | + |
| 62 | + public function getUserIdentifier(): string |
| 63 | + { |
| 64 | + return (string) ($this->sub ?? $this->username); |
| 65 | + } |
| 66 | + |
| 67 | + public function eraseCredentials(): void |
| 68 | + { |
| 69 | + } |
| 70 | +} |
0 commit comments