@@ -9,6 +9,23 @@ String outputState(int PINCHECK) {
9
9
return " " ;
10
10
}
11
11
12
+ // used by server.on functions to discern whether a user has the correct httpapitoken OR is authenticated by username and password
13
+ bool checkUserWebAuth (AsyncWebServerRequest * request) {
14
+ bool isAuthenticated = false ;
15
+
16
+ if (request->hasParam (" api" ) && (strcmp (request->getParam (" api" )->value ().c_str (), config.httpapitoken .c_str ()) == 0 )) {
17
+ Serial.println (" has api and token matches" );
18
+ isAuthenticated = true ;
19
+ }
20
+
21
+ if (request->authenticate (config.httpuser .c_str (), config.httppassword .c_str ())) {
22
+ Serial.println (" is authenticated via username and password" );
23
+ isAuthenticated = true ;
24
+ }
25
+ return isAuthenticated;
26
+ }
27
+
28
+
12
29
// handles uploads to the filserver
13
30
void handleUpload (AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final ) {
14
31
// make sure authenticated before allowing upload
@@ -277,19 +294,32 @@ void configureWebServer() {
277
294
});
278
295
279
296
server->on (" /backlighton" , HTTP_GET, [](AsyncWebServerRequest * request) {
280
- if (!request->authenticate (config.httpuser .c_str (), config.httppassword .c_str ())) {
297
+ String logmessage = " Client:" + request->client ()->remoteIP ().toString () + " " + request->url ();
298
+ Serial.println (logmessage);
299
+ syslog.log (logmessage);
300
+
301
+ if (checkUserWebAuth (request)) {
302
+ Serial.println (" LCD Backlight On" );
303
+ lcd->backlight ();
304
+ request->send (200 , " text/html" , " LCD Backlight On" );
305
+ } else {
281
306
return request->requestAuthentication ();
282
307
}
283
- lcd->backlight ();
284
- request->send (200 , " text/html" , " backlight on" );
285
308
});
286
309
310
+
287
311
server->on (" /backlightoff" , HTTP_GET, [](AsyncWebServerRequest * request) {
288
- if (!request->authenticate (config.httpuser .c_str (), config.httppassword .c_str ())) {
312
+ String logmessage = " Client:" + request->client ()->remoteIP ().toString () + " " + request->url ();
313
+ Serial.println (logmessage);
314
+ syslog.log (logmessage);
315
+
316
+ if (checkUserWebAuth (request)) {
317
+ Serial.println (" LCD Backlight Off" );
318
+ lcd->noBacklight ();
319
+ request->send (200 , " text/html" , " LCD Backlight Off" );
320
+ } else {
289
321
return request->requestAuthentication ();
290
322
}
291
- lcd->noBacklight ();
292
- request->send (200 , " text/html" , " backlight off" );
293
323
});
294
324
295
325
server->on (" /logged-out" , HTTP_GET, [](AsyncWebServerRequest * request) {
@@ -317,7 +347,7 @@ void configureWebServer() {
317
347
String logmessage = " Client:" + request->client ()->remoteIP ().toString () + " RFID:" + String (currentRFIDcard) + " " + request->url ();
318
348
Serial.println (logmessage);
319
349
syslog.log (logmessage);
320
- String tempstring = config.serverurl + config.getuserpage + " ?device=" + config.device + " &rfid=" + String (currentRFIDcard) + " &api=" + config.apitoken ;
350
+ String tempstring = config.serverurl + config.getuserpage + " ?device=" + config.device + " &rfid=" + String (currentRFIDcard) + " &api=" + config.serverapitoken ;
321
351
char getUserURL[tempstring.length () + 1 ];
322
352
tempstring.toCharArray (getUserURL, tempstring.length () + 1 );
323
353
Serial.print (" GetUserURL: " ); Serial.println (getUserURL);
@@ -337,11 +367,11 @@ void configureWebServer() {
337
367
Serial.print (" GrantURL: " ); Serial.println (grantURL);
338
368
if (strcmp (access, " grant" ) == 0 ) {
339
369
// granting access
340
- grantURL = config.serverurl + config.moduserpage + " ?device=" + config.device + " &modrfid=" + String (currentRFIDcard) + " &api=" + config.apitoken + " &access=true" ;
370
+ grantURL = config.serverurl + config.moduserpage + " ?device=" + config.device + " &modrfid=" + String (currentRFIDcard) + " &api=" + config.serverapitoken + " &access=true" ;
341
371
logmessage = " Web Admin: Granting access for " + String (currentRFIDcard);
342
372
} else {
343
373
// default fall back to revoking access
344
- grantURL = config.serverurl + config.moduserpage + " ?device=" + config.device + " &modrfid=" + String (currentRFIDcard) + " &api=" + config.apitoken + " &access=false" ;
374
+ grantURL = config.serverurl + config.moduserpage + " ?device=" + config.device + " &modrfid=" + String (currentRFIDcard) + " &api=" + config.serverapitoken + " &access=false" ;
345
375
logmessage = " Web Admin: Revoking access for " + String (currentRFIDcard);
346
376
}
347
377
Serial.println (logmessage);
0 commit comments