Skip to content

Commit 7e19678

Browse files
committed
only write access in API is enabled
1 parent 5ef1c7e commit 7e19678

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/WebAPIService.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ WebAPIService::WebAPIService(AsyncWebServer * server) {
2828

2929
// e.g. http://ems-esp/api?device=boiler&cmd=wwtemp&data=20&id=1
3030
void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
31-
// see if the API is enabled
32-
bool api_enabled;
33-
EMSESP::webSettingsService.read([&](WebSettings & settings) { api_enabled = settings.api_enabled; });
34-
3531
// must have device and cmd parameters
3632
if ((!request->hasParam(F_(device))) || (!request->hasParam(F_(cmd)))) {
3733
request->send(400, "text/plain", F("Invalid syntax"));
@@ -77,8 +73,10 @@ void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
7773
if (data.isEmpty()) {
7874
ok = Command::call(device_type, cmd.c_str(), nullptr, id.toInt(), json); // command only
7975
} else {
76+
// we only allow commands with parameters if the API is enabled
77+
bool api_enabled;
78+
EMSESP::webSettingsService.read([&](WebSettings & settings) { api_enabled = settings.api_enabled; });
8079
if (api_enabled) {
81-
// we only allow commands with parameters if the API is enabled
8280
ok = Command::call(device_type, cmd.c_str(), data.c_str(), id.toInt(), json); // has cmd, data and id
8381
} else {
8482
request->send(401, "text/plain", F("Unauthorized"));

src/WebDevicesService.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,16 @@ void WebDevicesService::device_data(AsyncWebServerRequest * request, JsonVariant
100100
request->send(response);
101101
}
102102

103+
// takes a command and its data value from a specific Device, from the Web
103104
void WebDevicesService::write_value(AsyncWebServerRequest * request, JsonVariant & json) {
105+
// only issue commands if the API is enabled
106+
EMSESP::webSettingsService.read([&](WebSettings & settings) {
107+
if (!settings.api_enabled) {
108+
request->send(403); // forbidden error
109+
return;
110+
}
111+
});
112+
104113
if (json.is<JsonObject>()) {
105114
JsonObject dv = json["devicevalue"];
106115

@@ -125,16 +134,14 @@ void WebDevicesService::write_value(AsyncWebServerRequest * request, JsonVariant
125134
}
126135

127136
if (ok) {
128-
AsyncWebServerResponse * response = request->beginResponse(200); // OK
129-
request->send(response);
137+
request->send(200);
130138
}
131139
return; // found device, quit
132140
}
133141
}
134142
}
135143

136-
AsyncWebServerResponse * response = request->beginResponse(204); // no content error
137-
request->send(response);
144+
request->send(204); // no content error
138145
}
139146
}
140147

0 commit comments

Comments
 (0)