forked from arnaud-lb/oauth2-php
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathOAuth2ServerException.php
120 lines (106 loc) · 2.62 KB
/
OAuth2ServerException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace OAuth2;
use Symfony\Component\HttpFoundation\Response;
/**
* OAuth2 errors that require termination of OAuth2 due to an error.
*/
class OAuth2ServerException extends \Exception
{
/**
* @var string
*/
protected $httpCode;
/**
* @var array
*/
protected $errorData = array();
/**
* @param string $httpStatusCode HTTP status code message as predefined.
* @param string $error A single error code.
* @param string $errorDescription (optional) A human-readable text providing additional information, used to assist in the understanding and resolution of the error occurred.
*/
public function __construct($httpStatusCode, $error, $errorDescription = null)
{
parent::__construct($error);
$this->httpCode = $httpStatusCode;
$this->errorData['error'] = $error;
$this->errorData['error_description'] = $errorDescription;
}
/**
* Get error description
*
* @return string
*/
public function getDescription()
{
return $this->errorData['error_description'];
}
/**
* Get HTTP code
*
* @return string
*/
public function getHttpCode()
{
return $this->httpCode;
}
/**
* Get HTTP Error Response
*
* @return Response
*
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.1
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2
*
* @ingroup oauth2_error
*/
public function getHttpResponse()
{
return new Response(
$this->getResponseBody(),
$this->getHttpCode(),
$this->getResponseHeaders()
);
}
/**
* Get HTTP Error Response headers
*
* @return array
*
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2
*
* @ingroup oauth2_error
*/
public function getResponseHeaders()
{
return array(
'Content-Type' => 'application/json',
'Cache-Control' => 'no-store',
'Pragma' => 'no-cache',
);
}
/**
* Get response body as JSON string
*
* @return string
*/
public function getResponseBody()
{
return json_encode($this->errorData);
}
/**
* Outputs response
*/
public function sendHttpResponse()
{
$this->getHttpResponse()->send();
exit; // TODO: refactor out this piece of code
}
/**
* @see \Exception::__toString()
*/
public function __toString()
{
return $this->getResponseBody();
}
}