$scopes * @param list $redirectUris */ public function __construct( public string $clientId, public array $redirectUris, public array $scopes = ['atproto'], public ?string $clientName = NULL, public ?string $clientUri = NULL, public ?string $logoUri = NULL, public ?string $jwksUri = NULL, public bool $requestRefreshToken = TRUE, ) { $clientParts = self::assertHttpsUrlWithoutPort($this->clientId, 'OAuth client ID'); if ($this->redirectUris === []) { throw new \InvalidArgumentException('At least one OAuth redirect URI is required.'); } foreach ($this->redirectUris as $redirectUri) { if ($redirectUri === '') { throw new \InvalidArgumentException('OAuth redirect URIs must be non-empty strings.'); } self::assertHttpsUrlWithoutDefaultPort($redirectUri, 'OAuth web-client redirect URI'); } if ($this->scopes === []) { throw new \InvalidArgumentException('At least one OAuth scope is required.'); } foreach ($this->scopes as $scope) { if (\preg_match('/^[\x21\x23-\x5B\x5D-\x7E]+$/D', $scope) !== 1) { throw new \InvalidArgumentException('OAuth scopes must be non-empty scope-token strings.'); } } if (!\in_array('atproto', $this->scopes, TRUE)) { throw new \InvalidArgumentException('ATProto OAuth scopes must include "atproto".'); } if ($this->clientUri !== NULL) { $clientUriParts = self::assertHttpsUrl($this->clientUri, 'OAuth client URI'); if (\strcasecmp($clientParts['host'], $clientUriParts['host']) !== 0) { throw new \InvalidArgumentException('OAuth client URI must use the client ID hostname.'); } } if ($this->logoUri !== NULL) { self::assertHttpsUrl($this->logoUri, 'OAuth logo URI'); } if ($this->jwksUri !== NULL) { self::assertHttpsUrl($this->jwksUri, 'OAuth JWKS URI'); } } /** * Returns the requested scopes as an OAuth scope string. */ public function scopeString(): string { return \implode(' ', $this->scopes); } /** * Returns the OAuth grant types declared in client metadata. * * @return list */ public function grantTypes(): array { return ( $this->requestRefreshToken ? ['authorization_code', 'refresh_token'] : ['authorization_code'] ); } /** * @return array{scheme: string, host: string, port?: int, path?: string, query?: string} */ private static function assertHttpsUrl(string $url, string $description): array { try { $parts = \parse_url($url); } // ValueError is an expected invalid-input result at this API boundary. // @mago-expect analysis:avoid-catching-error catch (\ValueError) { $parts = FALSE; } if (!\is_array($parts) || \filter_var($url, FILTER_VALIDATE_URL) === FALSE) { throw new \InvalidArgumentException(\sprintf('%s must be a valid HTTPS URL.', $description)); } $scheme = $parts['scheme'] ?? NULL; $host = $parts['host'] ?? NULL; if ( !\is_string($scheme) || \strtolower($scheme) !== 'https' || !\is_string($host) || isset($parts['user']) || isset($parts['pass']) || isset($parts['fragment']) ) { throw new \InvalidArgumentException(\sprintf('%s must be a valid HTTPS URL.', $description)); } $validated = [ 'scheme' => $scheme, 'host' => $host, ]; if (\is_int($parts['port'] ?? NULL)) { $validated['port'] = $parts['port']; } if (\is_string($parts['path'] ?? NULL)) { $validated['path'] = $parts['path']; } if (\is_string($parts['query'] ?? NULL)) { $validated['query'] = $parts['query']; } return $validated; } /** * @return array{scheme: string, host: string, port?: int, path?: string, query?: string} */ private static function assertHttpsUrlWithoutPort(string $url, string $description): array { $parts = self::assertHttpsUrl($url, $description); if (isset($parts['port'])) { throw new \InvalidArgumentException(\sprintf('%s must be a valid HTTPS URL.', $description)); } return $parts; } /** * @return array{scheme: string, host: string, port?: int, path?: string, query?: string} */ private static function assertHttpsUrlWithoutDefaultPort(string $url, string $description): array { $parts = self::assertHttpsUrl($url, $description); if (($parts['port'] ?? NULL) === 443) { throw new \InvalidArgumentException(\sprintf('%s must be a valid HTTPS URL.', $description)); } return $parts; } }