Skip to content

Commit

Permalink
Introduce numeric error codes in security webapi
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Dec 23, 2022
1 parent ea96051 commit 03ed626
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/WebApi_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ enum WebApiError {
NtpMinuteInvalid,
NtpSecondInvalid,
NtpTimeUpdated,

SecurityBase = 10000,
SecurityPasswordLength,
SecurityAuthSuccess,
};
9 changes: 9 additions & 0 deletions src/WebApi_security.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "WebApi_security.h"
#include "Configuration.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include "helper.h"
#include <AsyncJson.h>

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -79,13 +83,16 @@ 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;
}

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("code")] = WebApiError::SecurityPasswordLength;
retMsg[F("param")][F("max")] = WIFI_MAX_PASSWORD_STRLEN;
response->setLength();
request->send(response);
return;
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 03ed626

Please sign in to comment.