|
9 | 9 | namespace Discord\Endpoints;
|
10 | 10 |
|
11 | 11 | use Discord\OAuth2Authenticator;
|
12 |
| -use Psr\Http\Message\ServerRequestInterface; |
13 |
| -use React\Http\Message\Response; |
14 |
| -use VerifierServer\Endpoint; |
| 12 | +use VerifierServer\Endpoints\OAuth2Endpoint as __OAuth2Endpoint; |
15 | 13 |
|
16 |
| -class OAuth2Endpoint extends Endpoint |
| 14 | +class OAuth2Endpoint extends __OAuth2Endpoint |
17 | 15 | {
|
18 |
| - protected array $cache = []; |
19 |
| - |
20 |
| - public function __construct( |
21 |
| - protected array &$sessions, |
22 |
| - protected string $resolved_ip, |
23 |
| - protected string $web_address, |
24 |
| - protected int $http_port, |
25 |
| - protected string $client_id, |
26 |
| - protected string $client_secret, |
27 |
| - ){} |
28 |
| - |
29 |
| - /** |
30 |
| - * @param string $method |
31 |
| - * @param ServerRequestInterface $request |
32 |
| - * @param int|string &$response |
33 |
| - * @param array &$headers |
34 |
| - * @param string &$body |
35 |
| - */ |
36 |
| - public function handle( |
37 |
| - string $method, |
38 |
| - $request, |
39 |
| - int|string &$response, |
40 |
| - array &$headers, |
41 |
| - string &$body |
42 |
| - ): void |
43 |
| - { |
44 |
| - switch ($method) { |
45 |
| - case 'GET': |
46 |
| - $this->get($request, $response, $headers, $body); |
47 |
| - break; |
48 |
| - case 'POST': |
49 |
| - $this->post($request, $response, $headers, $body); |
50 |
| - break; |
51 |
| - case 'HEAD': |
52 |
| - case 'PUT': |
53 |
| - case 'DELETE': |
54 |
| - case 'PATCH': |
55 |
| - case 'OPTIONS': |
56 |
| - case 'CONNECT': |
57 |
| - default: |
58 |
| - $response = Response::STATUS_METHOD_NOT_ALLOWED; |
59 |
| - $headers = ['Content-Type' => 'text/plain']; |
60 |
| - $body = 'Method Not Allowed'; |
61 |
| - break; |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * @param ServerRequestInterface|string $request |
67 |
| - * @param int|string &$response |
68 |
| - * @param array &$headers |
69 |
| - * @param string &$body |
70 |
| - */ |
71 |
| - private function get( |
72 |
| - $request, |
73 |
| - int|string &$response, |
74 |
| - array &$headers, |
75 |
| - string &$body |
76 |
| - ): void |
77 |
| - { |
78 |
| - if (!$request instanceof ServerRequestInterface) { |
79 |
| - $response = Response::STATUS_METHOD_NOT_ALLOWED; |
80 |
| - $headers = ['Content-Type' => 'text/plain']; |
81 |
| - $body = 'Method Not Allowed'; |
82 |
| - return; |
83 |
| - } |
84 |
| - if (!$params = $request->getQueryParams()) { |
85 |
| - $response = Response::STATUS_BAD_REQUEST; |
86 |
| - $headers = ['Content-Type' => 'text/plain']; |
87 |
| - $body = 'Bad Request'; |
88 |
| - return; |
89 |
| - } |
90 |
| - |
91 |
| - $requesting_ip = $request->getServerParams()['REMOTE_ADDR'] ?? '127.0.0.1'; // For session management, will be deprecated in favor of a more robust solution |
92 |
| - $OAA = |
93 |
| - &$this->cache[$requesting_ip]['OAuth2Authenticator'] ?? |
94 |
| - $this->cache[$requesting_ip]['OAuth2Authenticator'] = new OAuth2Authenticator( |
95 |
| - $request, |
96 |
| - $this->sessions, |
97 |
| - $this->resolved_ip, |
98 |
| - $this->web_address, |
99 |
| - $this->http_port, |
100 |
| - $this->client_id, |
101 |
| - $this->client_secret |
102 |
| - ); |
103 |
| - /** @var OAuth2Authenticator $OAA */ |
104 |
| - |
105 |
| - if (isset($params['code'], $params['state'])) { |
106 |
| - if ($OAA->getToken($response, $headers, $body, $params['code'], $params['state'])) { |
107 |
| - $OAA->getUser(); |
108 |
| - } |
109 |
| - return; |
110 |
| - } |
111 |
| - if (isset($params['login'])) { |
112 |
| - $OAA->login($response, $headers, $body); |
113 |
| - return; |
114 |
| - } |
115 |
| - if (isset($params['logout'])) { |
116 |
| - $OAA->logout($response, $headers, $body); |
117 |
| - return; |
118 |
| - } |
119 |
| - if (isset($params['remove']) && $OAA->isAuthed()) { |
120 |
| - $OAA->removeToken($response, $headers, $body); |
121 |
| - return; |
122 |
| - } |
123 |
| - if (isset($params['user']) && $user = $OAA->getUser()) { |
124 |
| - $response = Response::STATUS_OK; |
125 |
| - $headers = ['Content-Type' => 'application/json']; |
126 |
| - $body = json_encode($user); |
127 |
| - return; |
128 |
| - } |
129 |
| - } |
130 |
| - |
131 |
| - /** |
132 |
| - * @param ServerRequestInterface|string $request |
133 |
| - * @param string|int &$response |
134 |
| - * @param array &$headers |
135 |
| - * @param string &$body |
136 |
| - */ |
137 |
| - private function post( |
138 |
| - $request, |
139 |
| - int|string &$response, |
140 |
| - array &$headers, |
141 |
| - string &$body |
142 |
| - ): void |
143 |
| - { |
144 |
| - $this->get($request, $response, $headers, $body); |
145 |
| - } |
146 |
| - |
147 |
| - public function __serialize(): array |
148 |
| - { |
149 |
| - $data = get_object_vars($this); |
150 |
| - unset($data['client_id'], $data['client_secret']); |
151 |
| - return $data; |
152 |
| - } |
153 |
| - |
154 |
| - public function __unserialize(array $data): void |
155 |
| - { |
156 |
| - foreach ($data as $key => $value) $this->$key = $value; |
157 |
| - $this->client_id = $_ENV['SS14_OAUTH2_CLIENT_ID'] ?? getenv('SS14_OAUTH2_CLIENT_ID') ?: ''; |
158 |
| - $this->client_secret = $_ENV['SS14_OAUTH2_CLIENT_SECRET'] ?? getenv('SS14_OAUTH2_CLIENT_SECRET') ?: ''; |
159 |
| - } |
160 |
| - |
161 |
| - public function __debugInfo(): array |
162 |
| - { |
163 |
| - $debugInfo = get_object_vars($this); |
164 |
| - unset($debugInfo['client_id'], $debugInfo['client_secret']); |
165 |
| - return $debugInfo; |
166 |
| - } |
| 16 | + protected string $auth = OAuth2Authenticator::class; |
167 | 17 | }
|
0 commit comments