1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * OAuth2 errors that require termination of OAuth2 due to |
---|
5 | * an error. |
---|
6 | * |
---|
7 | */ |
---|
8 | class OAuth2ServerException extends Exception { |
---|
9 | |
---|
10 | protected $httpCode; |
---|
11 | protected $errorData = array(); |
---|
12 | |
---|
13 | /** |
---|
14 | * @param $http_status_code |
---|
15 | * HTTP status code message as predefined. |
---|
16 | * @param $error |
---|
17 | * A single error code. |
---|
18 | * @param $error_description |
---|
19 | * (optional) A human-readable text providing additional information, |
---|
20 | * used to assist in the understanding and resolution of the error |
---|
21 | * occurred. |
---|
22 | */ |
---|
23 | public function __construct($http_status_code, $error, $error_description = NULL) { |
---|
24 | parent::__construct($error); |
---|
25 | |
---|
26 | $this->httpCode = $http_status_code; |
---|
27 | |
---|
28 | $this->errorData['error'] = $error; |
---|
29 | if ($error_description) { |
---|
30 | $this->errorData['error_description'] = $error_description; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * @return string |
---|
36 | */ |
---|
37 | public function getDescription() { |
---|
38 | return isset($this->errorData['error_description']) ? $this->errorData['error_description'] : null; |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * @return string |
---|
43 | */ |
---|
44 | public function getHttpCode() { |
---|
45 | return $this->httpCode; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Send out error message in JSON. |
---|
50 | * |
---|
51 | * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.1 |
---|
52 | * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2 |
---|
53 | * |
---|
54 | * @ingroup oauth2_error |
---|
55 | */ |
---|
56 | public function sendHttpResponse() { |
---|
57 | header("HTTP/1.1 " . $this->httpCode); |
---|
58 | $this->sendHeaders(); |
---|
59 | echo (string) $this; |
---|
60 | exit(); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Send out HTTP headers for JSON. |
---|
65 | * |
---|
66 | * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.1 |
---|
67 | * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2 |
---|
68 | * |
---|
69 | * @ingroup oauth2_section_5 |
---|
70 | */ |
---|
71 | protected function sendHeaders() { |
---|
72 | header("Content-Type: application/json"); |
---|
73 | header("Cache-Control: no-store"); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * @see Exception::__toString() |
---|
78 | */ |
---|
79 | public function __toString() { |
---|
80 | return json_encode($this->errorData); |
---|
81 | } |
---|
82 | } |
---|