From 03ed626da65326241b54d15b201bccd44f52d8df Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 23 Dec 2022 20:34:10 +0100 Subject: [PATCH] Introduce numeric error codes in security webapi --- include/WebApi_errors.h | 4 ++++ src/WebApi_security.cpp | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/include/WebApi_errors.h b/include/WebApi_errors.h index be0eb84..3c06468 100644 --- a/include/WebApi_errors.h +++ b/include/WebApi_errors.h @@ -73,4 +73,8 @@ enum WebApiError { NtpMinuteInvalid, NtpSecondInvalid, NtpTimeUpdated, + + SecurityBase = 10000, + SecurityPasswordLength, + SecurityAuthSuccess, }; \ No newline at end of file diff --git a/src/WebApi_security.cpp b/src/WebApi_security.cpp index e4efdf4..91f0927 100644 --- a/src/WebApi_security.cpp +++ b/src/WebApi_security.cpp @@ -5,6 +5,7 @@ #include "WebApi_security.h" #include "Configuration.h" #include "WebApi.h" +#include "WebApi_errors.h" #include "helper.h" #include @@ -52,6 +53,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request) if (!request->hasParam("data", true)) { retMsg[F("message")] = F("No values found!"); + retMsg[F("code")] = WebApiError::GenericNoValueFound; response->setLength(); request->send(response); return; @@ -61,6 +63,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request) if (json.length() > 1024) { retMsg[F("message")] = F("Data too large!"); + retMsg[F("code")] = WebApiError::GenericDataTooLarge; response->setLength(); request->send(response); return; @@ -71,6 +74,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request) if (error) { retMsg[F("message")] = F("Failed to parse data!"); + retMsg[F("code")] = WebApiError::GenericParseError; response->setLength(); request->send(response); return; @@ -79,6 +83,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request) if (!root.containsKey("password") && root.containsKey("allow_readonly")) { retMsg[F("message")] = F("Values are missing!"); + retMsg[F("code")] = WebApiError::GenericValueMissing; response->setLength(); request->send(response); return; @@ -86,6 +91,8 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request) if (root[F("password")].as().length() < 8 || root[F("password")].as().length() > WIFI_MAX_PASSWORD_STRLEN) { 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(); request->send(response); return; @@ -98,6 +105,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request) retMsg[F("type")] = F("success"); retMsg[F("message")] = F("Settings saved!"); + retMsg[F("code")] = WebApiError::GenericSuccess; response->setLength(); request->send(response); @@ -113,6 +121,7 @@ void WebApiSecurityClass::onAuthenticateGet(AsyncWebServerRequest* request) JsonObject retMsg = response->getRoot(); retMsg[F("type")] = F("success"); retMsg[F("message")] = F("Authentication successfull!"); + retMsg[F("code")] = WebApiError::SecurityAuthSuccess; response->setLength(); request->send(response);