1 | <?php |
||
14 | class OAuthServerException extends \Exception |
||
15 | { |
||
16 | /** |
||
17 | * @var int |
||
18 | */ |
||
19 | private $httpStatusCode; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $errorType; |
||
25 | |||
26 | /** |
||
27 | * @var null|string |
||
28 | */ |
||
29 | private $hint; |
||
30 | |||
31 | /** |
||
32 | * @var null|string |
||
33 | */ |
||
34 | private $redirectUri; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $payload; |
||
40 | |||
41 | /** |
||
42 | * Throw a new exception. |
||
43 | * |
||
44 | * @param string $message Error message |
||
45 | * @param int $code Error code |
||
46 | * @param string $errorType Error type |
||
47 | * @param int $httpStatusCode HTTP status code to send (default = 400) |
||
48 | * @param null|string $hint A helper hint |
||
49 | * @param null|string $redirectUri A HTTP URI to redirect the user back to |
||
50 | */ |
||
51 | 58 | public function __construct($message, $code, $errorType, $httpStatusCode = 400, $hint = null, $redirectUri = null) |
|
66 | |||
67 | /** |
||
68 | * Returns the current payload. |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | public function getPayload() |
||
73 | { |
||
74 | return $this->payload; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Updates the current payload. |
||
79 | * |
||
80 | * @param array $payload |
||
81 | */ |
||
82 | public function setPayload(array $payload) |
||
86 | |||
87 | /** |
||
88 | * Unsupported grant type error. |
||
89 | * |
||
90 | * @return static |
||
91 | */ |
||
92 | 2 | public static function unsupportedGrantType() |
|
99 | |||
100 | /** |
||
101 | * Invalid request error. |
||
102 | * |
||
103 | * @param string $parameter The invalid parameter |
||
104 | * @param null|string $hint |
||
105 | * |
||
106 | * @return static |
||
107 | */ |
||
108 | 22 | public static function invalidRequest($parameter, $hint = null) |
|
116 | |||
117 | /** |
||
118 | * Invalid client error. |
||
119 | * |
||
120 | * @return static |
||
121 | */ |
||
122 | 12 | public static function invalidClient() |
|
128 | |||
129 | /** |
||
130 | * Invalid scope error. |
||
131 | * |
||
132 | * @param string $scope The bad scope |
||
133 | * @param null|string $redirectUri A HTTP URI to redirect the user back to |
||
134 | * |
||
135 | * @return static |
||
136 | */ |
||
137 | 2 | public static function invalidScope($scope, $redirectUri = null) |
|
138 | { |
||
139 | 2 | $errorMessage = 'The requested scope is invalid, unknown, or malformed'; |
|
140 | |||
141 | 2 | if (empty($scope)) { |
|
142 | $hint = 'Specify a scope in the request or set a default scope'; |
||
143 | } else { |
||
144 | 2 | $hint = sprintf( |
|
145 | 2 | 'Check the `%s` scope', |
|
146 | 2 | htmlspecialchars($scope, ENT_QUOTES, 'UTF-8', false) |
|
147 | ); |
||
148 | } |
||
149 | |||
150 | 2 | return new static($errorMessage, 5, 'invalid_scope', 400, $hint, $redirectUri); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Invalid credentials error. |
||
155 | * |
||
156 | * @return static |
||
157 | */ |
||
158 | 1 | public static function invalidCredentials() |
|
162 | |||
163 | /** |
||
164 | * Server error. |
||
165 | * |
||
166 | * @param string $hint |
||
167 | * |
||
168 | * @return static |
||
169 | * |
||
170 | * @codeCoverageIgnore |
||
171 | */ |
||
172 | public static function serverError($hint) |
||
182 | |||
183 | /** |
||
184 | * Invalid refresh token. |
||
185 | * |
||
186 | * @param null|string $hint |
||
187 | * |
||
188 | * @return static |
||
189 | */ |
||
190 | 4 | public static function invalidRefreshToken($hint = null) |
|
194 | |||
195 | /** |
||
196 | * Access denied. |
||
197 | * |
||
198 | * @param null|string $hint |
||
199 | * @param null|string $redirectUri |
||
200 | * |
||
201 | * @return static |
||
202 | */ |
||
203 | 5 | public static function accessDenied($hint = null, $redirectUri = null) |
|
214 | |||
215 | /** |
||
216 | * Invalid grant. |
||
217 | * |
||
218 | * @param string $hint |
||
219 | * |
||
220 | * @return static |
||
221 | */ |
||
222 | 1 | public static function invalidGrant($hint = '') |
|
234 | |||
235 | /** |
||
236 | * @return string |
||
237 | */ |
||
238 | 2 | public function getErrorType() |
|
239 | { |
||
240 | 2 | return $this->errorType; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Generate a HTTP response. |
||
245 | * |
||
246 | * @param ResponseInterface $response |
||
247 | * @param bool $useFragment True if errors should be in the URI fragment instead of query string |
||
248 | * @param int $jsonOptions options passed to json_encode |
||
249 | * |
||
250 | * @return ResponseInterface |
||
251 | */ |
||
252 | public function generateHttpResponse(ResponseInterface $response, $useFragment = false, $jsonOptions = 0) |
||
253 | { |
||
254 | $headers = $this->getHttpHeaders(); |
||
255 | |||
256 | $payload = $this->getPayload(); |
||
257 | |||
258 | if ($this->redirectUri !== null) { |
||
259 | if ($useFragment === true) { |
||
260 | $this->redirectUri .= (strstr($this->redirectUri, '#') === false) ? '#' : '&'; |
||
261 | } else { |
||
262 | $this->redirectUri .= (strstr($this->redirectUri, '?') === false) ? '?' : '&'; |
||
263 | } |
||
264 | |||
265 | return $response->withStatus(302)->withHeader('Location', $this->redirectUri . http_build_query($payload)); |
||
266 | } |
||
267 | |||
268 | foreach ($headers as $header => $content) { |
||
269 | $response = $response->withHeader($header, $content); |
||
270 | } |
||
271 | |||
272 | $response->getBody()->write(json_encode($payload, $jsonOptions)); |
||
273 | |||
274 | return $response->withStatus($this->getHttpStatusCode()); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get all headers that have to be send with the error response. |
||
279 | * |
||
280 | * @return array Array with header values |
||
281 | */ |
||
282 | public function getHttpHeaders() |
||
|
|||
283 | { |
||
284 | $headers = [ |
||
285 | 'Content-type' => 'application/json', |
||
286 | ]; |
||
287 | |||
288 | // Add "WWW-Authenticate" header |
||
289 | // |
||
290 | // RFC 6749, section 5.2.: |
||
291 | // "If the client attempted to authenticate via the 'Authorization' |
||
292 | // request header field, the authorization server MUST |
||
293 | // respond with an HTTP 401 (Unauthorized) status code and |
||
294 | // include the "WWW-Authenticate" response header field |
||
295 | // matching the authentication scheme used by the client. |
||
296 | // @codeCoverageIgnoreStart |
||
297 | if ($this->errorType === 'invalid_client' && array_key_exists('HTTP_AUTHORIZATION', $_SERVER) !== false) { |
||
298 | $authScheme = strpos($_SERVER['HTTP_AUTHORIZATION'], 'Bearer') === 0 ? 'Bearer' : 'Basic'; |
||
299 | |||
300 | $headers['WWW-Authenticate'] = $authScheme . ' realm="OAuth"'; |
||
301 | } |
||
302 | // @codeCoverageIgnoreEnd |
||
303 | return $headers; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Check if the exception has an associated redirect URI. |
||
308 | * |
||
309 | * Returns whether the exception includes a redirect, since |
||
310 | * getHttpStatusCode() doesn't return a 302 when there's a |
||
311 | * redirect enabled. This helps when you want to override local |
||
312 | * error pages but want to let redirects through. |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | 2 | public function hasRedirect() |
|
320 | |||
321 | /** |
||
322 | * Returns the HTTP status code to send when the exceptions is output. |
||
323 | * |
||
324 | * @return int |
||
325 | */ |
||
326 | 2 | public function getHttpStatusCode() |
|
330 | |||
331 | /** |
||
332 | * @return null|string |
||
333 | */ |
||
334 | 9 | public function getHint() |
|
338 | } |
||
339 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: