|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\OpenIDClientTest\Issuer\Metadata\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\OpenIDClient\Issuer\Metadata\Provider\DiscoveryProviderInterface; |
|
8
|
|
|
use Facile\OpenIDClient\Issuer\Metadata\Provider\WebFingerProvider; |
|
9
|
|
|
use function http_build_query; |
|
10
|
|
|
use function json_encode; |
|
11
|
|
|
use Facile\OpenIDClientTest\TestCase; |
|
12
|
|
|
use Psr\Http\Client\ClientInterface; |
|
13
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
14
|
|
|
use Psr\Http\Message\RequestInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Psr\Http\Message\StreamInterface; |
|
17
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
18
|
|
|
use Psr\Http\Message\UriInterface; |
|
19
|
|
|
|
|
20
|
|
|
class WebFingerProviderTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testFetch(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$client = $this->prophesize(ClientInterface::class); |
|
25
|
|
|
$requestFactory = $this->prophesize(RequestFactoryInterface::class); |
|
26
|
|
|
$uriFactory = $this->prophesize(UriFactoryInterface::class); |
|
27
|
|
|
$discoveryProvider = $this->prophesize(DiscoveryProviderInterface::class); |
|
28
|
|
|
|
|
29
|
|
|
$resource = '[email protected]'; |
|
30
|
|
|
$provider = new WebFingerProvider( |
|
31
|
|
|
$client->reveal(), |
|
32
|
|
|
$requestFactory->reveal(), |
|
33
|
|
|
$uriFactory->reveal(), |
|
34
|
|
|
$discoveryProvider->reveal() |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$webFingerUrl = $this->prophesize(UriInterface::class); |
|
38
|
|
|
$webFingerUrl1 = $this->prophesize(UriInterface::class); |
|
39
|
|
|
|
|
40
|
|
|
$uriFactory->createUri('https://example.com/.well-known/webfinger') |
|
41
|
|
|
->willReturn($webFingerUrl1->reveal()); |
|
42
|
|
|
$webFingerUrl1->withQuery(http_build_query([ |
|
43
|
|
|
'resource' => 'acct:[email protected]', |
|
44
|
|
|
'rel' => 'http://openid.net/specs/connect/1.0/issuer', |
|
45
|
|
|
])) |
|
46
|
|
|
->willReturn($webFingerUrl->reveal()); |
|
47
|
|
|
$webFingerUrl->__toString()->willReturn('https://example.com/.well-known/webfinger'); |
|
48
|
|
|
|
|
49
|
|
|
$request = $this->prophesize(RequestInterface::class); |
|
50
|
|
|
$request1 = $this->prophesize(RequestInterface::class); |
|
51
|
|
|
$response = $this->prophesize(ResponseInterface::class); |
|
52
|
|
|
$stream = $this->prophesize(StreamInterface::class); |
|
53
|
|
|
|
|
54
|
|
|
$requestFactory->createRequest('GET', $webFingerUrl) |
|
55
|
|
|
->willReturn($request1->reveal()); |
|
56
|
|
|
$request1->withHeader('accept', 'application/json') |
|
57
|
|
|
->willReturn($request->reveal()); |
|
58
|
|
|
|
|
59
|
|
|
$client->sendRequest($request->reveal()) |
|
60
|
|
|
->willReturn($response->reveal()); |
|
61
|
|
|
|
|
62
|
|
|
$response->getStatusCode()->willReturn(200); |
|
63
|
|
|
$response->getBody()->willReturn($stream->reveal()); |
|
64
|
|
|
|
|
65
|
|
|
$responsePayload = [ |
|
66
|
|
|
'links' => [ |
|
67
|
|
|
[ |
|
68
|
|
|
'rel' => 'wrong-foo', |
|
69
|
|
|
'href' => 'wrong-url', |
|
70
|
|
|
], |
|
71
|
|
|
[ |
|
72
|
|
|
'rel' => 'http://openid.net/specs/connect/1.0/issuer', |
|
73
|
|
|
], |
|
74
|
|
|
[ |
|
75
|
|
|
'rel' => 'http://openid.net/specs/connect/1.0/issuer', |
|
76
|
|
|
'href' => 'https://openid-uri', |
|
77
|
|
|
], |
|
78
|
|
|
], |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$stream->__toString()->willReturn(json_encode($responsePayload)); |
|
82
|
|
|
|
|
83
|
|
|
$discoveryProvider->discovery('https://openid-uri')->willReturn([ |
|
84
|
|
|
'issuer' => 'https://openid-uri', |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
static::assertSame(['issuer' => 'https://openid-uri'], $provider->fetch($resource)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|