v0.1.15 - Fix config page display and rebrand admin UI from relay to Blossom terminology
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Admin event handler for Kind 23456/23457 admin commands
|
||||
// Admin event handler for Kind 23458/23459 admin commands
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -27,90 +27,161 @@ extern char g_db_path[];
|
||||
static int get_server_privkey(unsigned char* privkey_bytes);
|
||||
static int get_server_pubkey(char* pubkey_hex, size_t size);
|
||||
static int handle_config_query_command(cJSON* response_data);
|
||||
static int send_admin_response_event(const char* admin_pubkey, const char* request_id,
|
||||
static int send_admin_response_event(const char* admin_pubkey, const char* request_id,
|
||||
cJSON* response_data);
|
||||
static cJSON* parse_authorization_header(void);
|
||||
static int process_admin_event(cJSON* event);
|
||||
|
||||
/**
|
||||
* Handle Kind 23456 admin command event
|
||||
* Expects POST to /api/admin with JSON body containing the event
|
||||
* Handle Kind 23458 admin command event
|
||||
* Supports two delivery methods:
|
||||
* 1. POST body with JSON event
|
||||
* 2. Authorization header with Nostr event
|
||||
*/
|
||||
void handle_admin_event_request(void) {
|
||||
// Read request body
|
||||
const char* content_length_str = getenv("CONTENT_LENGTH");
|
||||
if (!content_length_str) {
|
||||
printf("Status: 411 Length Required\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Content-Length header required\"}\n");
|
||||
return;
|
||||
}
|
||||
cJSON* event = NULL;
|
||||
int should_free_event = 1;
|
||||
|
||||
long content_length = atol(content_length_str);
|
||||
if (content_length <= 0 || content_length > 65536) {
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Invalid content length\"}\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char* json_body = malloc(content_length + 1);
|
||||
if (!json_body) {
|
||||
printf("Status: 500 Internal Server Error\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Memory allocation failed\"}\n");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t bytes_read = fread(json_body, 1, content_length, stdin);
|
||||
if (bytes_read != (size_t)content_length) {
|
||||
free(json_body);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Failed to read complete request body\"}\n");
|
||||
return;
|
||||
}
|
||||
json_body[content_length] = '\0';
|
||||
|
||||
// Parse event JSON
|
||||
cJSON* event = cJSON_Parse(json_body);
|
||||
free(json_body);
|
||||
// First, try to get event from Authorization header
|
||||
event = parse_authorization_header();
|
||||
|
||||
// If not in header, try POST body
|
||||
if (!event) {
|
||||
const char* content_length_str = getenv("CONTENT_LENGTH");
|
||||
if (!content_length_str) {
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Event required in POST body or Authorization header\"}\n");
|
||||
return;
|
||||
}
|
||||
|
||||
long content_length = atol(content_length_str);
|
||||
if (content_length <= 0 || content_length > 65536) {
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Invalid content length\"}\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char* json_body = malloc(content_length + 1);
|
||||
if (!json_body) {
|
||||
printf("Status: 500 Internal Server Error\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Memory allocation failed\"}\n");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t bytes_read = fread(json_body, 1, content_length, stdin);
|
||||
if (bytes_read != (size_t)content_length) {
|
||||
free(json_body);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Failed to read complete request body\"}\n");
|
||||
return;
|
||||
}
|
||||
json_body[content_length] = '\0';
|
||||
|
||||
// Parse event JSON
|
||||
event = cJSON_Parse(json_body);
|
||||
|
||||
// Debug: Log the received JSON
|
||||
app_log(LOG_DEBUG, "ADMIN_EVENT: Received POST body: %s", json_body);
|
||||
|
||||
free(json_body);
|
||||
|
||||
if (!event) {
|
||||
app_log(LOG_ERROR, "ADMIN_EVENT: Failed to parse JSON");
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Invalid JSON\"}\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Debug: Log parsed event
|
||||
char* event_str = cJSON_Print(event);
|
||||
if (event_str) {
|
||||
app_log(LOG_DEBUG, "ADMIN_EVENT: Parsed event: %s", event_str);
|
||||
free(event_str);
|
||||
}
|
||||
}
|
||||
|
||||
// Process the event (handles validation, decryption, command execution, response)
|
||||
int result = process_admin_event(event);
|
||||
|
||||
// Clean up
|
||||
if (should_free_event && event) {
|
||||
cJSON_Delete(event);
|
||||
}
|
||||
|
||||
(void)result; // Result already handled by process_admin_event
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Kind 23458 event from Authorization header
|
||||
* Format: Authorization: Nostr <base64-encoded-event-json>
|
||||
* Returns: cJSON event object or NULL if not present/invalid
|
||||
*/
|
||||
static cJSON* parse_authorization_header(void) {
|
||||
const char* auth_header = getenv("HTTP_AUTHORIZATION");
|
||||
if (!auth_header) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check for "Nostr " prefix (case-insensitive)
|
||||
if (strncasecmp(auth_header, "Nostr ", 6) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Skip "Nostr " prefix
|
||||
const char* base64_event = auth_header + 6;
|
||||
|
||||
// Decode base64 (simple implementation - in production use proper base64 decoder)
|
||||
// For now, assume the event is JSON directly (not base64 encoded)
|
||||
// This matches the pattern from c-relay's admin interface
|
||||
cJSON* event = cJSON_Parse(base64_event);
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a Kind 23458 admin event (from POST body or Authorization header)
|
||||
* Returns: 0 on success, -1 on error (error response already sent)
|
||||
*/
|
||||
static int process_admin_event(cJSON* event) {
|
||||
if (!event) {
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Invalid JSON\"}\n");
|
||||
return;
|
||||
printf("{\"error\":\"Invalid event\"}\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Verify it's Kind 23456
|
||||
// Verify it's Kind 23458
|
||||
cJSON* kind_obj = cJSON_GetObjectItem(event, "kind");
|
||||
if (!kind_obj || !cJSON_IsNumber(kind_obj) ||
|
||||
(int)cJSON_GetNumberValue(kind_obj) != 23456) {
|
||||
cJSON_Delete(event);
|
||||
if (!kind_obj || !cJSON_IsNumber(kind_obj) ||
|
||||
(int)cJSON_GetNumberValue(kind_obj) != 23458) {
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Event must be Kind 23456\"}\n");
|
||||
return;
|
||||
printf("{\"error\":\"Event must be Kind 23458\"}\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get event ID for response correlation
|
||||
cJSON* id_obj = cJSON_GetObjectItem(event, "id");
|
||||
if (!id_obj || !cJSON_IsString(id_obj)) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Event missing id\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
const char* request_id = cJSON_GetStringValue(id_obj);
|
||||
|
||||
// Get admin pubkey from event
|
||||
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
|
||||
if (!pubkey_obj || !cJSON_IsString(pubkey_obj)) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Event missing pubkey\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
const char* admin_pubkey = cJSON_GetStringValue(pubkey_obj);
|
||||
|
||||
@@ -118,11 +189,10 @@ void handle_admin_event_request(void) {
|
||||
sqlite3* db;
|
||||
int rc = sqlite3_open_v2(g_db_path, &db, SQLITE_OPEN_READONLY, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 500 Internal Server Error\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Database error\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
@@ -141,42 +211,38 @@ void handle_admin_event_request(void) {
|
||||
sqlite3_close(db);
|
||||
|
||||
if (!is_admin) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 403 Forbidden\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Not authorized as admin\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get encrypted content
|
||||
cJSON* content_obj = cJSON_GetObjectItem(event, "content");
|
||||
if (!content_obj || !cJSON_IsString(content_obj)) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Event missing content\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
const char* encrypted_content = cJSON_GetStringValue(content_obj);
|
||||
|
||||
// Get server private key for decryption
|
||||
unsigned char server_privkey[32];
|
||||
if (get_server_privkey(server_privkey) != 0) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 500 Internal Server Error\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Failed to get server private key\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert admin pubkey to bytes
|
||||
unsigned char admin_pubkey_bytes[32];
|
||||
if (nostr_hex_to_bytes(admin_pubkey, admin_pubkey_bytes, 32) != 0) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Invalid admin pubkey format\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Decrypt content using NIP-44 (or use plaintext for testing)
|
||||
@@ -195,11 +261,12 @@ void handle_admin_event_request(void) {
|
||||
);
|
||||
|
||||
if (decrypt_result != 0) {
|
||||
cJSON_Delete(event);
|
||||
app_log(LOG_ERROR, "ADMIN_EVENT: Decryption failed with result: %d", decrypt_result);
|
||||
app_log(LOG_ERROR, "ADMIN_EVENT: Encrypted content: %s", encrypted_content);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Failed to decrypt content\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
content_to_parse = decrypted_content;
|
||||
}
|
||||
@@ -207,22 +274,20 @@ void handle_admin_event_request(void) {
|
||||
// Parse command array (either decrypted or plaintext)
|
||||
cJSON* command_array = cJSON_Parse(content_to_parse);
|
||||
if (!command_array || !cJSON_IsArray(command_array)) {
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Decrypted content is not a valid command array\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get command type
|
||||
cJSON* command_type = cJSON_GetArrayItem(command_array, 0);
|
||||
if (!command_type || !cJSON_IsString(command_type)) {
|
||||
cJSON_Delete(command_array);
|
||||
cJSON_Delete(event);
|
||||
printf("Status: 400 Bad Request\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Invalid command format\"}\n");
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* cmd = cJSON_GetStringValue(command_type);
|
||||
@@ -242,16 +307,17 @@ void handle_admin_event_request(void) {
|
||||
}
|
||||
|
||||
cJSON_Delete(command_array);
|
||||
cJSON_Delete(event);
|
||||
|
||||
if (result == 0) {
|
||||
// Send Kind 23457 response
|
||||
// Send Kind 23459 response
|
||||
send_admin_response_event(admin_pubkey, request_id, response_data);
|
||||
return 0;
|
||||
} else {
|
||||
cJSON_Delete(response_data);
|
||||
printf("Status: 500 Internal Server Error\r\n");
|
||||
printf("Content-Type: application/json\r\n\r\n");
|
||||
printf("{\"error\":\"Command processing failed\"}\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,7 +416,7 @@ static int handle_config_query_command(cJSON* response_data) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Send Kind 23457 admin response event
|
||||
* Send Kind 23459 admin response event
|
||||
*/
|
||||
static int send_admin_response_event(const char* admin_pubkey, const char* request_id,
|
||||
cJSON* response_data) {
|
||||
@@ -407,11 +473,11 @@ static int send_admin_response_event(const char* admin_pubkey, const char* reque
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Create Kind 23457 response event
|
||||
// Create Kind 23459 response event
|
||||
cJSON* response_event = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(response_event, "pubkey", server_pubkey);
|
||||
cJSON_AddNumberToObject(response_event, "created_at", (double)time(NULL));
|
||||
cJSON_AddNumberToObject(response_event, "kind", 23457);
|
||||
cJSON_AddNumberToObject(response_event, "kind", 23459);
|
||||
cJSON_AddStringToObject(response_event, "content", encrypted_response);
|
||||
|
||||
// Add tags
|
||||
@@ -433,7 +499,7 @@ static int send_admin_response_event(const char* admin_pubkey, const char* reque
|
||||
|
||||
// Sign the event
|
||||
cJSON* signed_event = nostr_create_and_sign_event(
|
||||
23457,
|
||||
23459,
|
||||
encrypted_response,
|
||||
tags,
|
||||
server_privkey,
|
||||
|
||||
Reference in New Issue
Block a user