move credentials into struct

This commit is contained in:
Florian Hoss 2022-04-22 08:46:37 +02:00
parent 6a2b4df655
commit bb1a67c49d
4 changed files with 15 additions and 9 deletions

View file

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

View file

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

View file

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

View file

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