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 | 65 | 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) |
|
138 | { |
||
139 | 4 | $errorMessage = 'The requested scope is invalid, unknown, or malformed'; |
|
140 | |||
141 | 4 | if (empty($scope)) { |
|
142 | $hint = 'Specify a scope in the request or set a default scope'; |
||
143 | } else { |
||
144 | 4 | $hint = sprintf( |
|
145 | 4 | 'Check the `%s` scope', |
|
146 | 4 | htmlspecialchars($scope, ENT_QUOTES, 'UTF-8', false) |
|
147 | ); |
||
148 | } |
||
149 | |||
150 | 4 | 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 | 9 | 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 | 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() |
|
303 | |||
304 | /** |
||
305 | * Returns the HTTP status code to send when the exceptions is output. |
||
306 | * |
||
307 | * @return int |
||
308 | */ |
||
309 | 5 | public function getHttpStatusCode() |
|
313 | |||
314 | /** |
||
315 | * @return null|string |
||
316 | */ |
||
317 | 14 | public function getHint() |
|
321 | } |
||
322 |
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: