senu 0.2.0

Declare a Rust function once and serve it as a web page, a JSON API and an MCP tool, with one permission check and one contract test suite
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
//! Authentication: the [`Authenticate`] trait, the built-in [`Basic`] and
//! [`Bearer`] authenticators, and `Authorization` header parsing.
//!
//! An authenticator turns a request's headers into a server-side
//! [`Principal`], or into nothing. It is the only place where a principal
//! is chosen, and it never sees the path or the query string.
//!
//! Secrets are compared with [`subtle::ConstantTimeEq`]. Senu makes no timing
//! guarantee beyond what that crate provides; in particular a length
//! mismatch is detected before the byte comparison.
//!
//! # Your own authenticator
//!
//! Sessions, API keys in a database or a header set by a trusted proxy are
//! each an implementation of the trait:
//!
//! ```
//! use axum::http::{HeaderMap, HeaderValue};
//! use senu::auth::{AuthFuture, Authenticate};
//! use senu::operation::{Permission, Principal, TenantId};
//!
//! const READ: Permission = Permission::new("read");
//!
//! struct ApiKeys; // would hold a database pool
//!
//! impl ApiKeys {
//!     async fn lookup(&self, key: &[u8]) -> Option<Principal> {
//!         // Look the key's hash up, then map the stored roles to constants.
//!         (key == b"k-123").then(|| Principal::owned("svc-reports", TenantId::new("t-7"), [READ]))
//!     }
//! }
//!
//! impl Authenticate for ApiKeys {
//!     fn authenticate<'a>(&'a self, headers: &'a HeaderMap) -> AuthFuture<'a> {
//!         Box::pin(async move {
//!             let key = senu::auth::bearer_token(headers)?;
//!             self.lookup(&key).await
//!         })
//!     }
//!
//!     fn challenge(&self) -> HeaderValue {
//!         HeaderValue::from_static("Bearer realm=\"reports\"")
//!     }
//! }
//! ```

use std::fmt;
use std::future::Future;
use std::pin::Pin;

use axum::http::{HeaderMap, HeaderValue, header};
use base64::Engine as _;
use subtle::ConstantTimeEq;

use crate::operation::Principal;

/// Longest `Authorization` header value that is parsed at all.
pub const MAX_AUTHORIZATION_BYTES: usize = 1024;

/// A configured credential. It never appears in `Debug` output and has no
/// `Display`, so it cannot leak through logs or error messages by accident.
#[derive(Clone, PartialEq, Eq)]
pub struct Secret(String);

/// Why a value cannot be a [`Secret`]. It never holds the value.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InvalidSecret;

impl fmt::Display for InvalidSecret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("a secret must be 1 to 256 visible ASCII characters (0x21-0x7E)")
    }
}

impl std::error::Error for InvalidSecret {}

/// Longest [`Secret`], in bytes.
pub const MAX_SECRET_BYTES: usize = 256;

impl Secret {
    /// Accepts 1 to 256 visible ASCII characters, the values that can
    /// travel in an `Authorization` header unchanged.
    pub fn new(value: impl Into<String>) -> Result<Self, InvalidSecret> {
        let value = value.into();
        let valid = (1..=MAX_SECRET_BYTES).contains(&value.len()) && value.bytes().all(is_vchar);
        if valid {
            Ok(Self(value))
        } else {
            Err(InvalidSecret)
        }
    }

    /// Compares `presented` with this secret in constant time for equal
    /// lengths.
    pub fn matches(&self, presented: &[u8]) -> bool {
        self.0.as_bytes().ct_eq(presented).into()
    }

    pub(crate) fn as_bytes(&self) -> &[u8] {
        self.0.as_bytes()
    }
}

impl fmt::Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Secret(<redacted>)")
    }
}

/// Credentials presented in an `Authorization` header.
#[derive(Clone, PartialEq, Eq)]
pub enum Presented {
    /// `Basic`, decoded and split at the first colon.
    Basic {
        /// The bytes before the first colon.
        user: Vec<u8>,
        /// The bytes after it, which may hold more colons.
        password: Vec<u8>,
    },
    /// `Bearer` with its token.
    Bearer(Vec<u8>),
}

impl fmt::Debug for Presented {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Basic { .. } => f.write_str("Basic(<redacted>)"),
            Self::Bearer(_) => f.write_str("Bearer(<redacted>)"),
        }
    }
}

/// Parses one `Authorization` header value.
///
/// The scheme matches case-insensitively. Anything malformed, oversized or
/// using another scheme yields `None`.
pub fn parse_authorization(value: &[u8]) -> Option<Presented> {
    if value.len() > MAX_AUTHORIZATION_BYTES {
        return None;
    }
    let space = value.iter().position(|&b| b == b' ')?;
    let (scheme, rest) = value.split_at(space);
    let credentials = trim_leading_spaces(rest);
    if credentials.is_empty() || !credentials.iter().all(|&b| is_vchar(b)) {
        return None;
    }
    if scheme.eq_ignore_ascii_case(b"basic") {
        let decoded = base64::engine::general_purpose::STANDARD
            .decode(credentials)
            .ok()?;
        let colon = decoded.iter().position(|&b| b == b':')?;
        Some(Presented::Basic {
            user: decoded[..colon].to_vec(),
            password: decoded[colon + 1..].to_vec(),
        })
    } else if scheme.eq_ignore_ascii_case(b"bearer") {
        Some(Presented::Bearer(credentials.to_vec()))
    } else {
        None
    }
}

/// True when `header` carries Basic credentials equal to `user` and
/// `password`.
pub fn basic_matches(header: Option<&[u8]>, user: &Secret, password: &Secret) -> bool {
    match header.and_then(parse_authorization) {
        Some(Presented::Basic {
            user: presented_user,
            password: presented_password,
        }) => {
            // Evaluate both comparisons so a wrong user name and a wrong
            // password take the same path.
            let user_ok = user.as_bytes().ct_eq(&presented_user);
            let password_ok = password.as_bytes().ct_eq(&presented_password);
            (user_ok & password_ok).into()
        }
        _ => false,
    }
}

/// True when `header` carries a Bearer token equal to `token`.
pub fn bearer_matches(header: Option<&[u8]>, token: &Secret) -> bool {
    match header.and_then(parse_authorization) {
        Some(Presented::Bearer(presented)) => token.matches(&presented),
        _ => false,
    }
}

/// The future of [`Authenticate::authenticate`].
pub type AuthFuture<'a> = Pin<Box<dyn Future<Output = Option<Principal>> + Send + 'a>>;

/// Decides who is calling, from the request's headers alone.
///
/// `None` is answered with 401 and [`challenge`](Self::challenge). The path,
/// the query string and the body are never passed in, so a credential in
/// the URL cannot be read. Implementations must not log credentials, and
/// should take the same time for a wrong credential as for a right one
/// where they can.
pub trait Authenticate: Send + Sync + 'static {
    /// The principal for these headers, or `None`.
    fn authenticate<'a>(&'a self, headers: &'a HeaderMap) -> AuthFuture<'a>;

    /// The `WWW-Authenticate` value sent with 401.
    fn challenge(&self) -> HeaderValue;
}

/// The `Authorization` value when exactly one is present. Two headers are
/// treated as none, so a proxy and the server cannot disagree about which
/// one counts.
pub fn single_authorization(headers: &HeaderMap) -> Option<&[u8]> {
    let mut values = headers.get_all(header::AUTHORIZATION).iter();
    match (values.next(), values.next()) {
        (Some(value), None) => Some(value.as_bytes()),
        _ => None,
    }
}

/// The Bearer token of the single `Authorization` header, for an
/// [`Authenticate`] implementation that looks tokens up itself.
pub fn bearer_token(headers: &HeaderMap) -> Option<Vec<u8>> {
    match single_authorization(headers).and_then(parse_authorization) {
        Some(Presented::Bearer(token)) => Some(token),
        _ => None,
    }
}

/// HTTP Basic with one fixed user, password and principal.
///
/// Basic sends the password with every request, so use it only over TLS,
/// and prefer your own session-based [`Authenticate`] for people.
#[derive(Debug)]
pub struct Basic {
    user: Secret,
    password: Secret,
    principal: Principal,
}

impl Basic {
    /// Binds `principal` to one user name and password. A user name that
    /// holds `:` can never match, because Basic splits at the first colon.
    pub fn new(user: Secret, password: Secret, principal: Principal) -> Self {
        Self {
            user,
            password,
            principal,
        }
    }
}

impl Authenticate for Basic {
    fn authenticate<'a>(&'a self, headers: &'a HeaderMap) -> AuthFuture<'a> {
        let matches = basic_matches(single_authorization(headers), &self.user, &self.password);
        Box::pin(std::future::ready(matches.then(|| self.principal.clone())))
    }

    fn challenge(&self) -> HeaderValue {
        HeaderValue::from_static("Basic realm=\"senu\", charset=\"UTF-8\"")
    }
}

/// Bearer tokens, each bound to a principal.
#[derive(Debug)]
pub struct Bearer {
    tokens: Vec<(Secret, Principal)>,
}

impl Bearer {
    /// Binds `principal` to one token.
    pub fn new(token: Secret, principal: Principal) -> Self {
        Self {
            tokens: vec![(token, principal)],
        }
    }

    /// Adds another token with its own principal. If two entries hold the
    /// same token, the first one wins.
    pub fn and(mut self, token: Secret, principal: Principal) -> Self {
        self.tokens.push((token, principal));
        self
    }
}

impl Authenticate for Bearer {
    fn authenticate<'a>(&'a self, headers: &'a HeaderMap) -> AuthFuture<'a> {
        let presented = bearer_token(headers);
        // Compare with every token, so the time does not depend on which
        // one matched.
        let mut found = None;
        if let Some(presented) = presented {
            for (token, principal) in &self.tokens {
                if token.matches(&presented) && found.is_none() {
                    found = Some(principal.clone());
                }
            }
        }
        Box::pin(std::future::ready(found))
    }

    fn challenge(&self) -> HeaderValue {
        HeaderValue::from_static("Bearer realm=\"senu\"")
    }
}

/// Visible ASCII, bytes 0x21 to 0x7E.
pub(crate) fn is_vchar(byte: u8) -> bool {
    (0x21..=0x7e).contains(&byte)
}

fn trim_leading_spaces(bytes: &[u8]) -> &[u8] {
    let start = bytes.iter().position(|&b| b != b' ').unwrap_or(bytes.len());
    &bytes[start..]
}

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

    fn secret(value: &str) -> Secret {
        Secret::new(value).expect("a valid secret")
    }

    fn basic_header(user: &str, password: &str) -> String {
        let raw = format!("{user}:{password}");
        format!(
            "Basic {}",
            base64::engine::general_purpose::STANDARD.encode(raw)
        )
    }

    #[test]
    fn parses_basic_credentials() {
        assert_eq!(
            parse_authorization(basic_header("demo", "pw").as_bytes()),
            Some(Presented::Basic {
                user: b"demo".to_vec(),
                password: b"pw".to_vec(),
            })
        );
    }

    #[test]
    fn basic_password_may_contain_colons() {
        assert_eq!(
            parse_authorization(basic_header("demo", "a:b:c").as_bytes()),
            Some(Presented::Basic {
                user: b"demo".to_vec(),
                password: b"a:b:c".to_vec(),
            })
        );
    }

    #[test]
    fn parses_bearer_tokens_with_any_visible_ascii() {
        assert_eq!(
            parse_authorization(b"Bearer abc!\"#$%~"),
            Some(Presented::Bearer(b"abc!\"#$%~".to_vec()))
        );
    }

    #[test]
    fn schemes_match_case_insensitively() {
        for scheme in ["basic", "BASIC", "bAsIc"] {
            let header = basic_header("u", "p").replacen("Basic", scheme, 1);
            assert!(
                matches!(
                    parse_authorization(header.as_bytes()),
                    Some(Presented::Basic { .. })
                ),
                "{scheme}"
            );
        }
        for scheme in ["bearer", "BEARER", "BeArEr"] {
            let header = format!("{scheme} token");
            assert_eq!(
                parse_authorization(header.as_bytes()),
                Some(Presented::Bearer(b"token".to_vec())),
                "{scheme}"
            );
        }
    }

    #[test]
    fn several_spaces_after_the_scheme_are_allowed() {
        assert_eq!(
            parse_authorization(b"Bearer   token"),
            Some(Presented::Bearer(b"token".to_vec()))
        );
    }

    #[test]
    fn malformed_values_are_rejected() {
        let cases: &[&[u8]] = &[
            b"",
            b"Bearer",
            b"Bearer ",
            b"Basic",
            b"Basic ",
            b"Bearer a b",
            b"Bearer a\tb",
            b"Bearer\ttoken",
            b" Bearer token",
            b"Bearer token ",
            b"Bearer tok\x7fen",
            b"Bearer t\xc3\xa9",
            b"Token abc",
            b"Digest abc",
            b"Bearertoken",
            b"Basic !!!notbase64",
            b"Basic ZGVtbw==",
        ];
        for value in cases {
            assert_eq!(
                parse_authorization(value),
                None,
                "{:?}",
                String::from_utf8_lossy(value)
            );
        }
    }

    #[test]
    fn oversized_values_are_rejected() {
        let token = "a".repeat(MAX_AUTHORIZATION_BYTES);
        let header = format!("Bearer {token}");
        assert!(header.len() > MAX_AUTHORIZATION_BYTES);
        assert_eq!(parse_authorization(header.as_bytes()), None);
    }

    #[test]
    fn secret_matches_only_the_exact_bytes() {
        let s = secret("correct-horse");
        assert!(s.matches(b"correct-horse"));
        assert!(!s.matches(b"correct-hors"));
        assert!(!s.matches(b"correct-horse!"));
        assert!(!s.matches(b"CORRECT-HORSE"));
        assert!(!s.matches(b""));
    }

    #[test]
    fn basic_matches_requires_both_user_and_password() {
        let (user, password) = (secret("demo"), secret("web-pass"));
        let ok = basic_header("demo", "web-pass");
        assert!(basic_matches(Some(ok.as_bytes()), &user, &password));
        let wrong_user = basic_header("demo2", "web-pass");
        assert!(!basic_matches(
            Some(wrong_user.as_bytes()),
            &user,
            &password
        ));
        let wrong_password = basic_header("demo", "web-pas");
        assert!(!basic_matches(
            Some(wrong_password.as_bytes()),
            &user,
            &password
        ));
        assert!(!basic_matches(None, &user, &password));
    }

    #[test]
    fn basic_matches_rejects_bearer_credentials() {
        let (user, password) = (secret("demo"), secret("web-pass"));
        assert!(!basic_matches(Some(b"Bearer web-pass"), &user, &password));
    }

    #[test]
    fn bearer_matches_only_the_configured_token() {
        let token = secret("api-token");
        assert!(bearer_matches(Some(b"Bearer api-token"), &token));
        assert!(bearer_matches(Some(b"bearer api-token"), &token));
        assert!(!bearer_matches(Some(b"Bearer api-token2"), &token));
        assert!(!bearer_matches(None, &token));
        let basic = basic_header("demo", "api-token");
        assert!(!bearer_matches(Some(basic.as_bytes()), &token));
    }

    #[test]
    fn debug_output_never_contains_credentials() {
        let s = secret("sentinel-secret");
        assert!(!format!("{s:?}").contains("sentinel"));
        let presented = parse_authorization(b"Bearer sentinel-secret");
        assert!(!format!("{presented:?}").contains("sentinel"));
        let presented = parse_authorization(basic_header("u", "sentinel-secret").as_bytes());
        assert!(!format!("{presented:?}").contains("sentinel"));
    }

    // Spec 0006, §3: the public constructor and the authenticators.

    fn headers(values: &[&str]) -> HeaderMap {
        let mut headers = HeaderMap::new();
        for value in values {
            headers.append(
                header::AUTHORIZATION,
                HeaderValue::from_str(value).expect("a header value"),
            );
        }
        headers
    }

    fn principal(name: &'static str) -> Principal {
        Principal::new(name, crate::operation::TenantId::from_static("t"), &[])
    }

    /// The built-in authenticators are ready at once, so one poll is enough.
    fn now(future: AuthFuture<'_>) -> Option<Principal> {
        let mut future = future;
        let mut context = std::task::Context::from_waker(std::task::Waker::noop());
        match future.as_mut().poll(&mut context) {
            std::task::Poll::Ready(principal) => principal,
            std::task::Poll::Pending => panic!("a built-in authenticator was not ready"),
        }
    }

    #[test]
    fn secrets_are_1_to_256_visible_ascii_characters() {
        assert!(Secret::new("a").is_ok());
        assert!(Secret::new("a".repeat(256)).is_ok());
        assert!(Secret::new("!~").is_ok());
        for bad in [
            String::new(),
            "a".repeat(257),
            "a b".to_owned(),
            "tab\there".to_owned(),
            "line\n".to_owned(),
            "caf\u{e9}".to_owned(),
            "del\u{7f}".to_owned(),
        ] {
            assert_eq!(Secret::new(bad.clone()).err(), Some(InvalidSecret));
        }
        let message = InvalidSecret.to_string();
        assert!(message.contains("1 to 256"), "{message}");
    }

    #[test]
    fn only_a_single_authorization_header_is_used() {
        assert_eq!(single_authorization(&headers(&[])), None);
        let one = headers(&["Bearer a"]);
        assert_eq!(single_authorization(&one), Some(&b"Bearer a"[..]));
        assert_eq!(
            single_authorization(&headers(&["Bearer a", "Bearer a"])),
            None
        );
        assert_eq!(bearer_token(&one), Some(b"a".to_vec()));
        assert_eq!(bearer_token(&headers(&["Bearer a", "Bearer a"])), None);
        assert_eq!(bearer_token(&headers(&[&basic_header("u", "p")])), None);
    }

    #[test]
    fn basic_yields_its_principal_only_for_the_exact_pair() {
        let auth = Basic::new(secret("demo"), secret("web-pass"), principal("web"));
        let good = basic_header("demo", "web-pass");
        assert_eq!(
            now(auth.authenticate(&headers(&[&good]))),
            Some(principal("web"))
        );
        for wrong in [
            basic_header("demo", "web-pasS"),
            basic_header("Demo", "web-pass"),
            basic_header("demo", ""),
            "Bearer web-pass".to_owned(),
        ] {
            assert_eq!(now(auth.authenticate(&headers(&[&wrong]))), None, "{wrong}");
        }
        assert_eq!(now(auth.authenticate(&headers(&[]))), None);
        assert_eq!(now(auth.authenticate(&headers(&[&good, &good]))), None);
        assert_eq!(auth.challenge(), "Basic realm=\"senu\", charset=\"UTF-8\"");
        let debug = format!("{auth:?}");
        assert!(
            !debug.contains("web-pass") && !debug.contains("demo"),
            "{debug}"
        );
    }

    #[test]
    fn bearer_maps_each_token_to_its_own_principal() {
        let auth = Bearer::new(secret("token-a"), principal("a"))
            .and(secret("token-b"), principal("b"))
            .and(secret("token-a"), principal("shadowed"));
        let call = |value: &str| now(auth.authenticate(&headers(&[value])));
        assert_eq!(call("Bearer token-a"), Some(principal("a")));
        assert_eq!(call("bearer token-b"), Some(principal("b")));
        for wrong in [
            "Bearer token-c",
            "Bearer token-",
            "Bearer token-aa",
            "Token token-a",
        ] {
            assert_eq!(call(wrong), None, "{wrong}");
        }
        assert_eq!(call(&basic_header("token-a", "token-a")), None);
        assert_eq!(now(auth.authenticate(&headers(&[]))), None);
        let twice = headers(&["Bearer token-a", "Bearer token-a"]);
        assert_eq!(now(auth.authenticate(&twice)), None);
        assert_eq!(auth.challenge(), "Bearer realm=\"senu\"");
        assert!(!format!("{auth:?}").contains("token-a"));
    }
}