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