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