Skip to main content

verify_request

Function verify_request 

Source
pub fn verify_request(
    authorization: &str,
    expected: &ExpectedRequest<'_>,
    now: UnixTime,
    policy: &RequestPolicy,
) -> Result<VerifiedRequest, VerifyRequestError>
Expand description

Verifies an Authorization header against the signed-request profile.

authorization is the raw header value. expected is the request the server actually received, with the URL it trusts. now is the server’s current time.

The header value is Nostr <base64>, where the base64 decodes to a kind-27235 Nostr event. The profile is stricter than NIP-98 requires:

  • the scheme is Nostr (matched case-insensitively, as HTTP schemes are), followed by one or more spaces and standard padded base64;
  • the header is at most RequestPolicy::max_token_bytes long;
  • the event parses and its ID and BIP-340 signature verify (SignedEvent::from_json);
  • the kind is HTTP_AUTH_KIND;
  • there is exactly one u tag and exactly one method tag, at most one payload tag, and each of those has exactly one value;
  • u equals the expected URL byte for byte;
  • method equals the expected method byte for byte (HTTP methods are case-sensitive, so a header signed for POST does not verify as post);
  • when the request has a body (even an empty one) the payload tag is mandatory and equals the lowercase hex SHA-256 of the body; when it has none, a payload tag is rejected;
  • created_at is within RequestPolicy::max_clock_skew of now, on either side, inclusive; that window is never more than ±60 seconds.

Other non-empty tags are ignored; an empty tag fails event parsing. The verifier does not provide single use: the same header verifies again until it leaves the time window. Durable replay protection belongs to the caller, keyed by VerifiedRequest::event_id.

§Examples

use stratum_node_core::{
    verify_request, ExpectedRequest, NostrSecretKey, RequestPolicy, UnixTime, VerifyRequestError,
};

let key = NostrSecretKey::from_hex(&"05".repeat(32))?;
let request = ExpectedRequest::new("GET", "http://127.0.0.1:7070/api/v1/config", None);
let signed_at = UnixTime::from_secs(1_700_000_000);
let header = key.sign_request(&request, signed_at, &[0u8; 32])?;

let policy = RequestPolicy::new();
let ok = verify_request(&header, &request, UnixTime::from_secs(1_700_000_060), &policy)?;
assert_eq!(ok.signer(), key.public());

let late = verify_request(&header, &request, UnixTime::from_secs(1_700_000_061), &policy);
assert!(matches!(late, Err(VerifyRequestError::CreatedTooLongAgo { .. })));

§Errors

Returns the VerifyRequestError variant naming the first failed check.