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 | 68 | 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 | 5 | public function getPayload() |
|
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 | 13 | 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 | 4 | public static function invalidScope($scope, $redirectUri = null) |
|
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 | 12 | 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() |
|
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 | 5 | public function generateHttpResponse(ResponseInterface $response, $useFragment = false, $jsonOptions = 0) |
|
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 | 5 | public function getHttpHeaders() |
|
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 | 5 | public function getHttpStatusCode() |
|
330 | |||
331 | /** |
||
332 | * @return null|string |
||
333 | */ |
||
334 | 14 | 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: