From bb1a67c49dacbf04eb3800de1c827d160f55c71c Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Fri, 22 Apr 2022 08:46:37 +0200 Subject: [PATCH] move credentials into struct --- ESPFirewall/lib/Firewall/src/Utils.hpp | 7 +++++++ ESPFirewall/lib/Firewall/src/esp32API.cpp | 12 ++++++------ ESPFirewall/lib/Firewall/src/esp32API.hpp | 3 +-- ESPFirewall/src/main.cpp | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/Utils.hpp b/ESPFirewall/lib/Firewall/src/Utils.hpp index 8be4958..b5dfed5 100644 --- a/ESPFirewall/lib/Firewall/src/Utils.hpp +++ b/ESPFirewall/lib/Firewall/src/Utils.hpp @@ -6,6 +6,7 @@ #include "esp32-hal-log.h" static const uint8_t IPV4ADDRESS_LENGTH = 16; +static const uint8_t CREDENTIALS_LENGTH = 32; typedef enum firewall_targets : uint8_t { @@ -44,6 +45,12 @@ typedef struct firewall_rules struct firewall_rules *next; } firewall_rule_t; +typedef struct credentials +{ + char password[CREDENTIALS_LENGTH]; + char username[CREDENTIALS_LENGTH]; +} credential_t; + namespace firewall { String protocol_to_string(firewall_protocol_t &protocol); diff --git a/ESPFirewall/lib/Firewall/src/esp32API.cpp b/ESPFirewall/lib/Firewall/src/esp32API.cpp index e922659..bc80051 100644 --- a/ESPFirewall/lib/Firewall/src/esp32API.cpp +++ b/ESPFirewall/lib/Firewall/src/esp32API.cpp @@ -27,18 +27,18 @@ namespace firewall ok_t API::setup_auth(const char *username, const char *password) { - if (!username || *username == 0x00 || strlen(username) > sizeof(this->username)) + if (!username || *username == 0x00 || strlen(username) > CREDENTIALS_LENGTH) { log_e("Username too long or missing!"); return ERROR; } - strncpy(this->username, username, sizeof(this->username)); - if (!password || *password == 0x00 || strlen(password) > sizeof(this->password)) + strncpy(credentials.username, username, CREDENTIALS_LENGTH); + if (!password || *password == 0x00 || strlen(password) > CREDENTIALS_LENGTH) { log_e("Password too long or missing!"); return ERROR; } - strncpy(this->password, password, sizeof(this->password)); + strncpy(credentials.password, password, CREDENTIALS_LENGTH); return SUCCESS; } @@ -46,8 +46,8 @@ namespace firewall { std::string reqUsername = request->getBasicAuthUser(); std::string reqPassword = request->getBasicAuthPassword(); - if ((strncmp(this->username, reqUsername.c_str(), sizeof(this->username)) != 0) || - (strncmp(this->password, reqPassword.c_str(), sizeof(this->password)) != 0)) + if ((strncmp(this->credentials.username, reqUsername.c_str(), CREDENTIALS_LENGTH) != 0) || + (strncmp(this->credentials.password, reqPassword.c_str(), CREDENTIALS_LENGTH) != 0)) { this->json_message_response(response, "unauthorized", 403); return DENIED; diff --git a/ESPFirewall/lib/Firewall/src/esp32API.hpp b/ESPFirewall/lib/Firewall/src/esp32API.hpp index fa7a203..95afed5 100644 --- a/ESPFirewall/lib/Firewall/src/esp32API.hpp +++ b/ESPFirewall/lib/Firewall/src/esp32API.hpp @@ -19,8 +19,7 @@ namespace firewall private: HTTPSServer *server; SSLCert *certificate; - char username[32]; - char password[32]; + credential_t credentials; ok_t setup_auth(const char *, const char *); auth_t check_auth(HTTPRequest *, HTTPResponse *); diff --git a/ESPFirewall/src/main.cpp b/ESPFirewall/src/main.cpp index 6101a4c..d7eed65 100644 --- a/ESPFirewall/src/main.cpp +++ b/ESPFirewall/src/main.cpp @@ -19,7 +19,7 @@ void setup_wifi() void setup() { setup_wifi(); - firewall_api = new firewall::API(api_username, api_password, 8080); + firewall_api = new firewall::API(api_username, api_password); } void loop()