1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Exception class for standard OAuth2 exceptions |
7
|
|
|
*/ |
8
|
|
|
class OAuthServerException extends Parroauth2Exception |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The http status code |
12
|
|
|
* |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $statusCode; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $errorType; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
private $hint; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* OAuthServerException constructor. |
30
|
|
|
* |
31
|
|
|
* @param int $statusCode |
32
|
|
|
* @param string $errorType |
33
|
|
|
* @param string $message |
34
|
|
|
* @param string|null $hint |
35
|
|
|
* @param \Exception $previous |
36
|
|
|
* @param int $code |
37
|
|
|
*/ |
38
|
20 |
|
public function __construct($statusCode, $errorType, $message, $hint = null, \Exception $previous = null, $code = 0) |
39
|
|
|
{ |
40
|
20 |
|
parent::__construct($message, $code, $previous); |
41
|
|
|
|
42
|
20 |
|
$this->statusCode = $statusCode; |
43
|
20 |
|
$this->errorType = $errorType; |
44
|
20 |
|
$this->hint = $hint; |
45
|
20 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the http status code |
49
|
|
|
* |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
11 |
|
public function getStatusCode() |
53
|
|
|
{ |
54
|
11 |
|
return $this->statusCode; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the error type |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
11 |
|
public function getErrorType() |
63
|
|
|
{ |
64
|
11 |
|
return $this->errorType; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the error hint |
69
|
|
|
* |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
11 |
|
public function getHint() |
73
|
|
|
{ |
74
|
11 |
|
return $this->hint; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create the exception from a standard OAuth2 error response |
79
|
|
|
* |
80
|
|
|
* @param string $type |
81
|
|
|
* @param string|null $message |
82
|
|
|
* @param string|null $hint |
83
|
|
|
* @param \Exception|null $previous |
84
|
|
|
* @param int $code |
85
|
|
|
* |
86
|
|
|
* @return OAuthServerException |
87
|
|
|
*/ |
88
|
18 |
|
public static function create($type, $message, $hint = null, \Exception $previous = null, $code = 0) |
89
|
|
|
{ |
90
|
18 |
|
switch ($type) { |
91
|
|
|
case AccessDeniedException::ERROR_TYPE: |
92
|
1 |
|
return new AccessDeniedException($message ?: 'Access denied', $hint, $previous, $code); |
93
|
|
|
|
94
|
17 |
|
case InvalidClientException::ERROR_TYPE: |
95
|
1 |
|
return new InvalidClientException($message ?: 'Invalid client', $hint, $previous, $code); |
96
|
|
|
|
97
|
16 |
|
case InvalidGrantException::ERROR_TYPE: |
98
|
2 |
|
return new InvalidGrantException($message ?: 'Invalid grant', $hint, $previous, $code); |
99
|
|
|
|
100
|
14 |
|
case InvalidRequestException::ERROR_TYPE: |
101
|
1 |
|
return new InvalidRequestException($message ?: 'Invalid request', $hint, $previous, $code); |
102
|
|
|
|
103
|
13 |
|
case InvalidScopeException::ERROR_TYPE: |
104
|
3 |
|
return new InvalidScopeException($message ?: 'Invalid scope', $hint, $previous, $code); |
105
|
|
|
|
106
|
10 |
|
case ServerErrorException::ERROR_TYPE: |
107
|
1 |
|
return new ServerErrorException($message ?: 'Server error', $hint, $previous, $code); |
108
|
|
|
|
109
|
9 |
|
case TemporarilyUnavailableException::ERROR_TYPE: |
110
|
1 |
|
return new TemporarilyUnavailableException( |
111
|
1 |
|
$message ?: 'Temporarily unavailable', |
112
|
|
|
$hint, |
113
|
|
|
$previous, |
114
|
|
|
$code |
115
|
|
|
); |
116
|
|
|
|
117
|
8 |
|
case UnauthorizedClientException::ERROR_TYPE: |
118
|
1 |
|
return new UnauthorizedClientException($message ?: 'Unauthorized client', $hint, $previous, $code); |
119
|
|
|
|
120
|
7 |
|
case UnsupportedGrantTypeException::ERROR_TYPE: |
121
|
1 |
|
return new UnsupportedGrantTypeException($message ?: 'Unsupported grant type', $hint, $previous, $code); |
122
|
|
|
|
123
|
6 |
|
case UnsupportedResponseTypeException::ERROR_TYPE: |
124
|
1 |
|
return new UnsupportedResponseTypeException( |
125
|
1 |
|
$message ?: 'Unsupported response type', |
126
|
|
|
$hint, |
127
|
|
|
$previous, |
128
|
|
|
$code |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
default: |
132
|
5 |
|
return new self(400, $type, $message ?: 'An error has occurred', $hint, $previous, $code); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|