Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\OAuth;
use Wotnak\Atproto\Client\AccessTokenProviderInterface;use Wotnak\Atproto\Client\DpopNonceAwareAuthenticatedRequestSignerInterface;
/** * OAuth session bound to an AT Protocol identity. * * @api */final readonly class OAuthSession implements AccessTokenProviderInterface {
/** * @param array<string, mixed>|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<string, mixed> */ 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<string, mixed> $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<string, mixed> $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<string, mixed> */ 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<string, mixed>|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<string, mixed> $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]; }
}