|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\OpenIDClientTest\Issuer\Metadata\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\OpenIDClient\Issuer\Metadata\Provider\DiscoveryProvider; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Psr\Http\Client\ClientInterface; |
|
11
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
12
|
|
|
use Psr\Http\Message\RequestInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\StreamInterface; |
|
15
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
16
|
|
|
use Psr\Http\Message\UriInterface; |
|
17
|
|
|
|
|
18
|
|
|
class DiscoveryProviderTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ClientInterface|ObjectProphecy */ |
|
21
|
|
|
private $client; |
|
22
|
|
|
|
|
23
|
|
|
/** @var RequestFactoryInterface|ObjectProphecy */ |
|
24
|
|
|
private $requestFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** @var UriFactoryInterface|ObjectProphecy */ |
|
27
|
|
|
private $uriFactory; |
|
28
|
|
|
|
|
29
|
|
|
protected function setUp(): void |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
|
|
33
|
|
|
$this->client = $this->prophesize(ClientInterface::class); |
|
34
|
|
|
$this->requestFactory = $this->prophesize(RequestFactoryInterface::class); |
|
35
|
|
|
$this->uriFactory = $this->prophesize(UriFactoryInterface::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $uri |
|
40
|
|
|
*/ |
|
41
|
|
|
private function prepareForDiscovery(string $uri, string $baseUri = ''): void |
|
42
|
|
|
{ |
|
43
|
|
|
$client = $this->client; |
|
44
|
|
|
$requestFactory = $this->requestFactory; |
|
45
|
|
|
$uriFactory = $this->uriFactory; |
|
46
|
|
|
|
|
47
|
|
|
$request1 = $this->prophesize(RequestInterface::class); |
|
48
|
|
|
$request2 = $this->prophesize(RequestInterface::class); |
|
49
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
|
50
|
|
|
$stream = $this->prophesize(StreamInterface::class); |
|
51
|
|
|
|
|
52
|
|
|
$response->getBody()->willReturn($stream->reveal()); |
|
53
|
|
|
$response->getStatusCode()->willReturn(200); |
|
54
|
|
|
$stream->__toString()->willReturn('{"issuer":"https://openid-uri' . $baseUri . '"}'); |
|
55
|
|
|
|
|
56
|
|
|
$request1->withHeader('accept', 'application/json') |
|
57
|
|
|
->shouldBeCalled() |
|
58
|
|
|
->willReturn($request2->reveal()); |
|
59
|
|
|
|
|
60
|
|
|
$uri1 = $this->prophesize(UriInterface::class); |
|
61
|
|
|
|
|
62
|
|
|
$uri1->getPath()->willReturn($baseUri . '/.well-known/openid-configuration'); |
|
63
|
|
|
|
|
64
|
|
|
$uri1->__toString()->willReturn('https://example.com' . $baseUri . '/.well-known/openid-configuration'); |
|
65
|
|
|
|
|
66
|
|
|
$requestFactory->createRequest('GET', 'https://example.com' . $baseUri . '/.well-known/openid-configuration') |
|
|
|
|
|
|
67
|
|
|
->willReturn($request1->reveal()); |
|
68
|
|
|
|
|
69
|
|
|
$client->sendRequest($request2->reveal()) |
|
|
|
|
|
|
70
|
|
|
->willReturn($response->reveal()); |
|
71
|
|
|
|
|
72
|
|
|
$uriFactory->createUri($uri)->willReturn($uri1->reveal()); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testDiscovery(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$client = $this->client; |
|
78
|
|
|
$requestFactory = $this->requestFactory; |
|
79
|
|
|
$uriFactory = $this->uriFactory; |
|
80
|
|
|
|
|
81
|
|
|
$uri = 'https://example.com'; |
|
82
|
|
|
$provider = new DiscoveryProvider( |
|
83
|
|
|
$client->reveal(), |
|
|
|
|
|
|
84
|
|
|
$requestFactory->reveal(), |
|
|
|
|
|
|
85
|
|
|
$uriFactory->reveal() |
|
|
|
|
|
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$uri2 = $this->prophesize(UriInterface::class); |
|
89
|
|
|
$uri2->__toString()->willReturn('https://example.com'); |
|
90
|
|
|
$uri2->getPath()->willReturn('/'); |
|
91
|
|
|
|
|
92
|
|
|
$uriFactory->createUri('https://example.com') |
|
|
|
|
|
|
93
|
|
|
->willReturn($uri2->reveal()); |
|
94
|
|
|
|
|
95
|
|
|
$uriOpenid = $this->prophesize(UriInterface::class); |
|
96
|
|
|
$uriOpenid->__toString()->willReturn('https://example.com/.well-known/openid-configuration'); |
|
97
|
|
|
$uri2->withPath('/.well-known/openid-configuration') |
|
98
|
|
|
->willReturn($uriOpenid->reveal()); |
|
99
|
|
|
|
|
100
|
|
|
$uriOAuth = $this->prophesize(UriInterface::class); |
|
101
|
|
|
$uriOAuth->__toString()->willReturn('https://example.com/.well-known/openid-configuration'); |
|
102
|
|
|
$uri2->withPath('/.well-known/oauth-authorization-server') |
|
103
|
|
|
->willReturn($uriOAuth->reveal()); |
|
104
|
|
|
|
|
105
|
|
|
$this->prepareForDiscovery('https://example.com/.well-known/openid-configuration'); |
|
106
|
|
|
|
|
107
|
|
|
static::assertSame(['issuer' => 'https://openid-uri'], $provider->discovery($uri)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testDiscoveryWithBaseUri(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$client = $this->client; |
|
113
|
|
|
$requestFactory = $this->requestFactory; |
|
114
|
|
|
$uriFactory = $this->uriFactory; |
|
115
|
|
|
|
|
116
|
|
|
$baseUri = '/realms/office'; |
|
117
|
|
|
$uri = 'https://example.com' . $baseUri; |
|
118
|
|
|
$provider = new DiscoveryProvider( |
|
119
|
|
|
$client->reveal(), |
|
|
|
|
|
|
120
|
|
|
$requestFactory->reveal(), |
|
|
|
|
|
|
121
|
|
|
$uriFactory->reveal() |
|
|
|
|
|
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
$uri2 = $this->prophesize(UriInterface::class); |
|
125
|
|
|
$uri2->__toString()->willReturn('https://example.com' . $baseUri); |
|
126
|
|
|
$uri2->getPath()->willReturn($baseUri); |
|
127
|
|
|
|
|
128
|
|
|
$uriFactory->createUri('https://example.com' . $baseUri) |
|
|
|
|
|
|
129
|
|
|
->willReturn($uri2->reveal()); |
|
130
|
|
|
|
|
131
|
|
|
$uriOpenid = $this->prophesize(UriInterface::class); |
|
132
|
|
|
$uriOpenid->__toString()->willReturn('https://example.com' . $baseUri . '/.well-known/openid-configuration'); |
|
133
|
|
|
$uri2->withPath($baseUri . '/.well-known/openid-configuration') |
|
134
|
|
|
->willReturn($uriOpenid->reveal()); |
|
135
|
|
|
|
|
136
|
|
|
$uriOAuth = $this->prophesize(UriInterface::class); |
|
137
|
|
|
$uriOAuth->__toString()->willReturn('https://example.com' . $baseUri . '/.well-known/openid-configuration'); |
|
138
|
|
|
$uri2->withPath($baseUri . '/.well-known/oauth-authorization-server') |
|
139
|
|
|
->willReturn($uriOAuth->reveal()); |
|
140
|
|
|
|
|
141
|
|
|
$this->prepareForDiscovery('https://example.com' . $baseUri . '/.well-known/openid-configuration', $baseUri); |
|
142
|
|
|
|
|
143
|
|
|
static::assertSame(['issuer' => 'https://openid-uri' . $baseUri], $provider->discovery($uri)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testDiscoveryWithWellKnown(): void |
|
147
|
|
|
{ |
|
148
|
|
|
$client = $this->client; |
|
149
|
|
|
$requestFactory = $this->requestFactory; |
|
150
|
|
|
$uriFactory = $this->uriFactory; |
|
151
|
|
|
|
|
152
|
|
|
$uri = 'https://example.com/.well-known/openid-configuration'; |
|
153
|
|
|
$provider = new DiscoveryProvider( |
|
154
|
|
|
$client->reveal(), |
|
|
|
|
|
|
155
|
|
|
$requestFactory->reveal(), |
|
|
|
|
|
|
156
|
|
|
$uriFactory->reveal() |
|
|
|
|
|
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
$this->prepareForDiscovery('https://example.com/.well-known/openid-configuration'); |
|
160
|
|
|
|
|
161
|
|
|
static::assertSame(['issuer' => 'https://openid-uri'], $provider->discovery($uri)); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: