refact auth

This commit is contained in:
2025-06-08 10:06:28 +01:00
parent 71b6cb7878
commit 214cf1ee04
236 changed files with 466 additions and 251 deletions

View File

@@ -16,7 +16,10 @@
#include "cJSON.h"
#include "rest.h"
#include "evse_api.h"
#include "cJSON.h"
#include "esp_log.h"
#include "esp_http_server.h"
#include "auth.h"
static const char *REST_TAG = "esp-rest";
#define REST_CHECK(a, str, goto_tag, ...) \
@@ -72,14 +75,6 @@ static struct {
int temperatureLimit;
} settings_config = {0, 0, 0, 0, 0};
// Estruturas para armazenar as configurações de autenticação e usuários
static struct {
bool RFID;
bool App;
bool Password;
} auth_methods = {false, false, false};
static struct {
char username[128];
} users[10] = {{"admin"}, {"user1"}};
@@ -586,65 +581,67 @@ static esp_err_t config_mqtt_post_handler(httpd_req_t *req)
return ESP_OK;
}
// Manipulador para o endpoint GET /api/v1/config/auth-methods
// GET /api/v1/config/auth-methods
static esp_err_t config_auth_methods_get_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "application/json");
// Criar objeto JSON com as configurações de métodos de autenticação
cJSON *config = cJSON_CreateObject();
cJSON_AddBoolToObject(config, "RFID", evse_is_require_auth());
cJSON_AddBoolToObject(config, "App", auth_methods.App);
cJSON_AddBoolToObject(config, "Password", auth_methods.Password);
cJSON_AddBoolToObject(config, "RFID", auth_is_enabled());
cJSON_AddBoolToObject(config, "App", false);
cJSON_AddBoolToObject(config, "Password", false);
// Convertendo para string e enviando a resposta
const char *config_str = cJSON_Print(config);
char *config_str = cJSON_PrintUnformatted(config);
httpd_resp_sendstr(req, config_str);
// Liberando a memória
free((void *)config_str);
free(config_str);
cJSON_Delete(config);
return ESP_OK;
}
// Manipulador para o endpoint POST /api/v1/config/auth-methods
// POST /api/v1/config/auth-methods
static esp_err_t config_auth_methods_post_handler(httpd_req_t *req)
{
char buf[512]; // Buffer para armazenar a requisição
char buf[512];
int len = httpd_req_recv(req, buf, sizeof(buf) - 1);
if (len <= 0) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid request body");
return ESP_FAIL;
}
buf[len] = '\0'; // Garantir que a string esteja terminada
buf[len] = '\0';
// Parse JSON recebido
cJSON *json = cJSON_Parse(buf);
if (json == NULL) {
if (!json) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
return ESP_FAIL;
}
// Atualizando as configurações de autenticação
cJSON *RFID = cJSON_GetObjectItem(json, "RFID");
if (RFID) evse_set_require_auth(RFID->valueint != 0);
if (cJSON_IsBool(RFID)) {
auth_set_enabled(cJSON_IsTrue(RFID));
}
/*
cJSON *App = cJSON_GetObjectItem(json, "App");
if (App) auth_methods.App = App->valueint;
if (cJSON_IsBool(App)) {
auth_methods.App = cJSON_IsTrue(App);
}
cJSON *Password = cJSON_GetObjectItem(json, "Password");
if (Password) auth_methods.Password = Password->valueint;
if (cJSON_IsBool(Password)) {
auth_methods.Password = cJSON_IsTrue(Password);
}*/
cJSON_Delete(json);
// Responder com uma mensagem de sucesso
httpd_resp_sendstr(req, "Configurações de Autenticação atualizadas com sucesso");
httpd_resp_sendstr(req, "Configurações de autenticação atualizadas com sucesso");
return ESP_OK;
}
// Manipulador para o endpoint GET /api/v1/config/users
static esp_err_t config_users_get_handler(httpd_req_t *req)
{