bsv-auth-actix-middleware 0.1.21

BSV BRC-31 authentication middleware for Actix-web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Request and response payload serialization for BRC-31 authentication.
//!
//! Provides a two-layer API:
//! - **Pure layer:** Generic functions accepting `&str`, `&[u8]`, `&[(String, String)]`
//!   for testability without Actix dependencies.
//! - **Actix wrapper layer:** Thin wrappers that extract data from `HttpRequest`
//!   and `HttpResponse` and delegate to the pure functions.
//!
//! Payload byte layout must be identical to the TypeScript implementation
//! (`auth-express-middleware`) for cross-language signature verification.

use actix_web::http::header::HeaderMap;
use actix_web::http::StatusCode;
use actix_web::HttpRequest;

// ---------------------------------------------------------------------------
// Varint encoding (matching bsv-rust-sdk auth_fetch.rs exactly)
// ---------------------------------------------------------------------------

/// Write a signed Bitcoin-style varint.
///
/// Negative values are encoded as their two's complement unsigned 64-bit
/// representation, matching the TS SDK behavior where `writeVarIntNum(-1)`
/// computes `-1 + 2^64 = 0xFFFFFFFFFFFFFFFF` and encodes it as a 9-byte
/// varint (`0xFF` prefix + 8 bytes LE).
pub(crate) fn write_varint_num(buf: &mut Vec<u8>, val: i64) {
    if val < 0 {
        // Treat as unsigned: reinterpret the signed i64 as u64 (two's complement).
        // For -1 this gives 0xFFFFFFFFFFFFFFFF, matching the TS SDK.
        let uval = val as u64;
        buf.push(0xff);
        buf.extend_from_slice(&uval.to_le_bytes());
        return;
    }
    let val = val as u64;
    if val < 0xfd {
        buf.push(val as u8);
    } else if val <= 0xffff {
        buf.push(0xfd);
        buf.extend_from_slice(&(val as u16).to_le_bytes());
    } else if val <= 0xffff_ffff {
        buf.push(0xfe);
        buf.extend_from_slice(&(val as u32).to_le_bytes());
    } else {
        buf.push(0xff);
        buf.extend_from_slice(&val.to_le_bytes());
    }
}

// ---------------------------------------------------------------------------
// Header filtering (pure layer)
// ---------------------------------------------------------------------------

/// Filter and sort request headers according to BRC-31 signing rules.
///
/// Includes:
/// - Headers starting with `x-bsv-` (excluding `x-bsv-auth*`)
/// - `content-type` (normalized: split on `;`, take first part, trimmed)
/// - `authorization`
///
/// Output is sorted alphabetically by key.
pub fn filter_and_sort_request_headers(headers: &[(String, String)]) -> Vec<(String, String)> {
    let mut included: Vec<(String, String)> = headers
        .iter()
        .filter_map(|(k, v)| {
            let key = k.to_lowercase();
            if key.starts_with("x-bsv-auth") {
                return None;
            }
            if key.starts_with("x-bsv-") || key == "content-type" || key == "authorization" {
                let value = if key == "content-type" {
                    v.split(';').next().unwrap_or("").trim().to_string()
                } else {
                    v.clone()
                };
                Some((key, value))
            } else {
                None
            }
        })
        .collect();
    included.sort_by(|(a, _), (b, _)| a.cmp(b));
    included
}

/// Filter and sort response headers according to BRC-31 signing rules.
///
/// Includes:
/// - Headers starting with `x-bsv-` (excluding `x-bsv-auth*`)
/// - `authorization`
///
/// Note: `content-type` is NOT included for responses (unlike requests).
/// Output is sorted alphabetically by key.
pub fn filter_and_sort_response_headers(headers: &[(String, String)]) -> Vec<(String, String)> {
    let mut included: Vec<(String, String)> = headers
        .iter()
        .filter_map(|(k, v)| {
            let key = k.to_lowercase();
            if key.starts_with("x-bsv-auth") {
                return None;
            }
            if key.starts_with("x-bsv-") || key == "authorization" {
                Some((key, v.clone()))
            } else {
                None
            }
        })
        .collect();
    included.sort_by(|(a, _), (b, _)| a.cmp(b));
    included
}

// ---------------------------------------------------------------------------
// Payload serialization (pure layer)
// ---------------------------------------------------------------------------

/// Serialize a request payload for BRC-31 signature verification.
///
/// Layout (matching TS `buildAuthMessageFromRequest`):
/// - `request_nonce` bytes (raw, no varint prefix)
/// - varint(method.len()) + method bytes
/// - varint(path.len()) + path bytes  (varint(-1) if empty)
/// - varint(query.len()) + query bytes (varint(-1) if empty)
/// - varint(headers.len()) then for each: varint(key.len()) + key + varint(val.len()) + val
/// - varint(body.len()) + body bytes   (varint(-1) if body is None)
pub fn serialize_request_payload(
    request_nonce: &[u8],
    method: &str,
    path: &str,
    query: &str,
    headers: &[(String, String)],
    body: Option<&[u8]>,
) -> Vec<u8> {
    let mut buf = Vec::new();

    // Request nonce (raw bytes)
    buf.extend_from_slice(request_nonce);

    // Method
    let method_bytes = method.as_bytes();
    write_varint_num(&mut buf, method_bytes.len() as i64);
    buf.extend_from_slice(method_bytes);

    // Path
    if !path.is_empty() {
        let path_bytes = path.as_bytes();
        write_varint_num(&mut buf, path_bytes.len() as i64);
        buf.extend_from_slice(path_bytes);
    } else {
        write_varint_num(&mut buf, -1);
    }

    // Query
    if !query.is_empty() {
        let query_bytes = query.as_bytes();
        write_varint_num(&mut buf, query_bytes.len() as i64);
        buf.extend_from_slice(query_bytes);
    } else {
        write_varint_num(&mut buf, -1);
    }

    // Headers
    write_varint_num(&mut buf, headers.len() as i64);
    for (key, value) in headers {
        let key_bytes = key.as_bytes();
        write_varint_num(&mut buf, key_bytes.len() as i64);
        buf.extend_from_slice(key_bytes);

        let value_bytes = value.as_bytes();
        write_varint_num(&mut buf, value_bytes.len() as i64);
        buf.extend_from_slice(value_bytes);
    }

    // Body
    match body {
        Some(b) if !b.is_empty() => {
            write_varint_num(&mut buf, b.len() as i64);
            buf.extend_from_slice(b);
        }
        _ => {
            write_varint_num(&mut buf, -1);
        }
    }

    buf
}

/// Serialize a response payload for BRC-31 signature verification.
///
/// Layout (matching TS `buildResponsePayload`):
/// - `request_nonce` bytes (raw, no varint prefix)
/// - varint(status_code)
/// - varint(headers.len()) then for each: varint(key.len()) + key + varint(val.len()) + val
/// - varint(body.len()) + body bytes (varint(-1) if body is None or empty)
pub fn serialize_response_payload(
    request_nonce: &[u8],
    status_code: u16,
    headers: &[(String, String)],
    body: Option<&[u8]>,
) -> Vec<u8> {
    let mut buf = Vec::new();

    // Request nonce (raw bytes)
    buf.extend_from_slice(request_nonce);

    // Status code
    write_varint_num(&mut buf, status_code as i64);

    // Headers
    write_varint_num(&mut buf, headers.len() as i64);
    for (key, value) in headers {
        let key_bytes = key.as_bytes();
        write_varint_num(&mut buf, key_bytes.len() as i64);
        buf.extend_from_slice(key_bytes);

        let value_bytes = value.as_bytes();
        write_varint_num(&mut buf, value_bytes.len() as i64);
        buf.extend_from_slice(value_bytes);
    }

    // Body
    match body {
        Some(b) if !b.is_empty() => {
            write_varint_num(&mut buf, b.len() as i64);
            buf.extend_from_slice(b);
        }
        _ => {
            write_varint_num(&mut buf, -1);
        }
    }

    buf
}

// ---------------------------------------------------------------------------
// Actix wrapper layer
// ---------------------------------------------------------------------------

/// Extract headers from an Actix `HttpRequest` as `(String, String)` pairs.
fn headers_from_request(req: &HttpRequest) -> Vec<(String, String)> {
    req.headers()
        .iter()
        .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
        .collect()
}

/// Extract headers from an Actix `HeaderMap` as `(String, String)` pairs.
fn headers_from_map(headers: &HeaderMap) -> Vec<(String, String)> {
    headers
        .iter()
        .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
        .collect()
}

/// Serialize a request payload from an Actix `HttpRequest` and body bytes.
///
/// Extracts method, path, query string, and headers from the request,
/// filters/sorts headers per BRC-31 rules, then delegates to
/// `serialize_request_payload`.
pub fn serialize_from_http_request(
    request_nonce: &[u8],
    req: &HttpRequest,
    body: &[u8],
) -> Vec<u8> {
    let raw_headers = headers_from_request(req);
    let filtered_headers = filter_and_sort_request_headers(&raw_headers);
    // Actix's query_string() returns the query WITHOUT the leading '?',
    // but the BRC-31 protocol (and TS SDK) includes the '?' prefix in the
    // serialized payload. Prepend '?' when the query is non-empty.
    let raw_query = req.query_string();
    let query = if raw_query.is_empty() {
        String::new()
    } else {
        format!("?{}", raw_query)
    };

    serialize_request_payload(
        request_nonce,
        req.method().as_str(),
        req.path(),
        &query,
        &filtered_headers,
        if body.is_empty() { None } else { Some(body) },
    )
}

/// Serialize a response payload from status code, headers, and body bytes.
///
/// Filters/sorts headers per BRC-31 response rules, then delegates to
/// `serialize_response_payload`.
pub fn serialize_from_http_response(
    request_nonce: &[u8],
    status: StatusCode,
    headers: &HeaderMap,
    body: &[u8],
) -> Vec<u8> {
    let raw_headers = headers_from_map(headers);
    let filtered_headers = filter_and_sort_response_headers(&raw_headers);
    serialize_response_payload(
        request_nonce,
        status.as_u16(),
        &filtered_headers,
        if body.is_empty() { None } else { Some(body) },
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // Varint encoding tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_varint_negative_writes_twos_complement_u64() {
        let mut buf = Vec::new();
        write_varint_num(&mut buf, -1);
        // -1 as u64 = 0xFFFFFFFFFFFFFFFF, encoded as 0xFF prefix + 8 LE bytes
        assert_eq!(
            buf,
            vec![0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
        );
    }

    #[test]
    fn test_varint_zero_writes_zero() {
        let mut buf = Vec::new();
        write_varint_num(&mut buf, 0);
        assert_eq!(buf, vec![0x00]);
    }

    #[test]
    fn test_varint_252_writes_single_byte() {
        let mut buf = Vec::new();
        write_varint_num(&mut buf, 252);
        assert_eq!(buf, vec![0xFC]);
    }

    #[test]
    fn test_varint_253_writes_fd_prefix() {
        let mut buf = Vec::new();
        write_varint_num(&mut buf, 253);
        // 0xFD followed by 253 as u16 LE = [0xFD, 0x00]
        assert_eq!(buf, vec![0xFD, 0xFD, 0x00]);
    }

    #[test]
    fn test_varint_65535_writes_fd_prefix() {
        let mut buf = Vec::new();
        write_varint_num(&mut buf, 0xFFFF);
        assert_eq!(buf, vec![0xFD, 0xFF, 0xFF]);
    }

    #[test]
    fn test_varint_65536_writes_fe_prefix() {
        let mut buf = Vec::new();
        write_varint_num(&mut buf, 0x10000);
        assert_eq!(buf, vec![0xFE, 0x00, 0x00, 0x01, 0x00]);
    }

    // -----------------------------------------------------------------------
    // Request header filtering tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_request_headers_include_x_bsv_topic() {
        let headers = vec![
            ("X-Bsv-Topic".to_string(), "hello".to_string()),
            ("Other-Header".to_string(), "ignored".to_string()),
        ];
        let result = filter_and_sort_request_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0], ("x-bsv-topic".to_string(), "hello".to_string()));
    }

    #[test]
    fn test_request_headers_exclude_x_bsv_auth() {
        let headers = vec![
            ("x-bsv-auth-version".to_string(), "0.1".to_string()),
            ("x-bsv-auth-nonce".to_string(), "abc".to_string()),
            ("x-bsv-topic".to_string(), "hello".to_string()),
        ];
        let result = filter_and_sort_request_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "x-bsv-topic");
    }

    #[test]
    fn test_request_headers_include_content_type_normalized() {
        let headers = vec![(
            "Content-Type".to_string(),
            "application/json; charset=utf-8".to_string(),
        )];
        let result = filter_and_sort_request_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "content-type");
        assert_eq!(result[0].1, "application/json");
    }

    #[test]
    fn test_request_headers_include_authorization() {
        let headers = vec![("Authorization".to_string(), "Bearer abc".to_string())];
        let result = filter_and_sort_request_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "authorization");
    }

    #[test]
    fn test_request_headers_sorted_alphabetically() {
        let headers = vec![
            ("x-bsv-topic".to_string(), "t".to_string()),
            ("authorization".to_string(), "a".to_string()),
            ("content-type".to_string(), "application/json".to_string()),
        ];
        let result = filter_and_sort_request_headers(&headers);
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].0, "authorization");
        assert_eq!(result[1].0, "content-type");
        assert_eq!(result[2].0, "x-bsv-topic");
    }

    // -----------------------------------------------------------------------
    // Response header filtering tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_response_headers_include_x_bsv_topic() {
        let headers = vec![("x-bsv-topic".to_string(), "hello".to_string())];
        let result = filter_and_sort_response_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "x-bsv-topic");
    }

    #[test]
    fn test_response_headers_exclude_x_bsv_auth() {
        let headers = vec![
            ("x-bsv-auth-version".to_string(), "0.1".to_string()),
            ("x-bsv-topic".to_string(), "hello".to_string()),
        ];
        let result = filter_and_sort_response_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "x-bsv-topic");
    }

    #[test]
    fn test_response_headers_include_authorization() {
        let headers = vec![("authorization".to_string(), "Bearer abc".to_string())];
        let result = filter_and_sort_response_headers(&headers);
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_response_headers_exclude_content_type() {
        let headers = vec![
            ("content-type".to_string(), "application/json".to_string()),
            ("x-bsv-topic".to_string(), "hello".to_string()),
        ];
        let result = filter_and_sort_response_headers(&headers);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "x-bsv-topic");
    }

    #[test]
    fn test_response_headers_sorted_alphabetically() {
        let headers = vec![
            ("x-bsv-topic".to_string(), "t".to_string()),
            ("authorization".to_string(), "a".to_string()),
            ("x-bsv-data".to_string(), "d".to_string()),
        ];
        let result = filter_and_sort_response_headers(&headers);
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].0, "authorization");
        assert_eq!(result[1].0, "x-bsv-data");
        assert_eq!(result[2].0, "x-bsv-topic");
    }

    // -----------------------------------------------------------------------
    // Fixture-based payload serialization tests
    // -----------------------------------------------------------------------

    /// Helper: 9-byte varint(-1) sentinel matching TS SDK behavior.
    fn varint_neg1() -> Vec<u8> {
        vec![0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
    }

    #[test]
    fn test_serialize_request_payload_fixture() {
        // Fixture: GET /test, no query, no body, one x-bsv-topic header
        let nonce = [0x01, 0x02, 0x03, 0x04]; // 4-byte nonce for test simplicity
        let method = "GET";
        let path = "/test";
        let query = "";
        let headers = vec![("x-bsv-topic".to_string(), "hello".to_string())];

        let result = serialize_request_payload(&nonce, method, path, query, &headers, None);

        // Expected bytes:
        // [0x01, 0x02, 0x03, 0x04]  -- nonce (4 bytes)
        // [0x03]                     -- varint(3) for "GET"
        // [0x47, 0x45, 0x54]        -- "GET"
        // [0x05]                     -- varint(5) for "/test"
        // [0x2F, 0x74, 0x65, 0x73, 0x74] -- "/test"
        // [0xFF, 0xFF x8]            -- varint(-1) for empty query (9 bytes)
        // [0x01]                     -- varint(1) for 1 header
        // [0x0B]                     -- varint(11) for "x-bsv-topic"
        // b"x-bsv-topic"            -- 11 bytes
        // [0x05]                     -- varint(5) for "hello"
        // b"hello"                   -- 5 bytes
        // [0xFF, 0xFF x8]            -- varint(-1) for no body (9 bytes)

        let mut expected = Vec::new();
        expected.extend_from_slice(&[0x01, 0x02, 0x03, 0x04]); // nonce
        expected.push(0x03); // varint(3)
        expected.extend_from_slice(b"GET");
        expected.push(0x05); // varint(5)
        expected.extend_from_slice(b"/test");
        expected.extend_from_slice(&varint_neg1()); // varint(-1) for empty query
        expected.push(0x01); // varint(1) header count
        expected.push(0x0B); // varint(11) for "x-bsv-topic"
        expected.extend_from_slice(b"x-bsv-topic");
        expected.push(0x05); // varint(5) for "hello"
        expected.extend_from_slice(b"hello");
        expected.extend_from_slice(&varint_neg1()); // varint(-1) for no body

        assert_eq!(
            result, expected,
            "Request payload bytes mismatch.\nGot:      {:02X?}\nExpected: {:02X?}",
            result, expected
        );
    }

    #[test]
    fn test_serialize_response_payload_fixture() {
        // Fixture: 200 OK, one x-bsv-topic header, body "ok"
        let nonce = [0x01, 0x02, 0x03, 0x04];
        let headers = vec![("x-bsv-topic".to_string(), "hello".to_string())];
        let body = b"ok";

        let result = serialize_response_payload(&nonce, 200, &headers, Some(body));

        // Expected bytes:
        // [0x01, 0x02, 0x03, 0x04]  -- nonce
        // [0xC8]                     -- varint(200)
        // [0x01]                     -- 1 header
        // [0x0B] b"x-bsv-topic"     -- header key
        // [0x05] b"hello"            -- header value
        // [0x02] b"ok"              -- body

        let mut expected = Vec::new();
        expected.extend_from_slice(&[0x01, 0x02, 0x03, 0x04]);
        expected.push(0xC8); // varint(200)
        expected.push(0x01); // 1 header
        expected.push(0x0B);
        expected.extend_from_slice(b"x-bsv-topic");
        expected.push(0x05);
        expected.extend_from_slice(b"hello");
        expected.push(0x02); // body length
        expected.extend_from_slice(b"ok");

        assert_eq!(
            result, expected,
            "Response payload bytes mismatch.\nGot:      {:02X?}\nExpected: {:02X?}",
            result, expected
        );
    }

    #[test]
    fn test_serialize_request_payload_with_query_and_body() {
        let nonce = [0xAA];
        let method = "POST";
        let path = "/api";
        let query = "?key=val";
        let headers: Vec<(String, String)> = vec![];
        let body = b"data";

        let result = serialize_request_payload(&nonce, method, path, query, &headers, Some(body));

        let mut expected = Vec::new();
        expected.push(0xAA); // nonce
        expected.push(0x04); // varint(4) for "POST"
        expected.extend_from_slice(b"POST");
        expected.push(0x04); // varint(4) for "/api"
        expected.extend_from_slice(b"/api");
        expected.push(0x08); // varint(8) for "?key=val"
        expected.extend_from_slice(b"?key=val");
        expected.push(0x00); // varint(0) for 0 headers
        expected.push(0x04); // varint(4) for body
        expected.extend_from_slice(b"data");

        assert_eq!(
            result, expected,
            "Request with query+body mismatch.\nGot:      {:02X?}\nExpected: {:02X?}",
            result, expected
        );
    }

    #[test]
    fn test_serialize_response_payload_no_body() {
        let nonce = [0xBB];
        let headers: Vec<(String, String)> = vec![];

        let result = serialize_response_payload(&nonce, 404, &headers, None);

        let mut expected = Vec::new();
        expected.push(0xBB);
        expected.push(0xFD); // varint(404) = 0xFD + 0x94, 0x01
        expected.extend_from_slice(&(404u16).to_le_bytes());
        expected.push(0x00); // 0 headers
        expected.extend_from_slice(&varint_neg1()); // varint(-1) for no body

        assert_eq!(
            result, expected,
            "Response no-body mismatch.\nGot:      {:02X?}\nExpected: {:02X?}",
            result, expected
        );
    }
}