Add login function
TODO ws request auth Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
#include "monitor.h"
|
||||
#include "nconfig.h"
|
||||
#include "system.h"
|
||||
#include "cJSON.h"
|
||||
#include "auth.h"
|
||||
|
||||
static const char* TAG = "WEBSERVER";
|
||||
|
||||
@@ -42,9 +44,70 @@ static esp_err_t index_handler(httpd_req_t* req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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) {
|
||||
httpd_resp_send_408(req);
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
content[ret] = '\0'; // Null-terminate the received data
|
||||
|
||||
ESP_LOGI(TAG, "Received login request: %s", content);
|
||||
|
||||
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");
|
||||
|
||||
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;
|
||||
|
||||
// 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();
|
||||
cJSON_AddStringToObject(response_root, "token", token);
|
||||
char *json_response = cJSON_Print(response_root);
|
||||
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_sendstr(req, json_response);
|
||||
|
||||
free(token); // Free the token generated by auth_generate_token
|
||||
free(json_response);
|
||||
cJSON_Delete(response_root);
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to generate token");
|
||||
}
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_401_UNAUTHORIZED, "Invalid credentials");
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
void start_webserver(void)
|
||||
{
|
||||
auth_init();
|
||||
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.stack_size = 1024 * 8;
|
||||
@@ -61,6 +124,10 @@ void start_webserver(void)
|
||||
httpd_uri_t index = {.uri = "/", .method = HTTP_GET, .handler = index_handler, .user_ctx = NULL};
|
||||
httpd_register_uri_handler(server, &index);
|
||||
|
||||
// Login endpoint
|
||||
httpd_uri_t login = {.uri = "/login", .method = HTTP_POST, .handler = login_handler, .user_ctx = NULL};
|
||||
httpd_register_uri_handler(server, &login);
|
||||
|
||||
register_wifi_endpoint(server);
|
||||
register_ws_endpoint(server);
|
||||
register_control_endpoint(server);
|
||||
|
||||
Reference in New Issue
Block a user