|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TMV\OpenIdClient\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
|
8
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
|
9
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
|
10
|
|
|
use Psr\Http\Client\ClientInterface; |
|
11
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
12
|
|
|
use TMV\OpenIdClient\Exception\RuntimeException; |
|
13
|
|
|
use function TMV\OpenIdClient\parseMetadataResponse; |
|
14
|
|
|
|
|
15
|
|
|
class DiscoveryMetadataProvider implements DiscoveryMetadataProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ClientInterface */ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** @var RequestFactoryInterface */ |
|
21
|
|
|
private $requestFactory; |
|
22
|
|
|
|
|
23
|
1 |
|
public function __construct( |
|
24
|
|
|
?ClientInterface $client = null, |
|
25
|
|
|
?RequestFactoryInterface $requestFactory = null |
|
26
|
|
|
) { |
|
27
|
1 |
|
$this->client = $client ?: Psr18ClientDiscovery::find(); |
|
28
|
1 |
|
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function discovery(string $uri): array |
|
32
|
|
|
{ |
|
33
|
1 |
|
$request = $this->requestFactory->createRequest('GET', $uri) |
|
34
|
1 |
|
->withHeader('accept', 'application/json') |
|
35
|
1 |
|
->withHeader('content-type', 'application/json'); |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
1 |
|
$response = $this->client->sendRequest($request); |
|
39
|
|
|
} catch (ClientExceptionInterface $e) { |
|
40
|
|
|
throw new RuntimeException('Unable to fetch provider metadata', 0, $e); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$data = parseMetadataResponse($response, 200); |
|
44
|
|
|
|
|
45
|
1 |
|
if (! \array_key_exists('issuer', $data)) { |
|
46
|
|
|
throw new RuntimeException('Invalid metadata content, no "issuer" key found'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
return $data; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|