Skip to content

Commit 04b8aa6

Browse files
committed
Add basic auth to when interface and an option to get all data on web ui
1 parent 4a64889 commit 04b8aa6

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

src/EasyMqtt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
106106

107107
setInterval(60, 10);
108108

109-
get("$system").setInterval(300);
109+
get("$system").setInterval(300); // every 5 min
110110
get("$system")["deviceId"] << [this]() {
111111
return deviceId;
112112
};
@@ -134,6 +134,7 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
134134
get("$system")["online"] << []() {
135135
return "ON";
136136
};
137+
get("$system")["time"].setInterval(900); // every 15 min
137138
get("$system")["time"] << [this]() {
138139
ntp().update();
139140
return String(ntp().getTime());
@@ -146,6 +147,7 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
146147
ESP.restart();
147148
}
148149
};
150+
149151
get("$system")["reset"] >> [this](String value) {
150152
if(value == "reset") {
151153
debug("Factory Reset");

src/WebPortal.cpp

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,30 @@ void WebPortal::setup(Entry& mqttEntry, ConfigEntry& config, NTPClient& ntpClien
3636
}
3737

3838
void WebPortal::handleRoot() {
39+
if(!auth()) return;
3940
String page = "";
4041
page += FPSTR(HTML_MAIN1);
4142
// Sensors
4243
mqtt->each([&](Entry* entry) {
43-
if(!entry->isInternal()) {
44-
String value = entry->getValue();
45-
if(value != NULL) {
46-
value.replace("{value}", entry->getValue());
47-
48-
page += FPSTR(HTML_SENSOR);
49-
page.replace("{value}", value);
50-
page.replace("{last_updated}", time(entry->getLastUpdate()));
44+
if(!entry->isInternal() || webServer->arg("show").equals("all")) {
45+
String value = entry->getValue();
46+
if(value != NULL) {
47+
page += FPSTR(HTML_SENSOR);
48+
if(getName(entry).endsWith("password")) {
49+
page.replace("{value}", "***");
50+
} else {
51+
value.replace("{value}", entry->getValue());
52+
page.replace("{value}", value);
53+
}
54+
page.replace("{last_updated}", time(entry->getLastUpdate()));
55+
}
56+
if(entry->isOut()) {
57+
page += FPSTR(HTML_INPUT);
58+
}
59+
page.replace("{name}", getName(entry));
60+
page.replace("{path}", getRestPath(entry));
5161
}
52-
if(entry->isOut()) {
53-
page += FPSTR(HTML_INPUT);
54-
}
55-
page.replace("{name}", getName(entry));
56-
page.replace("{path}", getRestPath(entry));
57-
}
58-
});
62+
});
5963

6064
// Config
6165
page += FPSTR(HTML_MAIN2);
@@ -68,36 +72,36 @@ void WebPortal::handleRoot() {
6872
String name = getName(config, entry).substring(1);
6973
page.replace("{key}", name);
7074
if(name.endsWith("password")) {
71-
page.replace("{type}", "password");
72-
page.replace("{value}", "");
75+
page.replace("{type}", "password");
76+
page.replace("{value}", "");
7377
} else {
74-
page.replace("{type}", "text");
75-
page.replace("{value}", entry->getValue());
78+
page.replace("{type}", "text");
79+
page.replace("{value}", entry->getValue());
7680
}
77-
});
81+
});
7882

7983
// About
8084
page += FPSTR(HTML_MAIN3);
8185
mqtt->each([&](Entry* entry) {
8286
if(entry->isOut() || entry->isIn()) {
83-
page += FPSTR(HTML_API_DOC);
84-
String path = entry->getTopic();
85-
if(entry->isOut()) path += "<span class=\"badge\">Set</span>";
86-
if(entry->isIn()) path += "<span class=\"badge\">Get</span>";
87-
page.replace("{path}", path);
87+
page += FPSTR(HTML_API_DOC);
88+
String path = entry->getTopic();
89+
if(entry->isOut()) path += "<span class=\"badge\">Set</span>";
90+
if(entry->isIn()) path += "<span class=\"badge\">Get</span>";
91+
page.replace("{path}", path);
8892
}
89-
});
93+
});
9094

9195
page += FPSTR(HTML_MAIN4);
9296
mqtt->each([&](Entry* entry) {
9397
if(entry->isOut() || entry->isIn()) {
94-
page += FPSTR(HTML_API_DOC);
95-
String path = getRestPath(entry);
96-
if(entry->isOut()) path += "<span class=\"badge\">POST</span>";
97-
if(entry->isIn()) path += "<span class=\"badge\">GET</span>";
98-
page.replace("{path}", path);
98+
page += FPSTR(HTML_API_DOC);
99+
String path = getRestPath(entry);
100+
if(entry->isOut()) path += "<span class=\"badge\">POST</span>";
101+
if(entry->isIn()) path += "<span class=\"badge\">GET</span>";
102+
page.replace("{path}", path);
99103
}
100-
});
104+
});
101105

102106
page += FPSTR(HTML_MAIN5);
103107
page.replace("{device_id}", mqtt->get("$system")["deviceId"].getValue());
@@ -106,6 +110,7 @@ void WebPortal::handleRoot() {
106110
}
107111

108112
void WebPortal::handleRest() {
113+
if(!auth()) return;
109114
Entry* entry = &mqtt->get(webServer->uri().substring(6).c_str());
110115
if(webServer->method() == HTTP_GET && entry->isIn()) {
111116
webServer->send(200, "application/json", "{\"value\":\"" + entry->getValue() + "\",\"updated\":\"" + time(entry->getLastUpdate()) + "\"}");
@@ -118,7 +123,7 @@ void WebPortal::handleRest() {
118123
}
119124

120125
void WebPortal::handleSaveConfig() {
121-
Serial.println("Save");
126+
if(!auth()) return;
122127
config->each([&](Entry* entry) {
123128
String name = getName(entry);
124129
name = name.substring(9);
@@ -135,6 +140,7 @@ void WebPortal::handleSaveConfig() {
135140
}
136141

137142
void WebPortal::handleNotFound() {
143+
if(!auth()) return;
138144
webServer->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
139145
webServer->sendHeader("Pragma", "no-cache");
140146
webServer->sendHeader("Expires", "-1");
@@ -161,3 +167,11 @@ String WebPortal::time(long time) {
161167

162168
return String(formated);
163169
}
170+
171+
bool WebPortal::auth() {
172+
if (!webServer->authenticate("admin", "password")) {
173+
webServer->requestAuthentication();
174+
return false;
175+
}
176+
return true;
177+
}

src/WebPortal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class WebPortal {
2020
String getRestPath(Entry* entry);
2121

2222
String time(long time);
23+
bool auth();
2324

2425
public:
2526
WebPortal();

0 commit comments

Comments
 (0)