Android: a new log-in page (Pair, OIDC, Username) and sign-in App Links

cf032d1 · gmorell · 2026-12-03 17:55

41 files +1995 -14
Message
{commit_body(@commit)}

Files changed

modified android/DESCRIPTION.md
+1 −1
@@ -2,4 +2,4 @@ Share photos from your phone straight into your CartoSnap trips.
2 2
3 3 The phone companion to CartoSnap, the shared photo map. Share photos from your gallery or camera and pick the trip to add them to, or send them straight into the trip you used last. Uploads carry on in the background through lost connections, restarts and the app being closed, and nothing is uploaded twice.
4 4
5 −Pair the phone with a code from CartoSnap's web settings (Settings → Connected devices).
5 +Sign in with your NEIAM account, an emailed sign-in link or your password, or pair the phone with a code from CartoSnap's web settings (Settings → Connected devices).
modified android/README.md
+26 −8
@@ -8,7 +8,7 @@ Two entries in the share sheet:
8 8 * **Add to last trip**: straight into the trip you used last, no
9 9 questions. The first time, with no last trip yet, it asks.
10 10
11 −The app's own screen pairs the phone, shows the upload queue (with failed
11 +The app's own screen signs the phone in, shows the upload queue (with failed
12 12 photos to retry or discard), and lets you change the last trip.
13 13
14 14 ## Uploads that don't get lost
@@ -47,12 +47,27 @@ and Google offers no way for other apps to read it. Photos still without
47 47 one say so when shared; set them on the web (the place search on a
48 48 photo's page). See `upload/Originals.kt`.
49 49
50 −## Pairing
50 +## Signing in
51 51
52 −1. On the web: **Settings → Connected devices → Pair a device**. It shows
53 − the server's address and a code like `XK4F-9TQ2` (one use, ten
54 − minutes).
55 −2. In the app: enter both, and **Pair**.
52 +The log-in page has three ways in, each in a bottom sheet (`?` opens Help):
53 +
54 +* **Pair**, as before:
55 + 1. On the web: **Settings → Connected devices → Pair a device**. It shows
56 + the server's address and a code like `XK4F-9TQ2` (one use, ten
57 + minutes).
58 + 2. In the app: tap **Pair**, enter both, and **Pair**.
59 +* **OIDC**: "Sign in with NEIAM" in the system browser (a Custom Tab, with
60 + PKCE), handed back to the app on `cartosnap://auth`.
61 +* **Username**: email me a sign-in link, or a password (for accounts that
62 + set one). The emailed link, `https://cs.neiam.org/users/log-in/<token>`,
63 + opens the app as a verified App Link; that works for cs.neiam.org only,
64 + every other way in works with any server.
65 +
66 +Each gives the phone the same kind of key, listed under Connected devices.
67 +The API, the security decisions and the server's deploy notes
68 +(`ANDROID_CERT_FINGERPRINTS`, `/.well-known/assetlinks.json`) are in
69 +[../docs/app-login.md](../docs/app-login.md). The page itself is
70 +`auth/login/` (shared with the sibling apps) and `auth/CartosnapLoginHost.kt`.
56 71
57 72 A dev server on `localhost` isn't reachable from the phone: set
58 73 `CARTOSNAP_PUBLIC_URL` to the laptop's LAN address (e.g.
@@ -68,7 +83,7 @@ Needs a JDK 17–21 (Android Studio's `jbr/` will do) and the Android SDK
68 83 cd android
69 84 JAVA_HOME=~/.local/share/JetBrains/Toolbox/apps/android-studio/jbr ./gradlew assembleDebug
70 85 # -> app/build/outputs/apk/debug/app-debug.apk
71 −./gradlew testDebugUnitTest # the queue, error handling and API client, no device needed
86 +./gradlew testDebugUnitTest # the queue, error handling, API client and log-in parsing, no device needed
72 87 ./gradlew installDebug # onto a connected phone
73 88 ```
74 89
@@ -82,6 +97,7 @@ All under `/api/v1`, with `Authorization: Bearer <key>` except pairing:
82 97 | | |
83 98 |---|---|
84 99 | `POST /pair` | `{code, label}` → `{token, user}` |
100 +| `/auth/*` | the log-in page: methods, password, magic links, the SSO exchange (docs/app-login.md) |
85 101 | `GET /me` | the user |
86 102 | `GET /trips` | trips you can add to |
87 103 | `POST /photos` | multipart `photo`, `client_ref`, `trip_id?` → `201` new, `200` already had it |
@@ -98,7 +114,9 @@ repository secrets, and skips publishing without them:
98 114 * `ANDROID_KEYSTORE_PASS`: its password
99 115
100 116 Make the keystore once and keep it: Android checks its certificate on every
101 −update, so losing it means uninstalling and reinstalling by hand.
117 +update, so losing it means uninstalling and reinstalling by hand. Its
118 +SHA-256 fingerprint goes in the server's `ANDROID_CERT_FINGERPRINTS`, so
119 +emailed sign-in links open the app (`keytool -list -v -keystore release.jks`).
102 120
103 121 ```sh
104 122 keytool -genkeypair -v -keystore release.jks -storetype PKCS12 \
modified android/app/build.gradle.kts
+9 −2
@@ -28,8 +28,13 @@ android {
28 28 versionCode = (System.getenv("GITHUB_RUN_NUMBER") ?: "").toIntOrNull() ?: 1
29 29 versionName = appVersionName
30 30
31 − // Where pairing starts out pointing; the field can be changed.
32 − buildConfigField("String", "DEFAULT_SERVER", "\"https://cs.neiam.org\"")
31 + // The server a fresh install signs in to (it can be changed), and the
32 + // host whose magic links open this app as verified App Links. The
33 + // server's /.well-known/assetlinks.json must list this build's
34 + // signing certificate (docs/app-login.md).
35 + val appLinksHost = "cs.neiam.org"
36 + manifestPlaceholders["appLinksHost"] = appLinksHost
37 + buildConfigField("String", "DEFAULT_SERVER", "\"https://$appLinksHost\"")
33 38 }
34 39
35 40 buildTypes {
@@ -92,6 +97,8 @@ dependencies {
92 97 implementation(libs.kotlinx.serialization.json)
93 98 implementation(libs.okhttp)
94 99 implementation(libs.coil.compose)
100 + // Custom Tabs: SSO sign-in runs in the system browser, never a WebView.
101 + implementation(libs.androidx.browser)
95 102
96 103 testImplementation(libs.junit)
97 104 }
modified android/app/src/main/AndroidManifest.xml
+35 −0
@@ -38,6 +38,41 @@
38 38 <action android:name="android.intent.action.MAIN"/>
39 39 <category android:name="android.intent.category.LAUNCHER"/>
40 40 </intent-filter>
41 +
42 + <!--
43 + cartosnap://auth?code=… — the end of a "Sign in with NEIAM" in
44 + the browser (Custom Tab): the server's one-time code, useless
45 + without the PKCE verifier that never leaves the app. A custom
46 + scheme because the server is whichever the user picked, and
47 + an App Link would have to name its host here.
48 + -->
49 + <intent-filter android:autoVerify="false">
50 + <action android:name="android.intent.action.VIEW"/>
51 + <category android:name="android.intent.category.DEFAULT"/>
52 + <category android:name="android.intent.category.BROWSABLE"/>
53 + <data android:scheme="cartosnap" android:host="auth"/>
54 + </intent-filter>
55 +
56 + <!--
57 + Magic links, as verified App Links (autoVerify), on the
58 + production host only (appLinksHost in app/build.gradle.kts):
59 + Android opens them in this app only when
60 + https://<host>/.well-known/assetlinks.json lists this
61 + package and its signing certificate (ANDROID_CERT_FINGERPRINTS
62 + on the server). Otherwise they open in the browser, where
63 + they work as before. Never a custom scheme: the token is a
64 + credential by itself.
65 +
66 + /users/log-in/<token> the web's magic link (phx.gen.auth)
67 + -->
68 + <intent-filter android:autoVerify="true">
69 + <action android:name="android.intent.action.VIEW"/>
70 + <category android:name="android.intent.category.DEFAULT"/>
71 + <category android:name="android.intent.category.BROWSABLE"/>
72 + <data android:scheme="https"/>
73 + <data android:host="${appLinksHost}"/>
74 + <data android:pathPrefix="/users/log-in/"/>
75 + </intent-filter>
41 76 </activity>
42 77
43 78 <!-- Photos shared from the gallery or camera. One screen, two ways
modified android/app/src/main/java/org/neiam/cartosnap/app/MainActivity.kt
+68 −3
@@ -1,36 +1,101 @@
1 1 package org.neiam.cartosnap.app
2 2
3 +import android.content.Intent
3 4 import android.os.Bundle
4 5 import androidx.activity.ComponentActivity
5 6 import androidx.activity.compose.setContent
6 7 import androidx.activity.enableEdgeToEdge
8 +import androidx.compose.runtime.collectAsState
7 9 import androidx.compose.runtime.getValue
8 10 import androidx.compose.runtime.mutableStateOf
9 11 import androidx.compose.runtime.remember
12 +import androidx.compose.runtime.rememberCoroutineScope
10 13 import androidx.compose.runtime.setValue
14 +import kotlinx.coroutines.flow.MutableStateFlow
15 +import kotlinx.coroutines.launch
16 +import org.neiam.cartosnap.app.auth.CartosnapLoginHost
17 +import org.neiam.cartosnap.app.auth.login.AuthLink
18 +import org.neiam.cartosnap.app.auth.login.AuthLinkHandler
19 +import org.neiam.cartosnap.app.auth.login.LoginFlows
20 +import org.neiam.cartosnap.app.auth.login.LoginPrefs
21 +import org.neiam.cartosnap.app.auth.login.LoginScreen
11 22 import org.neiam.cartosnap.app.data.Session
12 23 import org.neiam.cartosnap.app.ui.CartosnapTheme
13 24 import org.neiam.cartosnap.app.ui.HomeScreen
14 −import org.neiam.cartosnap.app.ui.PairScreen
15 25
16 −/** Pairing, and the upload queue once paired. Sharing happens in [ShareActivity]. */
26 +/**
27 + * The log-in page (auth/login: Pair, OIDC, Username) and, once signed in,
28 + * the upload queue. Sharing happens in [ShareActivity].
29 + *
30 + * Links it opens:
31 + * - https://<host>/users/log-in/<token> → magic link (verified App Link)
32 + * - cartosnap://auth?code=… → SSO sign-in handed back (neiam_auth)
33 + *
34 + * The activity is singleTask, so links that arrive while it runs (the
35 + * Custom Tab's return included) come through onNewIntent.
36 + */
17 37 class MainActivity : ComponentActivity() {
38 + private lateinit var host: CartosnapLoginHost
39 + private lateinit var flows: LoginFlows
40 + private val pendingLink = MutableStateFlow<AuthLink?>(null)
41 +
18 42 override fun onCreate(savedInstanceState: Bundle?) {
19 43 super.onCreate(savedInstanceState)
20 44 enableEdgeToEdge()
21 45 val session = Session(this)
46 + host = CartosnapLoginHost(this, session)
47 + flows = LoginFlows(host, LoginPrefs(this))
48 + if (savedInstanceState == null) handleIntent(intent)
22 49
23 50 setContent {
24 51 CartosnapTheme {
25 52 var credentials by remember { mutableStateOf(session.credentials()) }
53 + val scope = rememberCoroutineScope()
54 + val link by pendingLink.collectAsState()
26 55 val current = credentials
27 56
57 + // Any sign-in: home at once, then the account's name and
58 + // whatever waited for pairing.
59 + val onSignedIn: () -> Unit = {
60 + credentials = session.credentials()
61 + scope.launch {
62 + host.afterSignIn()
63 + credentials = session.credentials()
64 + }
65 + }
66 +
28 67 if (current == null) {
29 − PairScreen(session = session, onPaired = { credentials = it })
68 + LoginScreen(flows = flows, onSignedIn = onSignedIn)
30 69 } else {
31 70 HomeScreen(session = session, credentials = current, onUnpaired = { credentials = null })
32 71 }
72 +
73 + // A sign-in link, over whatever is showing. "Email me a new
74 + // link" from an expired one opens the log-in page's sheet,
75 + // which shows when signed out.
76 + AuthLinkHandler(
77 + flows = flows,
78 + link = link,
79 + onConsumed = {
80 + pendingLink.value = null
81 + // "Switch account" may have signed out and then failed.
82 + credentials = session.credentials()
83 + },
84 + onSignedIn = onSignedIn,
85 + )
33 86 }
34 87 }
35 88 }
89 +
90 + override fun onNewIntent(intent: Intent) {
91 + super.onNewIntent(intent)
92 + setIntent(intent)
93 + handleIntent(intent)
94 + }
95 +
96 + private fun handleIntent(intent: Intent?) {
97 + if (intent?.action != Intent.ACTION_VIEW) return
98 + val url = intent.data?.toString() ?: return
99 + AuthLink.parse(url, host.appScheme)?.let { pendingLink.value = it }
100 + }
36 101 }
added android/app/src/main/java/org/neiam/cartosnap/app/auth/CartosnapLoginHost.kt
+72 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/AuthLink.kt
+79 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/AuthLinkHandler.kt
+137 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/Autofill.kt
+32 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/HelpScreen.kt
+148 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/LoginApi.kt
+244 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/LoginFlows.kt
+169 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/LoginHost.kt
+109 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/LoginScreen.kt
+308 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/Pkce.kt
+30 −0

Click to load diff…

added android/app/src/main/java/org/neiam/cartosnap/app/auth/login/Sheets.kt
+296 −0

Click to load diff…

added android/app/src/test/java/org/neiam/cartosnap/app/auth/login/AuthLinkTest.kt
+79 −0

Click to load diff…

added android/app/src/test/java/org/neiam/cartosnap/app/auth/login/AuthMethodsTest.kt
+80 −0

Click to load diff…

added android/app/src/test/java/org/neiam/cartosnap/app/auth/login/LoginApiTest.kt
+44 −0

Click to load diff…

added android/app/src/test/java/org/neiam/cartosnap/app/auth/login/PkceTest.kt
+27 −0

Click to load diff…

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

Click to load diff…

Parents: 8849444