|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TMV\OpenIdClient\Exception; |
|
6
|
|
|
|
|
7
|
|
|
use function array_filter; |
|
8
|
|
|
use function array_key_exists; |
|
9
|
|
|
use function is_array; |
|
10
|
|
|
use function json_decode; |
|
11
|
|
|
use JsonSerializable; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
use Throwable; |
|
15
|
|
|
|
|
16
|
|
|
class OAuth2Exception extends RuntimeException implements JsonSerializable |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $error; |
|
20
|
|
|
|
|
21
|
|
|
/** @var null|string */ |
|
22
|
|
|
private $description; |
|
23
|
|
|
|
|
24
|
|
|
/** @var null|string */ |
|
25
|
|
|
private $errorUri; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ResponseInterface $response |
|
29
|
|
|
* @param Throwable|null $previous |
|
30
|
|
|
* |
|
31
|
|
|
* @throws RemoteException |
|
32
|
|
|
* |
|
33
|
|
|
* @return self |
|
34
|
|
|
*/ |
|
35
|
10 |
|
public static function fromResponse(ResponseInterface $response, Throwable $previous = null): self |
|
36
|
|
|
{ |
|
37
|
10 |
|
$data = json_decode((string) $response->getBody(), true); |
|
38
|
|
|
|
|
39
|
10 |
|
if (! is_array($data) || ! isset($data['error'])) { |
|
40
|
5 |
|
throw new RemoteException($response, $response->getReasonPhrase(), $previous); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
return self::fromParameters($data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $params |
|
48
|
|
|
* @param Throwable|null $previous |
|
49
|
|
|
* |
|
50
|
|
|
* @return self |
|
51
|
|
|
*/ |
|
52
|
8 |
|
public static function fromParameters(array $params, Throwable $previous = null): self |
|
53
|
|
|
{ |
|
54
|
8 |
|
if (! array_key_exists('error', $params)) { |
|
55
|
1 |
|
throw new InvalidArgumentException('Invalid OAuth2 exception', 0, $previous); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
7 |
|
return new self( |
|
59
|
7 |
|
$params['error'], |
|
60
|
7 |
|
$params['error_description'] ?? null, |
|
61
|
7 |
|
$params['error_uri'] ?? null, |
|
62
|
7 |
|
0, |
|
63
|
|
|
$previous |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
7 |
|
public function __construct( |
|
68
|
|
|
string $error, |
|
69
|
|
|
?string $description = null, |
|
70
|
|
|
?string $errorUri = null, |
|
71
|
|
|
int $code = 0, |
|
72
|
|
|
Throwable $previous = null |
|
73
|
|
|
) { |
|
74
|
7 |
|
$message = $error; |
|
75
|
7 |
|
if (null !== $description) { |
|
76
|
3 |
|
$message = sprintf('%s (%s)', $description, $error); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
7 |
|
parent::__construct($message, $code, $previous); |
|
80
|
7 |
|
$this->error = $error; |
|
81
|
7 |
|
$this->description = $description; |
|
82
|
7 |
|
$this->errorUri = $errorUri; |
|
83
|
7 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function getError(): string |
|
89
|
|
|
{ |
|
90
|
4 |
|
return $this->error; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string|null |
|
95
|
|
|
*/ |
|
96
|
4 |
|
public function getDescription(): ?string |
|
97
|
|
|
{ |
|
98
|
4 |
|
return $this->description; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string|null |
|
103
|
|
|
*/ |
|
104
|
4 |
|
public function getErrorUri(): ?string |
|
105
|
|
|
{ |
|
106
|
4 |
|
return $this->errorUri; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function jsonSerialize(): array |
|
110
|
|
|
{ |
|
111
|
|
|
$data = [ |
|
112
|
1 |
|
'error' => $this->getError(), |
|
113
|
1 |
|
'error_description' => $this->getDescription(), |
|
114
|
1 |
|
'error_uri' => $this->getErrorUri(), |
|
115
|
|
|
]; |
|
116
|
|
|
|
|
117
|
1 |
|
return array_filter($data); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|