|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alexandre |
|
5
|
|
|
* Date: 31/12/2017 |
|
6
|
|
|
* Time: 00:20 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OAuth2OLD\Endpoint; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use function GuzzleHttp\Psr7\build_query; |
|
13
|
|
|
use GuzzleHttp\Psr7\Response; |
|
14
|
|
|
use OAuth2OLD\Util\Uri; |
|
15
|
|
|
|
|
16
|
|
|
abstract class Endpoint |
|
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
|
|
|
private function containsNotAsciiChar(string $string) |
|
29
|
|
|
{ |
|
30
|
|
|
return preg_match('/[^\x20-\x7e]/', $string); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @see https://tools.ietf.org/html/rfc6749#section-1.7 |
|
35
|
|
|
* |
|
36
|
|
|
* HTTP Redirections |
|
37
|
|
|
* |
|
38
|
|
|
* This specification makes extensive use of HTTP redirections, in which |
|
39
|
|
|
* the client or the authorization server directs the resource owner's |
|
40
|
|
|
* user-agent to another destination. While the examples in this |
|
41
|
|
|
* specification show the use of the HTTP 302 status code, any other |
|
42
|
|
|
* method available via the user-agent to accomplish this redirection is |
|
43
|
|
|
* allowed and is considered to be an implementation detail |
|
44
|
|
|
* |
|
45
|
|
|
* @param $uri |
|
46
|
|
|
* @return Response |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function redirectResponse($uri) |
|
49
|
|
|
{ |
|
50
|
|
|
return new Response(302, ['Location' => $uri]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function jsonResponse($status, $data) |
|
54
|
|
|
{ |
|
55
|
|
|
if (is_array($data)) { |
|
56
|
|
|
$data = json_encode($data); |
|
57
|
|
|
} |
|
58
|
|
|
return new Response($status, ['Content-Type' => 'application/json'], $data); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function response($status, $data) |
|
62
|
|
|
{ |
|
63
|
|
|
return new Response($status, ['Content-Type' => 'text/html'], $data); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $redirectUri |
|
68
|
|
|
* @param string $error |
|
69
|
|
|
* @param null|string $description |
|
70
|
|
|
* @param null|string $uri |
|
71
|
|
|
* @return Response |
|
72
|
|
|
* @throws \Exception |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function errorResponse(string $redirectUri, string $error, ?string $description = null, ?string $uri = null) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->containsNotAsciiChar($error)) { |
|
77
|
|
|
throw new \Exception('Malformed error type. Expect ascii string. Got : ' . $error); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!in_array($error, self::errors)) { |
|
81
|
|
|
throw new \Exception('Unexpected error type. Expect one of : ' . implode(', ', self::errors)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$data = ['error' => $error]; |
|
85
|
|
|
|
|
86
|
|
|
if ($description) { |
|
87
|
|
|
if ($this->containsNotAsciiChar($description)) { |
|
88
|
|
|
throw new \Exception('Malformed error description. Expect ascii string. Got : ' . $description); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$data['error_description'] = $description; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($uri) { |
|
95
|
|
|
if ($this->containsNotAsciiChar($uri)) { |
|
96
|
|
|
throw new \Exception('Malformed error uri. Expect ascii string. Got : ' . $uri); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$data['error_uri'] = $uri; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$redirectUri = new Uri($redirectUri); |
|
103
|
|
|
$query = http_build_query($data); |
|
104
|
|
|
if ($redirectUri->getQuery()) { |
|
105
|
|
|
$query .= '&' . $redirectUri->getQuery(); |
|
106
|
|
|
} |
|
107
|
|
|
$redirectUri = $redirectUri->getScheme() . ':' . $redirectUri->getHierPart() . '?' . $query; |
|
108
|
|
|
|
|
109
|
|
|
return $this->redirectResponse($redirectUri); |
|
110
|
|
|
} |
|
111
|
|
|
} |