v0.1.10 - In the middle of working on getting admin api working
This commit is contained in:
@@ -11,8 +11,8 @@
|
||||
#include <unistd.h>
|
||||
#include "ginxsom.h"
|
||||
|
||||
// Database path (consistent with main.c)
|
||||
#define DB_PATH "db/ginxsom.db"
|
||||
// Use global database path from main.c
|
||||
extern char g_db_path[];
|
||||
|
||||
// Function declarations (moved from admin_api.h)
|
||||
void handle_admin_api_request(const char* method, const char* uri, const char* validated_pubkey, int is_authenticated);
|
||||
@@ -44,7 +44,7 @@ static int admin_nip94_get_origin(char* out, size_t out_size) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc) {
|
||||
// Default on DB error
|
||||
strncpy(out, "http://localhost:9001", out_size - 1);
|
||||
@@ -130,8 +130,12 @@ void handle_admin_api_request(const char* method, const char* uri, const char* v
|
||||
}
|
||||
|
||||
// Authentication now handled by centralized validation system
|
||||
// Health endpoint is exempt from authentication requirement
|
||||
if (strcmp(path, "/health") != 0) {
|
||||
// Health endpoint and POST /admin (Kind 23456 events) are exempt from authentication requirement
|
||||
// Kind 23456 events authenticate themselves via signed event validation
|
||||
int skip_auth = (strcmp(path, "/health") == 0) ||
|
||||
(strcmp(method, "POST") == 0 && strcmp(path, "/admin") == 0);
|
||||
|
||||
if (!skip_auth) {
|
||||
if (!is_authenticated || !validated_pubkey) {
|
||||
send_json_error(401, "admin_auth_required", "Valid admin authentication required");
|
||||
return;
|
||||
@@ -157,6 +161,13 @@ void handle_admin_api_request(const char* method, const char* uri, const char* v
|
||||
} else {
|
||||
send_json_error(404, "not_found", "API endpoint not found");
|
||||
}
|
||||
} else if (strcmp(method, "POST") == 0) {
|
||||
if (strcmp(path, "/admin") == 0) {
|
||||
// Handle Kind 23456/23457 admin event commands
|
||||
handle_admin_event_request();
|
||||
} else {
|
||||
send_json_error(404, "not_found", "API endpoint not found");
|
||||
}
|
||||
} else if (strcmp(method, "PUT") == 0) {
|
||||
if (strcmp(path, "/config") == 0) {
|
||||
handle_config_put_api();
|
||||
@@ -201,7 +212,7 @@ int verify_admin_pubkey(const char* event_pubkey) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc, is_admin = 0;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc) {
|
||||
return 0;
|
||||
}
|
||||
@@ -228,7 +239,7 @@ int is_admin_enabled(void) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc, enabled = 0;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc) {
|
||||
return 0; // Default disabled if can't access DB
|
||||
}
|
||||
@@ -254,7 +265,7 @@ void handle_stats_api(void) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc) {
|
||||
send_json_error(500, "database_error", "Failed to open database");
|
||||
return;
|
||||
@@ -349,7 +360,7 @@ void handle_config_get_api(void) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc) {
|
||||
send_json_error(500, "database_error", "Failed to open database");
|
||||
return;
|
||||
@@ -423,7 +434,7 @@ void handle_config_put_api(void) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READWRITE, NULL);
|
||||
if (rc) {
|
||||
free(json_body);
|
||||
cJSON_Delete(config_data);
|
||||
@@ -541,7 +552,7 @@ void handle_config_key_put_api(const char* key) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READWRITE, NULL);
|
||||
if (rc) {
|
||||
free(json_body);
|
||||
cJSON_Delete(request_data);
|
||||
@@ -621,7 +632,7 @@ void handle_files_api(void) {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc) {
|
||||
send_json_error(500, "database_error", "Failed to open database");
|
||||
return;
|
||||
@@ -715,7 +726,7 @@ void handle_health_api(void) {
|
||||
|
||||
// Check database connection
|
||||
sqlite3* db;
|
||||
int rc = sqlite3_open_v2(DB_PATH, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
int rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
cJSON_AddStringToObject(data, "database", "connected");
|
||||
sqlite3_close(db);
|
||||
|
||||
Reference in New Issue
Block a user