rustango 0.60.0

Batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
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
//! A chain of authentication backends, tried in order.
//!
//! ## The chain
//!
//! [`AuthBackendChain`] walks an ordered list of [`AuthBackend`]s and
//! returns the first `Some` result. `None` means "this backend knows
//! nothing about these credentials", so the chain moves on. An `Err`
//! stops the walk and goes back to the caller, so a DB outage in one
//! backend is never hidden by a fallthrough to the next.
//!
//! ```ignore
//! use std::sync::Arc;
//! use rustango::auth_backends::{
//!     AuthBackendChain, Credentials, RemoteUserBackend,
//! };
//!
//! let chain = AuthBackendChain::new()
//!     .with(Arc::new(my_password_backend))
//!     .with(Arc::new(RemoteUserBackend::trust_username));
//!
//! let creds = Credentials::password("alice", "s3cret");
//! let principal = chain.authenticate(&creds).await?;
//! ```
//!
//! ## Scope
//!
//! This module is only the registry. The concrete backends, such as
//! the tenant `User` table, the operator console and OAuth, keep
//! their own APIs; you wrap one in an `AuthBackend` impl and
//! register it.
//!
//! [`Principal`] is the small shared shape the chain returns,
//! because each backend has its own native record. It carries an
//! id, a username, two flags and an attribute bag. Map it to your
//! own user type when you need the full record.
//!
//! [`Principal`]: crate::auth_backends::Principal
//! [`AuthBackend`]: crate::auth_backends::AuthBackend
//! [`AuthBackendChain`]: crate::auth_backends::AuthBackendChain

use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

// ------------------------------------------------------------------ Credentials

/// The inputs handed to every backend in the chain. Most backends
/// read one field: a password backend reads `username` and
/// `password`, [`RemoteUserBackend`] reads `remote_user`, an OAuth
/// callback puts its token in `extras`.
#[derive(Debug, Default, Clone)]
pub struct Credentials {
    /// Login handle, set by any form-based flow.
    pub username: Option<String>,
    /// The plain-text password, for the backend to compare against
    /// a hash. **Never store it and never log it.**
    pub password: Option<String>,
    /// A user id from a trusted upstream, such as an SSO proxy's
    /// `X-Remote-User` header. It is only as trustworthy as the
    /// thing that filled it in; see [`RemoteUserBackend`].
    pub remote_user: Option<String>,
    /// Anything else a backend needs, such as a token, a provider
    /// name or an MFA code. Strings only; parse them yourself.
    pub extras: HashMap<String, String>,
}

impl Credentials {
    /// Credentials from a login form.
    #[must_use]
    pub fn password(username: impl Into<String>, password: impl Into<String>) -> Self {
        Self {
            username: Some(username.into()),
            password: Some(password.into()),
            ..Self::default()
        }
    }

    /// Credentials from a trusted upstream header. Only build these
    /// where you know the header came from your proxy.
    #[must_use]
    pub fn remote(remote_user: impl Into<String>) -> Self {
        Self {
            remote_user: Some(remote_user.into()),
            ..Self::default()
        }
    }

    /// Add one entry to `extras`.
    #[must_use]
    pub fn with_extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extras.insert(key.into(), value.into());
        self
    }
}

// ------------------------------------------------------------------ Principal

/// What every backend returns. Map it to your own user type when
/// you need more.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Principal {
    /// Opaque id chosen by the backend: often the numeric user id as
    /// a string, a UUID, or the username when there is nothing else.
    pub id: String,
    /// Login handle.
    pub username: String,
    /// `true` when the account is not disabled.
    pub is_active: bool,
    /// `true` when the account is an admin on that backend.
    pub is_superuser: bool,
    /// [`AuthBackend::name`] of the backend that authenticated this
    /// principal. A handler can branch on it, for example to skip
    /// MFA for a federated login.
    pub backend: String,
    /// Extra claims that do not fit the fields above, such as
    /// groups or OAuth claims.
    pub attributes: HashMap<String, serde_json::Value>,
}

// ------------------------------------------------------------------ AuthError

#[derive(Debug)]
pub enum AuthError {
    /// The backend ran and the credentials did not match. This
    /// stops the chain. Prefer `Ok(None)`, which lets an unknown
    /// username fall through to the next backend.
    InvalidCredentials,
    /// The backend itself failed: a DB outage, an LDAP timeout. It
    /// stops the chain so the caller sees the real problem instead
    /// of "no backend recognised you".
    Backend(String),
    /// Any other backend error. It also stops the chain.
    Other(String),
}

impl fmt::Display for AuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidCredentials => f.write_str("invalid credentials"),
            Self::Backend(msg) => write!(f, "auth backend failure: {msg}"),
            Self::Other(msg) => f.write_str(msg),
        }
    }
}

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

// ------------------------------------------------------------------ AuthBackend

/// One authentication source. `authenticate` returns:
///
/// - `Ok(Some(principal))`: accepted, and the chain stops.
/// - `Ok(None)`: nothing to say, so the chain tries the next one.
/// - `Err(_)`: the backend failed, and the chain stops.
///
/// Return `Ok(None)` for a wrong password too, not an error, so the
/// response cannot tell a caller which store holds the account.
#[async_trait::async_trait]
pub trait AuthBackend: Send + Sync {
    /// Short name for this backend, copied into
    /// [`Principal::backend`].
    fn name(&self) -> &'static str;

    /// Try to authenticate these credentials.
    async fn authenticate(&self, creds: &Credentials) -> Result<Option<Principal>, AuthError>;

    /// Look a principal up by its [`Principal::id`], for reloading a
    /// user from a session. Override it if your backend can; the
    /// default returns `Ok(None)`.
    async fn get_user(&self, _id: &str) -> Result<Option<Principal>, AuthError> {
        Ok(None)
    }
}

// ------------------------------------------------------------------ AuthBackendChain

/// Ordered list of backends. Walk via [`Self::authenticate`].
#[derive(Default, Clone)]
pub struct AuthBackendChain {
    backends: Vec<Arc<dyn AuthBackend>>,
}

impl AuthBackendChain {
    /// An empty chain. Every call returns `Ok(None)` until you add
    /// a backend, so nobody can log in.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a backend to the end of the chain. Order matters: the
    /// first backend that accepts wins.
    #[must_use]
    pub fn with(mut self, backend: Arc<dyn AuthBackend>) -> Self {
        self.backends.push(backend);
        self
    }

    /// How many backends are registered.
    #[must_use]
    pub fn len(&self) -> usize {
        self.backends.len()
    }

    /// `true` when nothing is registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.backends.is_empty()
    }

    /// Walk the chain and return the first backend that accepts.
    /// `Ok(None)` means no backend did; an `Err` stops the walk.
    pub async fn authenticate(&self, creds: &Credentials) -> Result<Option<Principal>, AuthError> {
        for backend in &self.backends {
            match backend.authenticate(creds).await? {
                Some(principal) => return Ok(Some(principal)),
                None => continue,
            }
        }
        Ok(None)
    }

    /// Walk the chain to reload a principal by id, usually one held
    /// in a session. The first hit wins; an `Err` stops the walk.
    pub async fn get_user(&self, id: &str) -> Result<Option<Principal>, AuthError> {
        for backend in &self.backends {
            match backend.get_user(id).await? {
                Some(principal) => return Ok(Some(principal)),
                None => continue,
            }
        }
        Ok(None)
    }
}

// ------------------------------------------------------------------ RemoteUserBackend

/// Trust the user-identity header of an upstream proxy, such as
/// Cloudflare Access or Tailscale. The proxy does the
/// authentication; this backend takes the name on faith.
///
/// **Security: this backend performs no check of its own.** Anyone
/// who can set `remote_user` becomes that user. Register it only
/// when the deployment guarantees the header comes from your proxy:
/// strip the header from every un-proxied request in middleware, or
/// listen on an address only the proxy can reach.
pub struct RemoteUserBackend {
    /// Decides which remote users to admit, for example by group.
    /// The default admits any non-empty name.
    pub admit: Arc<dyn Fn(&str) -> bool + Send + Sync>,
}

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

impl Default for RemoteUserBackend {
    fn default() -> Self {
        Self::trust_username()
    }
}

impl RemoteUserBackend {
    /// Admit any non-empty name. Use it when the SSO proxy already
    /// enforces who may reach the app.
    #[must_use]
    pub fn trust_username() -> Self {
        Self {
            admit: Arc::new(|u| !u.is_empty()),
        }
    }

    /// Admit only the names the predicate accepts, which adds a
    /// local allow-list on top of the upstream's check.
    #[must_use]
    pub fn with_predicate<F>(predicate: F) -> Self
    where
        F: Fn(&str) -> bool + Send + Sync + 'static,
    {
        Self {
            admit: Arc::new(predicate),
        }
    }
}

#[async_trait::async_trait]
impl AuthBackend for RemoteUserBackend {
    fn name(&self) -> &'static str {
        "remote_user"
    }

    async fn authenticate(&self, creds: &Credentials) -> Result<Option<Principal>, AuthError> {
        let Some(username) = creds.remote_user.as_deref() else {
            return Ok(None);
        };
        if !(self.admit)(username) {
            return Ok(None);
        }
        Ok(Some(Principal {
            id: username.to_owned(),
            username: username.to_owned(),
            is_active: true,
            is_superuser: false,
            backend: "remote_user".to_owned(),
            attributes: HashMap::new(),
        }))
    }
}

// ------------------------------------------------------------------ Tests

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

    /// Accepts one fixed `(username, password)` pair.
    struct FixedPasswordBackend {
        username: &'static str,
        password: &'static str,
        is_superuser: bool,
    }

    #[async_trait::async_trait]
    impl AuthBackend for FixedPasswordBackend {
        fn name(&self) -> &'static str {
            "fixed_password"
        }
        async fn authenticate(&self, creds: &Credentials) -> Result<Option<Principal>, AuthError> {
            let (Some(u), Some(p)) = (creds.username.as_deref(), creds.password.as_deref()) else {
                return Ok(None);
            };
            if u == self.username && p == self.password {
                return Ok(Some(Principal {
                    id: "1".into(),
                    username: u.into(),
                    is_active: true,
                    is_superuser: self.is_superuser,
                    backend: "fixed_password".into(),
                    attributes: HashMap::new(),
                }));
            }
            Ok(None)
        }
        async fn get_user(&self, id: &str) -> Result<Option<Principal>, AuthError> {
            if id == "1" {
                Ok(Some(Principal {
                    id: "1".into(),
                    username: self.username.into(),
                    is_active: true,
                    is_superuser: self.is_superuser,
                    backend: "fixed_password".into(),
                    attributes: HashMap::new(),
                }))
            } else {
                Ok(None)
            }
        }
    }

    /// Always errors, to pin the stop-on-error rule.
    struct ExplodingBackend;
    #[async_trait::async_trait]
    impl AuthBackend for ExplodingBackend {
        fn name(&self) -> &'static str {
            "exploding"
        }
        async fn authenticate(&self, _: &Credentials) -> Result<Option<Principal>, AuthError> {
            Err(AuthError::Backend("simulated DB outage".into()))
        }
    }

    #[tokio::test]
    async fn empty_chain_returns_none() {
        let chain = AuthBackendChain::new();
        assert!(chain.is_empty());
        let r = chain
            .authenticate(&Credentials::password("alice", "x"))
            .await
            .unwrap();
        assert!(r.is_none());
    }

    #[tokio::test]
    async fn single_backend_match() {
        let chain = AuthBackendChain::new().with(Arc::new(FixedPasswordBackend {
            username: "alice",
            password: "s3cret",
            is_superuser: false,
        }));
        let p = chain
            .authenticate(&Credentials::password("alice", "s3cret"))
            .await
            .unwrap()
            .expect("backend should authenticate");
        assert_eq!(p.username, "alice");
        assert_eq!(p.backend, "fixed_password");
        assert!(p.is_active);
        assert!(!p.is_superuser);
    }

    #[tokio::test]
    async fn single_backend_miss_returns_none() {
        let chain = AuthBackendChain::new().with(Arc::new(FixedPasswordBackend {
            username: "alice",
            password: "s3cret",
            is_superuser: false,
        }));
        let r = chain
            .authenticate(&Credentials::password("alice", "wrong"))
            .await
            .unwrap();
        assert!(r.is_none(), "wrong password should miss");
    }

    #[tokio::test]
    async fn chain_falls_through_to_next_backend_on_none() {
        let chain = AuthBackendChain::new()
            .with(Arc::new(FixedPasswordBackend {
                username: "alice",
                password: "s3cret",
                is_superuser: false,
            }))
            .with(Arc::new(FixedPasswordBackend {
                username: "bob",
                password: "hunter2",
                is_superuser: true,
            }));
        let p = chain
            .authenticate(&Credentials::password("bob", "hunter2"))
            .await
            .unwrap()
            .expect("second backend should authenticate bob");
        assert_eq!(p.username, "bob");
        assert!(p.is_superuser);
    }

    #[tokio::test]
    async fn first_backend_wins_when_both_match() {
        // Both accept the same credentials, so order decides.
        let chain = AuthBackendChain::new()
            .with(Arc::new(FixedPasswordBackend {
                username: "alice",
                password: "s3cret",
                is_superuser: false,
            }))
            .with(Arc::new(FixedPasswordBackend {
                username: "alice",
                password: "s3cret",
                is_superuser: true, // must not reach the result
            }));
        let p = chain
            .authenticate(&Credentials::password("alice", "s3cret"))
            .await
            .unwrap()
            .unwrap();
        assert!(
            !p.is_superuser,
            "first backend wins; second backend's superuser flag must NOT leak"
        );
    }

    #[tokio::test]
    async fn err_short_circuits_chain() {
        let chain = AuthBackendChain::new()
            .with(Arc::new(ExplodingBackend))
            .with(Arc::new(FixedPasswordBackend {
                username: "alice",
                password: "s3cret",
                is_superuser: false,
            }));
        let err = chain
            .authenticate(&Credentials::password("alice", "s3cret"))
            .await
            .unwrap_err();
        assert!(matches!(err, AuthError::Backend(_)));
    }

    #[tokio::test]
    async fn get_user_walks_chain() {
        let chain = AuthBackendChain::new().with(Arc::new(FixedPasswordBackend {
            username: "alice",
            password: "s3cret",
            is_superuser: false,
        }));
        let p = chain.get_user("1").await.unwrap().unwrap();
        assert_eq!(p.username, "alice");
        // An unknown id falls through to `None`.
        let r = chain.get_user("999").await.unwrap();
        assert!(r.is_none());
    }

    // ---------- RemoteUserBackend ----------

    #[tokio::test]
    async fn remote_user_backend_authenticates_header() {
        let chain = AuthBackendChain::new().with(Arc::new(RemoteUserBackend::trust_username()));
        let p = chain
            .authenticate(&Credentials::remote("alice"))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(p.username, "alice");
        assert_eq!(p.backend, "remote_user");
        assert_eq!(p.id, "alice");
    }

    #[tokio::test]
    async fn remote_user_backend_ignores_empty_header() {
        let chain = AuthBackendChain::new().with(Arc::new(RemoteUserBackend::trust_username()));
        // Empty name: the admit predicate says no.
        let r = chain.authenticate(&Credentials::remote("")).await.unwrap();
        assert!(r.is_none());
        // No remote_user at all: also none.
        let r = chain
            .authenticate(&Credentials::password("alice", "x"))
            .await
            .unwrap();
        assert!(r.is_none());
    }

    #[tokio::test]
    async fn remote_user_backend_with_allowlist_predicate() {
        let chain =
            AuthBackendChain::new().with(Arc::new(RemoteUserBackend::with_predicate(|u| {
                u == "alice"
            })));
        assert!(chain
            .authenticate(&Credentials::remote("alice"))
            .await
            .unwrap()
            .is_some());
        // Bob passed upstream, but our allow-list rejects him.
        assert!(chain
            .authenticate(&Credentials::remote("bob"))
            .await
            .unwrap()
            .is_none());
    }

    #[tokio::test]
    async fn remote_user_falls_through_to_password_backend() {
        // A common setup: SSO header for proxied requests, password
        // form for local admin. Remote first, so SSO wins.
        let chain = AuthBackendChain::new()
            .with(Arc::new(RemoteUserBackend::trust_username()))
            .with(Arc::new(FixedPasswordBackend {
                username: "alice",
                password: "s3cret",
                is_superuser: false,
            }));

        // No header, so this falls through to the password backend.
        let p = chain
            .authenticate(&Credentials::password("alice", "s3cret"))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(p.backend, "fixed_password");

        // With the header, the remote backend wins.
        let p = chain
            .authenticate(&Credentials::remote("alice"))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(p.backend, "remote_user");
    }

    // ---------- Credentials ergonomics ----------

    #[test]
    fn credentials_with_extra_chains() {
        let c = Credentials::password("alice", "x")
            .with_extra("mfa_code", "123456")
            .with_extra("device_id", "iphone");
        assert_eq!(c.username.as_deref(), Some("alice"));
        assert_eq!(c.extras.get("mfa_code").map(String::as_str), Some("123456"));
        assert_eq!(
            c.extras.get("device_id").map(String::as_str),
            Some("iphone")
        );
    }

    #[test]
    fn auth_error_displays_each_variant() {
        assert_eq!(
            format!("{}", AuthError::InvalidCredentials),
            "invalid credentials"
        );
        assert_eq!(
            format!("{}", AuthError::Backend("oops".into())),
            "auth backend failure: oops"
        );
        assert_eq!(format!("{}", AuthError::Other("custom".into())), "custom");
    }
}