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\AuthorizationEndpoint\Tests\ParameterChecker; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException; |
18
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\DisplayParameterChecker; |
19
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterCheckerManager; |
20
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\PromptParameterChecker; |
21
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\RedirectUriParameterChecker; |
22
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ResponseTypeAndResponseModeParameterChecker; |
23
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\StateParameterChecker; |
24
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\FragmentResponseMode; |
25
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\QueryResponseMode; |
26
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseModeManager; |
27
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType; |
28
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseTypeManager; |
29
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
30
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
31
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
32
|
|
|
use PHPUnit\Framework\TestCase; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @group ParameterCheckerManager |
36
|
|
|
*/ |
37
|
|
|
final class ParameterCheckerManagerTest extends TestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
*/ |
42
|
|
|
public function anAuthorizationRequestIsReceivedButTheDisplayParameterIsNotValid() |
43
|
|
|
{ |
44
|
|
|
$client = Client::createEmpty(); |
45
|
|
|
$client = $client->create( |
46
|
|
|
ClientId::create('CLIENT_ID'), |
47
|
|
|
DataBag::create([]), |
48
|
|
|
null |
49
|
|
|
); |
50
|
|
|
$authorization = Authorization::create($client, [ |
51
|
|
|
'display' => 'foo', |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
56
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
57
|
|
|
} catch (OAuth2AuthorizationException $e) { |
58
|
|
|
self::assertEquals(400, $e->getCode()); |
59
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
60
|
|
|
self::assertEquals('Invalid parameter "display". Allowed values are page, popup, touch, wap', $e->getErrorDescription()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function anAuthorizationRequestIsReceivedButThePromptParameterIsNotValid() |
68
|
|
|
{ |
69
|
|
|
$client = Client::createEmpty(); |
70
|
|
|
$client = $client->create( |
71
|
|
|
ClientId::create('CLIENT_ID'), |
72
|
|
|
DataBag::create([]), |
73
|
|
|
null |
74
|
|
|
); |
75
|
|
|
$authorization = Authorization::create($client, [ |
76
|
|
|
'prompt' => 'foo', |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
81
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
82
|
|
|
} catch (OAuth2AuthorizationException $e) { |
83
|
|
|
self::assertEquals(400, $e->getCode()); |
84
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
85
|
|
|
self::assertEquals('Invalid parameter "prompt". Allowed values are none, login, consent, select_account', $e->getErrorDescription()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
*/ |
92
|
|
|
public function anAuthorizationRequestIsReceivedButThePromptParameterNoneMustBeUsedAlone() |
93
|
|
|
{ |
94
|
|
|
$client = Client::createEmpty(); |
95
|
|
|
$client = $client->create( |
96
|
|
|
ClientId::create('CLIENT_ID'), |
97
|
|
|
DataBag::create([]), |
98
|
|
|
null |
99
|
|
|
); |
100
|
|
|
$authorization = Authorization::create($client, [ |
101
|
|
|
'prompt' => 'none login', |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
106
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
107
|
|
|
} catch (OAuth2AuthorizationException $e) { |
108
|
|
|
self::assertEquals(400, $e->getCode()); |
109
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
110
|
|
|
self::assertEquals('Invalid parameter "prompt". Prompt value "none" must be used alone.', $e->getErrorDescription()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @test |
116
|
|
|
*/ |
117
|
|
|
public function anAuthorizationRequestIsReceivedButNoRedirectUriIsSet() |
118
|
|
|
{ |
119
|
|
|
$client = Client::createEmpty(); |
120
|
|
|
$client = $client->create( |
121
|
|
|
ClientId::create('CLIENT_ID'), |
122
|
|
|
DataBag::create([]), |
123
|
|
|
null |
124
|
|
|
); |
125
|
|
|
$authorization = Authorization::create($client, []); |
126
|
|
|
|
127
|
|
|
try { |
128
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
129
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
130
|
|
|
} catch (OAuth2AuthorizationException $e) { |
131
|
|
|
self::assertEquals(400, $e->getCode()); |
132
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
133
|
|
|
self::assertEquals('The parameter "redirect_uri" is mandatory.', $e->getErrorDescription()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @test |
139
|
|
|
*/ |
140
|
|
|
public function anAuthorizationRequestIsReceivedButNoResponseTypeIsSet() |
141
|
|
|
{ |
142
|
|
|
$client = Client::createEmpty(); |
143
|
|
|
$client = $client->create( |
144
|
|
|
ClientId::create('CLIENT_ID'), |
145
|
|
|
DataBag::create([]), |
146
|
|
|
null |
147
|
|
|
); |
148
|
|
|
$authorization = Authorization::create($client, [ |
149
|
|
|
'redirect_uri' => 'https://www.foo.bar/callback', |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
154
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
155
|
|
|
} catch (OAuth2AuthorizationException $e) { |
156
|
|
|
self::assertEquals(400, $e->getCode()); |
157
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
158
|
|
|
self::assertEquals('The parameter "response_type" is mandatory.', $e->getErrorDescription()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @test |
164
|
|
|
*/ |
165
|
|
|
public function anAuthorizationRequestIsReceivedButTheResponseModeIsNotSupported() |
166
|
|
|
{ |
167
|
|
|
$client = Client::createEmpty(); |
168
|
|
|
$client = $client->create( |
169
|
|
|
ClientId::create('CLIENT_ID'), |
170
|
|
|
DataBag::create([ |
171
|
|
|
'response_types' => ['foo'], |
172
|
|
|
]), |
173
|
|
|
null |
174
|
|
|
); |
175
|
|
|
$authorization = Authorization::create($client, [ |
176
|
|
|
'redirect_uri' => 'https://www.foo.bar/callback', |
177
|
|
|
'response_type' => 'foo', |
178
|
|
|
'response_mode' => 'foo', |
179
|
|
|
]); |
180
|
|
|
|
181
|
|
|
try { |
182
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
183
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
184
|
|
|
} catch (OAuth2AuthorizationException $e) { |
185
|
|
|
self::assertEquals(400, $e->getCode()); |
186
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
187
|
|
|
self::assertEquals('The response mode "foo" is not supported. Please use one of the following values: query, fragment.', $e->getErrorDescription()); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @test |
193
|
|
|
*/ |
194
|
|
|
public function anAuthorizationRequestIsReceivedButTheResponseTypeIsNotSupportedByThisServer() |
195
|
|
|
{ |
196
|
|
|
$client = Client::createEmpty(); |
197
|
|
|
$client = $client->create( |
198
|
|
|
ClientId::create('CLIENT_ID'), |
199
|
|
|
DataBag::create([]), |
200
|
|
|
null |
201
|
|
|
); |
202
|
|
|
$authorization = Authorization::create($client, [ |
203
|
|
|
'redirect_uri' => 'https://www.foo.bar/callback', |
204
|
|
|
'response_type' => 'bar', |
205
|
|
|
]); |
206
|
|
|
|
207
|
|
|
try { |
208
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
209
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
210
|
|
|
} catch (OAuth2AuthorizationException $e) { |
211
|
|
|
self::assertEquals(400, $e->getCode()); |
212
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
213
|
|
|
self::assertEquals('The response type "bar" is not supported by this server', $e->getErrorDescription()); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @test |
219
|
|
|
*/ |
220
|
|
|
public function anAuthorizationRequestIsReceivedButTheResponseTypeIsNotAllowedForTheClient() |
221
|
|
|
{ |
222
|
|
|
$client = Client::createEmpty(); |
223
|
|
|
$client = $client->create( |
224
|
|
|
ClientId::create('CLIENT_ID'), |
225
|
|
|
DataBag::create([]), |
226
|
|
|
null |
227
|
|
|
); |
228
|
|
|
$authorization = Authorization::create($client, [ |
229
|
|
|
'redirect_uri' => 'https://www.foo.bar/callback', |
230
|
|
|
'response_type' => 'foo', |
231
|
|
|
]); |
232
|
|
|
|
233
|
|
|
try { |
234
|
|
|
$this->getParameterCheckerManager()->process($authorization); |
235
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
236
|
|
|
} catch (OAuth2AuthorizationException $e) { |
237
|
|
|
self::assertEquals(400, $e->getCode()); |
238
|
|
|
self::assertEquals('invalid_request', $e->getMessage()); |
239
|
|
|
self::assertEquals('The response type "foo" is not allowed for this client.', $e->getErrorDescription()); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @test |
245
|
|
|
*/ |
246
|
|
|
public function anAuthorizationRequestIsReceivedAndIsValid() |
247
|
|
|
{ |
248
|
|
|
$client = Client::createEmpty(); |
249
|
|
|
$client = $client->create( |
250
|
|
|
ClientId::create('CLIENT_ID'), |
251
|
|
|
DataBag::create([ |
252
|
|
|
'response_types' => ['foo'], |
253
|
|
|
]), |
254
|
|
|
null |
255
|
|
|
); |
256
|
|
|
$authorization = Authorization::create($client, [ |
257
|
|
|
'redirect_uri' => 'https://www.foo.bar/callback', |
258
|
|
|
'response_type' => 'foo', |
259
|
|
|
'state' => '0123456789', |
260
|
|
|
'prompt' => 'login consent', |
261
|
|
|
'display' => 'wap', |
262
|
|
|
'response_mode' => 'fragment', |
263
|
|
|
]); |
264
|
|
|
|
265
|
|
|
$authorization = $this->getParameterCheckerManager()->process($authorization); |
266
|
|
|
|
267
|
|
|
self::assertInstanceOf(FragmentResponseMode::class, $authorization->getResponseMode()); |
268
|
|
|
self::assertInstanceOf(ResponseType::class, $authorization->getResponseType()); |
269
|
|
|
self::assertEquals(['login', 'consent'], $authorization->getPrompt()); |
270
|
|
|
self::assertFalse($authorization->hasPrompt('none')); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @var null|ParameterCheckerManager |
275
|
|
|
*/ |
276
|
|
|
private $extensionManager = null; |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return ParameterCheckerManager |
280
|
|
|
*/ |
281
|
|
|
private function getParameterCheckerManager(): ParameterCheckerManager |
282
|
|
|
{ |
283
|
|
|
if (null === $this->extensionManager) { |
284
|
|
|
$responseType = $this->prophesize(ResponseType::class); |
285
|
|
|
$responseType->name()->willReturn('foo'); |
286
|
|
|
$responseType->getResponseMode()->willReturn('query'); |
287
|
|
|
$responseTypeManager = new ResponseTypeManager(); |
288
|
|
|
$responseTypeManager->add($responseType->reveal()); |
289
|
|
|
|
290
|
|
|
$responseModeManager = new ResponseModeManager(); |
291
|
|
|
$responseModeManager->add(new QueryResponseMode()); |
292
|
|
|
$responseModeManager->add(new FragmentResponseMode()); |
293
|
|
|
|
294
|
|
|
$this->extensionManager = new ParameterCheckerManager(); |
295
|
|
|
$this->extensionManager->add(new DisplayParameterChecker()); |
296
|
|
|
$this->extensionManager->add(new PromptParameterChecker()); |
297
|
|
|
$this->extensionManager->add(new RedirectUriParameterChecker()); |
298
|
|
|
$this->extensionManager->add(new ResponseTypeAndResponseModeParameterChecker($responseTypeManager, $responseModeManager, true)); |
299
|
|
|
$this->extensionManager->add(new StateParameterChecker()); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $this->extensionManager; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|