recourse: Android login page (Scan / Sign in with NEIAM / Username sheets, help page) and app sign-in API (neiam-auth v0.6.0 app-login: /api/v1/auth/{methods,password}, NEIAM via Custom Tab + PKCE + one-time code to recourse://auth, no web session; the web's suspension and sso refusals; tokens are ordinary hashed api_tokens with the pairing page's 90-day sliding expiry)

01e509e · gmorell · 2026-12-03 17:16

56 files +2292 -135

Files changed

modified Cargo.lock
+2 −2
@@ -2456,8 +2456,8 @@ checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d"
2456 2456
2457 2457 [[package]]
2458 2458 name = "neiam-auth"
2459 −version = "0.5.0"
2460 −source = "git+https://gg.neiam.co/neiam/neiam-auth.git?tag=v0.5.0#9291654570cdc234e676066f56b688ba5ef901e0"
2459 +version = "0.6.0"
2460 +source = "git+https://gg.neiam.co/neiam/neiam-auth.git?tag=v0.6.0#0fc380b1fb0f39da413e4b2bbe47d8407fbb30dd"
2461 2461 dependencies = [
2462 2462 "axum",
2463 2463 "base64 0.22.1",
modified Cargo.toml
+1 −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.5.0", features = ["axum", "sea-orm", "password-reset"] }
51 +neiam-auth = { git = "https://gg.neiam.co/neiam/neiam-auth.git", tag = "v0.6.0", features = ["axum", "sea-orm", "password-reset", "app-login"] }
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" }
modified Justfile
+2 −1
@@ -90,11 +90,12 @@ check:
90 90 # their own rows with unique names, but use a scratch database, not your dev one:
91 91 # podman exec recourse_postgres createdb -U recourse_user recourse_test
92 92 test-db url="postgres://recourse_user:recourse_password@localhost:5439/recourse_test":
93 − TEST_DATABASE_URL={{url}} cargo test --test api_db
93 + TEST_DATABASE_URL={{url}} cargo test --test api_db --test app_login
94 94
95 95 # Re-capture the API responses the Android ContractTest decodes (needs the test database)
96 96 fixtures url="postgres://recourse_user:recourse_password@localhost:5439/recourse_test":
97 97 TEST_DATABASE_URL={{url}} WRITE_ANDROID_FIXTURES=1 cargo test --test api_db android_fixtures
98 + TEST_DATABASE_URL={{url}} WRITE_ANDROID_FIXTURES=1 cargo test --test app_login android_fixtures
98 99
99 100 # A fresh database (no users yet) is also migrated and given demo content
100 101 # (`seed --demo`); an existing one is left alone.
modified README.md
+60 −5
@@ -547,10 +547,17 @@ Details worth knowing:
547 547
548 548 `android/` is the phone client (Kotlin + Compose): browse in Classic or Focus
549 549 (the profile's Layout), read, reply, start topics, react, bookmark, watch,
550 −tags, search, notifications, profile. Pair it from **Devices** in the account
551 −menu, which shows a QR of `recourse://pair?base=…&token=…` once; the same page
552 −renames and revokes devices. Build with `just apk` (it finds a JDK 17–21 even
553 −when the system `java` is 25); see [`android/README.md`](android/README.md).
550 +tags, search, notifications, profile. It opens on a login page with three big
551 +buttons, each a bottom sheet: **Scan** (pair with the QR from **Devices** in
552 +the account menu, `recourse://pair?base=…&token=…`, shown once, or type the two
553 +fields; the camera runs only while that sheet is open), **Sign in with NEIAM**
554 +(in a Custom Tab, PKCE, back to the app through `recourse://auth`) and
555 +**Username** (the web password, [where the server allows
556 +it](#app-sign-in-no-qr)). A button the server has turned off is greyed out with
557 +the reason; the `?` opens a help page. All three end with a device token that
558 +**Devices** lists, renames and revokes. Build with `just apk` (it finds a JDK
559 +17–21 even when the system `java` is 25), `just apk-test` for the unit tests;
560 +see [`android/README.md`](android/README.md).
554 561
555 562 It talks to `/api/v1` ([`docs/api.md`](docs/api.md)), bearer tokens only, with
556 563 the web's visibility, trust, verification, view and read rules — the API calls
@@ -559,6 +566,54 @@ against a scratch database; `just fixtures` refreshes the responses the app's
559 566 contract test decodes. The plan, and what came out differently, is
560 567 [`docs/android.md`](docs/android.md).
561 568
569 +### App sign-in (no QR)
570 +
571 +Two more ways for the app to get the same kind of token, from
572 +[neiam-auth](https://gg.neiam.co/neiam/neiam-auth) v0.6.0's `app-login` feature
573 +(`src/auth/app_login.rs`). A token minted either way is an ordinary
574 +`api_tokens` row, as the pairing page mints one: only its hash stored, labelled
575 +with the phone's name, the 90-day sliding expiry, listed and revoked on
576 +**Devices**. These routes are unauthenticated and sit beside the bearer-only
577 +`/api/v1` nest; nothing of recourse's own lives under `/api/v1/auth`. Their
578 +errors are flat `{error, message}`, not the `/api/v1` envelope.
579 +
580 +| Route | Purpose |
581 +|---|---|
582 +| `GET /api/v1/auth/methods` | `{scan, oidc: {enabled, label, provider, login_path}, password, app_name, server}`: which buttons the app enables |
583 +| `POST /api/v1/auth/password` | `{username_or_email, password, device_name}` → `{token, token_id, label, expires_at, user: {id, username, email}}` |
584 +| `GET /auth/oidc/neiam/login?app=1&challenge=…&device_name=…` | "Sign in with NEIAM" from a Custom Tab; ends at `recourse://auth?code=…` |
585 +| `POST /api/v1/auth/oidc/exchange` | `{code, code_verifier, device_name}` → the same as the password answer |
586 +
587 +- **Password:** the web form's check (the username, or the email here too,
588 + case-insensitive) and its refusals: off with `OIDC_PASSWORD_LOGIN=false` or
589 + `OIDC_LOGIN_MODE=sso_strict` (403 `password_login_off`); a suspended account
590 + gets 403 with the login page's "This account is suspended until …"; under
591 + `sso`, an account linked to a `REQUIRED_FOR_LINKED` provider gets 403 "This
592 + account signs in with NEIAM." Those come only after the right password. A
593 + wrong password, an unknown user, a deactivated account and a NEIAM-only
594 + account all get the same 401, in the same time (a dummy argon2 check and a
595 + 400 ms floor). 5 tries per account name and 20 per address per 15 minutes
596 + (429), on the `TRUSTED_PROXIES`-resolved address. The password is never
597 + logged. recourse has no second factor, so there is nothing to step past.
598 + An unverified address can sign in (as on the web) and still can't write.
599 +- **NEIAM:** the app makes a PKCE pair and opens the normal OIDC login with
600 + `app=1`. The callback runs the web's link and provisioning policy (including
601 + its deactivated and suspended refusals and the trusted-email verification)
602 + but starts **no web session**; it stores a one-time code (SHA-256 only, 2
603 + minutes, bound to the user and the challenge, table `app_login_codes`) and
604 + redirects to `recourse://auth?code=…`. The exchange needs the verifier; any
605 + attempt, right or wrong, consumes the code. An account suspended or
606 + deactivated in between gets no token.
607 +- **Why a custom scheme, not an App Link:** the app talks to whichever server
608 + the user picks, and an App Link needs its host in the manifest at build time.
609 + A scheme can be claimed by another app, which then holds a code it can't
610 + redeem. There is no `assetlinks.json` to serve.
611 +- Env: nothing new is needed. `APP_LOGIN_REDIRECT_URI` overrides the scheme
612 + (set it empty to turn app NEIAM sign-in off); `APP_LOGIN_OIDC_PROVIDER` picks
613 + the provider (default: the first). The NEIAM client's redirect URI is the web
614 + callback's (`/auth/oidc/neiam/callback`), unchanged.
615 +- Tests: `tests/app_login.rs` (`just test-db` runs it with the API tests).
616 +
562 617 ## Registration
563 618
564 619 Invite-only by default: `/register` needs an invite link (`/register?token=…`,
@@ -658,7 +713,7 @@ The flow is [neiam-auth](https://gg.neiam.co/neiam/neiam-auth)'s
658 713
659 714 ## Sign in with NEIAM
660 715
661 −[`neiam-auth`](https://gg.neiam.co/neiam/neiam-auth) v0.4.0 (git dependency,
716 +[`neiam-auth`](https://gg.neiam.co/neiam/neiam-auth) v0.6.0 (git dependency,
662 717 pinned to the tag, `sea-orm` feature for sea-orm 1.1). **Off by default**: until `OIDC_ENABLED=true` there is no
663 718 button and `/auth/oidc/*` returns 404 (except `/auth/oidc/health`). The
664 719 `oidc_identities` migration always runs.
modified android/README.md
+30 −5
@@ -12,7 +12,7 @@ Android Studio ships both.
12 12 ```bash
13 13 # From the repo root:
14 14 just apk # -> android/app/build/outputs/apk/debug/app-debug.apk
15 −just apk-test # JVM unit tests: the pairing link, the API contract
15 +just apk-test # JVM unit tests: the pairing link, sign-in, the API contract
16 16 just adb-devices # is there anything to install onto?
17 17 just apk-install # onto a connected device / running emulator
18 18 ```
@@ -24,13 +24,38 @@ usual places. A system `java` that is 25, or a JRE with no compiler, fails as
24 24 like a project problem rather than a toolchain one. The SDK is
25 25 `android/local.properties` (gitignored) or `ANDROID_HOME`, else `~/Android/Sdk`.
26 26
27 +## Signing in
28 +
29 +The login page has the app's name over "login", a `?` (help) in the corner, and
30 +three buttons that share the rest of the screen: stacked in portrait, side by
31 +side in landscape (the header becomes a left column). Each opens a bottom
32 +sheet; back or a swipe down closes it. `ui/login/LoginScreen.kt`,
33 +`data/AppLogin.kt`; the theme is the app's own (Blueprint and the rest).
34 +
35 +- **Scan:** pairing, below.
36 +- **Sign in with NEIAM:** type the server once (remembered), and the app opens
37 + `<server>/auth/oidc/neiam/login?app=1&challenge=…` in a Custom Tab with a
38 + fresh PKCE pair. The server sends the browser back to
39 + `recourse://auth?code=…`; the app trades the code and its verifier at
40 + `POST /api/v1/auth/oidc/exchange` for a device token. The verifier waits in
41 + the encrypted prefs (ten minutes at most) in case Android kills the app while
42 + the browser is in front.
43 +- **Username:** server, username or email, password (autofill hints; Done
44 + submits) → `POST /api/v1/auth/password`.
45 +
46 +On a server the app has talked to, `GET /api/v1/auth/methods` decides which
47 +buttons are on; a turned-off one is greyed out and says why. With no answer
48 +(no server yet, an older server) all three stay on and their errors explain.
49 +The server side: "App sign-in (no QR)" in `../README.md`.
50 +
27 51 ## Pairing
28 52
29 53 1. On the web, sign in → account menu → **Devices** → *Pair a device*. The
30 54 page shows a QR of `recourse://pair?base=<origin>&token=<token>`, once.
31 −2. In the app, tap **Scan QR**. Or open the code from the phone's own camera
32 − or browser — the app handles `recourse://` and opens with both fields
33 − filled in. Or type the two fields.
55 +2. In the app, tap **Scan** and point the camera at it (the viewfinder runs
56 + only while the Scan sheet is open). Or open the code from the phone's own
57 + camera or browser — the app handles `recourse://` and opens the Scan sheet
58 + with both fields filled in. Or type the two fields.
34 59 3. The app calls `GET /api/v1/me` and stores the pair (in
35 60 `EncryptedSharedPreferences`, excluded from backups) only once that
36 61 answers, so a stale code fails here rather than as an empty forum later.
@@ -42,7 +67,7 @@ a laptop and you'll need to type the LAN address instead.
42 67 Unpair: **Me** → *Unpair this device* (revokes this device's token on the
43 68 server too). Revoke from elsewhere: **Devices** on the web, or **Me →
44 69 Devices** in the app. A token unused for 90 days expires. Any 401 sends the
45 −app back to pairing.
70 +app back to the login page.
46 71
47 72 ## What's here
48 73
modified android/app/build.gradle.kts
+2 −0

Click to load diff…

modified android/app/src/main/AndroidManifest.xml
+20 −0

Click to load diff…

modified android/app/src/main/java/org/neiam/recourse/app/MainActivity.kt
+74 −12

Click to load diff…

added android/app/src/main/java/org/neiam/recourse/app/data/AppLogin.kt
+191 −0

Click to load diff…

modified android/app/src/main/java/org/neiam/recourse/app/data/TokenStore.kt
+34 −2

Click to load diff…

modified android/app/src/main/java/org/neiam/recourse/app/ui/PairScreen.kt
+165 −93

Click to load diff…

added android/app/src/main/java/org/neiam/recourse/app/ui/login/Autofill.kt
+34 −0

Click to load diff…

added android/app/src/main/java/org/neiam/recourse/app/ui/login/LoginScreen.kt
+623 −0

Click to load diff…

added android/app/src/test/java/org/neiam/recourse/app/data/AppLoginTest.kt
+103 −0

Click to load diff…

modified android/app/src/test/java/org/neiam/recourse/app/data/ContractTest.kt
+27 −1

Click to load diff…

added android/app/src/test/resources/api/auth_error.json
+4 −0

Click to load diff…

added android/app/src/test/resources/api/auth_methods.json
+12 −0

Click to load diff…

added android/app/src/test/resources/api/auth_token.json
+11 −0

Click to load diff…

modified android/gradle/libs.versions.toml
+2 −0

Click to load diff…

modified docs/api.md
+7 −0

Click to load diff…

modified migration/Cargo.toml
+1 −1

Click to load diff…

modified migration/src/lib.rs
+2 −0

Click to load diff…

added src/auth/app_login.rs
+186 −0

Click to load diff…

modified src/auth/mod.rs
+1 −0

Click to load diff…

modified src/auth/oidc.rs
+37 −10

Click to load diff…

modified src/auth/password_reset.rs
+1 −1

Click to load diff…

modified src/main.rs
+10 −1

Click to load diff…

added tests/app_login.rs
+650 −0

Click to load diff…

Parents: 952e601