Introduce numeric error codes in security webapi

This commit is contained in:
Thomas Basler 2022-12-23 20:34:10 +01:00
parent ea96051203
commit 03ed626da6
2 changed files with 13 additions and 0 deletions

View File

@ -73,4 +73,8 @@ enum WebApiError {
NtpMinuteInvalid, NtpMinuteInvalid,
NtpSecondInvalid, NtpSecondInvalid,
NtpTimeUpdated, NtpTimeUpdated,
SecurityBase = 10000,
SecurityPasswordLength,
SecurityAuthSuccess,
}; };

View File

@ -5,6 +5,7 @@
#include "WebApi_security.h" #include "WebApi_security.h"
#include "Configuration.h" #include "Configuration.h"
#include "WebApi.h" #include "WebApi.h"
#include "WebApi_errors.h"
#include "helper.h" #include "helper.h"
#include <AsyncJson.h> #include <AsyncJson.h>
@ -52,6 +53,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
if (!request->hasParam("data", true)) { if (!request->hasParam("data", true)) {
retMsg[F("message")] = F("No values found!"); retMsg[F("message")] = F("No values found!");
retMsg[F("code")] = WebApiError::GenericNoValueFound;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -61,6 +63,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
if (json.length() > 1024) { if (json.length() > 1024) {
retMsg[F("message")] = F("Data too large!"); retMsg[F("message")] = F("Data too large!");
retMsg[F("code")] = WebApiError::GenericDataTooLarge;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -71,6 +74,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
if (error) { if (error) {
retMsg[F("message")] = F("Failed to parse data!"); retMsg[F("message")] = F("Failed to parse data!");
retMsg[F("code")] = WebApiError::GenericParseError;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -79,6 +83,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
if (!root.containsKey("password") if (!root.containsKey("password")
&& root.containsKey("allow_readonly")) { && root.containsKey("allow_readonly")) {
retMsg[F("message")] = F("Values are missing!"); retMsg[F("message")] = F("Values are missing!");
retMsg[F("code")] = WebApiError::GenericValueMissing;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -86,6 +91,8 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
if (root[F("password")].as<String>().length() < 8 || root[F("password")].as<String>().length() > WIFI_MAX_PASSWORD_STRLEN) { if (root[F("password")].as<String>().length() < 8 || root[F("password")].as<String>().length() > WIFI_MAX_PASSWORD_STRLEN) {
retMsg[F("message")] = F("Password must between 8 and " STR(WIFI_MAX_PASSWORD_STRLEN) " characters long!"); retMsg[F("message")] = F("Password must between 8 and " STR(WIFI_MAX_PASSWORD_STRLEN) " characters long!");
retMsg[F("code")] = WebApiError::SecurityPasswordLength;
retMsg[F("param")][F("max")] = WIFI_MAX_PASSWORD_STRLEN;
response->setLength(); response->setLength();
request->send(response); request->send(response);
return; return;
@ -98,6 +105,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
retMsg[F("type")] = F("success"); retMsg[F("type")] = F("success");
retMsg[F("message")] = F("Settings saved!"); retMsg[F("message")] = F("Settings saved!");
retMsg[F("code")] = WebApiError::GenericSuccess;
response->setLength(); response->setLength();
request->send(response); request->send(response);
@ -113,6 +121,7 @@ void WebApiSecurityClass::onAuthenticateGet(AsyncWebServerRequest* request)
JsonObject retMsg = response->getRoot(); JsonObject retMsg = response->getRoot();
retMsg[F("type")] = F("success"); retMsg[F("type")] = F("success");
retMsg[F("message")] = F("Authentication successfull!"); retMsg[F("message")] = F("Authentication successfull!");
retMsg[F("code")] = WebApiError::SecurityAuthSuccess;
response->setLength(); response->setLength();
request->send(response); request->send(response);