Refactored code by breaking the main.c up into BUD files.
This commit is contained in:
@@ -9,12 +9,26 @@
|
||||
#include <sys/mount.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include "admin_api.h"
|
||||
#include "ginxsom.h"
|
||||
#include "../nostr_core_lib/nostr_core/request_validator.h"
|
||||
|
||||
// Database path (consistent with main.c)
|
||||
#define DB_PATH "db/ginxsom.db"
|
||||
|
||||
// Function declarations (moved from admin_api.h)
|
||||
void handle_admin_api_request(const char* method, const char* uri);
|
||||
void handle_stats_api(void);
|
||||
void handle_config_get_api(void);
|
||||
void handle_config_put_api(void);
|
||||
void handle_files_api(void);
|
||||
void handle_health_api(void);
|
||||
int authenticate_admin_request(const char* auth_header);
|
||||
int is_admin_enabled(void);
|
||||
int verify_admin_pubkey(const char* event_pubkey);
|
||||
void send_json_response(int status, const char* json_content);
|
||||
void send_json_error(int status, const char* error, const char* message);
|
||||
int parse_query_params(const char* query_string, char params[][256], int max_params);
|
||||
|
||||
// Forward declarations for local utility functions
|
||||
static int admin_nip94_get_origin(char* out, size_t out_size);
|
||||
static void admin_nip94_build_blob_url(const char* origin, const char* sha256, const char* mime_type, char* out, size_t out_size);
|
||||
@@ -154,35 +168,32 @@ int authenticate_admin_request(const char* auth_header) {
|
||||
return 0; // No auth header
|
||||
}
|
||||
|
||||
// Use existing authentication system with "admin" method
|
||||
int auth_result = authenticate_request(auth_header, "admin", NULL);
|
||||
if (auth_result != NOSTR_SUCCESS) {
|
||||
return 0; // Invalid Nostr event
|
||||
// Use unified request validation system for admin operations
|
||||
nostr_request_t request = {
|
||||
.operation = "admin",
|
||||
.auth_header = auth_header,
|
||||
.event = NULL,
|
||||
.resource_hash = NULL,
|
||||
.mime_type = NULL,
|
||||
.file_size = 0,
|
||||
.client_ip = getenv("REMOTE_ADDR"),
|
||||
.app_context = NULL
|
||||
};
|
||||
|
||||
nostr_request_result_t result;
|
||||
int auth_result = nostr_validate_request(&request, &result);
|
||||
|
||||
if (auth_result != NOSTR_SUCCESS || !result.valid) {
|
||||
return 0; // Authentication failed
|
||||
}
|
||||
|
||||
// Extract pubkey from validated event using existing parser
|
||||
char event_json[4096];
|
||||
int parse_result = parse_authorization_header(auth_header, event_json, sizeof(event_json));
|
||||
if (parse_result != NOSTR_SUCCESS) {
|
||||
return 0;
|
||||
// Extract pubkey from validation result and verify admin status
|
||||
const char* event_pubkey = result.pubkey[0] ? result.pubkey : NULL;
|
||||
if (!event_pubkey) {
|
||||
return 0; // No pubkey available
|
||||
}
|
||||
|
||||
cJSON* event = cJSON_Parse(event_json);
|
||||
if (!event) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
cJSON* pubkey_json = cJSON_GetObjectItem(event, "pubkey");
|
||||
if (!pubkey_json || !cJSON_IsString(pubkey_json)) {
|
||||
cJSON_Delete(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* event_pubkey = cJSON_GetStringValue(pubkey_json);
|
||||
int is_admin = verify_admin_pubkey(event_pubkey);
|
||||
|
||||
cJSON_Delete(event);
|
||||
return is_admin;
|
||||
return verify_admin_pubkey(event_pubkey);
|
||||
}
|
||||
|
||||
int verify_admin_pubkey(const char* event_pubkey) {
|
||||
|
||||
Reference in New Issue
Block a user