@@ -36,26 +36,30 @@ void WebPortal::setup(Entry& mqttEntry, ConfigEntry& config, NTPClient& ntpClien
36
36
}
37
37
38
38
void WebPortal::handleRoot () {
39
+ if (!auth ()) return ;
39
40
String page = " " ;
40
41
page += FPSTR (HTML_MAIN1);
41
42
// Sensors
42
43
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));
51
61
}
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
+ });
59
63
60
64
// Config
61
65
page += FPSTR (HTML_MAIN2);
@@ -68,36 +72,36 @@ void WebPortal::handleRoot() {
68
72
String name = getName (config, entry).substring (1 );
69
73
page.replace (" {key}" , name);
70
74
if (name.endsWith (" password" )) {
71
- page.replace (" {type}" , " password" );
72
- page.replace (" {value}" , " " );
75
+ page.replace (" {type}" , " password" );
76
+ page.replace (" {value}" , " " );
73
77
} else {
74
- page.replace (" {type}" , " text" );
75
- page.replace (" {value}" , entry->getValue ());
78
+ page.replace (" {type}" , " text" );
79
+ page.replace (" {value}" , entry->getValue ());
76
80
}
77
- });
81
+ });
78
82
79
83
// About
80
84
page += FPSTR (HTML_MAIN3);
81
85
mqtt->each ([&](Entry* entry) {
82
86
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);
88
92
}
89
- });
93
+ });
90
94
91
95
page += FPSTR (HTML_MAIN4);
92
96
mqtt->each ([&](Entry* entry) {
93
97
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);
99
103
}
100
- });
104
+ });
101
105
102
106
page += FPSTR (HTML_MAIN5);
103
107
page.replace (" {device_id}" , mqtt->get (" $system" )[" deviceId" ].getValue ());
@@ -106,6 +110,7 @@ void WebPortal::handleRoot() {
106
110
}
107
111
108
112
void WebPortal::handleRest () {
113
+ if (!auth ()) return ;
109
114
Entry* entry = &mqtt->get (webServer->uri ().substring (6 ).c_str ());
110
115
if (webServer->method () == HTTP_GET && entry->isIn ()) {
111
116
webServer->send (200 , " application/json" , " {\" value\" :\" " + entry->getValue () + " \" ,\" updated\" :\" " + time (entry->getLastUpdate ()) + " \" }" );
@@ -118,7 +123,7 @@ void WebPortal::handleRest() {
118
123
}
119
124
120
125
void WebPortal::handleSaveConfig () {
121
- Serial. println ( " Save " ) ;
126
+ if (! auth ()) return ;
122
127
config->each ([&](Entry* entry) {
123
128
String name = getName (entry);
124
129
name = name.substring (9 );
@@ -135,6 +140,7 @@ void WebPortal::handleSaveConfig() {
135
140
}
136
141
137
142
void WebPortal::handleNotFound () {
143
+ if (!auth ()) return ;
138
144
webServer->sendHeader (" Cache-Control" , " no-cache, no-store, must-revalidate" );
139
145
webServer->sendHeader (" Pragma" , " no-cache" );
140
146
webServer->sendHeader (" Expires" , " -1" );
@@ -161,3 +167,11 @@ String WebPortal::time(long time) {
161
167
162
168
return String (formated);
163
169
}
170
+
171
+ bool WebPortal::auth () {
172
+ if (!webServer->authenticate (" admin" , " password" )) {
173
+ webServer->requestAuthentication ();
174
+ return false ;
175
+ }
176
+ return true ;
177
+ }
0 commit comments