Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
2025-09-26 12:09:36 +09:00
parent 358090db5d
commit e07aad2d7d
7 changed files with 73 additions and 44 deletions

View File

@@ -1,6 +1,8 @@
#include "webserver.h"
#include <stdio.h>
#include <string.h>
#include "auth.h"
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_wifi.h"
@@ -11,8 +13,6 @@
#include "monitor.h"
#include "nconfig.h"
#include "system.h"
#include "cJSON.h"
#include "auth.h"
static const char* TAG = "WEBSERVER";
@@ -28,9 +28,11 @@ static esp_err_t index_handler(httpd_req_t* req)
size_t remaining = index_html_size;
const char* ptr = (const char*)index_html_start;
while (remaining > 0) {
while (remaining > 0)
{
size_t to_send = remaining < 2048 ? remaining : 2048;
if (httpd_resp_send_chunk(req, ptr, to_send) != ESP_OK) {
if (httpd_resp_send_chunk(req, ptr, to_send) != ESP_OK)
{
ESP_LOGE(TAG, "File sending failed!");
httpd_resp_send_chunk(req, NULL, 0);
httpd_resp_send_500(req);
@@ -48,8 +50,10 @@ static esp_err_t login_handler(httpd_req_t* req)
{
char content[100]; // Adjust size as needed for username/password
int ret = httpd_req_recv(req, content, sizeof(content) - 1); // -1 for null terminator
if (ret <= 0) { // 0 means connection closed, < 0 means error
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
if (ret <= 0)
{ // 0 means connection closed, < 0 means error
if (ret == HTTPD_SOCK_ERR_TIMEOUT)
{
httpd_resp_send_408(req);
}
return ESP_FAIL;
@@ -58,33 +62,37 @@ static esp_err_t login_handler(httpd_req_t* req)
ESP_LOGI(TAG, "Received login request: %s", content);
cJSON *root = cJSON_Parse(content);
if (root == NULL) {
cJSON* root = cJSON_Parse(content);
if (root == NULL)
{
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
return ESP_FAIL;
}
cJSON *username_json = cJSON_GetObjectItemCaseSensitive(root, "username");
cJSON *password_json = cJSON_GetObjectItemCaseSensitive(root, "password");
cJSON* username_json = cJSON_GetObjectItemCaseSensitive(root, "username");
cJSON* password_json = cJSON_GetObjectItemCaseSensitive(root, "password");
if (!cJSON_IsString(username_json) || (username_json->valuestring == NULL) ||
!cJSON_IsString(password_json) || (password_json->valuestring == NULL)) {
if (!cJSON_IsString(username_json) || (username_json->valuestring == NULL) || !cJSON_IsString(password_json) ||
(password_json->valuestring == NULL))
{
cJSON_Delete(root);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing username or password");
return ESP_FAIL;
}
const char *username = username_json->valuestring;
const char *password = password_json->valuestring;
const char* username = username_json->valuestring;
const char* password = password_json->valuestring;
// TODO: Implement actual credential validation
// For now, a simple hardcoded check
if (strcmp(username, "admin") == 0 && strcmp(password, "password") == 0) {
char *token = auth_generate_token();
if (token) {
cJSON *response_root = cJSON_CreateObject();
if (strcmp(username, "admin") == 0 && strcmp(password, "password") == 0)
{
char* token = auth_generate_token();
if (token)
{
cJSON* response_root = cJSON_CreateObject();
cJSON_AddStringToObject(response_root, "token", token);
char *json_response = cJSON_Print(response_root);
char* json_response = cJSON_Print(response_root);
httpd_resp_set_type(req, "application/json");
httpd_resp_sendstr(req, json_response);
@@ -92,10 +100,14 @@ static esp_err_t login_handler(httpd_req_t* req)
free(token); // Free the token generated by auth_generate_token
free(json_response);
cJSON_Delete(response_root);
} else {
}
else
{
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to generate token");
}
} else {
}
else
{
httpd_resp_send_err(req, HTTPD_401_UNAUTHORIZED, "Invalid credentials");
}