recourse: Forgot password (neiam-auth v0.5.0 password-reset: /password/forgot and /password/reset, neutral answer, 1-hour single-use link through the app's mailer, sessions and device tokens revoked, address marked verified; per-IP limit on the TRUSTED_PROXIES address; token redacted from request spans)

952e601 · gmorell · 2026-12-03 06:31

25 files +739 -32

Files changed

modified Cargo.lock
+3 −2
@@ -2456,13 +2456,14 @@ checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d"
2456 2456
2457 2457 [[package]]
2458 2458 name = "neiam-auth"
2459 −version = "0.4.0"
2460 −source = "git+https://gg.neiam.co/neiam/neiam-auth.git?tag=v0.4.0#f7dd0fa92c3de9ec17a82bcf1e2b2a86d44dfcc5"
2459 +version = "0.5.0"
2460 +source = "git+https://gg.neiam.co/neiam/neiam-auth.git?tag=v0.5.0#9291654570cdc234e676066f56b688ba5ef901e0"
2461 2461 dependencies = [
2462 2462 "axum",
2463 2463 "base64 0.22.1",
2464 2464 "chrono",
2465 2465 "cookie",
2466 + "getrandom 0.3.4",
2466 2467 "hkdf",
2467 2468 "http",
2468 2469 "metrics",
modified Cargo.toml
+2 −1
@@ -48,7 +48,7 @@ migration = { path = "./migration" }
48 48 # unless OIDC_ENABLED=true. Pinned to a tag so image rebuilds never pick up
49 49 # crate changes silently. HTTPS, not SSH: the image build has no credentials.
50 50 # `sea-orm` (not `sea-orm-2`): recourse is on sea-orm 1.1.
51 −neiam-auth = { git = "https://gg.neiam.co/neiam/neiam-auth.git", tag = "v0.4.0", features = ["axum", "sea-orm"] }
51 +neiam-auth = { git = "https://gg.neiam.co/neiam/neiam-auth.git", tag = "v0.5.0", features = ["axum", "sea-orm", "password-reset"] }
52 52 # Outbound mail (email verification) through mail-prism. Vendored, not a git
53 53 # dependency: the mail-prism repo is private. See crates/mail-prism-client/VENDORED.md.
54 54 mail-prism-client = { path = "crates/mail-prism-client" }
@@ -85,3 +85,4 @@ tower = { version = "0.5", features = ["util"] }
85 85 # The live-update socket end to end in tests/moderation_db.rs.
86 86 tokio-tungstenite = "0.27"
87 87 futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
88 +
modified README.md
+35 −0
@@ -366,6 +366,7 @@ directly to the internet, no proxy).
366 366 | `MAIL_FROM_ADDRESS` | _(the key's application address)_ | Must equal the mail-prism application address if set |
367 367 | `MAIL_FROM_NAME` | `APP_NAME` | From display name |
368 368 | `EMAIL_VERIFICATION_TTL_HOURS` | `48` | Lifetime of a verification link |
369 +| `PASSWORD_RESET_TTL_MINUTES` | `60` | How long a "forgot password" link works (1 to 1440). The link's base is `PUBLIC_URL` |
369 370 | `REQUIRE_EMAIL_VERIFICATION` | `true` | Unverified accounts can read but not post/reply/react/bookmark/tag |
370 371 | `OIDC_ENABLED`, `OIDC_LOGIN_MODE`, `OIDC_*` | off | "Sign in with NEIAM"; see below and the neiam-auth README |
371 372 | `OIDC_PASSWORD_LOGIN` | `true` | `false` hides and refuses password login (implied by `OIDC_LOGIN_MODE=sso_strict`) |
@@ -621,6 +622,40 @@ message, link included, to its log at INFO
621 622 the link; `manage user resend-verification <name>` prints a fresh one;
622 623 `manage email test <to>` checks the mail-prism setup.
623 624
625 +## Forgot password
626 +
627 +The login page links to `/password/forgot`, which asks for the account's
628 +email. The answer is always the same ("If that address has an account, a
629 +link is on its way"), whether or not the address has an account. The link
630 +(`/password/reset?token=…`) works once and expires after an hour
631 +(`PASSWORD_RESET_TTL_MINUTES`). It stops working when the user asks again, or
632 +when the password or email changes. Setting a new password:
633 +
634 +- signs the account out everywhere (every web session and every paired
635 + device's API token);
636 +- marks the address verified (the link reached it);
637 +- sends the user to `/login` to sign in with the new password.
638 +
639 +The flow is [neiam-auth](https://gg.neiam.co/neiam/neiam-auth)'s
640 +`password-reset` feature. The app's side is `src/auth/password_reset.rs`.
641 +
642 +- Mail goes through the app's mailer, the same one as email verification:
643 + mail-prism when `MAIL_PRISM_URL` + `MAIL_PRISM_API_KEY` are set, else the
644 + log, where the link can be read off. The mail is plain text in a raw send.
645 + It isn't a synced mail-prism template.
646 +- Deactivated and currently suspended accounts get no mail. In
647 + `OIDC_LOGIN_MODE=sso`, an account linked to a `REQUIRED_FOR_LINKED`
648 + provider gets a mail saying to sign in with NEIAM, with no link. Accounts
649 + created through NEIAM can set a password when the login mode allows them
650 + one. With password login off (`OIDC_PASSWORD_LOGIN=false` or
651 + `sso_strict`), the routes 404 and the link is hidden.
652 +- Limits: 3 requests per address, and 20 per client address per hour (the
653 + address `TRUSTED_PROXIES` resolves; see
654 + [Client addresses](#client-addresses)), per replica. Over the per-IP
655 + limit the answer is a 429.
656 +- Request spans redact `token=` (and the OIDC callback's `code`/`state`).
657 + The reset pages send `Referrer-Policy: no-referrer`.
658 +
624 659 ## Sign in with NEIAM
625 660
626 661 [`neiam-auth`](https://gg.neiam.co/neiam/neiam-auth) v0.4.0 (git dependency,
modified migration/Cargo.toml
+1 −1
@@ -10,7 +10,7 @@ path = "src/lib.rs"
10 10
11 11 [dependencies]
12 12 # The oidc_identities migration. Same tag as the app's dependency.
13 −neiam-auth = { git = "https://gg.neiam.co/neiam/neiam-auth.git", tag = "v0.4.0", features = ["sea-orm"] }
13 +neiam-auth = { git = "https://gg.neiam.co/neiam/neiam-auth.git", tag = "v0.5.0", features = ["sea-orm", "password-reset"] }
14 14 async-std = { version = "1", features = ["attributes", "tokio1"] }
15 15
16 16 [dependencies.sea-orm-migration]
modified migration/src/lib.rs
+2 −0
@@ -63,6 +63,8 @@ impl MigratorTrait for Migrator {
63 63 Box::new(m20240101_000024_topic_held::Migration),
64 64 Box::new(m20240101_000025_seed_markers::Migration),
65 65 Box::new(m20240101_000026_post_links::Migration),
66 + // "Forgot password?" tokens (neiam-auth), FK to users(id).
67 + Box::new(neiam_auth::sea_orm::password_reset::Migration::new("users")),
66 68 ]
67 69 }
68 70 }
modified src/auth/mod.rs
+1 −0

Click to load diff…

added src/auth/password_reset.rs
+215 −0

Click to load diff…

modified src/handlers/auth.rs
+3 −0

Click to load diff…

modified src/mailer.rs
+76 −26

Click to load diff…

modified src/main.rs
+7 −2

Click to load diff…

modified src/web.rs
+14 −0

Click to load diff…

added templates/auth/forgot_password.html
+32 −0

Click to load diff…

added templates/auth/forgot_password_sent.html
+13 −0

Click to load diff…

modified templates/auth/login.html
+4 −0

Click to load diff…

added templates/auth/reset_password.html
+34 −0

Click to load diff…

added templates/auth/reset_password_invalid.html
+13 −0

Click to load diff…

added tests/password_reset_db.rs
+284 −0

Click to load diff…

Parents: 7ce364f