use basic auth again

This commit is contained in:
Florian Hoss 2022-04-24 00:41:16 +02:00
parent bda65724aa
commit a4870fd6fd
2 changed files with 22 additions and 0 deletions

View file

@ -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)

View file

@ -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();