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