tests
This commit is contained in:
139
src/main.c
139
src/main.c
@@ -1528,7 +1528,26 @@ void handle_head_upload_request(void) {
|
||||
int auth_result = nostr_validate_request(&request, &result);
|
||||
|
||||
if (auth_result != NOSTR_SUCCESS || !result.valid) {
|
||||
send_upload_error_response(401, "authentication_failed", "Invalid or expired authentication", XREASON_AUTH_INVALID);
|
||||
const char* error_type = "authentication_failed";
|
||||
const char* message = "Invalid or expired authentication";
|
||||
const char* details = result.reason[0] ? result.reason : "Authentication validation failed";
|
||||
|
||||
// Provide more specific error messages based on the reason
|
||||
if (strstr(result.reason, "whitelist")) {
|
||||
error_type = "pubkey_not_whitelisted";
|
||||
message = "Public key not authorized";
|
||||
details = result.reason;
|
||||
} else if (strstr(result.reason, "blacklist")) {
|
||||
error_type = "access_denied";
|
||||
message = "Access denied by policy";
|
||||
details = result.reason;
|
||||
} else if (strstr(result.reason, "size")) {
|
||||
error_type = "file_too_large";
|
||||
message = "File size exceeds policy limits";
|
||||
details = result.reason;
|
||||
}
|
||||
|
||||
send_upload_error_response(401, error_type, message, details);
|
||||
log_request("HEAD", "/upload", "auth_failed", 401);
|
||||
return;
|
||||
}
|
||||
@@ -1915,8 +1934,20 @@ void handle_list_request(const char* pubkey) {
|
||||
int auth_result = nostr_validate_request(&request, &result);
|
||||
|
||||
if (auth_result != NOSTR_SUCCESS || !result.valid) {
|
||||
send_error_response(401, "authentication_failed", "Invalid or expired authentication",
|
||||
"The provided Nostr event is invalid, expired, or does not authorize this operation");
|
||||
const char* error_type = "authentication_failed";
|
||||
const char* message = "Invalid or expired authentication";
|
||||
const char* details = result.reason[0] ? result.reason : "The provided Nostr event is invalid, expired, or does not authorize this operation";
|
||||
|
||||
// Provide more specific error messages based on the reason
|
||||
if (strstr(result.reason, "whitelist")) {
|
||||
error_type = "pubkey_not_whitelisted";
|
||||
message = "Public key not authorized";
|
||||
} else if (strstr(result.reason, "blacklist")) {
|
||||
error_type = "access_denied";
|
||||
message = "Access denied by policy";
|
||||
}
|
||||
|
||||
send_error_response(401, error_type, message, details);
|
||||
log_request("GET", "/list", "failed", 401);
|
||||
return;
|
||||
}
|
||||
@@ -2382,8 +2413,20 @@ void handle_delete_request(const char* sha256) {
|
||||
int auth_result = nostr_validate_request(&request, &result);
|
||||
|
||||
if (auth_result != NOSTR_SUCCESS || !result.valid) {
|
||||
send_error_response(401, "authentication_failed", "Invalid or expired authentication",
|
||||
"The provided Nostr event is invalid, expired, or does not authorize this operation");
|
||||
const char* error_type = "authentication_failed";
|
||||
const char* message = "Invalid or expired authentication";
|
||||
const char* details = result.reason[0] ? result.reason : "The provided Nostr event is invalid, expired, or does not authorize this operation";
|
||||
|
||||
// Provide more specific error messages based on the reason
|
||||
if (strstr(result.reason, "whitelist")) {
|
||||
error_type = "pubkey_not_whitelisted";
|
||||
message = "Public key not authorized";
|
||||
} else if (strstr(result.reason, "blacklist")) {
|
||||
error_type = "access_denied";
|
||||
message = "Access denied by policy";
|
||||
}
|
||||
|
||||
send_error_response(401, error_type, message, details);
|
||||
log_request("DELETE", "/delete", "failed", 401);
|
||||
return;
|
||||
}
|
||||
@@ -2669,57 +2712,43 @@ void handle_upload_request(void) {
|
||||
auth_result, result.valid, result.reason);
|
||||
|
||||
if (auth_result == NOSTR_SUCCESS && !result.valid) {
|
||||
auth_result = result.error_code;
|
||||
if (auth_result != NOSTR_SUCCESS) {
|
||||
free(file_data);
|
||||
|
||||
// Provide specific error messages based on the authentication failure type
|
||||
const char* error_type = "authentication_failed";
|
||||
const char* message = "Authentication failed";
|
||||
const char* details = "The request failed nostr authentication";
|
||||
|
||||
switch (auth_result) {
|
||||
case NOSTR_ERROR_EVENT_INVALID_CONTENT:
|
||||
error_type = "event_expired";
|
||||
message = "Authentication event expired";
|
||||
details = "The provided nostr event has expired and is no longer valid";
|
||||
break;
|
||||
case NOSTR_ERROR_EVENT_INVALID_SIGNATURE:
|
||||
error_type = "invalid_signature";
|
||||
message = "Invalid cryptographic signature";
|
||||
details = "The event signature verification failed";
|
||||
break;
|
||||
case NOSTR_ERROR_EVENT_INVALID_PUBKEY:
|
||||
error_type = "invalid_pubkey";
|
||||
message = "Invalid public key";
|
||||
details = "The event contains an invalid or malformed public key";
|
||||
break;
|
||||
case NOSTR_ERROR_EVENT_INVALID_ID:
|
||||
error_type = "invalid_event_id";
|
||||
message = "Invalid event ID";
|
||||
details = "The event ID does not match the calculated hash";
|
||||
break;
|
||||
case NOSTR_ERROR_INVALID_INPUT:
|
||||
error_type = "invalid_format";
|
||||
message = "Invalid authorization format";
|
||||
details = "The authorization header format is invalid or malformed";
|
||||
break;
|
||||
default:
|
||||
error_type = "authentication_failed";
|
||||
message = "Authentication failed";
|
||||
// Use C-style string formatting for error details
|
||||
static char error_details_buffer[256];
|
||||
snprintf(error_details_buffer, sizeof(error_details_buffer),
|
||||
"The request failed nostr authentication (error code: %d - %s)",
|
||||
auth_result, nostr_strerror(auth_result));
|
||||
details = error_details_buffer;
|
||||
break;
|
||||
}
|
||||
|
||||
send_error_response(401, error_type, message, details);
|
||||
log_request("PUT", "/upload", "auth_failed", 401);
|
||||
return;
|
||||
free(file_data);
|
||||
|
||||
// Use the detailed reason from the authentication system
|
||||
const char* error_type = "authentication_failed";
|
||||
const char* message = "Authentication failed";
|
||||
const char* details = result.reason[0] ? result.reason : "The request failed authentication";
|
||||
|
||||
// Provide more specific error types based on the reason content
|
||||
if (strstr(result.reason, "whitelist")) {
|
||||
error_type = "pubkey_not_whitelisted";
|
||||
message = "Public key not authorized";
|
||||
} else if (strstr(result.reason, "blacklist")) {
|
||||
error_type = "access_denied";
|
||||
message = "Access denied by policy";
|
||||
} else if (strstr(result.reason, "expired")) {
|
||||
error_type = "event_expired";
|
||||
message = "Authentication event expired";
|
||||
} else if (strstr(result.reason, "signature")) {
|
||||
error_type = "invalid_signature";
|
||||
message = "Invalid cryptographic signature";
|
||||
} else if (strstr(result.reason, "size")) {
|
||||
error_type = "file_too_large";
|
||||
message = "File size exceeds policy limits";
|
||||
} else if (strstr(result.reason, "MIME") || strstr(result.reason, "mime")) {
|
||||
error_type = "unsupported_type";
|
||||
message = "File type not allowed by policy";
|
||||
} else if (strstr(result.reason, "hash")) {
|
||||
error_type = "hash_blocked";
|
||||
message = "File hash blocked by policy";
|
||||
} else if (strstr(result.reason, "format") || strstr(result.reason, "invalid")) {
|
||||
error_type = "invalid_format";
|
||||
message = "Invalid authorization format";
|
||||
}
|
||||
|
||||
send_error_response(401, error_type, message, details);
|
||||
log_request("PUT", "/upload", "auth_failed", 401);
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract uploader pubkey from validation result if auth was provided
|
||||
|
||||
Reference in New Issue
Block a user