neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Sign-in API for the Android app: /api/v1/auth/* (neiam_auth v0.5.0)
83f70ee · gmorell · 2026-12-03 18:05
Message
{commit_body(@commit)}
Files changed
modified
README.md
+9
−1
| 38 | 38 | OIDC or LDAP groups (NEIAM). There are API tokens and a Sentry-shaped |
| 39 | 39 | `/api/0` for project, key and issue management. |
| 40 | 40 | - **Android app**: a read-only client for issues, events and monitors, |
| 41 | − paired by scanning a QR from the API tokens page. | |
| 41 | + paired by scanning a QR from the API tokens page, or signed in with | |
| 42 | + NEIAM, a password or an emailed link. | |
| 42 | 43 | |
| 43 | 44 | ROADMAP.md is the design document: the wire contract, the architecture, |
| 44 | 45 | and what each slice built, as built. |
| 111 | 112 | token signs the phone out. CI publishes signed builds to the forge's |
| 112 | 113 | F-Droid repository. See android/README.md. |
| 113 | 114 | |
| 115 | +The app can also sign in with NEIAM (OIDC in the system browser, with | |
| 116 | +PKCE) or by email (a sign-in link, or a password), through `/api/v1/auth/*`; | |
| 117 | +each way in yields the same read-only token. Emailed sign-in links open the | |
| 118 | +app when it is installed. The API, the security decisions and the deploy | |
| 119 | +notes (`ANDROID_CERT_FINGERPRINTS`, `/.well-known/assetlinks.json`, | |
| 120 | +`APP_LOGIN_REDIRECT_URI`) are in [docs/app-login.md](docs/app-login.md). | |
| 121 | + | |
| 114 | 122 | ## Deploy |
| 115 | 123 | |
| 116 | 124 | One OCI image: `podman build --format docker -t vigil .`, or `docker build`. |
modified
config/config.exs
+15
−0
| 37 | 37 | user_schema: Vigil.Accounts.User, |
| 38 | 38 | adapter: Vigil.NeiamAuthAdapter |
| 39 | 39 | |
| 40 | +# The Android app's sign-in (neiam_auth's NeiamAuth.App, docs/app-login.md): | |
| 41 | +# the package /.well-known/assetlinks.json vouches for (the signing-cert | |
| 42 | +# fingerprints come from ANDROID_CERT_FINGERPRINTS at runtime). | |
| 43 | +config :neiam_auth, :app, | |
| 44 | + app_name: "Vigil", | |
| 45 | + android_package: "org.neiam.vigil.app", | |
| 46 | + # SSO in the app hands its one-time code back to the app's own scheme, | |
| 47 | + # the one its vigil://pair links use (APP_LOGIN_REDIRECT_URI overrides; | |
| 48 | + # empty turns it off). | |
| 49 | + redirect_uri: "vigil://auth" | |
| 50 | + | |
| 51 | +# Credentials in request params stay out of the logs: passwords, magic-link, | |
| 52 | +# invite and API tokens, the SSO callback's code and the app's PKCE verifier. | |
| 53 | +config :phoenix, :filter_parameters, ["password", "token", "code", "code_verifier"] | |
| 54 | + | |
| 40 | 55 | # Background work, all of it (ROADMAP.md, Architecture). Queues are sized |
| 41 | 56 | # per kind of work; crontab entries are added by the slices that need them. |
| 42 | 57 | config :vigil, Oban, |
added
docs/app-login.md
+186
−0
| 1 | +# Android log-in and App Links | |
| 2 | + | |
| 3 | +The Android app's log-in page and the `/api/v1/auth/*` API behind it. The | |
| 4 | +shapes are shared with every NEIAM app (Elixir and Rust), so all their | |
| 5 | +Android apps sign in the same way. The shared server parts live in | |
| 6 | +neiam_auth (`NeiamAuth.App`, v0.5.0). | |
| 7 | + | |
| 8 | +## The log-in page | |
| 9 | + | |
| 10 | +- A header band (22% of the height; a left column in landscape) with the | |
| 11 | + app's name, "login" under it, and a `?` that opens Help. The app's own | |
| 12 | + theme, colours and B612 typeface are kept. | |
| 13 | +- Three equal buttons share the rest: **Scan**, **OIDC** ("Sign in with | |
| 14 | + NEIAM") and **Username**. Each opens its flow in a bottom sheet. Back or a | |
| 15 | + swipe down closes it. Back from the page leaves the app. | |
| 16 | +- A method the server has turned off is greyed out, with the server's | |
| 17 | + reason (`GET /api/v1/auth/methods`). | |
| 18 | +- **Scan** is the existing pairing, unchanged: on the web, Settings → API | |
| 19 | + tokens, tick **Mobile app** and create a token; the QR beside it, | |
| 20 | + `vigil://pair?base=…&token=…`, holds the server and the token, and the | |
| 21 | + app checks it with `GET /api/0/`. The camera runs only while the sheet is | |
| 22 | + open. A `vigil://pair` link opened on the phone fills the sheet in; the | |
| 23 | + user still taps Pair (it used to pair at once). A token short of the | |
| 24 | + Mobile app scopes still pairs, as "Pair anyway" did. | |
| 25 | +- **OIDC** opens the IdP in a Custom Tab (the system browser, never a | |
| 26 | + WebView), with PKCE. See [SSO](#sso-from-the-app). | |
| 27 | +- **Username**: the email, with "Email me a sign-in link" first. Below it | |
| 28 | + are a password field and "Sign in", for accounts that have a password | |
| 29 | + (Settings → password on the web). | |
| 30 | +- **Help** explains each way in, where the QR is, that emailed sign-in links | |
| 31 | + open the app, and the server in use (which can be changed there). | |
| 32 | + | |
| 33 | +The code is `android/app/src/main/java/org/neiam/vigil/app/auth/login/`. | |
| 34 | +It is the same in every NEIAM app except the package name. The app's own | |
| 35 | +glue is `auth/VigilLoginHost.kt`. | |
| 36 | + | |
| 37 | +## API | |
| 38 | + | |
| 39 | +All under `/api/v1`. A sign-in answers with the same kind of token pairing | |
| 40 | +uses: an API token with the Mobile app scopes (`org:read project:read | |
| 41 | +team:read member:read event:read`, read only), labelled with the device's | |
| 42 | +name (`Vigil.ApiTokens.issue_app_token/3`). It is listed and revoked on | |
| 43 | +Settings → API tokens like any other, and the app uses it on `/api/0` | |
| 44 | +exactly as a paired one. | |
| 45 | + | |
| 46 | +```json | |
| 47 | +{"token": "...", "token_type": "Bearer", "user": {"id": "...", "email": "..."}} | |
| 48 | +``` | |
| 49 | + | |
| 50 | +Errors are `{"error": "<code>", "message": "..."}`, and every answer is | |
| 51 | +`Cache-Control: no-store`, exactly as the Rust apps' (neiam-auth v0.6.0). | |
| 52 | + | |
| 53 | +| Endpoint | Body | Answers | | |
| 54 | +|---|---|---| | |
| 55 | +| `GET /auth/methods` | | `{app_name, server, scan, oidc: {enabled, label, provider, login_path}, password, magic_link, login_mode, reasons}` | | |
| 56 | +| `POST /auth/password` | `{username_or_email, password, device_name}` (`login` is taken too) | 200 token; 401 `invalid_credentials` (the same for an unknown email, a wrong password and an account with no password, and at least 400 ms either way); 403 `refused` (must use SSO) / `password_login_off` (sso_strict); 400 `bad_request`; 429 `rate_limited` | | |
| 57 | +| `POST /auth/magic_link` | `{email}` | 202, always the same neutral answer; 403 `password_login_off`; 429 | | |
| 58 | +| `POST /auth/magic_link/exchange` | `{token, device_name}` | 200 token (plus `confirmed: true` when the link confirmed the account); 400 `invalid_link`; 403 `refused` / `unconfirmed` / `password_login_off`; 429 | | |
| 59 | +| `POST /auth/magic_link/check` (bearer) | `{token}` | `{valid, same_user}`; never spends the link; 401 without a token | | |
| 60 | +| `POST /auth/oidc/exchange` | `{code, code_verifier, device_name}` | 200 token; 400 `invalid_grant` / `bad_request`; 429 | | |
| 61 | +| `GET /.well-known/assetlinks.json` (site root) | | the Digital Asset Links statement | | |
| 62 | + | |
| 63 | +`/api/0` and the pairing QR are unchanged. | |
| 64 | + | |
| 65 | +### What each path checks | |
| 66 | + | |
| 67 | +They make exactly the web's checks, with the web's own functions: | |
| 68 | + | |
| 69 | +- **Login mode** (neiam_auth): password and magic links are off under | |
| 70 | + `OIDC_LOGIN_MODE=sso_strict` (`NeiamAuth.local_login_enabled?/0`). Under | |
| 71 | + `sso`, an account linked to a `REQUIRED_FOR_LINKED` provider is refused | |
| 72 | + once it is identified (`NeiamAuth.password_login_allowed?/1`). | |
| 73 | +- **Password**: `Accounts.get_user_by_email_and_password/2`, as the web | |
| 74 | + log-in (`UserSessionController`). Confirmation isn't checked, as on the | |
| 75 | + web: in phx.gen.auth 1.8 an account only gets a password after it was | |
| 76 | + confirmed. Vigil has no 2FA, and no other account kinds to refuse. | |
| 77 | +- **Magic-link request**: the web form's email (`deliver_login_instructions` | |
| 78 | + with the web's own `/users/log-in/:token` URL), and the same rules: nothing | |
| 79 | + is sent to an unknown account or one that must use SSO, and the answer | |
| 80 | + doesn't say which. | |
| 81 | +- **Magic-link exchange**: `Accounts.login_user_by_magic_link/1`, the web's | |
| 82 | + own. The link is single-use and expires as on the web (15 minutes). | |
| 83 | + Following it confirms an unconfirmed account, which expires that | |
| 84 | + account's other sessions as on the web (`UserAuth.disconnect_sessions`). | |
| 85 | + The login mode is checked before the token is spent, so a refused link | |
| 86 | + stays usable. An unconfirmed account that has a password (phx.gen.auth | |
| 87 | + raises there) is refused with `unconfirmed`. | |
| 88 | +- **SSO**: neiam_auth's own flow and the adapter's rules | |
| 89 | + (`Vigil.NeiamAuthAdapter`): `OIDC_PROVISION=invite_only`, group sync. A | |
| 90 | + first NEIAM login with no account and no invite is refused in the | |
| 91 | + browser, as on the web. | |
| 92 | +- **Rate limits** (`NeiamAuth.App.rate_limit/3`, per node; the Rust | |
| 93 | + crate's): password 20 per IP and 5 per account per 15 min; each exchange | |
| 94 | + 30 per IP per 15 min; magic-link requests 10 per IP and 5 per account per | |
| 95 | + 15 min. This is not the ingest rate limiter, which is for events. | |
| 96 | +- Passwords, tokens, codes and verifiers are kept out of the logs | |
| 97 | + (`:filter_parameters` in config/config.exs). | |
| 98 | + | |
| 99 | +## Magic links in the app | |
| 100 | + | |
| 101 | +The email is the web's normal one. Its link, | |
| 102 | +`https://vigil.neiam.org/users/log-in/<token>`, is a verified App Link. App | |
| 103 | +Links name their host in the manifest, so this works for the production | |
| 104 | +server, **vigil.neiam.org**, only; with any other server the app signs in | |
| 105 | +by Scan, SSO or password, and its emailed links open in the browser as | |
| 106 | +before. | |
| 107 | + | |
| 108 | +- App installed and verified: the link opens the app. Signed out, the app | |
| 109 | + signs in and goes on to the organization. Signed in as the same user, it | |
| 110 | + just carries on (`/auth/magic_link/check`). Signed in as someone else, it | |
| 111 | + asks first. | |
| 112 | +- An expired or used link shows "This sign-in link has expired or was | |
| 113 | + already used", with **Email me a new link**. | |
| 114 | +- App not installed, or the email opened on a computer: the link opens the | |
| 115 | + website and works exactly as before. Nothing about the web flow changed. | |
| 116 | + | |
| 117 | +Magic links only ever as App Links, never a custom scheme: the token is a | |
| 118 | +credential by itself. The app never fetches or prefetches a link; it acts | |
| 119 | +only when the user opens one. | |
| 120 | + | |
| 121 | +Signing out (**Unpair this phone**) forgets the token on the phone, as | |
| 122 | +before; Vigil has no API to revoke it from the phone, so it stays listed | |
| 123 | +on Settings → API tokens until revoked there. | |
| 124 | + | |
| 125 | +## SSO from the app | |
| 126 | + | |
| 127 | +1. The app makes a PKCE verifier and S256 challenge and opens a Custom Tab | |
| 128 | + at `/auth/oidc/neiam/login?app=1&challenge=<S256>&device_name=<name>`. | |
| 129 | +2. The usual OIDC flow runs, with the usual redirect URI | |
| 130 | + (`https://vigil.neiam.org/auth/oidc/neiam/callback`). **Authelia needs | |
| 131 | + no change.** | |
| 132 | +3. The callback starts no web session. neiam_auth stores a one-time code | |
| 133 | + (random, SHA-256 at rest, bound to the user and the challenge, 2 minutes) | |
| 134 | + and redirects to `vigil://auth?code=...`, the app's own scheme (the same | |
| 135 | + one as its `vigil://pair` links). A refusal shows an error page in the | |
| 136 | + browser tab instead. | |
| 137 | +4. The app posts the code and its verifier to `/auth/oidc/exchange`, on the | |
| 138 | + server the sign-in was started on. The code works once. An expired code | |
| 139 | + or a wrong verifier is refused, and a wrong verifier burns the code. | |
| 140 | + | |
| 141 | +A custom scheme, as in the Rust apps, because the app talks to whichever | |
| 142 | +server the user picked, and an App Link must name its host at build time. | |
| 143 | +Another app claiming `vigil://` would get a code it can't spend: the PKCE | |
| 144 | +verifier never leaves this app. | |
| 145 | + | |
| 146 | +## Deploying | |
| 147 | + | |
| 148 | +- **Migration**: `create_neiam_auth_app_codes` (neiam_auth v0.5.0). It runs | |
| 149 | + with the release's usual migrate step. | |
| 150 | +- **Env**: | |
| 151 | + - `ANDROID_CERT_FINGERPRINTS`: the SHA-256 fingerprint(s) of the APK | |
| 152 | + signing certificate(s), comma-separated. Without it, | |
| 153 | + `/.well-known/assetlinks.json` is `[]` and every link opens in the | |
| 154 | + browser, as before. Get it from the release keystore (the CI secret | |
| 155 | + `ANDROID_KEYSTORE`, base64): | |
| 156 | + | |
| 157 | + ```sh | |
| 158 | + keytool -list -v -keystore release.jks -alias vigil | grep SHA256 | |
| 159 | + ``` | |
| 160 | + | |
| 161 | + Add the debug key's fingerprint (`~/.android/debug.keystore`) too while | |
| 162 | + testing debug builds. | |
| 163 | + - `APP_LOGIN_REDIRECT_URI` (optional): where SSO in the app hands its | |
| 164 | + code over; default `vigil://auth` (config/config.exs). Empty turns SSO | |
| 165 | + in the app off. `APP_LOGIN_OIDC_PROVIDER` (optional): the provider the | |
| 166 | + app's button uses; default the first one. | |
| 167 | + - `OIDC_*` is as before; `ANDROID_APP_PACKAGE` is optional (the package, | |
| 168 | + `org.neiam.vigil.app`, is set in config/config.exs). | |
| 169 | + - Mail: "Email me a sign-in link" is offered only when mail really | |
| 170 | + leaves (mail-prism or `SMTP_RELAY`); with `MAIL_PROVIDER=log` it is | |
| 171 | + greyed out. | |
| 172 | +- **Ingress**: `/.well-known/assetlinks.json` must reach Phoenix at | |
| 173 | + `https://vigil.neiam.org/.well-known/assetlinks.json`, over HTTPS, with | |
| 174 | + no redirect and no auth in front. The existing catch-all ingress for the | |
| 175 | + host (`/` to `vigil-svc`) does that; `/api/v1/auth/*` likewise. | |
| 176 | +- **Client IPs**: the per-IP limits key on `conn.remote_ip`. Vigil reads | |
| 177 | + no `X-Forwarded-For`, so behind Traefik every client shares the proxy's | |
| 178 | + address and the per-IP limits act as server-wide ones (the per-account | |
| 179 | + limits are unaffected). | |
| 180 | +- **Authelia**: nothing. The app reuses the web's redirect URI. | |
| 181 | +- **Android**: the App Link host is `appLinksHost` in | |
| 182 | + `android/app/build.gradle.kts` (`vigil.neiam.org`). It is also the | |
| 183 | + default server of a fresh install. | |
| 184 | +- **Verifying**: after installing a release build, | |
| 185 | + `adb shell pm get-app-links org.neiam.vigil.app` should say `verified` for | |
| 186 | + `vigil.neiam.org`. |
modified
docs/deploy.md
+10
−0
| 167 | 167 | Off unless `OIDC_ENABLED=true`. `OIDC_LOGIN_MODE`, `PUBLIC_URL`, |
| 168 | 168 | `OIDC_PROVIDERS` and the per-provider `OIDC_*` variables are neiam_auth's; |
| 169 | 169 | see its README (`deps/neiam_auth/README.md`). |
| 170 | + | |
| 171 | +### The Android app's sign-in | |
| 172 | + | |
| 173 | +| Variable | Default | | | |
| 174 | +|---|---|---| | |
| 175 | +| `ANDROID_CERT_FINGERPRINTS` | unset | SHA-256 fingerprints of the APK signing certificates, comma-separated, for `/.well-known/assetlinks.json`. Unset: emailed sign-in links open in the browser | | |
| 176 | +| `APP_LOGIN_REDIRECT_URI` | `vigil://auth` | Where "Sign in with NEIAM" in the app hands its one-time code. Empty turns SSO in the app off | | |
| 177 | +| `APP_LOGIN_OIDC_PROVIDER` | the first provider | The provider the app's SSO button uses | | |
| 178 | + | |
| 179 | +See [app-login.md](app-login.md). |
modified
lib/vigil/api_tokens.ex
+23
−0
| 36 | 36 | %ApiToken{user_id: user_id} |> ApiToken.create_changeset(attrs) |> Repo.insert() |
| 37 | 37 | end |
| 38 | 38 | |
| 39 | + @doc """ | |
| 40 | + Signs the Android app in: a token exactly as pairing makes one (the | |
| 41 | + Mobile app scopes, `mobile_scopes/0`), labelled with the device's name, | |
| 42 | + in the body every `/api/v1/auth/*` sign-in answers with. It is listed and | |
| 43 | + revoked on Settings → API tokens like any other. `method` (`:password`, | |
| 44 | + `:magic_link`, `:oidc`) is the caller's; the token is the same. | |
| 45 | + Used by `VigilWeb.Api.V1.AuthController` and `NeiamAuth.App.ExchangePlug`. | |
| 46 | + """ | |
| 47 | + def issue_app_token(%User{} = user, device_name, _method) do | |
| 48 | + case create_token(user, %{label: device_name, scopes: @mobile_scopes}) do | |
| 49 | + {:ok, token} -> | |
| 50 | + {:ok, | |
| 51 | + %{ | |
| 52 | + token: token.token, | |
| 53 | + token_type: "Bearer", | |
| 54 | + user: %{id: user.id, email: user.email} | |
| 55 | + }} | |
| 56 | + | |
| 57 | + {:error, _changeset} -> | |
| 58 | + {:error, :refused, "Couldn't make a token for this device."} | |
| 59 | + end | |
| 60 | + end | |
| 61 | + | |
| 39 | 62 | @doc "Revokes one of the user's tokens." |
| 40 | 63 | def delete_token(%User{id: user_id}, token_id) do |
| 41 | 64 | with {:ok, id} <- Ecto.UUID.cast(token_id), |
modified
lib/vigil/mailer.ex
+9
−0
Click to load diff…
added
lib/vigil_web/api/v1/auth_controller.ex
+224
−0
Click to load diff…
modified
lib/vigil_web/router.ex
+27
−4
Click to load diff…
modified
mix.exs
+1
−1
Click to load diff…
modified
mix.lock
+1
−1
Click to load diff…
added
priv/repo/migrations/20261203175928_create_neiam_auth_app_codes.exs
+22
−0
Click to load diff…
added
test/vigil_web/api/v1/auth_controller_test.exs
+558
−0
Click to load diff…
added
test/vigil_web/assetlinks_test.exs
+35
−0
Click to load diff…
Parents: 6a2ac0f