1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 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\AuthorizationRequest; |
15
|
|
|
|
16
|
|
|
use Assert\Assertion; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode; |
18
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType\ResponseType; |
19
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
20
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
21
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer; |
22
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
23
|
|
|
use function Safe\sprintf; |
24
|
|
|
|
25
|
|
|
class AuthorizationRequest |
26
|
|
|
{ |
27
|
|
|
public const CONSENT_NOT_GIVEN = 'consent_not_given'; |
28
|
|
|
public const CONSENT_ALLOW = 'consent_allow'; |
29
|
|
|
public const CONSENT_DENY = 'consent_deny'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $authorized = self::CONSENT_NOT_GIVEN; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Client |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var null|UserAccount |
43
|
|
|
*/ |
44
|
|
|
private $userAccount; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var DataBag |
48
|
|
|
*/ |
49
|
|
|
private $metadata; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ResponseType] |
53
|
|
|
*/ |
54
|
|
|
private $responseType; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var null|ResponseMode |
58
|
|
|
*/ |
59
|
|
|
private $responseMode; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $queryParameters = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private $responseParameters = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
private $responseHeaders = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var null|ResourceServer |
78
|
|
|
*/ |
79
|
|
|
private $resourceServer; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private $attributes = []; |
85
|
|
|
|
86
|
|
|
public function __construct(Client $client, array $queryParameters) |
87
|
|
|
{ |
88
|
|
|
$this->client = $client; |
89
|
|
|
$this->queryParameters = $queryParameters; |
90
|
|
|
$this->metadata = new DataBag([]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getQueryParams(): array |
94
|
|
|
{ |
95
|
|
|
return $this->queryParameters; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function hasQueryParam(string $param): bool |
99
|
|
|
{ |
100
|
|
|
return \array_key_exists($param, $this->queryParameters); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function getQueryParam(string $param) |
107
|
|
|
{ |
108
|
|
|
Assertion::true($this->hasQueryParam($param), sprintf('The parameter "%s" is missing.', $param)); |
109
|
|
|
|
110
|
|
|
return $this->queryParameters[$param]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getClient(): Client |
114
|
|
|
{ |
115
|
|
|
return $this->client; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function hasResponseMode(): bool |
119
|
|
|
{ |
120
|
|
|
return null === $this->responseMode; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getResponseMode(): ResponseMode |
124
|
|
|
{ |
125
|
|
|
Assertion::notNull($this->responseMode, 'No response mode'); |
126
|
|
|
|
127
|
|
|
return $this->responseMode; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getRedirectUri(): string |
131
|
|
|
{ |
132
|
|
|
return $this->getQueryParam('redirect_uri'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setUserAccount(UserAccount $userAccount): void |
136
|
|
|
{ |
137
|
|
|
$this->userAccount = $userAccount; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function hasUserAccount(): bool |
141
|
|
|
{ |
142
|
|
|
return null !== $this->userAccount; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getUserAccount(): UserAccount |
146
|
|
|
{ |
147
|
|
|
Assertion::notNull($this->userAccount, 'internal_server_error'); |
148
|
|
|
|
149
|
|
|
return $this->userAccount; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param mixed $value |
154
|
|
|
*/ |
155
|
|
|
public function setResponseParameter(string $responseParameter, $value): void |
156
|
|
|
{ |
157
|
|
|
$this->responseParameters[$responseParameter] = $value; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getResponseParameters(): array |
161
|
|
|
{ |
162
|
|
|
return $this->responseParameters; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
public function getResponseParameter(string $param) |
169
|
|
|
{ |
170
|
|
|
Assertion::true($this->hasResponseParameter($param), sprintf('Invalid response parameter "%s".', $param)); |
171
|
|
|
|
172
|
|
|
return $this->getResponseParameters()[$param]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function hasResponseParameter(string $param): bool |
176
|
|
|
{ |
177
|
|
|
return \array_key_exists($param, $this->getResponseParameters()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param mixed $value |
182
|
|
|
*/ |
183
|
|
|
public function setResponseHeader(string $responseHeader, $value): void |
184
|
|
|
{ |
185
|
|
|
$this->responseHeaders[$responseHeader] = $value; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getResponseHeaders(): array |
189
|
|
|
{ |
190
|
|
|
return $this->responseHeaders; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string[] |
195
|
|
|
*/ |
196
|
|
|
public function getPrompt(): array |
197
|
|
|
{ |
198
|
|
|
if (!$this->hasQueryParam('prompt')) { |
199
|
|
|
return []; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return explode(' ', $this->getQueryParam('prompt')); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function hasUiLocales(): bool |
206
|
|
|
{ |
207
|
|
|
return $this->hasQueryParam('ui_locales'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return string[] |
212
|
|
|
*/ |
213
|
|
|
public function getUiLocales(): array |
214
|
|
|
{ |
215
|
|
|
return $this->hasQueryParam('ui_locales') ? explode(' ', $this->getQueryParam('ui_locales')) : []; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function hasPrompt(string $prompt): bool |
219
|
|
|
{ |
220
|
|
|
return \in_array($prompt, $this->getPrompt(), true); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function isAuthorized(): bool |
224
|
|
|
{ |
225
|
|
|
return self::CONSENT_ALLOW === $this->authorized; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function hasConsentBeenGiven(): bool |
229
|
|
|
{ |
230
|
|
|
return self::CONSENT_NOT_GIVEN !== $this->authorized; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function allow(): void |
234
|
|
|
{ |
235
|
|
|
$this->authorized = self::CONSENT_ALLOW; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function deny(): void |
239
|
|
|
{ |
240
|
|
|
$this->authorized = self::CONSENT_DENY; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function getResourceServer(): ?ResourceServer |
244
|
|
|
{ |
245
|
|
|
return $this->resourceServer; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function setResourceServer(ResourceServer $resourceServer): void |
249
|
|
|
{ |
250
|
|
|
$this->resourceServer = $resourceServer; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function hasScope(): bool |
254
|
|
|
{ |
255
|
|
|
return $this->hasQueryParam('scope'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function getScope(): string |
259
|
|
|
{ |
260
|
|
|
return $this->getQueryParam('scope'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function getMetadata(): DataBag |
264
|
|
|
{ |
265
|
|
|
return $this->metadata; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function hasAttribute(string $key): bool |
269
|
|
|
{ |
270
|
|
|
return \array_key_exists($key, $this->attributes); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param null|mixed$value |
275
|
|
|
*/ |
276
|
|
|
public function setAttribute(string $key, $value): void |
277
|
|
|
{ |
278
|
|
|
$this->attributes[$key] = $value; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return null|mixed $value |
283
|
|
|
*/ |
284
|
|
|
public function getAttribute(string $key) |
285
|
|
|
{ |
286
|
|
|
Assertion::true($this->hasAttribute($key), sprintf('The attribute with key "%s" does not exist', $key)); |
287
|
|
|
|
288
|
|
|
return $this->attributes[$key]; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|