Skip to content

Commit

Permalink
web user and password
Browse files Browse the repository at this point in the history
  • Loading branch information
richonguzman committed Aug 13, 2024
1 parent 60aa2b0 commit ee95931
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 3 deletions.
4 changes: 4 additions & 0 deletions data/igate_conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
"username": "",
"password": ""
},
"webadmin": {
"username": "admin",
"password": ""
},
"other": {
"rememberStationTime": 30,
"lowPowerMode": false,
Expand Down
69 changes: 69 additions & 0 deletions data_embed/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,75 @@ <h5>
</div>
<hr />

<div class="row my-5 d-flex align-items-top">
<div class="col-lg-3 col-sm-12">
<h5>
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
fill="currentColor"
class="bi bi-cloud-upload-fill"
viewBox="0 0 16 16"
>
<path
fill-rule="evenodd"
d="M8 0a5.53 5.53 0 0 0-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 4.095 0 5.555 0 7.318 0 9.366 1.708 11 3.781 11H7.5V5.707L5.354 7.854a.5.5 0 1 1-.708-.708l3-3a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 5.707V11h4.188C14.502 11 16 9.57 16 7.773c0-1.636-1.242-2.969-2.834-3.194C12.923 1.999 10.69 0 8 0m-.5 14.5V11h1v3.5a.5.5 0 0 1-1 0"
/>
</svg>
Admin
</h5>
<small
>Set your username and password to allow
access to the web interface.</small
>
</div>
<div class="col-lg-9 col-sm-12">
<div class="row">
<div class="col-12">
<div class="form-check form-switch">
<input
type="checkbox"
name="webadmin.active"
id="webadmin.active"
class="form-check-input"
/>
<label
for="webadmin.active"
class="form-label"
>Web interface authentication</label
>
</div>
</div>
</div>
<div class="col-12">
<label for="webadmin.username" class="form-label"
>Username</label
>
<input
type="text"
name="webadmin.username"
id="webadmin.username"
class="form-control"
value="admin"
required=""
/>
</div>
<div class="col-12 mt-3">
<label for="webadmin.password" class="form-label"
>Password</label
>
<input
type="password"
name="webadmin.password"
id="webadmin.password"
class="form-control"
/>
</div>
</div>
</div>
<hr />

<div class="row my-5 d-flex align-items-top">
<div class="col-lg-3 col-sm-12">
<h5>
Expand Down
35 changes: 35 additions & 0 deletions data_embed/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ function loadSettings(settings) {
document.getElementById("ota.username").value = settings.ota.username;
document.getElementById("ota.password").value = settings.ota.password;

// Webadmin
document.getElementById("webadmin.active").checked = settings.webadmin.active;
document.getElementById("webadmin.username").value = settings.webadmin.username;
document.getElementById("webadmin.password").value = settings.webadmin.password;

// Experimental
document.getElementById("other.backupDigiMode").checked = settings.other.backupDigiMode;

Expand Down Expand Up @@ -276,6 +281,20 @@ function toggleFields() {
externalVoltagePinInput.disabled = !sendExternalVoltageCheckbox.checked;
voltageDividerR1.disabled = !sendExternalVoltageCheckbox.checked;
voltageDividerR2.disabled = !sendExternalVoltageCheckbox.checked;

const WebadminCheckbox = document.querySelector(
'input[name="webadmin.active"]'
);

const WebadminUsername = document.querySelector(
'input[name="webadmin.username"]'
);

const WebadminPassword = document.querySelector(
'input[name="webadmin.password"]'
);
WebadminUsername.disabled = !WebadminCheckbox.checked;
WebadminPassword.disabled = !WebadminCheckbox.checked;
}

const sendExternalVoltageCheckbox = document.querySelector(
Expand All @@ -299,6 +318,22 @@ sendExternalVoltageCheckbox.addEventListener("change", function () {
voltageDividerR2.disabled = !this.checked;
});

const WebadminCheckbox = document.querySelector(
'input[name="webadmin.active"]'
);

const WebadminUsername = document.querySelector(
'input[name="webadmin.username"]'
);

const WebadminPassword = document.querySelector(
'input[name="webadmin.password"]'
);
WebadminCheckbox.addEventListener("change", function () {
WebadminUsername.disabled = !this.checked;
WebadminPassword.disabled = !this.checked;
});

document.querySelector(".new button").addEventListener("click", function () {
const networksContainer = document.querySelector(".list-networks");

Expand Down
16 changes: 14 additions & 2 deletions src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ void Configuration::writeFile() {

data["other"]["rememberStationTime"] = rememberStationTime;

data["other"]["backupDigiMode"] = backupDigiMode;
data["other"]["backupDigiMode"] = backupDigiMode;

data["other"]["lowPowerMode"] = lowPowerMode;
data["other"]["lowVoltageCutOff"] = lowVoltageCutOff;

data["personalNote"] = personalNote;
data["personalNote"] = personalNote;

data["webadmin"]["active"] = webadmin.active;
data["webadmin"]["username"] = webadmin.username;
data["webadmin"]["password"] = webadmin.password;

serializeJson(data, configFile);

Expand Down Expand Up @@ -188,6 +192,10 @@ bool Configuration::readFile() {

personalNote = data["personalNote"].as<String>();

webadmin.active = data["webadmin"]["active"].as<bool>();
webadmin.username = data["webadmin"]["username"].as<String>();
webadmin.password = data["webadmin"]["password"].as<String>();

int stationMode = data["stationMode"].as<int>(); // deprecated but need to specify config version

if (stationMode == 0) {
Expand Down Expand Up @@ -362,6 +370,10 @@ void Configuration::init() {

personalNote = "";

webadmin.active = false;
webadmin.username = "admin";
webadmin.password = "";

Serial.println("All is Written!");
}

Expand Down
8 changes: 8 additions & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class OTA {
String password;
};

class WEBADMIN {
public:
bool active;
String username;
String password;
};

class Configuration {
public:
bool reload; // ?
Expand All @@ -129,6 +136,7 @@ class Configuration {
SYSLOG syslog;
TNC tnc;
OTA ota;
WEBADMIN webadmin;

void init();
void writeFile();
Expand Down
12 changes: 12 additions & 0 deletions src/web_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ namespace WEB_Utils {
}

void handleHome(AsyncWebServerRequest *request) {
if(Config.webadmin.active && !request->authenticate(Config.webadmin.username.c_str(), Config.webadmin.password.c_str()))
return request->requestAuthentication();

AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", (const uint8_t*)web_index_html, web_index_html_len);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
Expand All @@ -61,6 +64,9 @@ namespace WEB_Utils {
}

void handleReadConfiguration(AsyncWebServerRequest *request) {
if(Config.webadmin.active && !request->authenticate(Config.webadmin.username.c_str(), Config.webadmin.password.c_str()))
return request->requestAuthentication();

File file = SPIFFS.open("/igate_conf.json");

String fileContent;
Expand Down Expand Up @@ -193,6 +199,12 @@ namespace WEB_Utils {

Config.personalNote = request->getParam("personalNote", true)->value();

Config.webadmin.active = request->hasParam("webadmin.active", true);
if (Config.webadmin.active) {
Config.webadmin.username = request->getParam("webadmin.username", true)->value();
Config.webadmin.password = request->getParam("webadmin.password", true)->value();
}

Config.writeFile();

AsyncWebServerResponse *response = request->beginResponse(302, "text/html", "");
Expand Down
4 changes: 3 additions & 1 deletion src/wifi_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ namespace WIFI_Utils {
#endif
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected as ");
Serial.println(WiFi.localIP());
Serial.print(WiFi.localIP());
Serial.print(" / MAC Address: ");
Serial.println(WiFi.macAddress());
show_display("", " Connected!!", "" , " loading ...", 1000);
} else if (WiFi.status() != WL_CONNECTED) {
startAP = true;
Expand Down

0 comments on commit ee95931

Please sign in to comment.