1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\TokenRevocationEndpoint\Tests; |
15
|
|
|
|
16
|
|
|
use Http\Message\MessageFactory\DiactorosMessageFactory; |
17
|
|
|
use Http\Message\ResponseFactory; |
18
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
19
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
20
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
21
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
22
|
|
|
use OAuth2Framework\Component\Core\Token\Token; |
23
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
24
|
|
|
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenRevocationGetEndpoint; |
25
|
|
|
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHint; |
26
|
|
|
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHintManager; |
27
|
|
|
use PHPUnit\Framework\TestCase; |
28
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @group TokenRevocationEndpoint |
32
|
|
|
*/ |
33
|
|
|
final class TokenRevocationGetEndpointTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function aTokenTypeHintManagerCanHandleTokenTypeHints() |
39
|
|
|
{ |
40
|
|
|
self::assertNotEmpty($this->getTokenTypeHintManager()->getTokenTypeHints()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function theTokenRevocationEndpointReceivesAValidGetRequest() |
47
|
|
|
{ |
48
|
|
|
$endpoint = $this->getTokenRevocationGetEndpoint(); |
49
|
|
|
|
50
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
51
|
|
|
$request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN']); |
52
|
|
|
$request->getAttribute('client')->willReturn($this->getClient()); |
53
|
|
|
|
54
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
55
|
|
|
|
56
|
|
|
$response = $endpoint->process($request->reveal(), $handler->reveal()); |
57
|
|
|
|
58
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
59
|
|
|
$response->getBody()->rewind(); |
60
|
|
|
self::assertEquals('', $response->getBody()->getContents()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function theTokenRevocationEndpointReceivesAValidGetRequestWithTokenTypeHint() |
67
|
|
|
{ |
68
|
|
|
$endpoint = $this->getTokenRevocationGetEndpoint(); |
69
|
|
|
|
70
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
71
|
|
|
$request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN', 'token_type_hint' => 'foo']); |
72
|
|
|
$request->getAttribute('client')->willReturn($this->getClient()); |
73
|
|
|
|
74
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
75
|
|
|
|
76
|
|
|
$response = $endpoint->process($request->reveal(), $handler->reveal()); |
77
|
|
|
|
78
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
79
|
|
|
$response->getBody()->rewind(); |
80
|
|
|
self::assertEquals('', $response->getBody()->getContents()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
|
public function theTokenRevocationEndpointReceivesAValidGetRequestWithCallback() |
87
|
|
|
{ |
88
|
|
|
$endpoint = $this->getTokenRevocationGetEndpoint(); |
89
|
|
|
|
90
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
91
|
|
|
$request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN', 'callback' => 'callThisFunctionPlease']); |
92
|
|
|
$request->getAttribute('client')->willReturn($this->getClient()); |
93
|
|
|
|
94
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
95
|
|
|
|
96
|
|
|
$response = $endpoint->process($request->reveal(), $handler->reveal()); |
97
|
|
|
|
98
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
99
|
|
|
$response->getBody()->rewind(); |
100
|
|
|
self::assertEquals('callThisFunctionPlease()', $response->getBody()->getContents()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
*/ |
106
|
|
|
public function theTokenDoesNotExistAndCannotBeRevoked() |
107
|
|
|
{ |
108
|
|
|
$endpoint = $this->getTokenRevocationGetEndpoint(); |
109
|
|
|
|
110
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
111
|
|
|
$request->getQueryParams()->willReturn(['token' => 'UNKNOWN_TOKEN', 'callback' => 'callThisFunctionPlease']); |
112
|
|
|
$request->getAttribute('client')->willReturn($this->getClient()); |
113
|
|
|
|
114
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
115
|
|
|
|
116
|
|
|
$response = $endpoint->process($request->reveal(), $handler->reveal()); |
117
|
|
|
|
118
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
119
|
|
|
$response->getBody()->rewind(); |
120
|
|
|
self::assertEquals('callThisFunctionPlease()', $response->getBody()->getContents()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @test |
125
|
|
|
*/ |
126
|
|
|
public function theTokenRevocationEndpointReceivesFromAnotherClient() |
127
|
|
|
{ |
128
|
|
|
$endpoint = $this->getTokenRevocationGetEndpoint(); |
129
|
|
|
|
130
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
131
|
|
|
$request->getQueryParams()->willReturn(['token' => 'TOKEN_FOR_ANOTHER_CLIENT', 'callback' => 'callThisFunctionPlease']); |
132
|
|
|
$request->getAttribute('client')->willReturn($this->getClient()); |
133
|
|
|
|
134
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
135
|
|
|
|
136
|
|
|
$response = $endpoint->process($request->reveal(), $handler->reveal()); |
137
|
|
|
|
138
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
139
|
|
|
$response->getBody()->rewind(); |
140
|
|
|
self::assertEquals('callThisFunctionPlease({"error":"invalid_request","error_description":"The parameter \"token\" is invalid."})', $response->getBody()->getContents()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
*/ |
146
|
|
|
public function theTokenRevocationEndpointReceivesARequestWithAnUnsupportedTokenHint() |
147
|
|
|
{ |
148
|
|
|
$endpoint = $this->getTokenRevocationGetEndpoint(); |
149
|
|
|
|
150
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
151
|
|
|
$request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN', 'token_type_hint' => 'bar']); |
152
|
|
|
$request->getAttribute('client')->willReturn($this->getClient()); |
153
|
|
|
|
154
|
|
|
$handler = $this->prophesize(RequestHandlerInterface::class); |
155
|
|
|
|
156
|
|
|
$response = $endpoint->process($request->reveal(), $handler->reveal()); |
157
|
|
|
|
158
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
159
|
|
|
$response->getBody()->rewind(); |
160
|
|
|
self::assertEquals('{"error":"unsupported_token_type","error_description":"The token type hint \"bar\" is not supported. Please use one of the following values: foo."}', $response->getBody()->getContents()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @var TokenTypeHintManager|null |
165
|
|
|
*/ |
166
|
|
|
private $tokenTypeHintManager = null; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return TokenTypeHintManager |
170
|
|
|
*/ |
171
|
|
|
private function getTokenTypeHintManager(): TokenTypeHintManager |
172
|
|
|
{ |
173
|
|
|
if (null === $this->tokenTypeHintManager) { |
174
|
|
|
$token1 = $this->prophesize(Token::class); |
175
|
|
|
$token1->getClientId()->willReturn(ClientId::create('CLIENT_ID')); |
176
|
|
|
|
177
|
|
|
$token2 = $this->prophesize(Token::class); |
178
|
|
|
$token2->getClientId()->willReturn(ClientId::create('OTHER_CLIENT_ID')); |
179
|
|
|
|
180
|
|
|
$tokenType = $this->prophesize(TokenTypeHint::class); |
181
|
|
|
$tokenType->find('VALID_TOKEN')->willReturn($token1->reveal()); |
182
|
|
|
$tokenType->find('TOKEN_FOR_ANOTHER_CLIENT')->willReturn($token2->reveal()); |
183
|
|
|
$tokenType->find('UNKNOWN_TOKEN')->willReturn(null); |
184
|
|
|
$tokenType->hint()->willReturn('foo'); |
185
|
|
|
$tokenType->revoke($token1)->willReturn(null); |
186
|
|
|
|
187
|
|
|
$this->tokenTypeHintManager = new TokenTypeHintManager(); |
188
|
|
|
$this->tokenTypeHintManager->add($tokenType->reveal()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->tokenTypeHintManager; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @var TokenRevocationGetEndpoint|null |
196
|
|
|
*/ |
197
|
|
|
private $tokenRevocationEndpoint = null; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return TokenRevocationGetEndpoint |
201
|
|
|
*/ |
202
|
|
|
private function getTokenRevocationGetEndpoint(): TokenRevocationGetEndpoint |
203
|
|
|
{ |
204
|
|
|
if (null === $this->tokenRevocationEndpoint) { |
205
|
|
|
$this->tokenRevocationEndpoint = new TokenRevocationGetEndpoint( |
206
|
|
|
$this->getTokenTypeHintManager(), |
207
|
|
|
$this->getResponseFactory(), |
208
|
|
|
true |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $this->tokenRevocationEndpoint; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @var ResponseFactory|null |
217
|
|
|
*/ |
218
|
|
|
private $responseFactory = null; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return ResponseFactory |
222
|
|
|
*/ |
223
|
|
|
private function getResponseFactory(): ResponseFactory |
224
|
|
|
{ |
225
|
|
|
if (null === $this->responseFactory) { |
226
|
|
|
$this->responseFactory = new DiactorosMessageFactory(); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->responseFactory; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @var Client|null |
234
|
|
|
*/ |
235
|
|
|
private $client = null; |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return Client |
239
|
|
|
*/ |
240
|
|
|
private function getClient(): Client |
241
|
|
|
{ |
242
|
|
|
if (null === $this->client) { |
243
|
|
|
$this->client = Client::createEmpty(); |
244
|
|
|
$this->client = $this->client->create( |
245
|
|
|
ClientId::create('CLIENT_ID'), |
246
|
|
|
DataBag::create([]), |
247
|
|
|
UserAccountId::create('USER_ACCOUNT') |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this->client; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..