|null $dpopPrivateJwk */ public function __construct( public string $did, public ?string $handle, public ?string $pdsUrl, public string $authorizationServer, #[\SensitiveParameter] public TokenSet $tokens, #[\SensitiveParameter] public ?array $dpopPrivateJwk = NULL, public ?string $dpopAlgorithm = 'ES256', public ?string $authorizationServerNonce = NULL, public ?string $resourceServerNonce = NULL, ) {} /** * Returns the access token. */ public function getAccessToken(): string { return $this->tokens->accessToken; } /** * Creates an authenticated session with a caller-provided DPoP signer. */ public function withSigner(DpopNonceAwareAuthenticatedRequestSignerInterface $signer): OAuthAuthenticatedSession { return new OAuthAuthenticatedSession($this, $signer); } /** * Creates an authenticated session using the persisted DPoP key. */ public function authenticatedSession(): OAuthAuthenticatedSession { return new OAuthAuthenticatedSession($this); } /** * @internal */ public function withResourceServerNonce(?string $nonce): self { return new self( did: $this->did, handle: $this->handle, pdsUrl: $this->pdsUrl, authorizationServer: $this->authorizationServer, tokens: $this->tokens, dpopPrivateJwk: $this->dpopPrivateJwk, dpopAlgorithm: $this->dpopAlgorithm, authorizationServerNonce: $this->authorizationServerNonce, resourceServerNonce: $nonce, ); } /** * Returns the OAuth session for encrypted persistence. * * @return array */ public function toArray(): array { return [ 'did' => $this->did, 'handle' => $this->handle, 'pdsUrl' => $this->pdsUrl, 'authorizationServer' => $this->authorizationServer, 'tokens' => $this->tokens->toArray(), 'dpopPrivateJwk' => $this->dpopPrivateJwk, 'dpopAlgorithm' => $this->dpopAlgorithm, 'authorizationServerNonce' => $this->authorizationServerNonce, 'resourceServerNonce' => $this->resourceServerNonce, ]; } /** * Restores an OAuth session from its persisted representation. * * @param array $data */ public static function fromArray(#[\SensitiveParameter] array $data): self { foreach (['did', 'authorizationServer', 'tokens'] as $required) { if (!isset($data[$required])) { throw new \InvalidArgumentException(\sprintf('OAuth session data is missing "%s".', $required)); } } if ( !\is_string($data['did']) || $data['did'] === '' || !\is_string($data['authorizationServer']) || $data['authorizationServer'] === '' || !\is_array($data['tokens']) ) { throw new \InvalidArgumentException('OAuth session data is invalid.'); } /** @var array $tokens */ $tokens = $data['tokens']; $dpopPrivateJwk = self::optionalStringKeyedArray($data, 'dpopPrivateJwk'); return new self( did: $data['did'], handle: self::optionalString($data, 'handle'), pdsUrl: self::optionalString($data, 'pdsUrl'), authorizationServer: $data['authorizationServer'], tokens: TokenSet::fromArray($tokens), dpopPrivateJwk: $dpopPrivateJwk, dpopAlgorithm: \array_key_exists('dpopAlgorithm', $data) ? self::optionalString($data, 'dpopAlgorithm') : 'ES256', authorizationServerNonce: self::optionalString($data, 'authorizationServerNonce'), resourceServerNonce: self::optionalString($data, 'resourceServerNonce'), ); } /** * @internal * * @return array */ public function __debugInfo(): array { return [ 'did' => $this->did, 'handle' => $this->handle, 'pdsUrl' => $this->pdsUrl, 'authorizationServer' => $this->authorizationServer, 'tokens' => '[redacted]', 'dpopPrivateJwk' => $this->dpopPrivateJwk === NULL ? NULL : '[redacted]', 'dpopAlgorithm' => $this->dpopAlgorithm, 'authorizationServerNonce' => $this->authorizationServerNonce === NULL ? NULL : '[redacted]', 'resourceServerNonce' => $this->resourceServerNonce === NULL ? NULL : '[redacted]', ]; } /** * @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 session "%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 session "%s" must be a non-empty string or null.', $key)); } return $values[$key]; } }