Debian Codegraph @

lib/3rdParty/composer/facile-it/php-openid-client/src/Issuer/Metadata/Provider/WebFingerProvider.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Facile\OpenIDClient\Issuer\Metadata\Provider;
6
7 use Facile\JoseVerifier\TokenVerifierInterface;
8 use Facile\OpenIDClient\Exception\InvalidArgumentException;
9 use Facile\OpenIDClient\Exception\RuntimeException;
10 use Psr\Http\Client\ClientExceptionInterface;
11 use Psr\Http\Client\ClientInterface;
12 use Psr\Http\Message\RequestFactoryInterface;
13 use Psr\Http\Message\UriFactoryInterface;
14 use Override;
15
16 use function array_key_exists;
17 use function array_pop;
18 use function explode;
19 use function Facile\OpenIDClient\parse_metadata_response;
20 use function http_build_query;
21 use function is_array;
22 use function is_string;
23 use function parse_url;
24 use function preg_match;
25 use function preg_replace;
26 use function strpos;
27 use function substr;
28
29 /**
30 * @psalm-import-type IssuerRemoteMetadataType from TokenVerifierInterface
31 */
32 final class WebFingerProvider implements RemoteProviderInterface, WebFingerProviderInterface
33 {
34 private const OIDC_DISCOVERY = '/.well-known/openid-configuration';
35
36 private const WEBFINGER = '/.well-known/webfinger';
37
38 private const REL = 'http://openid.net/specs/connect/1.0/issuer';
39
40 private const AAD_MULTITENANT_DISCOVERY = 'https://login.microsoftonline.com/common/v2.0$' . self::OIDC_DISCOVERY;
41
42 private ClientInterface $client;
43
44 private RequestFactoryInterface $requestFactory;
45
46 private UriFactoryInterface $uriFactory;
47
48 private DiscoveryProviderInterface $discoveryProvider;
49
50 public function __construct(
51 ClientInterface $client,
52 RequestFactoryInterface $requestFactory,
53 UriFactoryInterface $uriFactory,
54 DiscoveryProviderInterface $discoveryProvider
55 ) {
56 $this->client = $client;
57 $this->requestFactory = $requestFactory;
58 $this->uriFactory = $uriFactory;
59 $this->discoveryProvider = $discoveryProvider;
60 }
61
62 #[Override]
63 public function isAllowedUri(string $uri): bool
64 {
65 return true;
66 }
67
68 #[Override]
69 public function fetch(string $uri): array
70 {
71 $uri = $this->normalizeWebfinger($uri);
72 $parts = explode('@', $uri, 2);
73 if (isset($parts[1])) {
74 $parsedUrl = parse_url('https://' . $parts[1]);
75 } else {
76 $parsedUrl = parse_url($uri);
77 }
78
79 if (! is_array($parsedUrl) || ! array_key_exists('host', $parsedUrl)) {
80 throw new RuntimeException('Unable to parse resource');
81 }
82
83 $host = $parsedUrl['host'];
84
85 /** @var string|int|null $port */
86 $port = $parsedUrl['port'] ?? null;
87
88 if (((int) $port) > 0) {
89 $host .= ':' . ((int) $port);
90 }
91
92 $webFingerUrl = $this->uriFactory->createUri('https://' . $host . self::WEBFINGER)
93 ->withQuery(http_build_query(['resource' => $uri, 'rel' => self::REL]));
94
95 $request = $this->requestFactory->createRequest('GET', $webFingerUrl)
96 ->withHeader('accept', 'application/json');
97
98 try {
99 $data = parse_metadata_response($this->client->sendRequest($request));
100 } catch (ClientExceptionInterface $e) {
101 throw new RuntimeException('Unable to fetch provider metadata', 0, $e);
102 }
103
104 /** @var array<array-key, null|array{rel?: string, href?: string}> $links */
105 $links = $data['links'] ?? [];
106 $href = null;
107 foreach ($links as $link) {
108 if (! is_array($link)) {
109 continue;
110 }
111
112 if (($link['rel'] ?? null) !== self::REL) {
113 continue;
114 }
115
116 if (! array_key_exists('href', $link)) {
117 continue;
118 }
119
120 $href = $link['href'];
121 }
122
123 if (! is_string($href) || 0 !== strpos($href, 'https://')) {
124 throw new InvalidArgumentException('Invalid issuer location');
125 }
126
127 $metadata = $this->discoveryProvider->discovery($href);
128
129 if (($metadata['issuer'] ?? null) !== $href) {
130 throw new RuntimeException('Discovered issuer mismatch');
131 }
132
133 /** @var IssuerRemoteMetadataType $metadata */
134 return $metadata;
135 }
136
137 private function normalizeWebfinger(string $input): string
138 {
139 $hasScheme = static function (string $resource): bool {
140 if (false !== strpos($resource, '://')) {
141 return true;
142 }
143
144 $authority = explode('#', (string) preg_replace('/(\/|\?)/', '#', $resource))[0];
145
146 if (false === ($index = strpos($authority, ':'))) {
147 return false;
148 }
149
150 $hostOrPort = substr($resource, $index + 1);
151
152 return ! (bool) preg_match('/^\d+$/', $hostOrPort);
153 };
154
155 $acctSchemeAssumed = static function (string $input): bool {
156 if (false === strpos($input, '@')) {
157 return false;
158 }
159
160 $parts = explode('@', $input);
161 $host = array_pop($parts);
162
163 return ! (bool) preg_match('/[:\/?]+/', $host);
164 };
165
166 if ($hasScheme($input)) {
167 $output = $input;
168 } elseif ($acctSchemeAssumed($input)) {
169 $output = 'acct:' . $input;
170 } else {
171 $output = 'https://' . $input;
172 }
173
174 return explode('#', $output)[0];
175 }
176 }