Skip to content

Commit 1e2b75f

Browse files
committed
Enabling api token on backlight, fix serverurl/api loading
- implement api for laptop to device comms - fix issue with serverurl and serverapitoken not loading - rename apitoken to be serverapitoken - create httpapitoken for laptop-to-device api calls
1 parent 5319edb commit 1e2b75f

File tree

5 files changed

+73
-21
lines changed

5 files changed

+73
-21
lines changed

configuration_functions.ino

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ void loadConfiguration(const char *filename, Config &config) {
8787
config.httppassword = default_httppassword;
8888
}
8989

90-
config.apitoken = doc["apitoken"].as<String>();
91-
if (config.apitoken == "null") {
90+
config.httpapitoken = doc["httpapitoken"].as<String>();
91+
if (config.httpapitoken == "null") {
9292
initiatesave = true;
93-
config.apitoken = default_apitoken;
93+
config.httpapitoken = default_httpapitoken;
9494
}
9595

9696
config.syslogserver = doc["syslogserver"].as<String>();
@@ -191,6 +191,12 @@ void loadConfiguration(const char *filename, Config &config) {
191191
config.serverurl = default_serverurl;
192192
}
193193

194+
config.serverapitoken = doc["serverapitoken"].as<String>();
195+
if (config.serverapitoken == "null") {
196+
initiatesave = true;
197+
config.serverapitoken = default_serverapitoken;
198+
}
199+
194200
config.checkuserpage = doc["checkuserpage"].as<String>();
195201
if (config.checkuserpage == "null") {
196202
initiatesave = true;
@@ -266,7 +272,7 @@ void saveConfiguration(const char *filename, const Config &config) {
266272
doc["ledpin"] = config.ledpin;
267273
doc["httpuser"] = config.httpuser;
268274
doc["httppassword"] = config.httppassword;
269-
doc["apitoken"] = config.apitoken;
275+
doc["httpapitoken"] = config.httpapitoken;
270276
doc["syslogserver"] = config.syslogserver;
271277
doc["syslogport"] = config.syslogport;
272278
doc["inmaintenance"] = config.inmaintenance;
@@ -284,6 +290,7 @@ void saveConfiguration(const char *filename, const Config &config) {
284290
doc["webserverporthttps"] = config.webserverporthttps;
285291
doc["webapiwaittime"] = config.webapiwaittime;
286292
doc["serverurl"] = config.serverurl;
293+
doc["serverapitoken"] = config.serverapitoken;
287294
doc["checkuserpage"] = config.checkuserpage;
288295
doc["getuserpage"] = config.getuserpage;
289296
doc["moduserpage"] = config.moduserpage;
@@ -328,14 +335,11 @@ void printConfig() {
328335
Serial.print(" appname: "); Serial.println(config.appname);
329336
Serial.print(" ssid: "); Serial.println(config.ssid);
330337
Serial.print(" wifipassword: "); Serial.println("**********");
331-
//Serial.print(" wifipassword: "); Serial.println(config.wifipassword);
332338
Serial.print(" relaypin: "); Serial.println(config.relaypin);
333339
Serial.print(" ledpin: "); Serial.println(config.ledpin);
334340
Serial.print(" httpuser: "); Serial.println(config.httpuser);
335341
Serial.print(" httppassword: "); Serial.println("**********");
336-
//Serial.print(" httppassword: "); Serial.println(config.httppassword);
337-
Serial.print(" apitoken: "); Serial.println("**********");
338-
//Serial.print(" apitoken: "); Serial.println(config.apitoken);
342+
Serial.print(" httpapitoken: "); Serial.println("**********");
339343
Serial.print(" syslogserver: "); Serial.println(config.syslogserver);
340344
Serial.print(" syslogport: "); Serial.println(config.syslogport);
341345
Serial.print(" inmaintenance: "); Serial.println(config.inmaintenance);
@@ -353,6 +357,7 @@ void printConfig() {
353357
Serial.print("webserverporthttps: "); Serial.println(config.webserverporthttps);
354358
Serial.print(" webapiwaittime: "); Serial.println(config.webapiwaittime);
355359
Serial.print(" serverurl: "); Serial.println(config.serverurl);
360+
Serial.print(" serverapitoken: "); Serial.println("**********");
356361
Serial.print(" checkuserpage: "); Serial.println(config.checkuserpage);
357362
Serial.print(" getuserpage: "); Serial.println(config.getuserpage);
358363
Serial.print(" moduserpage: "); Serial.println(config.moduserpage);

defaults.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const int default_relaypin = 26;
77
const int default_ledpin = 2;
88
const String default_httpuser = "admin";
99
const String default_httppassword = "admin";
10-
const String default_apitoken = "abcde";
10+
const String default_httpapitoken = "xyz";
1111
const String default_syslogserver = "192.168.10.21";
1212
const int default_syslogport = 514;
1313
const int default_inmaintenance = 0;
@@ -25,6 +25,7 @@ const int default_webserverporthttp = 80;
2525
const int default_webserverporthttps = 443;
2626
const int default_webapiwaittime = 2;
2727
const String default_serverurl = "http://192.168.10.21:8180";
28+
const String default_serverapitoken = "abcde";
2829
const String default_checkuserpage = "/check.php";
2930
const String default_getuserpage = "/getuser.php";
3031
const String default_moduserpage = "/moduser.php";

eeh-esp32-rfid.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// asyncelegantota library https://github.com/ayushsharma82/AsyncElegantOTA
2525
// file upload progress based upon https://codepen.io/PerfectIsShit/pen/zogMXP
2626

27-
#define FIRMWARE_VERSION "v1.5.9a-ota"
27+
#define FIRMWARE_VERSION "v1.6-ota"
2828

2929
// configuration structure
3030
struct Config {
@@ -37,7 +37,7 @@ struct Config {
3737
int ledpin; // led pin number
3838
String httpuser; // username to access web admin
3939
String httppassword; // password to access web admin
40-
String apitoken; // api token used to authenticate against the user management system
40+
String httpapitoken; // api token used to authenticate against the device
4141
String syslogserver; // hostname or ip of the syslog server
4242
int syslogport; // sylog port number
4343
bool inmaintenance; // records whether the device is in maintenance mode between reboots
@@ -55,6 +55,7 @@ struct Config {
5555
int webserverporthttps; // https port number for the web admin
5656
int webapiwaittime; // forced delay in seconds between web api calls
5757
String serverurl; // url of authentication server, e.g. "http://something.com/" or "https://192.168.20.60"
58+
String serverapitoken; // api token used to authenticate against the user management system
5859
String checkuserpage; // check user webpage hosted on authentication server, e.g. "checkuser.php"
5960
String getuserpage; // get user webpage hosted on authentication server, e.g. "getuser.php"
6061
String moduserpage; // mod user webpage hosted on authentication server, e.g. "moduser.php"
@@ -211,6 +212,10 @@ void setup() {
211212
Serial.print(" NTP Server: "); Serial.println(config.ntpserver);
212213
Serial.print(" NTP Time Sync: "); Serial.println(config.ntpsynctime);
213214
Serial.print(" NTP Time Zone: "); Serial.println(config.ntptimezone);
215+
Serial.print(" Server URL: "); Serial.println(config.serverurl);
216+
Serial.print(" Check User Page: "); Serial.println(config.checkuserpage);
217+
Serial.print(" Get User Page: "); Serial.println(config.getuserpage);
218+
Serial.print(" Mod User Page: "); Serial.println(config.moduserpage);
214219
if (config.influxdbenable) {
215220
Serial.println(" InfluxDB Enabled: true");
216221
Serial.print(" InfluxDB Server: "); Serial.println(config.influxdbserver);
@@ -310,7 +315,7 @@ void dowebcall(const char *foundrfid) {
310315
if (WiFi.status() == WL_CONNECTED) {
311316
StaticJsonDocument<300> doc;
312317

313-
String tempstring = config.serverurl + config.checkuserpage + "?device=" + config.device + "&rfid=" + String(currentRFIDcard) + "&api=" + config.apitoken;
318+
String tempstring = config.serverurl + config.checkuserpage + "?device=" + config.device + "&rfid=" + String(currentRFIDcard) + "&api=" + config.serverapitoken;
314319
char checkURL[tempstring.length() + 1];
315320
tempstring.toCharArray(checkURL, tempstring.length() + 1);
316321

webpages.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const char index_html[] PROGMEM = R"rawliteral(
3030
<button onclick="displayConfig()">Display Running Config</button>
3131
<button onclick="showUploadButton()">Upload File - Simple</button>
3232
<button onclick="showUploadButtonFancy()">Upload File - Fancy</button>
33+
<button onclick="changeBacklightButton('on')">LCD Backlight On</button>
34+
<button onclick="changeBacklightButton('off')">LCD Backlight Off</button>
3335
<button onclick="listFilesButton()">List Files</button>
3436
<button onclick="refreshNTP()">Refresh NTP</button>
3537
<button onclick="logoutCurrentUserButton()">Logout Current User</button>
@@ -118,6 +120,15 @@ function revokeAccessButton() {
118120
document.getElementById("userdetails").innerHTML = xhr.responseText;
119121
},5000);
120122
}
123+
function changeBacklightButton(state) {
124+
document.getElementById("statusdetails").innerHTML = "Turning LCD Backlight " . state;
125+
var xhr = new XMLHttpRequest();
126+
xhr.open("GET", "/backlight" + state, true);
127+
xhr.send();
128+
setTimeout(function(){
129+
document.getElementById("statusdetails").innerHTML = "LCD Backlight " + state;
130+
},2000);
131+
}
121132
function rebootButton() {
122133
document.getElementById("statusdetails").innerHTML = "Invoking Reboot ...";
123134
var xhr = new XMLHttpRequest();

webserver_functions.ino

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ String outputState(int PINCHECK) {
99
return "";
1010
}
1111

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+
1229
// handles uploads to the filserver
1330
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
1431
// make sure authenticated before allowing upload
@@ -277,19 +294,32 @@ void configureWebServer() {
277294
});
278295

279296
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 {
281306
return request->requestAuthentication();
282307
}
283-
lcd->backlight();
284-
request->send(200, "text/html", "backlight on");
285308
});
286309

310+
287311
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 {
289321
return request->requestAuthentication();
290322
}
291-
lcd->noBacklight();
292-
request->send(200, "text/html", "backlight off");
293323
});
294324

295325
server->on("/logged-out", HTTP_GET, [](AsyncWebServerRequest * request) {
@@ -317,7 +347,7 @@ void configureWebServer() {
317347
String logmessage = "Client:" + request->client()->remoteIP().toString() + " RFID:" + String(currentRFIDcard) + " " + request->url();
318348
Serial.println(logmessage);
319349
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;
321351
char getUserURL[tempstring.length() + 1];
322352
tempstring.toCharArray(getUserURL, tempstring.length() + 1);
323353
Serial.print("GetUserURL: "); Serial.println(getUserURL);
@@ -337,11 +367,11 @@ void configureWebServer() {
337367
Serial.print("GrantURL: "); Serial.println(grantURL);
338368
if (strcmp(access, "grant") == 0) {
339369
// 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";
341371
logmessage = "Web Admin: Granting access for " + String(currentRFIDcard);
342372
} else {
343373
// 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";
345375
logmessage = "Web Admin: Revoking access for " + String(currentRFIDcard);
346376
}
347377
Serial.println(logmessage);

0 commit comments

Comments
 (0)