10.6 KiB · text History 3208487

Android log-in and App Links

The Android app’s log-in page and the /api/v1/auth/* API behind it. The shapes are shared with every NEIAM app (Elixir and Rust), so all their Android apps sign in the same way. The shared server parts live in neiam_auth (NeiamAuth.App, v0.5.0).

The log-in page

  • A header band (22% of the height; a left column in landscape) with the app’s name, “login” under it, and a ? that opens Help. The app’s own theme, colours and B612 typeface are kept.
  • Three equal buttons share the rest: Scan, OIDC (“Sign in with NEIAM”) and Username. Each opens its flow in a bottom sheet. Back or a swipe down closes it. Back from the page leaves the app.
  • A method the server has turned off is greyed out, with the server’s reason (GET /api/v1/auth/methods).
  • Scan is the existing pairing, unchanged: on the web, Settings → API tokens, tick Mobile app and create a token; the QR beside it, vigil://pair?base=…&token=…, holds the server and the token, and the app checks it with GET /api/0/. The camera runs only while the sheet is open. A vigil://pair link opened on the phone fills the sheet in; the user still taps Pair (it used to pair at once). A token short of the Mobile app scopes still pairs, as “Pair anyway” did.
  • OIDC opens the IdP in a Custom Tab (the system browser, never a WebView), with PKCE. See SSO.
  • Username: the email, with “Email me a sign-in link” first. Below it are a password field and “Sign in”, for accounts that have a password (Settings → password on the web).
  • The server: the last one used, else the build’s default (DEFAULT_SERVER, vigil.neiam.org), so the page loads at once. The OIDC and Username sheets show it as text with Change, which opens an editor already filled in with it: Use applies it, Reset to default goes back to vigil.neiam.org, and an address that isn’t one keeps the server in use with an error. Back closes the editor before the sheet. If the server can’t be reached, OIDC and Username still open, so it can be changed.
  • Help explains each way in, where the QR is, that emailed sign-in links open the app, and the server in use (changed in the sheets).

The code is android/app/src/main/java/org/neiam/vigil/app/auth/login/. It is the same in every NEIAM app except the package name. The app’s own glue is auth/VigilLoginHost.kt.

API

All under /api/v1. A sign-in answers with the same kind of token pairing uses: an API token with the Mobile app scopes (org:read project:read team:read member:read event:read event:write: reading, plus triage), labelled with the device’s name (Vigil.ApiTokens.issue_app_token/3). It is listed and revoked on Settings → API tokens like any other, and the app uses it on /api/0 exactly as a paired one.

{"token": "...", "token_type": "Bearer", "user": {"id": "...", "email": "..."}}

Errors are {"error": "<code>", "message": "..."}, and every answer is Cache-Control: no-store, exactly as the Rust apps’ (neiam-auth v0.6.0).

Endpoint Body Answers
GET /auth/methods {app_name, server, scan, oidc: {enabled, label, provider, login_path}, password, magic_link, login_mode, reasons}
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
POST /auth/magic_link {email} 202, always the same neutral answer; 403 password_login_off; 429
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
POST /auth/magic_link/check (bearer) {token} {valid, same_user}; never spends the link; 401 without a token
POST /auth/oidc/exchange {code, code_verifier, device_name} 200 token; 400 invalid_grant / bad_request; 429
GET /.well-known/assetlinks.json (site root) the Digital Asset Links statement

/api/0 and the pairing QR are unchanged.

What each path checks

They make exactly the web’s checks, with the web’s own functions:

  • Login mode (neiam_auth): password and magic links are off under OIDC_LOGIN_MODE=sso_strict (NeiamAuth.local_login_enabled?/0). Under sso, an account linked to a REQUIRED_FOR_LINKED provider is refused once it is identified (NeiamAuth.password_login_allowed?/1).
  • Password: Accounts.get_user_by_email_and_password/2, as the web log-in (UserSessionController). Confirmation isn’t checked, as on the web: in phx.gen.auth 1.8 an account only gets a password after it was confirmed. Vigil has no 2FA, and no other account kinds to refuse.
  • Magic-link request: the web form’s email (deliver_login_instructions with the web’s own /users/log-in/:token URL), and the same rules: nothing is sent to an unknown account or one that must use SSO, and the answer doesn’t say which.
  • Magic-link exchange: Accounts.login_user_by_magic_link/1, the web’s own. The link is single-use and expires as on the web (15 minutes). Following it confirms an unconfirmed account, which expires that account’s other sessions as on the web (UserAuth.disconnect_sessions). The login mode is checked before the token is spent, so a refused link stays usable. An unconfirmed account that has a password (phx.gen.auth raises there) is refused with unconfirmed.
  • SSO: neiam_auth’s own flow and the adapter’s rules (Vigil.NeiamAuthAdapter): OIDC_PROVISION=invite_only, group sync. A first NEIAM login with no account and no invite is refused in the browser, as on the web.
  • Rate limits (NeiamAuth.App.rate_limit/3, per node; the Rust crate’s): password 20 per IP and 5 per account per 15 min; each exchange 30 per IP per 15 min; magic-link requests 10 per IP and 5 per account per 15 min. This is not the ingest rate limiter, which is for events.
  • Passwords, tokens, codes and verifiers are kept out of the logs (:filter_parameters in config/config.exs).

The email is the web’s normal one. Its link, https://vigil.neiam.org/users/log-in/<token>, is a verified App Link. App Links name their host in the manifest, so this works for the production server, vigil.neiam.org, only; with any other server the app signs in by Scan, SSO or password, and its emailed links open in the browser as before.

  • App installed and verified: the link opens the app. Signed out, the app signs in and goes on to the organization. Signed in as the same user, it just carries on (/auth/magic_link/check). Signed in as someone else, it asks first.
  • An expired or used link shows “This sign-in link has expired or was already used”, with Email me a new link.
  • App not installed, or the email opened on a computer: the link opens the website and works exactly as before. Nothing about the web flow changed.

Magic links only ever as App Links, never a custom scheme: the token is a credential by itself. The app never fetches or prefetches a link; it acts only when the user opens one.

Signing out (Unpair this phone) forgets the token on the phone, as before; Vigil has no API to revoke it from the phone, so it stays listed on Settings → API tokens until revoked there.

SSO from the app

  1. The app makes a PKCE verifier and S256 challenge and opens a Custom Tab at /auth/oidc/neiam/login?app=1&challenge=<S256>&device_name=<name>.
  2. The usual OIDC flow runs, with the usual redirect URI (https://vigil.neiam.org/auth/oidc/neiam/callback). Authelia needs no change.
  3. The callback starts no web session. neiam_auth stores a one-time code (random, SHA-256 at rest, bound to the user and the challenge, 2 minutes) and redirects to vigil://auth?code=..., the app’s own scheme (the same one as its vigil://pair links). A refusal shows an error page in the browser tab instead.
  4. The app posts the code and its verifier to /auth/oidc/exchange, on the server the sign-in was started on. The code works once. An expired code or a wrong verifier is refused, and a wrong verifier burns the code.

A custom scheme, as in the Rust apps, because the app talks to whichever server the user picked, and an App Link must name its host at build time. Another app claiming vigil:// would get a code it can’t spend: the PKCE verifier never leaves this app.

Deploying

  • Migration: create_neiam_auth_app_codes (neiam_auth v0.5.0). It runs with the release’s usual migrate step.
  • Env:
    • ANDROID_CERT_FINGERPRINTS: the SHA-256 fingerprint(s) of the APK signing certificate(s), comma-separated. Without it, /.well-known/assetlinks.json is [] and every link opens in the browser, as before. Get it from the release keystore (the CI secret ANDROID_KEYSTORE, base64):

      keytool -list -v -keystore release.jks -alias vigil | grep SHA256

      Add the debug key’s fingerprint (~/.android/debug.keystore) too while testing debug builds.

    • APP_LOGIN_REDIRECT_URI (optional): where SSO in the app hands its code over; default vigil://auth (config/config.exs). Empty turns SSO in the app off. APP_LOGIN_OIDC_PROVIDER (optional): the provider the app’s button uses; default the first one.

    • OIDC_* is as before; ANDROID_APP_PACKAGE is optional (the package, org.neiam.vigil.app, is set in config/config.exs).

    • Mail: “Email me a sign-in link” is offered only when mail really leaves (mail-prism or SMTP_RELAY); with MAIL_PROVIDER=log it is greyed out.

  • Ingress: /.well-known/assetlinks.json must reach Phoenix at https://vigil.neiam.org/.well-known/assetlinks.json, over HTTPS, with no redirect and no auth in front. The existing catch-all ingress for the host (/ to vigil-svc) does that; /api/v1/auth/* likewise.
  • Client IPs: the per-IP limits key on conn.remote_ip. Vigil reads no X-Forwarded-For, so behind Traefik every client shares the proxy’s address and the per-IP limits act as server-wide ones (the per-account limits are unaffected).
  • Authelia: nothing. The app reuses the web’s redirect URI.
  • Android: the App Link host is appLinksHost in android/app/build.gradle.kts (vigil.neiam.org). It is also the default server of a fresh install.
  • Verifying: after installing a release build, adb shell pm get-app-links org.neiam.vigil.app should say verified for vigil.neiam.org.