1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Alexandre |
5
|
|
|
* Date: 31/12/2017 |
6
|
|
|
* Time: 17:05 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2OLD\Endpoint\Server\Messages\Authorization; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use GuzzleHttp\Psr7\Response; |
13
|
|
|
use GuzzleHttp\Psr7\Uri; |
14
|
|
|
use Psr\Http\Message\UriInterface; |
15
|
|
|
|
16
|
|
|
class ErrorResponse extends Response |
17
|
|
|
{ |
18
|
|
|
const errors = [ |
19
|
|
|
'invalid_request', |
20
|
|
|
'unauthorized_client', |
21
|
|
|
'access_denied', |
22
|
|
|
'unsupported_response_type', |
23
|
|
|
'invalid_scope', |
24
|
|
|
'server_error', |
25
|
|
|
'temporarily_unavailable', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ErrorResponse constructor. |
30
|
|
|
* @param UriInterface $redirectUri |
31
|
|
|
* @param string $error |
32
|
|
|
* @param null|string $errorDescription |
33
|
|
|
* @param null|string $errorUri |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
public function __construct(UriInterface $redirectUri, string $error, ?string $errorDescription = null, ?string $errorUri = null) |
37
|
|
|
{ |
38
|
|
|
if ($this->containsNotAsciiChar($error)) { |
39
|
|
|
throw new \Exception('Malformed error type. Expect ascii string. Got : ' . $error); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!in_array($error, self::errors)) { |
43
|
|
|
throw new \Exception('Unexpected error type. Expect one of : ' . implode(', ', self::errors)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$redirectUri = Uri::withQueryValue($redirectUri, 'error', $error); |
47
|
|
|
|
48
|
|
|
if ($errorDescription) { |
49
|
|
|
if ($this->containsNotAsciiChar($errorDescription)) { |
50
|
|
|
throw new \Exception('Malformed error description. Expect ascii string. Got : ' . $errorDescription); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$redirectUri = Uri::withQueryValue($redirectUri, 'error_description', $errorDescription); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($errorUri) { |
57
|
|
|
if ($this->containsNotAsciiChar($errorUri)) { |
58
|
|
|
throw new \Exception('Malformed error uri. Expect ascii string. Got : ' . $errorUri); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$redirectUri = Uri::withQueryValue($redirectUri, 'error_uri', $errorUri); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
parent::__construct(302, ['Location' => $redirectUri->__toString()]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function containsNotAsciiChar(string $string) |
68
|
|
|
{ |
69
|
|
|
return preg_match('/[^\x20-\x7e]/', $string); |
70
|
|
|
} |
71
|
|
|
} |