client = $client; $this->requestFactory = $requestFactory; $this->uriFactory = $uriFactory; } #[Override] public function isAllowedUri(string $uri): bool { return (int) preg_match('/https?:\/\//', $uri) > 0; } /** * @return array * * @psalm-return IssuerRemoteMetadataType * * @psalm-suppress MixedReturnTypeCoercion */ #[Override] public function discovery(string $url): array { $uri = $this->uriFactory->createUri($url); $uriPath = $uri->getPath() ?: '/'; if (str_contains($uriPath, '/.well-known/')) { return $this->fetchOpenIdConfiguration((string) $uri); } $uris = [ $uri->withPath(rtrim($uriPath, '/') . self::OIDC_DISCOVERY), $uri->withPath('/' === $uriPath ? self::OAUTH2_DISCOVERY : rtrim($uriPath, '/') . self::OAUTH2_DISCOVERY), ]; foreach ($uris as $wellKnownUri) { try { return $this->fetchOpenIdConfiguration((string) $wellKnownUri); } catch (RuntimeException $e) { } } throw new RuntimeException('Unable to fetch provider metadata'); } /** * @return array * * @psalm-return IssuerRemoteMetadataType */ private function fetchOpenIdConfiguration(string $uri): array { $request = $this->requestFactory->createRequest('GET', $uri) ->withHeader('accept', 'application/json'); try { /** @psalm-var IssuerRemoteMetadataType $data */ $data = parse_metadata_response($this->client->sendRequest($request)); } catch (ClientExceptionInterface $e) { throw new RuntimeException('Unable to fetch provider metadata', 0, $e); } if (! array_key_exists('issuer', $data)) { throw new RuntimeException('Invalid metadata content, no "issuer" key found'); } return $data; } #[Override] public function fetch(string $uri): array { return $this->discovery($uri); } }