$scopes * @param array|null $dpopPrivateJwk */ public function __construct( #[\SensitiveParameter] public string $state, #[\SensitiveParameter] public string $codeVerifier, public string $redirectUri, public string $authorizationServer, public string $clientId, public array $scopes = ['atproto'], public ?string $did = NULL, public ?string $handle = NULL, public ?string $pdsUrl = NULL, #[\SensitiveParameter] public ?array $dpopPrivateJwk = NULL, public ?string $dpopAlgorithm = 'ES256', public ?string $authorizationServerNonce = NULL, public ?int $createdAt = NULL, ) {} /** * Returns the authorization state for persistence. * * @return array */ public function toArray(): array { return [ 'state' => $this->state, 'codeVerifier' => $this->codeVerifier, 'redirectUri' => $this->redirectUri, 'authorizationServer' => $this->authorizationServer, 'clientId' => $this->clientId, 'scopes' => $this->scopes, 'did' => $this->did, 'handle' => $this->handle, 'pdsUrl' => $this->pdsUrl, 'dpopPrivateJwk' => $this->dpopPrivateJwk, 'dpopAlgorithm' => $this->dpopAlgorithm, 'authorizationServerNonce' => $this->authorizationServerNonce, 'createdAt' => $this->createdAt, ]; } /** * @internal */ public function withAuthorizationServerNonce(?string $nonce): self { return new self( state: $this->state, codeVerifier: $this->codeVerifier, redirectUri: $this->redirectUri, authorizationServer: $this->authorizationServer, clientId: $this->clientId, scopes: $this->scopes, did: $this->did, handle: $this->handle, pdsUrl: $this->pdsUrl, dpopPrivateJwk: $this->dpopPrivateJwk, dpopAlgorithm: $this->dpopAlgorithm, authorizationServerNonce: $nonce, createdAt: $this->createdAt, ); } /** * Restores authorization state from its persisted representation. * * @param array $data */ public static function fromArray(#[\SensitiveParameter] array $data): self { $state = self::requiredString($data, 'state'); $codeVerifier = self::requiredString($data, 'codeVerifier'); $redirectUri = self::requiredString($data, 'redirectUri'); $authorizationServer = self::requiredString($data, 'authorizationServer'); $clientId = self::requiredString($data, 'clientId'); $scopes = ['atproto']; if (\array_key_exists('scopes', $data)) { if (!\is_array($data['scopes']) || !\array_is_list($data['scopes']) || $data['scopes'] === []) { throw new \InvalidArgumentException('OAuth authorization state "scopes" must be a non-empty list.'); } $scopes = []; foreach ($data['scopes'] as $scope) { if (!(\is_string($scope) && $scope !== '')) { throw new \InvalidArgumentException('OAuth authorization state scopes must be non-empty strings.'); } $scopes[] = $scope; } if (!\in_array('atproto', $scopes, TRUE)) { throw new \InvalidArgumentException('OAuth authorization state scopes must include "atproto".'); } } $dpopPrivateJwk = self::optionalStringKeyedArray($data, 'dpopPrivateJwk'); return new self( state: $state, codeVerifier: $codeVerifier, redirectUri: $redirectUri, authorizationServer: $authorizationServer, clientId: $clientId, scopes: $scopes, did: self::optionalString($data, 'did'), handle: self::optionalString($data, 'handle'), pdsUrl: self::optionalString($data, 'pdsUrl'), dpopPrivateJwk: $dpopPrivateJwk, dpopAlgorithm: \array_key_exists('dpopAlgorithm', $data) ? self::optionalString($data, 'dpopAlgorithm') : 'ES256', authorizationServerNonce: self::optionalString($data, 'authorizationServerNonce'), createdAt: self::optionalInteger($data, 'createdAt'), ); } /** * @internal * * @return array */ public function __debugInfo(): array { return [ 'state' => '[redacted]', 'codeVerifier' => '[redacted]', 'redirectUri' => $this->redirectUri, 'authorizationServer' => $this->authorizationServer, 'clientId' => $this->clientId, 'scopes' => $this->scopes, 'did' => $this->did, 'handle' => $this->handle, 'pdsUrl' => $this->pdsUrl, 'dpopPrivateJwk' => $this->dpopPrivateJwk === NULL ? NULL : '[redacted]', 'dpopAlgorithm' => $this->dpopAlgorithm, 'authorizationServerNonce' => $this->authorizationServerNonce === NULL ? NULL : '[redacted]', 'createdAt' => $this->createdAt, ]; } /** * @param array $values */ private static function requiredString(array $values, string $key): string { $value = $values[$key] ?? NULL; if (!\is_string($value) || $value === '') { throw new \InvalidArgumentException(\sprintf('OAuth authorization state is missing "%s".', $key)); } return $value; } /** * @return array|null */ private static function optionalStringKeyedArray(array $values, string $key): ?array { if (!\array_key_exists($key, $values) || $values[$key] === NULL) { return NULL; } $value = $values[$key]; if (!\is_array($value)) { throw new \InvalidArgumentException(\sprintf('OAuth authorization state "%s" must be an object or null.', $key)); } $normalized = []; foreach ($value as $key => $item) { if (!\is_string($key)) { throw new \InvalidArgumentException('OAuth DPoP private JWK keys must be strings.'); } $normalized[$key] = $item; } return $normalized; } /** @param array $values */ private static function optionalString(array $values, string $key): ?string { if (!\array_key_exists($key, $values) || $values[$key] === NULL) { return NULL; } if (!\is_string($values[$key]) || $values[$key] === '') { throw new \InvalidArgumentException(\sprintf( 'OAuth authorization state "%s" must be a non-empty string or null.', $key, )); } return $values[$key]; } /** @param array $values */ private static function optionalInteger(array $values, string $key): ?int { if (!\array_key_exists($key, $values) || $values[$key] === NULL) { return NULL; } if (!\is_int($values[$key])) { throw new \InvalidArgumentException(\sprintf('OAuth authorization state "%s" must be an integer or null.', $key)); } return $values[$key]; } }