Debian Codegraph @

lib/3rdParty/composer/facile-it/php-openid-client/src/Service/IntrospectionService.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\OpenIDClient\Service;
6
7 use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
8 use Facile\OpenIDClient\Exception\RuntimeException;
9 use Psr\Http\Client\ClientExceptionInterface;
10 use Psr\Http\Client\ClientInterface;
11 use Psr\Http\Message\RequestFactoryInterface;
12
13 use function Facile\OpenIDClient\get_endpoint_uri;
14 use function Facile\OpenIDClient\parse_metadata_response;
15
16 /**
17 * RFC 7662 Token Introspection.
18 *
19 * @see https://tools.ietf.org/html/rfc7662 RFC 7662
20 *
21 * @psalm-api
22 */
23 final class IntrospectionService
24 {
25 private ClientInterface $client;
26
27 private RequestFactoryInterface $requestFactory;
28
29 public function __construct(
30 ClientInterface $client,
31 RequestFactoryInterface $requestFactory
32 ) {
33 $this->client = $client;
34 $this->requestFactory = $requestFactory;
35 }
36
37 /**
38 * @param array<string, mixed> $params
39 *
40 * @return array<string, mixed>
41 */
42 public function introspect(OpenIDClient $client, string $token, array $params = []): array
43 {
44 $endpointUri = get_endpoint_uri($client, 'introspection_endpoint');
45
46 $authMethod = $client->getAuthMethodFactory()
47 ->create($client->getMetadata()->getIntrospectionEndpointAuthMethod());
48
49 $tokenRequest = $this->requestFactory->createRequest('POST', $endpointUri)
50 ->withHeader('content-type', 'application/x-www-form-urlencoded');
51
52 $params += [
53 'token' => $token,
54 'aud' => $client->getIssuer()->getMetadata()->getIntrospectionEndpoint(),
55 ];
56 $tokenRequest = $authMethod->createRequest($tokenRequest, $client, $params);
57
58 $httpClient = $client->getHttpClient() ?? $this->client;
59
60 try {
61 $response = $httpClient->sendRequest($tokenRequest);
62 } catch (ClientExceptionInterface $e) {
63 throw new RuntimeException('Unable to get introspection response', 0, $e);
64 }
65
66 return parse_metadata_response($response, 200);
67 }
68 }