|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\OAuth2\HttpClient\Test\Authorization; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\OAuth2\HttpClient\Authorization\CachedProvider; |
|
8
|
|
|
use Facile\OAuth2\HttpClient\Request\OAuth2RequestInterface; |
|
9
|
|
|
use Facile\OpenIDClient\Client\ClientInterface; |
|
10
|
|
|
use Facile\OpenIDClient\Client\Metadata\ClientMetadataInterface; |
|
11
|
|
|
use Facile\OpenIDClient\Issuer\IssuerInterface; |
|
12
|
|
|
use Facile\OpenIDClient\Issuer\Metadata\IssuerMetadataInterface; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
15
|
|
|
|
|
16
|
|
|
class CachedProviderTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testShouldReturnCachedValue(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$issuer = $this->prophesize(IssuerInterface::class); |
|
21
|
|
|
$issuerMetadata = $this->prophesize(IssuerMetadataInterface::class); |
|
22
|
|
|
$clientMetadata = $this->prophesize(ClientMetadataInterface::class); |
|
23
|
|
|
$client = $this->prophesize(ClientInterface::class); |
|
24
|
|
|
$client->getMetadata()->willReturn($clientMetadata->reveal()); |
|
25
|
|
|
$client->getIssuer()->willReturn($issuer->reveal()); |
|
26
|
|
|
$issuer->getMetadata()->willReturn($issuerMetadata->reveal()); |
|
27
|
|
|
$clientMetadata->getClientId()->willReturn('client-id'); |
|
28
|
|
|
$issuerMetadata->getIssuer()->willReturn('https://issuer.com'); |
|
29
|
|
|
$request = $this->prophesize(OAuth2RequestInterface::class); |
|
30
|
|
|
|
|
31
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
32
|
|
|
|
|
33
|
|
|
$grantParams = [ |
|
34
|
|
|
'foo' => 'bar', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
$expectedCacheKey = hash('sha1', json_encode([ |
|
38
|
|
|
'issuer' => 'https://issuer.com', |
|
39
|
|
|
'client_id' => 'client-id', |
|
40
|
|
|
'grantParams' => $grantParams, |
|
41
|
|
|
])); |
|
42
|
|
|
|
|
43
|
|
|
$request->getGrantParams()->willReturn($grantParams); |
|
44
|
|
|
$cache->get($expectedCacheKey)->willReturn('Bearer foo'); |
|
45
|
|
|
|
|
46
|
|
|
$expected = 'Bearer foo'; |
|
47
|
|
|
$provider = new CachedProvider($cache->reveal()); |
|
48
|
|
|
$this->assertSame($expected, $provider->getAuthorization($client->reveal(), $request->reveal())); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testShouldSaveCachedValue(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$issuer = $this->prophesize(IssuerInterface::class); |
|
54
|
|
|
$issuerMetadata = $this->prophesize(IssuerMetadataInterface::class); |
|
55
|
|
|
$clientMetadata = $this->prophesize(ClientMetadataInterface::class); |
|
56
|
|
|
$client = $this->prophesize(ClientInterface::class); |
|
57
|
|
|
$client->getMetadata()->willReturn($clientMetadata->reveal()); |
|
58
|
|
|
$client->getIssuer()->willReturn($issuer->reveal()); |
|
59
|
|
|
$issuer->getMetadata()->willReturn($issuerMetadata->reveal()); |
|
60
|
|
|
$clientMetadata->getClientId()->willReturn('client-id'); |
|
61
|
|
|
$issuerMetadata->getIssuer()->willReturn('https://issuer.com'); |
|
62
|
|
|
$request = $this->prophesize(OAuth2RequestInterface::class); |
|
63
|
|
|
|
|
64
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
65
|
|
|
|
|
66
|
|
|
$grantParams = [ |
|
67
|
|
|
'foo' => 'bar', |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$expectedCacheKey = hash('sha1', json_encode([ |
|
71
|
|
|
'issuer' => 'https://issuer.com', |
|
72
|
|
|
'client_id' => 'client-id', |
|
73
|
|
|
'grantParams' => $grantParams, |
|
74
|
|
|
])); |
|
75
|
|
|
|
|
76
|
|
|
$expected = 'Bearer foo'; |
|
77
|
|
|
|
|
78
|
|
|
$request->getGrantParams()->willReturn($grantParams); |
|
79
|
|
|
$cache->set($expectedCacheKey, $expected, 101) |
|
80
|
|
|
->shouldBeCalled() |
|
81
|
|
|
->willReturn('Bearer foo'); |
|
82
|
|
|
|
|
83
|
|
|
$provider = new CachedProvider($cache->reveal()); |
|
84
|
|
|
$provider->saveAuthorization($client->reveal(), $request->reveal(), $expected, 101); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testShouldSaveCachedValueWithProvidedHashAlg(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$issuer = $this->prophesize(IssuerInterface::class); |
|
90
|
|
|
$issuerMetadata = $this->prophesize(IssuerMetadataInterface::class); |
|
91
|
|
|
$clientMetadata = $this->prophesize(ClientMetadataInterface::class); |
|
92
|
|
|
$client = $this->prophesize(ClientInterface::class); |
|
93
|
|
|
$client->getMetadata()->willReturn($clientMetadata->reveal()); |
|
94
|
|
|
$client->getIssuer()->willReturn($issuer->reveal()); |
|
95
|
|
|
$issuer->getMetadata()->willReturn($issuerMetadata->reveal()); |
|
96
|
|
|
$clientMetadata->getClientId()->willReturn('client-id'); |
|
97
|
|
|
$issuerMetadata->getIssuer()->willReturn('https://issuer.com'); |
|
98
|
|
|
$request = $this->prophesize(OAuth2RequestInterface::class); |
|
99
|
|
|
|
|
100
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
101
|
|
|
|
|
102
|
|
|
$hashAlg = 'sha256'; |
|
103
|
|
|
$grantParams = [ |
|
104
|
|
|
'foo' => 'bar', |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
$expectedCacheKey = hash($hashAlg, json_encode([ |
|
108
|
|
|
'issuer' => 'https://issuer.com', |
|
109
|
|
|
'client_id' => 'client-id', |
|
110
|
|
|
'grantParams' => $grantParams, |
|
111
|
|
|
])); |
|
112
|
|
|
|
|
113
|
|
|
$expected = 'Bearer foo'; |
|
114
|
|
|
|
|
115
|
|
|
$request->getGrantParams()->willReturn($grantParams); |
|
116
|
|
|
$cache->set($expectedCacheKey, $expected, 101) |
|
117
|
|
|
->shouldBeCalled() |
|
118
|
|
|
->willReturn('Bearer foo'); |
|
119
|
|
|
|
|
120
|
|
|
$provider = new CachedProvider($cache->reveal()); |
|
121
|
|
|
$provider->setHashAlg($hashAlg); |
|
122
|
|
|
$provider->saveAuthorization($client->reveal(), $request->reveal(), $expected, 101); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testShouldSaveCachedValueWithDefaultTtl(): void |
|
126
|
|
|
{ |
|
127
|
|
|
$issuer = $this->prophesize(IssuerInterface::class); |
|
128
|
|
|
$issuerMetadata = $this->prophesize(IssuerMetadataInterface::class); |
|
129
|
|
|
$clientMetadata = $this->prophesize(ClientMetadataInterface::class); |
|
130
|
|
|
$client = $this->prophesize(ClientInterface::class); |
|
131
|
|
|
$client->getMetadata()->willReturn($clientMetadata->reveal()); |
|
132
|
|
|
$client->getIssuer()->willReturn($issuer->reveal()); |
|
133
|
|
|
$issuer->getMetadata()->willReturn($issuerMetadata->reveal()); |
|
134
|
|
|
$clientMetadata->getClientId()->willReturn('client-id'); |
|
135
|
|
|
$issuerMetadata->getIssuer()->willReturn('https://issuer.com'); |
|
136
|
|
|
$request = $this->prophesize(OAuth2RequestInterface::class); |
|
137
|
|
|
|
|
138
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
139
|
|
|
|
|
140
|
|
|
$grantParams = [ |
|
141
|
|
|
'foo' => 'bar', |
|
142
|
|
|
]; |
|
143
|
|
|
|
|
144
|
|
|
$expectedCacheKey = hash('sha1', json_encode([ |
|
145
|
|
|
'issuer' => 'https://issuer.com', |
|
146
|
|
|
'client_id' => 'client-id', |
|
147
|
|
|
'grantParams' => $grantParams, |
|
148
|
|
|
])); |
|
149
|
|
|
|
|
150
|
|
|
$expected = 'Bearer foo'; |
|
151
|
|
|
|
|
152
|
|
|
$request->getGrantParams()->willReturn($grantParams); |
|
153
|
|
|
$cache->set($expectedCacheKey, $expected, 501) |
|
154
|
|
|
->shouldBeCalled() |
|
155
|
|
|
->willReturn('Bearer foo'); |
|
156
|
|
|
|
|
157
|
|
|
$provider = new CachedProvider($cache->reveal(), 501); |
|
158
|
|
|
$provider->saveAuthorization($client->reveal(), $request->reveal(), $expected); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|