From a4870fd6fd916a2468d54ad956f8f8a135837b0e Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sun, 24 Apr 2022 00:41:16 +0200 Subject: [PATCH] use basic auth again --- ESPFirewall/lib/Firewall/src/API.cpp | 21 +++++++++++++++++++++ ESPFirewall/lib/Firewall/src/API.hpp | 1 + 2 files changed, 22 insertions(+) diff --git a/ESPFirewall/lib/Firewall/src/API.cpp b/ESPFirewall/lib/Firewall/src/API.cpp index cf8186d..a77bf8a 100644 --- a/ESPFirewall/lib/Firewall/src/API.cpp +++ b/ESPFirewall/lib/Firewall/src/API.cpp @@ -41,6 +41,19 @@ namespace fw return SUCCESS; } + auth_t API::check_auth() + { + if (server->authenticate(this->credentials.username, this->credentials.password)) + { + return AUTHENTICATED; + } + else + { + this->json_message_response("unauthorized", 403); + return DENIED; + } + } + void API::setup_routing() { this->server->on(UriRegex("/api/v1/firewall/([0-9]+)"), HTTP_GET, std::bind(&API::get_firewall_rule_handler, this)); @@ -57,6 +70,8 @@ namespace fw void API::get_firewall_rule_handler() { + if (this->check_auth() == DENIED) + return; String param = this->server->pathArg(0); int rule_number = atoi(param.c_str()); firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number); @@ -72,11 +87,15 @@ namespace fw void API::get_firewall_rules_handler() { + if (this->check_auth() == DENIED) + return; this->json_generic_response(this->construct_json_firewall(), 200); } void API::post_firewall_handler() { + if (this->check_auth() == DENIED) + return; if (request_has_firewall_parameter()) { firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); @@ -103,6 +122,8 @@ namespace fw void API::delete_firewall_handler() { + if (this->check_auth() == DENIED) + return; String param = this->server->pathArg(0); int rule_number = atoi(param.c_str()); if (delete_rule_from_firewall(rule_number) == SUCCESS) diff --git a/ESPFirewall/lib/Firewall/src/API.hpp b/ESPFirewall/lib/Firewall/src/API.hpp index fa2d7b0..569d675 100644 --- a/ESPFirewall/lib/Firewall/src/API.hpp +++ b/ESPFirewall/lib/Firewall/src/API.hpp @@ -24,6 +24,7 @@ namespace fw credential_t credentials; ok_t setup_auth(const char *, const char *); + auth_t check_auth(); void setup_routing(); void get_firewall_rule_handler();