This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
AuthenticationResource.class.php
261 lines (231 loc) · 6.76 KB
/
AuthenticationResource.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Copyright (c) 2011 Cyso Managed Hosting < development [at] cyso . nl >
*
* This file is part of TonicDNS.
*
* TonicDNS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TonicDNS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TonicDNS. If not, see <http://www.gnu.org/licenses/>.
*
* @package resources
* @license http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Authentication Resource.
* @uri /authenticate
* @uri /authenticate/:token
* @uri /authentication
* @uri /authentication/:token
*/
class AuthenticationResource extends AnonymousResource {
/**
* Token backend to use.
*/
private $backend = null;
/**
* Resource constructor
* @ignore
* @param str[] parameters Parameters passed in from the URL as matched from the URI regex
*/
function __construct($parameters) {
parent::__construct($parameters);
try {
switch (PowerDnsConfig::TOKEN_BACKEND) {
case "PDO":
$this->backend = new PDOTokenBackend();
break;
default:
$this->backend = new SqliteTokenBackend();
break;
}
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
}
/**
* Corresponds to login.
*
* ### Request: ###
*
* ~~~
* {
* "username": <username>,
* "password": <password>,
* "local_user": <username>
* }
* ~~~
*
* ### Response: ###
*
* ~~~
* {
* "username": <string>,
* "valid_until": <int>,
* "hash": <string>,
* "token": <string>
* }
* ~~~
*
* ### Errors: ###
*
* * 500 - Invalid request or missing username/password.
* * 403 - Username/password incorrect.
*
* @access public
* @param mixed $request Request parameters
* @return Response Authentication Token if successful, error message if false.
*/
public function put($request) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if ($data == null) {
$response->code = Response::BADREQUEST;
$response->error = "Request body was malformed. Ensure the body is in valid format.";
$response->error_detail = "BODY_MALFORMED";
return $response;
}
if (!isset($data->username) || !isset($data->password)) {
$response->code = Response::BADREQUEST;
$response->error = "Username and/or password was missing or invalid. Ensure that the body is in valid format and all required parameters are present.";
$response->error_detail = "MISSING_REQUIRED_PARAMETERS";
return $response;
}
$validator = new AuthenticationValidator($data);
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
$token = new Token();
$token->username = $data->username;
$token->password = $data->password;
$token = $this->backend->createToken($token);
if ($token == null) {
$response->code = Response::FORBIDDEN;
$response->error = "Username and/or password was invalid.";
$response->error_detail = "AUTH_INVALID_CREDENTIALS";
return $response;
}
$response->code = Response::OK;
$response->body = $token->toArray();
$response->log_message = "Token was successfully created.";
return $response;
}
/**
* Corresponds to session validation.
*
* If the session is valid, the duration is refreshed. If it is
* not, but it does exist, it will be destroyed.
*
* ### Response: ###
*
* ~~~
* true
* ~~~
*
* ### Errors: ###
*
* * 500 - Missing or token with invalid format.
* * 403 - Invalid token.
*
* @access public
* @param mixed $request Request parameters
* @param string $token Authentication token
* @return Response True if session is still valid, error message otherwise.
*/
public function post($request, $token = null) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if (empty($token)) {
$response->code = Response::BADREQUEST;
$response->error = "Token was missing or invalid.";
$response->error_detail = "MISSING_REQUIRED_PARAMETERS";
return $response;
}
$validator = new AuthenticationValidator();
$validator->token = $token;
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
$t = $this->backend->refreshToken($token);
if ($t == null) {
$response->code = Response::FORBIDDEN;
$response->error = "Token was invalid.";
$response->error_detail = "AUTH_INVALID_TOKEN";
return $response;
}
$response->code = Response::OK;
$response->body = true;
$response->log_message = "Token was successfully validated.";
return $response;
}
/**
* Corresponds to session logout.
*
* ### Response: ###
*
* ~~~
* true
* ~~~
*
* ### Errors: ###
*
* * 500 - Missing or token with invalid format.
* * 500 - Could not destroy token.
* * 403 - Invalid token.
*
* @access public
* @param mixed $request Request parameters
* @param string $token Authentication token
* @return Response True if session was terminated, error message otherwise.
*/
public function delete($request, $token = null) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if (!isset($token)) {
$response->code = Response::BADREQUEST;
$response->error = "Token was missing or invalid.";
$response->error_detail = "MISSING_REQUIRED_PARAMETERS";
return $response;
}
$validator = new AuthenticationValidator();
$validator->token = $token;
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
$t = $this->backend->retrieveToken($token);
if ($t == null) {
$response->code = Response::FORBIDDEN;
$response->error = "Token was invalid.";
$response->error_detail = "AUTH_INVALID_TOKEN";
return $response;
}
if (!$this->backend->destroyToken($t)) {
$response->code = Response::INTERNALSERVERERROR;
$response->error = "Token could not be destroyed.";
$response->error_detail = "INTERNAL_SERVER_ERROR";
return $response;
}
$response->code = Response::OK;
$response->body = true;
$response->log_message = "Token was successfully invalidated.";
return $response;
}
}