| 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\RuntimeException; |
| 9 |
use Psr\Http\Client\ClientExceptionInterface; |
| 10 |
use Psr\Http\Client\ClientInterface; |
| 11 |
use Psr\Http\Message\RequestFactoryInterface; |
| 12 |
use Psr\Http\Message\UriFactoryInterface; |
| 13 |
use Override; |
| 14 |
|
| 15 |
use function array_key_exists; |
| 16 |
use function Facile\OpenIDClient\parse_metadata_response; |
| 17 |
use function preg_match; |
| 18 |
use function rtrim; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class DiscoveryProvider implements DiscoveryProviderInterface |
| 24 |
{ |
| 25 |
private const OIDC_DISCOVERY = '/.well-known/openid-configuration'; |
| 26 |
|
| 27 |
private const OAUTH2_DISCOVERY = '/.well-known/oauth-authorization-server'; |
| 28 |
|
| 29 |
private ClientInterface $client; |
| 30 |
|
| 31 |
private RequestFactoryInterface $requestFactory; |
| 32 |
|
| 33 |
private UriFactoryInterface $uriFactory; |
| 34 |
|
| 35 |
public function __construct( |
| 36 |
ClientInterface $client, |
| 37 |
RequestFactoryInterface $requestFactory, |
| 38 |
UriFactoryInterface $uriFactory |
| 39 |
) { |
| 40 |
$this->client = $client; |
| 41 |
$this->requestFactory = $requestFactory; |
| 42 |
$this->uriFactory = $uriFactory; |
| 43 |
} |
| 44 |
|
| 45 |
#[Override] |
| 46 |
public function isAllowedUri(string $uri): bool |
| 47 |
{ |
| 48 |
return (int) preg_match('/https?:\/\//', $uri) > 0; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[Override] |
| 59 |
public function discovery(string $url): array |
| 60 |
{ |
| 61 |
$uri = $this->uriFactory->createUri($url); |
| 62 |
$uriPath = $uri->getPath() ?: '/'; |
| 63 |
|
| 64 |
if (str_contains($uriPath, '/.well-known/')) { |
| 65 |
return $this->fetchOpenIdConfiguration((string) $uri); |
| 66 |
} |
| 67 |
|
| 68 |
$uris = [ |
| 69 |
$uri->withPath(rtrim($uriPath, '/') . self::OIDC_DISCOVERY), |
| 70 |
$uri->withPath('/' === $uriPath |
| 71 |
? self::OAUTH2_DISCOVERY |
| 72 |
: rtrim($uriPath, '/') . self::OAUTH2_DISCOVERY), |
| 73 |
]; |
| 74 |
|
| 75 |
foreach ($uris as $wellKnownUri) { |
| 76 |
try { |
| 77 |
return $this->fetchOpenIdConfiguration((string) $wellKnownUri); |
| 78 |
} catch (RuntimeException $e) { |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
throw new RuntimeException('Unable to fetch provider metadata'); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private function fetchOpenIdConfiguration(string $uri): array |
| 91 |
{ |
| 92 |
$request = $this->requestFactory->createRequest('GET', $uri) |
| 93 |
->withHeader('accept', 'application/json'); |
| 94 |
|
| 95 |
try { |
| 96 |
|
| 97 |
$data = parse_metadata_response($this->client->sendRequest($request)); |
| 98 |
} catch (ClientExceptionInterface $e) { |
| 99 |
throw new RuntimeException('Unable to fetch provider metadata', 0, $e); |
| 100 |
} |
| 101 |
|
| 102 |
if (! array_key_exists('issuer', $data)) { |
| 103 |
throw new RuntimeException('Invalid metadata content, no "issuer" key found'); |
| 104 |
} |
| 105 |
|
| 106 |
return $data; |
| 107 |
} |
| 108 |
|
| 109 |
#[Override] |
| 110 |
public function fetch(string $uri): array |
| 111 |
{ |
| 112 |
return $this->discovery($uri); |
| 113 |
} |
| 114 |
} |