rustio-admin 0.22.0

Django Admin, but for Rust. A small, focused admin framework.
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
//! HTTP handlers for the unauthenticated password-recovery surface
//! (R1 commit #8). See `DESIGN_RECOVERY.md` §3 + §8.
//!
//! Routes wired in commit #9; this module owns:
//!
//! - `GET  /admin/forgot-password`            → [`show_forgot_password`]
//! - `POST /admin/forgot-password`            → [`do_forgot_password`]
//! - `GET  /admin/forgot-password/sent`       → [`show_forgot_password_sent`]
//! - `GET  /admin/reset-password/<token>`     → [`show_reset_password`]
//! - `POST /admin/reset-password/<token>`     → [`do_reset_password`]
//!
//! ## Uniform-response invariant (LOCKED — `DESIGN_RECOVERY.md` §2.3)
//!
//! Outward-facing pages MUST NOT reveal:
//!
//! - whether an email is registered (`do_forgot_password` redirects
//!   to `/admin/forgot-password/sent` regardless of the underlying
//!   [`IssueOutcome`] — known, unknown, inactive, rate-limited all
//!   collapse to one response);
//! - whether a reset token is malformed, expired, or already
//!   consumed (`show_reset_password` and `do_reset_password` render
//!   the same "this link is no longer valid" card across all three
//!   sub-cases);
//! - whether the consume path was rate-limited (renders identically
//!   to "invalid", per the disclosure rule).
//!
//! `IssueOutcome` and `ConsumeOutcome` distinctions exist only for
//! audit/log/observability; the handler maps them to indistinguish-
//! able pages.
//!
//! ## What this module does NOT do (commits that follow)
//!
//! - **Routes** are registered in commit #9.
//! - **Login-page touch-ups** ("Forgot your password?" link,
//!   `?password_reset=success` banner) are commit #9.
//! - **Active-sessions revoke buttons** are commit #10.
//! - **`do_password_change` drift correction** is commit #11.
//! - **Token sweeper** is commit #12.

use std::sync::Arc;

use serde::Serialize;

use crate::auth::recovery::{
    check_reset_token_valid, consume_reset_token, issue_reset_token, ConsumeOutcome,
    PasswordPolicyError,
};
use crate::error::Result;
use crate::http::{Request, Response};
use crate::middleware::{CorrelationId, RateLimiter};

use super::handlers::{csrf_token, AdminCtx};
use super::render::{BaseContext, FlashCtx, FormField, FormSection};
use super::types::Admin;

// ---- RecoveryState --------------------------------------------------------

/// Per-process recovery state. The two rate-limit buckets live here
/// because they're stateful (token-bucket counters per IP) and the
/// runtime functions need them on every request.
///
/// Built once at route-registration time (commit #9) via
/// [`RecoveryState::from_admin`]; the `Arc<RateLimiter>` fields are
/// then cloned into each route closure for the process lifetime.
/// No global / static / `OnceLock` state — the `Arc<RecoveryState>`
/// is owned by the route handler closures.
#[derive(Clone)]
pub(crate) struct RecoveryState {
    pub request_limiter: Arc<RateLimiter>,
    pub consume_limiter: Arc<RateLimiter>,
}

impl RecoveryState {
    // internal:
    /// Build from `Admin`, sizing the buckets from
    /// `RecoveryPolicy::request_rate_limit()` and
    /// `consume_rate_limit()`. Called once at registration time.
    pub(crate) fn from_admin(admin: &Admin) -> Self {
        let policy = admin.active_recovery_policy();
        let (req_cap, req_window) = policy.request_rate_limit();
        let (cons_cap, cons_window) = policy.consume_rate_limit();
        Self {
            request_limiter: Arc::new(RateLimiter::new(req_cap, req_window)),
            consume_limiter: Arc::new(RateLimiter::new(cons_cap, cons_window)),
        }
    }
}

// ---- Helpers --------------------------------------------------------------

fn correlation_id(req: &Request) -> Option<String> {
    req.ctx().get::<CorrelationId>().map(|c| c.0.clone())
}

// ---- Render contexts ------------------------------------------------------

/// Context for `admin/forgot_password.html`. Identity is always
/// `None` (unauthenticated flow).
#[derive(Serialize)]
struct ForgotPasswordCtx {
    #[serde(flatten)]
    base: BaseContext,
    page_title: &'static str,
    sections: Vec<FormSection>,
    /// Optional uniform error banner — used only for form-level
    /// validation feedback that doesn't reveal account existence.
    /// Set to `None` for the success path; we never render an error
    /// distinguishing "account exists" from "doesn't exist".
    error: Option<String>,
    /// Per-request UUID v7 from the correlation_id middleware.
    /// Rendered in a `<meta>` tag for support-ticket diagnostics.
    correlation_id: Option<String>,
}

/// Context for `admin/forgot_password_sent.html` (the post-submit
/// "check your inbox" card). No form, no errors, no policy details —
/// pure static copy plus the diagnostics meta.
#[derive(Serialize)]
struct ForgotPasswordSentCtx {
    #[serde(flatten)]
    base: BaseContext,
    page_title: &'static str,
    correlation_id: Option<String>,
}

/// Context for `admin/reset_password.html`. Two render modes
/// distinguished by the `invalid` flag — when `true`, the template
/// shows the "no longer valid" card and ignores the form fields;
/// when `false`, the form is rendered with the policy minimum text.
#[derive(Serialize)]
struct ResetPasswordCtx {
    #[serde(flatten)]
    base: BaseContext,
    page_title: &'static str,
    /// `true` ⇒ render the generic "this link is no longer valid"
    /// card. Used for unknown / expired / consumed tokens
    /// indistinguishably (§2.3) and for the rate-limited POST path.
    invalid: bool,
    /// Form-action target (the same opaque URL parameter the user
    /// arrived with). Rendered ONLY into the `<form action>` and
    /// the `Request a new link` href; never echoed back as visible
    /// content.
    token: String,
    /// Live policy minimum length, derived from
    /// `admin.active_password_policy().min_length()` so users see
    /// the floor before submitting. Default 10; projects may
    /// override.
    min_length: usize,
    sections: Vec<FormSection>,
    /// Form-level errors (e.g. "passwords didn't match"). NEVER the
    /// raw policy enum debug; only user-safe `Display` strings.
    errors: Vec<String>,
    flash: Option<FlashCtx>,
    correlation_id: Option<String>,
}

// ---- Form-section builders -----------------------------------------------

fn forgot_password_form_sections(prefilled_email: &str) -> Vec<FormSection> {
    vec![FormSection {
        title: None,
        fields: vec![FormField {
            name: "email",
            label: "Email".to_string(),
            widget: "input",
            input_type: "email",
            value: prefilled_email.to_string(),
            hint: None,
            placeholder: None,
            required: true,
            options: None,
            multiple: false,
            span: 2,
            autocomplete: Some("username"),
            autofocus: true,
            disabled: false,
            maxlength: None,
            searchable: false,
            has_more: false,
            search_url: None,
            errors: vec![],
            target_model: None,
            checked: false,
        }],
    }]
}

fn reset_password_form_sections(
    min_length: usize,
    field_errors: &[(&'static str, String)],
) -> Vec<FormSection> {
    let err_for = |name: &str| -> Vec<String> {
        field_errors
            .iter()
            .filter(|(n, _)| *n == name)
            .map(|(_, m)| m.clone())
            .collect()
    };
    let hint = format!("At least {min_length} characters.");
    vec![FormSection {
        title: None,
        fields: vec![
            FormField {
                name: "new_password1",
                label: "New password".to_string(),
                widget: "input",
                input_type: "password",
                value: String::new(),
                hint: Some(hint.clone()),
                placeholder: None,
                required: true,
                options: None,
                multiple: false,
                span: 2,
                autocomplete: Some("new-password"),
                autofocus: true,
                disabled: false,
                maxlength: None,
                searchable: false,
                has_more: false,
                search_url: None,
                errors: err_for("new_password1"),
                target_model: None,
                checked: false,
            },
            FormField {
                name: "new_password2",
                label: "Confirm".to_string(),
                widget: "input",
                input_type: "password",
                value: String::new(),
                hint: None,
                placeholder: None,
                required: true,
                options: None,
                multiple: false,
                span: 2,
                autocomplete: Some("new-password"),
                autofocus: false,
                disabled: false,
                maxlength: None,
                searchable: false,
                has_more: false,
                search_url: None,
                errors: err_for("new_password2"),
                target_model: None,
                checked: false,
            },
        ],
    }]
}

// ---- Handlers: forgot-password -------------------------------------------

pub(crate) async fn show_forgot_password(ctx: &AdminCtx, req: &Request) -> Result<Response> {
    let view = ForgotPasswordCtx {
        base: BaseContext::new(None, csrf_token(req), &ctx.admin),
        page_title: "Reset your password",
        sections: forgot_password_form_sections(""),
        error: None,
        correlation_id: correlation_id(req),
    };
    let body = ctx.templates.render("admin/forgot_password.html", &view)?;
    Ok(Response::html(body))
}

/// `POST /admin/forgot-password`. Always redirects to the uniform
/// "check your inbox" page regardless of whether the email matched
/// an active user, was unknown, was inactive, or was rate-limited.
/// Disclosure rule §2.3.
pub(crate) async fn do_forgot_password(
    ctx: &AdminCtx,
    state: &RecoveryState,
    req: Request,
) -> Result<Response> {
    let cid = correlation_id(&req);
    let form = req.form()?;
    let email = form.get("email").unwrap_or("").to_string();

    // Outcome variants are observed only for log/audit purposes;
    // the user-facing response is the same across all of them.
    let _outcome = issue_reset_token(
        &ctx.db,
        &ctx.admin,
        &state.request_limiter,
        &req,
        &email,
        cid.as_deref(),
    )
    .await?;

    Ok(Response::redirect("/admin/forgot-password/sent"))
}

pub(crate) async fn show_forgot_password_sent(ctx: &AdminCtx, req: &Request) -> Result<Response> {
    let view = ForgotPasswordSentCtx {
        base: BaseContext::new(None, csrf_token(req), &ctx.admin),
        page_title: "Check your email",
        correlation_id: correlation_id(req),
    };
    let body = ctx
        .templates
        .render("admin/forgot_password_sent.html", &view)?;
    Ok(Response::html(body))
}

// ---- Handlers: reset-password --------------------------------------------

/// `GET /admin/reset-password/<token>`. Renders the new-password
/// form when the token is currently valid; renders the generic "no
/// longer valid" card otherwise (unknown / expired / consumed all
/// fall through to the same card).
///
/// The check is non-mutating; the POST path's atomic UPDATE is the
/// authoritative consume. This GET-time check is purely a UX
/// courtesy so users clicking a stale link don't fill in the form
/// before being told it's invalid.
pub(crate) async fn show_reset_password(
    ctx: &AdminCtx,
    req: &Request,
    token: &str,
) -> Result<Response> {
    let valid = check_reset_token_valid(&ctx.db, token).await?;
    let min_length = ctx.admin.active_password_policy().min_length();
    let view = ResetPasswordCtx {
        base: BaseContext::new(None, csrf_token(req), &ctx.admin),
        page_title: if valid {
            "Set a new password"
        } else {
            "This link is no longer valid"
        },
        invalid: !valid,
        token: token.to_string(),
        min_length,
        sections: if valid {
            reset_password_form_sections(min_length, &[])
        } else {
            Vec::new()
        },
        errors: Vec::new(),
        flash: None,
        correlation_id: correlation_id(req),
    };
    let body = ctx.templates.render("admin/reset_password.html", &view)?;
    Ok(Response::html(body))
}

/// `POST /admin/reset-password/<token>`. Validates the form
/// (passwords match) before delegating to
/// [`consume_reset_token`]. Maps the typed [`ConsumeOutcome`]
/// variants to user-facing pages per the disclosure rule:
///
/// - `Consumed` → 303 redirect to `/admin/login?password_reset=success`.
/// - `Invalid` / `RateLimited` → render the "no longer valid" card.
/// - `PolicyRejected(_)` → re-render the form with the policy's
///   `Display` string mapped onto `new_password1`.
pub(crate) async fn do_reset_password(
    ctx: &AdminCtx,
    state: &RecoveryState,
    req: Request,
    token: &str,
) -> Result<Response> {
    let cid = correlation_id(&req);
    let form = req.form()?;
    let pw1 = form.get("new_password1").unwrap_or("").to_string();
    let pw2 = form.get("new_password2").unwrap_or("").to_string();
    let min_length = ctx.admin.active_password_policy().min_length();

    // UX-level mismatch check happens BEFORE the runtime call so a
    // user mistyping doesn't trigger a rate-limit hit + audit
    // breadcrumb. The mismatch is a re-render with a field error;
    // the token stays valid.
    if pw1 != pw2 {
        return render_reset_password_form_error(
            ctx,
            &req,
            token,
            min_length,
            &[(
                "new_password2",
                "The two password fields didn't match.".into(),
            )],
            cid,
        );
    }

    let outcome = consume_reset_token(
        &ctx.db,
        &ctx.admin,
        &state.consume_limiter,
        &req,
        token,
        &pw1,
        cid.as_deref(),
    )
    .await?;

    match outcome {
        ConsumeOutcome::Consumed { .. } => {
            // Successful consume: redirect to login with a success
            // flag the login page reads from `?password_reset=success`
            // (commit #9 wires the banner). Do NOT mint a session —
            // the user signs in afresh so MFA (R3+) gets exercised.
            Ok(Response::redirect("/admin/login?password_reset=success"))
        }
        ConsumeOutcome::Invalid | ConsumeOutcome::RateLimited => {
            // Indistinguishable to the user per §2.3.
            render_reset_password_invalid(ctx, &req, token, min_length, cid)
        }
        ConsumeOutcome::PolicyRejected(err) => {
            // Map the policy error to a user-safe Display string
            // attached to `new_password1`. Never render the Debug
            // form; never include the candidate plaintext (variants
            // structurally don't carry it — pinned by commit #7
            // tests, but defence-in-depth: we only render Display).
            let user_message = render_password_policy_error(&err);
            render_reset_password_form_error(
                ctx,
                &req,
                token,
                min_length,
                &[("new_password1", user_message)],
                cid,
            )
        }
    }
}

/// Render the new-password form with one or more field errors —
/// the user can retry without burning the token.
fn render_reset_password_form_error(
    ctx: &AdminCtx,
    req: &Request,
    token: &str,
    min_length: usize,
    field_errors: &[(&'static str, String)],
    cid: Option<String>,
) -> Result<Response> {
    let view = ResetPasswordCtx {
        base: BaseContext::new(None, csrf_token(req), &ctx.admin),
        page_title: "Set a new password",
        invalid: false,
        token: token.to_string(),
        min_length,
        sections: reset_password_form_sections(min_length, field_errors),
        errors: field_errors.iter().map(|(_, m)| m.clone()).collect(),
        flash: None,
        correlation_id: cid,
    };
    let body = ctx.templates.render("admin/reset_password.html", &view)?;
    Ok(Response::html(body).with_status(hyper::StatusCode::BAD_REQUEST))
}

/// Render the generic "this link is no longer valid" card. Used
/// for the `Invalid` and `RateLimited` consume outcomes — the
/// distinction exists in audit/logs, never in this rendered HTML.
fn render_reset_password_invalid(
    ctx: &AdminCtx,
    req: &Request,
    token: &str,
    min_length: usize,
    cid: Option<String>,
) -> Result<Response> {
    let view = ResetPasswordCtx {
        base: BaseContext::new(None, csrf_token(req), &ctx.admin),
        page_title: "This link is no longer valid",
        invalid: true,
        token: token.to_string(),
        min_length,
        sections: Vec::new(),
        errors: Vec::new(),
        flash: None,
        correlation_id: cid,
    };
    let body = ctx.templates.render("admin/reset_password.html", &view)?;
    // 200 (not 4xx) — the user reached a valid URL; the resource
    // happens to no longer be claimable. Status code carries no
    // disclosure value.
    Ok(Response::html(body))
}

/// Map a [`PasswordPolicyError`] to the user-facing string shown on
/// the form. Always uses `Display` (which the trait's default impl
/// is responsible for keeping plaintext-free) — never `Debug`.
fn render_password_policy_error(err: &PasswordPolicyError) -> String {
    err.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::{DefaultPasswordPolicy, PasswordPolicy};

    /// `RecoveryState::from_admin` reads the recovery policy and
    /// builds two limiters sized from its tuples. We can't easily
    /// inspect the internal bucket capacity from outside the
    /// limiter, but we can exercise the constructor and confirm
    /// it doesn't panic on the framework defaults.
    #[test]
    fn recovery_state_constructs_from_default_admin() {
        let admin = Admin::new();
        let state = RecoveryState::from_admin(&admin);
        // Both limiters allow at least one request from a fresh
        // bucket — defaults are 5/15min and 10/5min, both ≥ 1.
        assert!(state.request_limiter.allow("test-ip-1"));
        assert!(state.consume_limiter.allow("test-ip-1"));
    }

    /// The form-section builder wires `min_length` into the field
    /// hint so users see the live policy floor on the rendered
    /// page — even when the project overrides the default.
    #[test]
    fn reset_password_form_renders_live_policy_minimum() {
        let sections = reset_password_form_sections(10, &[]);
        assert_eq!(sections.len(), 1);
        let section = &sections[0];
        assert_eq!(section.fields.len(), 2);
        let pw1 = &section.fields[0];
        assert_eq!(pw1.name, "new_password1");
        assert_eq!(
            pw1.hint.as_deref(),
            Some("At least 10 characters."),
            "hint must reflect the live policy minimum"
        );

        // Project override: the helper must propagate, not hardcode.
        let sections = reset_password_form_sections(16, &[]);
        assert_eq!(
            sections[0].fields[0].hint.as_deref(),
            Some("At least 16 characters."),
        );
    }

    /// Field errors get routed onto the right field's `errors` Vec.
    #[test]
    fn reset_password_form_routes_field_errors_to_named_field() {
        let sections = reset_password_form_sections(
            10,
            &[
                ("new_password1", "policy err".into()),
                ("new_password2", "match err".into()),
            ],
        );
        assert_eq!(sections[0].fields[0].errors, vec!["policy err"]);
        assert_eq!(sections[0].fields[1].errors, vec!["match err"]);
    }

    /// The forgot-password form is single-field (email only).
    #[test]
    fn forgot_password_form_has_only_email_field() {
        let sections = forgot_password_form_sections("");
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].fields.len(), 1);
        assert_eq!(sections[0].fields[0].name, "email");
        assert_eq!(sections[0].fields[0].input_type, "email");
        assert!(sections[0].fields[0].autofocus);
    }

    /// Property: the rendered policy-error string never carries the
    /// candidate plaintext. Pinned at the rendering boundary (the
    /// trait's Display is responsible for the property; this test
    /// catches regressions in `render_password_policy_error`).
    #[test]
    fn render_password_policy_error_does_not_leak_plaintext() {
        // Construct a policy + run a known-too-short candidate
        // through it; render the error; assert the plaintext is
        // absent.
        let policy = DefaultPasswordPolicy::new();
        let plaintext = "Pwn4Ge#zZ"; // 9 chars — fails 10-char floor
        let err = policy
            .validate(plaintext)
            .expect_err("9-char password should fail the 10-char floor");
        let rendered = render_password_policy_error(&err);
        assert!(
            !rendered.contains(plaintext),
            "rendered policy error leaked plaintext: {rendered}"
        );
    }

    /// `correlation_id` field is wrapped in `Option<String>`; when
    /// the middleware didn't run (no CorrelationId in the request
    /// context), helpers must still construct a coherent context
    /// without panicking.
    #[test]
    fn render_contexts_tolerate_absent_correlation_id() {
        let admin = Admin::new();
        let base = BaseContext::new(None, "csrf-test".into(), &admin);
        let ctx = ForgotPasswordCtx {
            base,
            page_title: "Reset your password",
            sections: forgot_password_form_sections(""),
            error: None,
            correlation_id: None,
        };
        let json = serde_json::to_string(&ctx).expect("serializes cleanly with absent cid");
        assert!(json.contains("\"correlation_id\":null"));
    }
}