9.6 KiB · text History 8849444

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.
  • Three equal buttons share the rest: Pair, 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).
  • Pair is the existing pairing, unchanged: Settings → Connected devices → Pair a device on the web shows the server and a code like XK4F-9TQ2, which the app trades for its device key (POST /api/v1/pair). CartoSnap has no QR code, so this sheet takes the two typed in; it is the shared page’s “Scan” sheet, without the camera.
  • 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 set a password (Settings on the web).
  • Help explains each way in, where the pairing code is, that emailed sign-in links open the app, and the server in use (which can be changed there).

The code is android/app/src/main/java/org/neiam/cartosnap/app/auth/login/. It is the same as in the other NEIAM apps except the package name and the Pair sheet (code instead of QR). The app’s own glue is auth/CartosnapLoginHost.kt.

API

All under /api/v1. A sign-in answers with a device key: the same key pairing yields (Accounts.create_api_token/2), labelled with the device’s name. It is listed and can be unpaired under Settings → Connected devices, and lapses after 90 days unused, 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). (The rest of the app API keeps its own {"error": {"code", "message"}}.)

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 key; 401 invalid_credentials (the same for an unknown email and a wrong 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 key (plus confirmed: true when the link confirmed the account); 400 invalid_link; 403 refused / unconfirmed / password_login_off; 429
POST /auth/magic_link/check (device key) {token} {valid, same_user}; never spends the link
POST /auth/oidc/exchange {code, code_verifier, device_name} 200 key; 400 invalid_grant / bad_request; 429
GET /.well-known/assetlinks.json (site root) the Digital Asset Links statement

POST /pair, GET /me and PUT/DELETE /device are unchanged.

What each path checks

They make exactly the web’s checks, with the web’s own functions (UserSessionController, UserLive.Login):

  • 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, through CartosnapApp.NeiamAuthAdapter.sso_required?/1, which has no rule of its own).
  • Password: Accounts.get_user_by_email_and_password/2, as the web log-in (an unknown email still costs a dummy hash). Confirmation isn’t checked, as on the web: an account only gets a password from Settings, after it was confirmed. CartoSnap has no 2FA and no admin or guest rule.
  • 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. Following it confirms an unconfirmed account, which expires that account’s other sessions and keys as on the web (UserAuth.disconnect_sessions/1). 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.
  • Registration stays invite-only: none of these make an account. SSO from the app follows CartosnapApp.NeiamAuthAdapter as the web does (OIDC_PROVISION, OIDC_PROVISION_GROUPS); a login refused as not invited shows the refusal in the browser tab, and the invite flow stays 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. CartoSnap had no limiter of its own.
  • 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://cs.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, cs.neiam.org, only; with any other server the app signs in by pairing, 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 home. Signed in as the same user, it just goes home (/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.

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://cs.neiam.org/auth/oidc/neiam/callback, OIDC_REDIRECT_URI in the deployment). 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 cartosnap://auth?code=..., the app’s own scheme. 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 cartosnap:// 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, see android/README.md):

      keytool -list -v -keystore release.jks | 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 cartosnap://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_*, CARTOSNAP_PUBLIC_URL (the server in /auth/methods, as in the pairing panel) and the mail settings are as before; without SMTP_RELAY the app’s “Email me a sign-in link” is off. ANDROID_APP_PACKAGE is optional (the package is set in config/config.exs).

  • Ingress: /.well-known/assetlinks.json must reach Phoenix at https://cs.neiam.org/.well-known/assetlinks.json, over HTTPS, with no redirect and no auth in front. The existing catch-all ingress for the host (path /) does that.
  • Authelia: nothing. The app reuses the web’s redirect URI.
  • Android: the App Link host is appLinksHost in android/app/build.gradle.kts (cs.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.cartosnap.app should say verified for cs.neiam.org.