|
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\ResponseMode; |
|
15
|
|
|
|
|
16
|
|
|
use Http\Message\MessageFactory\DiactorosMessageFactory; |
|
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\FormPostResponseMode; |
|
18
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\FormPostResponseRenderer; |
|
19
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\FragmentResponseMode; |
|
20
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\QueryResponseMode; |
|
21
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode; |
|
22
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseModeManager; |
|
23
|
|
|
use PHPUnit\Framework\TestCase; |
|
24
|
|
|
use Prophecy\Argument; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @group ResponseMode |
|
28
|
|
|
*/ |
|
29
|
|
|
class ResponseModeTest extends TestCase |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
* @expectedException \InvalidArgumentException |
|
34
|
|
|
* @expectedExceptionMessage The response mode with name "foo" is not supported. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function genericCalls() |
|
37
|
|
|
{ |
|
38
|
|
|
self::assertEquals(['query', 'fragment', 'form_post'], $this->getResponseModeManager()->list()); |
|
39
|
|
|
self::assertTrue($this->getResponseModeManager()->has('query')); |
|
40
|
|
|
self::assertFalse($this->getResponseModeManager()->has('foo')); |
|
41
|
|
|
self::assertInstanceOf(ResponseMode::class, $this->getResponseModeManager()->get('fragment')); |
|
42
|
|
|
$this->getResponseModeManager()->get('foo'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @test |
|
47
|
|
|
*/ |
|
48
|
|
|
public function buildQueryResponseForUrl() |
|
49
|
|
|
{ |
|
50
|
|
|
$mode = $this->getResponseModeManager()->get('query'); |
|
51
|
|
|
$response = $mode->buildResponse('https://localhost/foo?bar=bar#foo=foo', [ |
|
52
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
self::assertTrue($response->hasHeader('Location')); |
|
56
|
|
|
self::assertEquals(['https://localhost/foo?bar=bar&access_token=ACCESS_TOKEN#_=_'], $response->getHeader('Location')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function buildQueryResponseForPrivateUri() |
|
63
|
|
|
{ |
|
64
|
|
|
$mode = $this->getResponseModeManager()->get('query'); |
|
65
|
|
|
$response = $mode->buildResponse('com.example.app:/oauth2redirect/example-provider', [ |
|
66
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
self::assertTrue($response->hasHeader('Location')); |
|
70
|
|
|
self::assertEquals(['com.example.app:/oauth2redirect/example-provider?access_token=ACCESS_TOKEN#_=_'], $response->getHeader('Location')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function buildQueryResponseForUrn() |
|
77
|
|
|
{ |
|
78
|
|
|
$mode = $this->getResponseModeManager()->get('query'); |
|
79
|
|
|
$response = $mode->buildResponse('urn:ietf:wg:oauth:2.0:oob', [ |
|
80
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
self::assertTrue($response->hasHeader('Location')); |
|
84
|
|
|
self::assertEquals(['urn:ietf:wg:oauth:2.0:oob?access_token=ACCESS_TOKEN#_=_'], $response->getHeader('Location')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @test |
|
89
|
|
|
*/ |
|
90
|
|
|
public function buildFragmentResponseForUrl() |
|
91
|
|
|
{ |
|
92
|
|
|
$mode = $this->getResponseModeManager()->get('fragment'); |
|
93
|
|
|
$response = $mode->buildResponse('https://localhost/foo?bar=bar#foo=foo', [ |
|
94
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
self::assertTrue($response->hasHeader('Location')); |
|
98
|
|
|
self::assertEquals(['https://localhost/foo?bar=bar#access_token=ACCESS_TOKEN&_=_'], $response->getHeader('Location')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @test |
|
103
|
|
|
*/ |
|
104
|
|
|
public function buildFragmentResponseForPrivateUri() |
|
105
|
|
|
{ |
|
106
|
|
|
$mode = $this->getResponseModeManager()->get('fragment'); |
|
107
|
|
|
$response = $mode->buildResponse('com.example.app:/oauth2redirect/example-provider', [ |
|
108
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
109
|
|
|
]); |
|
110
|
|
|
|
|
111
|
|
|
self::assertTrue($response->hasHeader('Location')); |
|
112
|
|
|
self::assertEquals(['com.example.app:/oauth2redirect/example-provider#access_token=ACCESS_TOKEN&_=_'], $response->getHeader('Location')); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @test |
|
117
|
|
|
*/ |
|
118
|
|
|
public function buildFragmentResponseForUrn() |
|
119
|
|
|
{ |
|
120
|
|
|
$mode = $this->getResponseModeManager()->get('fragment'); |
|
121
|
|
|
$response = $mode->buildResponse('urn:ietf:wg:oauth:2.0:oob', [ |
|
122
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
123
|
|
|
]); |
|
124
|
|
|
|
|
125
|
|
|
self::assertTrue($response->hasHeader('Location')); |
|
126
|
|
|
self::assertEquals(['urn:ietf:wg:oauth:2.0:oob#access_token=ACCESS_TOKEN&_=_'], $response->getHeader('Location')); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @test |
|
131
|
|
|
*/ |
|
132
|
|
|
public function buildFormPostResponseForUrl() |
|
133
|
|
|
{ |
|
134
|
|
|
$mode = $this->getResponseModeManager()->get('form_post'); |
|
135
|
|
|
$response = $mode->buildResponse('https://localhost/foo?bar=bar#foo=foo', [ |
|
136
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
137
|
|
|
]); |
|
138
|
|
|
|
|
139
|
|
|
$response->getBody()->rewind(); |
|
140
|
|
|
$body = $response->getBody()->getContents(); |
|
141
|
|
|
self::assertEquals('["https:\/\/localhost\/foo?bar=bar#_=_",{"access_token":"ACCESS_TOKEN"}]', $body); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @test |
|
146
|
|
|
*/ |
|
147
|
|
|
public function buildFormPostResponseForPrivateUri() |
|
148
|
|
|
{ |
|
149
|
|
|
$mode = $this->getResponseModeManager()->get('form_post'); |
|
150
|
|
|
$response = $mode->buildResponse('com.example.app:/oauth2redirect/example-provider', [ |
|
151
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
152
|
|
|
]); |
|
153
|
|
|
|
|
154
|
|
|
$response->getBody()->rewind(); |
|
155
|
|
|
$body = $response->getBody()->getContents(); |
|
156
|
|
|
self::assertEquals('["com.example.app:\/oauth2redirect\/example-provider#_=_",{"access_token":"ACCESS_TOKEN"}]', $body); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @test |
|
161
|
|
|
*/ |
|
162
|
|
|
public function buildFormPostResponseForUrn() |
|
163
|
|
|
{ |
|
164
|
|
|
$mode = $this->getResponseModeManager()->get('form_post'); |
|
165
|
|
|
$response = $mode->buildResponse('urn:ietf:wg:oauth:2.0:oob', [ |
|
166
|
|
|
'access_token' => 'ACCESS_TOKEN', |
|
167
|
|
|
]); |
|
168
|
|
|
|
|
169
|
|
|
$response->getBody()->rewind(); |
|
170
|
|
|
$body = $response->getBody()->getContents(); |
|
171
|
|
|
self::assertEquals('["urn:ietf:wg:oauth:2.0:oob#_=_",{"access_token":"ACCESS_TOKEN"}]', $body); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @var null|ResponseModeManager |
|
176
|
|
|
*/ |
|
177
|
|
|
private $responseModeManager = null; |
|
178
|
|
|
|
|
179
|
|
|
private function getResponseModeManager(): ResponseModeManager |
|
180
|
|
|
{ |
|
181
|
|
|
if (null === $this->responseModeManager) { |
|
182
|
|
|
$this->responseModeManager = new ResponseModeManager(); |
|
183
|
|
|
$this->responseModeManager->add(new QueryResponseMode( |
|
184
|
|
|
new DiactorosMessageFactory() |
|
185
|
|
|
)); |
|
186
|
|
|
$this->responseModeManager->add(new FragmentResponseMode( |
|
187
|
|
|
new DiactorosMessageFactory() |
|
188
|
|
|
)); |
|
189
|
|
|
$formPostResponseRenderer = $this->prophesize(FormPostResponseRenderer::class); |
|
190
|
|
|
$formPostResponseRenderer->render(Argument::type('string'), ['access_token' => 'ACCESS_TOKEN'])->will(function ($args) { |
|
191
|
|
|
return json_encode($args); |
|
192
|
|
|
}); |
|
193
|
|
|
$this->responseModeManager->add(new FormPostResponseMode( |
|
194
|
|
|
$formPostResponseRenderer->reveal(), |
|
195
|
|
|
new DiactorosMessageFactory() |
|
196
|
|
|
)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $this->responseModeManager; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|