neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Android: a new log-in page (Scan, OIDC, Username) and sign-in App Links
a76b392 · gmorell · 2026-12-03 18:05
Message
{commit_body(@commit)}
Files changed
modified
android/README.md
+54
−13
| 3 | 3 | A read-only phone companion for [Vigil](../): what's breaking across an |
| 4 | 4 | organization, each issue's latest event with its stack trace, and which |
| 5 | 5 | cron and uptime monitors are failing. It talks to the same Sentry-shaped |
| 6 | −`/api/0` that sentry-cli uses, with an API token scanned from the web. | |
| 6 | +`/api/0` that sentry-cli uses, with an API token: scanned from the web, or | |
| 7 | +made for it when it signs in (`/api/v1/auth/*`). | |
| 7 | 8 | |
| 8 | 9 | Sister app to `../../gitgud/android`, `../../bigtrippr/android` and the rest |
| 9 | 10 | of the neiam family: the same Compose shell, the web's own OKLCh themes |
| 13 | 14 | |
| 14 | 15 | | Surface | | |
| 15 | 16 | |---|---| |
| 16 | −| **Pairing** | Scan the QR from Settings → API tokens, open its `vigil://pair` link on the phone, or type the server and token. Verified before anything is stored | | |
| 17 | +| **Log-in** | The family's shared log-in page: **Scan** (the QR from Settings → API tokens, its `vigil://pair` link, or the server and token typed in), **Sign in with NEIAM** (SSO in a Custom Tab), or **Username** (email me a sign-in link, or a password). Each way in ends with the same kind of read-only token. See [docs/app-login.md](../docs/app-login.md) | | |
| 17 | 18 | | **Organizations** | Picked once and remembered; skipped when you're in only one. Switch from the menu | |
| 18 | 19 | | **Issues** | Across the organization or one project; Unresolved (the default), Ignored, Resolved or All; the web's search syntax (`level:error`, `release:1.2`, `browser:Firefox`, free text). Level, title, culprit, events, users, last seen and project per row. Pages load as you scroll (Sentry's Link-header cursors); pull to refresh | |
| 19 | 20 | | **Issue** | Counts, first and last seen, releases, assignee; the latest event's exception chain and stack, newest call first, in-app frames outlined with their source lines and library frames folded; breadcrumbs, the request, the event's tags, user and runtime; a tag summary. **Open in browser** goes to the web page, where it can be triaged | |
| 41 | 42 | Single activity, single module, no DI framework. Dependency versions match |
| 42 | 43 | bigtrippr's and waxx's, so the family builds from one Gradle cache. |
| 43 | 44 | |
| 44 | −## Pairing | |
| 45 | +## Pairing (Scan) | |
| 45 | 46 | |
| 46 | 47 | 1. Signed in on the web: **Settings → API tokens**. Tick **Mobile app**, |
| 47 | 48 | which ticks the read-only bundle the app needs (`org:read`, |
| 49 | 50 | token a label and create it. |
| 50 | 51 | 2. A QR appears beside the token, encoding |
| 51 | 52 | `vigil://pair?base=<server>&token=<token>`, with the same link under it. |
| 52 | −3. In the app, **Scan code** and point it at the QR. Or open the link on | |
| 53 | − the phone, or enter the server and token by hand. | |
| 53 | +3. In the app, **Scan** and point it at the QR. Or open the link on the | |
| 54 | + phone (the Scan sheet opens filled in; tap **Pair**), or paste the link | |
| 55 | + or enter the server and token by hand in the sheet. | |
| 54 | 56 | 4. The app calls `GET /api/0/` and keeps the pair only once the server |
| 55 | − names the token's owner. If the token lacks part of the bundle it says | |
| 56 | − which scopes, and pairing anyway is your choice. | |
| 57 | + names the token's owner. A token short of the bundle still pairs; the | |
| 58 | + screens it can't read say so. | |
| 57 | 59 | |
| 58 | 60 | A paired phone *is* its token: there is no separate device list. Revoke |
| 59 | 61 | the token on the tokens page and the app notices on its next request and |
| 60 | −returns to pairing. **Unpair this phone** in the menu forgets the token on | |
| 61 | −the phone, but doesn't revoke it. | |
| 62 | +returns to the log-in page. **Unpair this phone** in the menu forgets the | |
| 63 | +token on the phone, but doesn't revoke it. | |
| 64 | + | |
| 65 | +## Log-in and App Links | |
| 66 | + | |
| 67 | +The log-in page (Scan, OIDC, Username), the sign-in API and the App Links | |
| 68 | +are described in [docs/app-login.md](../docs/app-login.md). Signing in | |
| 69 | +with SSO, a password or an emailed link makes a token like the Mobile app | |
| 70 | +one (the same read-only scopes), labelled with the phone's name, listed | |
| 71 | +and revoked on Settings → API tokens. | |
| 72 | + | |
| 73 | +App Links need the server to vouch for this app's signing certificate. | |
| 74 | +`/.well-known/assetlinks.json` is built from the server's | |
| 75 | +`ANDROID_CERT_FINGERPRINTS` env (comma-separated SHA-256 fingerprints). Read | |
| 76 | +them with: | |
| 77 | + | |
| 78 | +```sh | |
| 79 | +keytool -list -v -keystore release.jks -alias vigil | grep SHA256 | |
| 80 | +keytool -list -v -keystore ~/.android/debug.keystore \ | |
| 81 | + -storepass android -alias androiddebugkey | grep SHA256 | |
| 82 | +``` | |
| 83 | + | |
| 84 | +Without them, Android doesn't verify the links, and sign-in links open in | |
| 85 | +the browser (where they work as before). Scan, SSO and passwords still work. | |
| 86 | + | |
| 87 | +App Links are for magic links only (`/users/log-in/<token>`). "Sign in | |
| 88 | +with NEIAM" comes back on `vigil://auth?code=…` (the server's | |
| 89 | +`redirect_uri`), so it works with any server; the code is useless without | |
| 90 | +the PKCE verifier kept in the app. | |
| 91 | + | |
| 92 | +The App Links host is `appLinksHost` in `app/build.gradle.kts` | |
| 93 | +(`vigil.neiam.org`). It is also the server a fresh install signs in to; | |
| 94 | +Help → Change server picks another. | |
| 62 | 95 | |
| 63 | 96 | In each organization a token can do only what your role there allows, so |
| 64 | 97 | the app shows what you'd see on the web. The QR carries a bearer token in |
| 96 | 129 | LAN uses the host's LAN address. |
| 97 | 130 | 3. In dev the endpoint's URL is `http://localhost:4000`, so the pairing |
| 98 | 131 | code names `localhost`, which the phone can't reach. Make the token with |
| 99 | − **Mobile app** ticked as usual, then on the phone choose **Enter it by | |
| 100 | − hand** and give the address above with the token. | |
| 132 | + **Mobile app** ticked as usual, then on the phone open **Scan** and, | |
| 133 | + under **Or paste it**, give the address above with the token. For the | |
| 134 | + other ways in, **Help → Change server** to the same address. | |
| 101 | 135 | |
| 102 | 136 | Debug builds allow plain `http://`; release builds don't. The app upgrades |
| 103 | 137 | `http://` to `https://` for real domain names (a proxy that doesn't send |
| 131 | 165 | | `GET /organizations/{org}/monitors/` | `project:read` | cron monitors, with Vigil's `health` | |
| 132 | 166 | | `GET /organizations/{org}/uptime/` | `project:read` | Vigil's own | |
| 133 | 167 | |
| 168 | +Signing in (no token yet) uses `/api/v1/auth/*`, the API every neiam app | |
| 169 | +shares: see [docs/app-login.md](../docs/app-login.md). | |
| 170 | + | |
| 134 | 171 | An issue's `id` is its short id within the project (`SHOP-12` is `12`), as |
| 135 | 172 | in the web's issue URLs. |
| 136 | 173 |
| 190 | 227 | |
| 191 | 228 | ``` |
| 192 | 229 | app/src/main/java/org/neiam/vigil/app/ |
| 193 | − MainActivity.kt # nav host; vigil://pair links; theme | |
| 230 | + MainActivity.kt # nav host; vigil://pair, vigil://auth and magic links; theme | |
| 194 | 231 | data/ |
| 195 | 232 | Links.kt # vigil://pair parsing; the Link header's next cursor |
| 196 | 233 | TokenStore.kt # (server, token), encrypted; https upgrade |
| 198 | 235 | VigilClient.kt # Retrofit interface, error messages |
| 199 | 236 | Models.kt # JSON shapes; the mobile scope bundle |
| 200 | 237 | auth/ |
| 201 | − PairScreen.kt # scan | link | by hand, verified, scope check | |
| 238 | + VigilLoginHost.kt # Vigil's glue for the shared log-in page | |
| 239 | + login/ # the log-in page, the same in every neiam app: | |
| 240 | + # Scan | OIDC | Username sheets, Help, App Links, | |
| 241 | + # PKCE, the /api/v1/auth client | |
| 202 | 242 | ui/ |
| 203 | 243 | OrgPickerScreen.kt |
| 204 | 244 | HomeScreen.kt # Issues | Monitors tabs; menu: org, theme, unpair |
| 211 | 251 | theme/ # the web's themes, status colours and B612 |
| 212 | 252 | app/src/test/ |
| 213 | 253 | …/LinksTest.kt, ContractTest.kt, CommonTest.kt |
| 254 | + …/auth/login/ # methods parsing, PKCE, link matching, errors | |
| 214 | 255 | resources/api/*.json # real API responses the contract test decodes |
| 215 | 256 | ``` |
| 216 | 257 |
modified
android/app/build.gradle.kts
+9
−0
| 32 | 32 | // for an update to count as one. Local builds stay at 1. |
| 33 | 33 | versionCode = (System.getenv("GITHUB_RUN_NUMBER") ?: "").toIntOrNull() ?: 1 |
| 34 | 34 | versionName = appVersionName |
| 35 | + // The server a fresh install signs in to, and the host whose emailed | |
| 36 | + // sign-in links open this app as verified App Links. The server's | |
| 37 | + // /.well-known/assetlinks.json must list this build's signing | |
| 38 | + // certificate. | |
| 39 | + val appLinksHost = "vigil.neiam.org" | |
| 40 | + manifestPlaceholders["appLinksHost"] = appLinksHost | |
| 41 | + buildConfigField("String", "DEFAULT_SERVER", "\"https://$appLinksHost\"") | |
| 35 | 42 | } |
| 36 | 43 | |
| 37 | 44 | buildTypes { |
| 95 | 102 | implementation(libs.okhttp.logging) |
| 96 | 103 | |
| 97 | 104 | implementation(libs.zxing.android.embedded) |
| 105 | + // Custom Tabs: SSO sign-in runs in the system browser, never a WebView. | |
| 106 | + implementation(libs.androidx.browser) | |
| 98 | 107 | |
| 99 | 108 | testImplementation(libs.junit) |
| 100 | 109 | } |
modified
android/app/src/main/AndroidManifest.xml
+35
−0
| 36 | 36 | <category android:name="android.intent.category.BROWSABLE"/> |
| 37 | 37 | <data android:scheme="vigil" android:host="pair"/> |
| 38 | 38 | </intent-filter> |
| 39 | + | |
| 40 | + <!-- | |
| 41 | + vigil://auth?code=… — the end of a "Sign in with NEIAM" in the | |
| 42 | + browser (Custom Tab): the server's one-time code, useless | |
| 43 | + without the PKCE verifier that never leaves the app. A custom | |
| 44 | + scheme because the server is whichever the user picked, and | |
| 45 | + an App Link would have to name its host here. | |
| 46 | + --> | |
| 47 | + <intent-filter android:autoVerify="false"> | |
| 48 | + <action android:name="android.intent.action.VIEW"/> | |
| 49 | + <category android:name="android.intent.category.DEFAULT"/> | |
| 50 | + <category android:name="android.intent.category.BROWSABLE"/> | |
| 51 | + <data android:scheme="vigil" android:host="auth"/> | |
| 52 | + </intent-filter> | |
| 53 | + | |
| 54 | + <!-- | |
| 55 | + Magic links, as verified App Links (autoVerify), on the | |
| 56 | + production host only (appLinksHost in app/build.gradle.kts): | |
| 57 | + Android opens them in this app only when | |
| 58 | + https://<host>/.well-known/assetlinks.json lists this | |
| 59 | + package and its signing certificate (ANDROID_CERT_FINGERPRINTS | |
| 60 | + on the server). Otherwise they open in the browser, where | |
| 61 | + they work as before. Never a custom scheme: the token is a | |
| 62 | + credential by itself. | |
| 63 | + | |
| 64 | + /users/log-in/<token> the web's magic link (phx.gen.auth) | |
| 65 | + --> | |
| 66 | + <intent-filter android:autoVerify="true"> | |
| 67 | + <action android:name="android.intent.action.VIEW"/> | |
| 68 | + <category android:name="android.intent.category.DEFAULT"/> | |
| 69 | + <category android:name="android.intent.category.BROWSABLE"/> | |
| 70 | + <data android:scheme="https"/> | |
| 71 | + <data android:host="${appLinksHost}"/> | |
| 72 | + <data android:pathPrefix="/users/log-in/"/> | |
| 73 | + </intent-filter> | |
| 39 | 74 | </activity> |
| 40 | 75 | </application> |
| 41 | 76 | </manifest> |
modified
android/app/src/main/java/org/neiam/vigil/app/MainActivity.kt
+128
−89
| 7 | 7 | import androidx.activity.SystemBarStyle |
| 8 | 8 | import androidx.activity.compose.setContent |
| 9 | 9 | import androidx.activity.enableEdgeToEdge |
| 10 | +import androidx.compose.foundation.layout.Box | |
| 10 | 11 | import androidx.compose.foundation.layout.fillMaxSize |
| 11 | 12 | import androidx.compose.material3.MaterialTheme |
| 12 | 13 | import androidx.compose.material3.Surface |
| 13 | 14 | import androidx.compose.runtime.Composable |
| 14 | 15 | import androidx.compose.runtime.LaunchedEffect |
| 16 | +import androidx.compose.runtime.collectAsState | |
| 15 | 17 | import androidx.compose.runtime.getValue |
| 16 | 18 | import androidx.compose.runtime.mutableStateOf |
| 17 | 19 | import androidx.compose.runtime.remember |
| 23 | 25 | import androidx.navigation.compose.composable |
| 24 | 26 | import androidx.navigation.compose.rememberNavController |
| 25 | 27 | import androidx.navigation.navArgument |
| 26 | −import org.neiam.vigil.app.auth.PairScreen | |
| 27 | −import org.neiam.vigil.app.data.PairLink | |
| 28 | +import kotlinx.coroutines.flow.MutableStateFlow | |
| 29 | +import org.neiam.vigil.app.auth.VigilLoginHost | |
| 30 | +import org.neiam.vigil.app.auth.login.AuthLink | |
| 31 | +import org.neiam.vigil.app.auth.login.AuthLinkHandler | |
| 32 | +import org.neiam.vigil.app.auth.login.LoginFlows | |
| 33 | +import org.neiam.vigil.app.auth.login.LoginPrefs | |
| 34 | +import org.neiam.vigil.app.auth.login.LoginScreen | |
| 28 | 35 | import org.neiam.vigil.app.data.PrefsStore |
| 29 | 36 | import org.neiam.vigil.app.data.TokenStore |
| 30 | 37 | import org.neiam.vigil.app.data.VigilClient |
| 37 | 44 | import org.neiam.vigil.app.ui.theme.appThemeForKey |
| 38 | 45 | |
| 39 | 46 | /** |
| 40 | − * The one activity. Routes: pair, orgs?auto=, home/{org}, | |
| 41 | − * issue/{org}/{project}/{id}. | |
| 47 | + * The one activity. Routes: login (the shared log-in page, auth/login: | |
| 48 | + * Scan, OIDC, Username), orgs?auto=, home/{org}, issue/{org}/{project}/{id}. | |
| 42 | 49 | * |
| 43 | − * Opened by a `vigil://pair?base=…&token=…` link (singleTask, so a running | |
| 44 | − * app gets onNewIntent rather than a second copy): pair, verified at once. | |
| 50 | + * Links it is opened with: | |
| 51 | + * - vigil://pair?base=…&token=… → the Scan sheet, filled in | |
| 52 | + * - https://<host>/users/log-in/<token> → magic link (App Link) | |
| 53 | + * - vigil://auth?code=… → SSO sign-in handed back (neiam_auth) | |
| 54 | + * | |
| 55 | + * singleTask, so a link that arrives while the app runs (the Custom Tab's | |
| 56 | + * return included) comes through onNewIntent rather than a second copy. | |
| 45 | 57 | */ |
| 46 | 58 | class MainActivity : ComponentActivity() { |
| 47 | − private var pairLink by mutableStateOf<PairLink?>(null) | |
| 59 | + private lateinit var flows: LoginFlows | |
| 60 | + private val pendingLink = MutableStateFlow<AuthLink?>(null) | |
| 61 | + private val goToLogin = MutableStateFlow(false) | |
| 48 | 62 | |
| 49 | 63 | override fun onCreate(savedInstanceState: Bundle?) { |
| 50 | 64 | super.onCreate(savedInstanceState) |
| 51 | 65 | val tokens = TokenStore(this) |
| 52 | 66 | val prefs = PrefsStore(this) |
| 53 | − pairLink = linkIn(intent) | |
| 67 | + flows = LoginFlows(VigilLoginHost(tokens, prefs), LoginPrefs(this)) | |
| 68 | + if (savedInstanceState == null) handleIntent(intent) | |
| 54 | 69 | |
| 55 | 70 | setContent { |
| 56 | 71 | var themeKey by remember { mutableStateOf(prefs.themeKey) } |
| 67 | 82 | VigilApp( |
| 68 | 83 | tokens = tokens, |
| 69 | 84 | prefs = prefs, |
| 70 | − pairLink = pairLink, | |
| 71 | − onPairLinkHandled = { pairLink = null }, | |
| 85 | + flows = flows, | |
| 86 | + pendingLink = pendingLink, | |
| 87 | + goToLogin = goToLogin, | |
| 72 | 88 | theme = theme, |
| 73 | 89 | themeKey = themeKey, |
| 74 | 90 | onPickTheme = { picked -> |
| 83 | 99 | |
| 84 | 100 | override fun onNewIntent(intent: Intent) { |
| 85 | 101 | super.onNewIntent(intent) |
| 86 | − linkIn(intent)?.let { pairLink = it } | |
| 102 | + handleIntent(intent) | |
| 87 | 103 | } |
| 88 | 104 | |
| 89 | − private fun linkIn(intent: Intent?): PairLink? = | |
| 90 | − intent?.takeIf { it.action == Intent.ACTION_VIEW }?.dataString?.let(::parsePair) | |
| 105 | + private fun handleIntent(intent: Intent?) { | |
| 106 | + if (intent?.action != Intent.ACTION_VIEW) return | |
| 107 | + val url = intent.dataString ?: return | |
| 108 | + parsePair(url)?.let { | |
| 109 | + flows.offerPairing(it.baseUrl, it.token) | |
| 110 | + goToLogin.value = true | |
| 111 | + return | |
| 112 | + } | |
| 113 | + AuthLink.parse(url, flows.host.appScheme)?.let { pendingLink.value = it } | |
| 114 | + } | |
| 91 | 115 | } |
| 92 | 116 | |
| 93 | 117 | @Composable |
| 94 | 118 | private fun VigilApp( |
| 95 | 119 | tokens: TokenStore, |
| 96 | 120 | prefs: PrefsStore, |
| 97 | − pairLink: PairLink?, | |
| 98 | − onPairLinkHandled: () -> Unit, | |
| 121 | + flows: LoginFlows, | |
| 122 | + pendingLink: MutableStateFlow<AuthLink?>, | |
| 123 | + goToLogin: MutableStateFlow<Boolean>, | |
| 99 | 124 | theme: AppTheme, |
| 100 | 125 | themeKey: String, |
| 101 | 126 | onPickTheme: (String) -> Unit, |
| 105 | 130 | val api = remember(credentials) { credentials?.let(VigilClient::authenticated) } |
| 106 | 131 | val start = remember { |
| 107 | 132 | when { |
| 108 | − credentials == null -> "pair" | |
| 133 | + credentials == null || goToLogin.value -> "login" | |
| 109 | 134 | prefs.orgSlug != null -> "home/${Uri.encode(prefs.orgSlug)}" |
| 110 | 135 | else -> "orgs?auto=true" |
| 111 | 136 | } |
| 112 | 137 | } |
| 113 | − // The link being acted on: kept after the activity's copy is cleared, so | |
| 114 | − // recomposition doesn't lose the prefilled screen. | |
| 115 | − var pendingPair by remember { mutableStateOf<PairLink?>(null) } | |
| 116 | 138 | |
| 117 | − fun toPair() { | |
| 139 | + fun toLogin() { | |
| 118 | 140 | credentials = null |
| 119 | − nav.navigate("pair") { popUpTo(0) } | |
| 141 | + nav.navigate("login") { popUpTo(0) } | |
| 120 | 142 | } |
| 121 | 143 | |
| 122 | 144 | fun unpair() { |
| 123 | 145 | tokens.clear() |
| 124 | 146 | prefs.orgSlug = null |
| 125 | − toPair() | |
| 147 | + toLogin() | |
| 126 | 148 | } |
| 127 | 149 | |
| 128 | 150 | // A 401 means the token was revoked (or expired): this phone is signed out. |
| 129 | 151 | fun unauthorized() = unpair() |
| 130 | 152 | |
| 131 | − LaunchedEffect(pairLink) { | |
| 132 | − if (pairLink == null) return@LaunchedEffect | |
| 133 | − pendingPair = pairLink | |
| 134 | − nav.navigate("pair") { popUpTo(0) } | |
| 135 | − onPairLinkHandled() | |
| 153 | + // Signed in, by any way in: the stored token is the one to use now. | |
| 154 | + fun signedIn() { | |
| 155 | + credentials = tokens.load() | |
| 156 | + nav.navigate("orgs?auto=true") { popUpTo(0) } | |
| 136 | 157 | } |
| 137 | 158 | |
| 138 | − NavHost(navController = nav, startDestination = start) { | |
| 139 | − composable("pair") { | |
| 140 | − PairScreen( | |
| 141 | − prefilled = pendingPair, | |
| 142 | − tokens = tokens, | |
| 143 | − onPaired = { | |
| 144 | − pendingPair = null | |
| 145 | − prefs.orgSlug = null | |
| 146 | − credentials = tokens.load() | |
| 147 | − nav.navigate("orgs?auto=true") { popUpTo(0) } | |
| 148 | − }, | |
| 149 | − ) | |
| 159 | + // A vigil://pair link: the log-in page, with the Scan sheet filled in. | |
| 160 | + val wantLogin by goToLogin.collectAsState() | |
| 161 | + LaunchedEffect(wantLogin) { | |
| 162 | + if (wantLogin) { | |
| 163 | + goToLogin.value = false | |
| 164 | + if (nav.currentDestination?.route != "login") nav.navigate("login") { popUpTo(0) } | |
| 150 | 165 | } |
| 166 | + } | |
| 151 | 167 | |
| 152 | − composable( | |
| 153 | − "orgs?auto={auto}", | |
| 154 | − arguments = listOf(navArgument("auto") { type = NavType.BoolType; defaultValue = false }), | |
| 155 | − ) { entry -> | |
| 156 | − val signedIn = api ?: return@composable LaunchedEffect(Unit) { toPair() } | |
| 157 | − OrgPickerScreen( | |
| 158 | − api = signedIn, | |
| 159 | − autoPick = entry.arguments?.getBoolean("auto") ?: false, | |
| 160 | − onPicked = { org -> | |
| 161 | − prefs.orgSlug = org.slug | |
| 162 | − nav.navigate("home/${Uri.encode(org.slug)}") { popUpTo(0) } | |
| 163 | − }, | |
| 164 | − onUnpair = ::unpair, | |
| 165 | − onUnauthorized = ::unauthorized, | |
| 166 | − ) | |
| 167 | − } | |
| 168 | + Box(modifier = Modifier.fillMaxSize()) { | |
| 169 | + NavHost(navController = nav, startDestination = start) { | |
| 170 | + composable("login") { | |
| 171 | + LoginScreen(flows = flows, onSignedIn = ::signedIn) | |
| 172 | + } | |
| 168 | 173 | |
| 169 | − composable("home/{org}", arguments = listOf(navArgument("org") { type = NavType.StringType })) { entry -> | |
| 170 | − val signedIn = api ?: return@composable LaunchedEffect(Unit) { toPair() } | |
| 171 | − val org = entry.arguments?.getString("org").orEmpty() | |
| 172 | − HomeScreen( | |
| 173 | − api = signedIn, | |
| 174 | − org = org, | |
| 175 | − theme = theme, | |
| 176 | − themeKey = themeKey, | |
| 177 | − onPickTheme = onPickTheme, | |
| 178 | − onOpenIssue = { issue -> | |
| 179 | − nav.navigate("issue/${Uri.encode(org)}/${Uri.encode(issue.project.slug)}/${Uri.encode(issue.id)}") | |
| 180 | − }, | |
| 181 | − onSwitchOrg = { nav.navigate("orgs?auto=false") }, | |
| 182 | − onUnpair = ::unpair, | |
| 183 | − onUnauthorized = ::unauthorized, | |
| 184 | − ) | |
| 174 | + composable( | |
| 175 | + "orgs?auto={auto}", | |
| 176 | + arguments = listOf(navArgument("auto") { type = NavType.BoolType; defaultValue = false }), | |
| 177 | + ) { entry -> | |
| 178 | + val signedIn = api ?: return@composable LaunchedEffect(Unit) { toLogin() } | |
| 179 | + OrgPickerScreen( | |
| 180 | + api = signedIn, | |
| 181 | + autoPick = entry.arguments?.getBoolean("auto") ?: false, | |
| 182 | + onPicked = { org -> | |
| 183 | + prefs.orgSlug = org.slug | |
| 184 | + nav.navigate("home/${Uri.encode(org.slug)}") { popUpTo(0) } | |
| 185 | + }, | |
| 186 | + onUnpair = ::unpair, | |
| 187 | + onUnauthorized = ::unauthorized, | |
| 188 | + ) | |
| 189 | + } | |
| 190 | + | |
| 191 | + composable("home/{org}", arguments = listOf(navArgument("org") { type = NavType.StringType })) { entry -> | |
| 192 | + val signedIn = api ?: return@composable LaunchedEffect(Unit) { toLogin() } | |
| 193 | + val org = entry.arguments?.getString("org").orEmpty() | |
| 194 | + HomeScreen( | |
| 195 | + api = signedIn, | |
| 196 | + org = org, | |
| 197 | + theme = theme, | |
| 198 | + themeKey = themeKey, | |
| 199 | + onPickTheme = onPickTheme, | |
| 200 | + onOpenIssue = { issue -> | |
| 201 | + nav.navigate("issue/${Uri.encode(org)}/${Uri.encode(issue.project.slug)}/${Uri.encode(issue.id)}") | |
| 202 | + }, | |
| 203 | + onSwitchOrg = { nav.navigate("orgs?auto=false") }, | |
| 204 | + onUnpair = ::unpair, | |
| 205 | + onUnauthorized = ::unauthorized, | |
| 206 | + ) | |
| 207 | + } | |
| 208 | + | |
| 209 | + composable( | |
| 210 | + "issue/{org}/{project}/{id}", | |
| 211 | + arguments = listOf( | |
| 212 | + navArgument("org") { type = NavType.StringType }, | |
| 213 | + navArgument("project") { type = NavType.StringType }, | |
| 214 | + navArgument("id") { type = NavType.StringType }, | |
| 215 | + ), | |
| 216 | + ) { entry -> | |
| 217 | + val signedIn = api ?: return@composable LaunchedEffect(Unit) { toLogin() } | |
| 218 | + val args = entry.arguments | |
| 219 | + IssueScreen( | |
| 220 | + api = signedIn, | |
| 221 | + org = args?.getString("org").orEmpty(), | |
| 222 | + project = args?.getString("project").orEmpty(), | |
| 223 | + id = args?.getString("id").orEmpty(), | |
| 224 | + onBack = { nav.popBackStack() }, | |
| 225 | + onUnauthorized = ::unauthorized, | |
| 226 | + ) | |
| 227 | + } | |
| 185 | 228 | } |
| 186 | 229 | |
| 187 | − composable( | |
| 188 | − "issue/{org}/{project}/{id}", | |
| 189 | − arguments = listOf( | |
| 190 | − navArgument("org") { type = NavType.StringType }, | |
| 191 | − navArgument("project") { type = NavType.StringType }, | |
| 192 | − navArgument("id") { type = NavType.StringType }, | |
| 193 | − ), | |
| 194 | − ) { entry -> | |
| 195 | − val signedIn = api ?: return@composable LaunchedEffect(Unit) { toPair() } | |
| 196 | − val args = entry.arguments | |
| 197 | − IssueScreen( | |
| 198 | − api = signedIn, | |
| 199 | − org = args?.getString("org").orEmpty(), | |
| 200 | − project = args?.getString("project").orEmpty(), | |
| 201 | − id = args?.getString("id").orEmpty(), | |
| 202 | − onBack = { nav.popBackStack() }, | |
| 203 | − onUnauthorized = ::unauthorized, | |
| 204 | − ) | |
| 230 | + // A sign-in link (App Link, or the SSO hand-back), over whatever is showing. | |
| 231 | + val link by pendingLink.collectAsState() | |
| 232 | + AuthLinkHandler( | |
| 233 | + flows = flows, | |
| 234 | + link = link, | |
| 235 | + onConsumed = { pendingLink.value = null }, | |
| 236 | + onSignedIn = ::signedIn, | |
| 237 | + ) | |
| 238 | + // "Email me a new link" from an expired link asks for the log-in page. | |
| 239 | + val sheet by flows.requestedSheet.collectAsState() | |
| 240 | + LaunchedEffect(sheet) { | |
| 241 | + if (sheet != null && nav.currentDestination?.route != "login" && tokens.load() == null) { | |
| 242 | + nav.navigate("login") { popUpTo(0) } | |
| 243 | + } | |
| 205 | 244 | } |
| 206 | 245 | } |
| 207 | 246 | } |
deleted
android/app/src/main/java/org/neiam/vigil/app/auth/PairScreen.kt
+0
−227
| 1 | −package org.neiam.vigil.app.auth | |
| 2 | − | |
| 3 | −import androidx.activity.compose.rememberLauncherForActivityResult | |
| 4 | −import androidx.compose.animation.AnimatedVisibility | |
| 5 | −import androidx.compose.foundation.layout.Arrangement | |
| 6 | −import androidx.compose.foundation.layout.Column | |
| 7 | −import androidx.compose.foundation.layout.Spacer | |
| 8 | −import androidx.compose.foundation.layout.fillMaxSize | |
| 9 | −import androidx.compose.foundation.layout.fillMaxWidth | |
| 10 | −import androidx.compose.foundation.layout.height | |
| 11 | −import androidx.compose.foundation.layout.imePadding | |
| 12 | −import androidx.compose.foundation.layout.padding | |
| 13 | −import androidx.compose.foundation.layout.size | |
| 14 | −import androidx.compose.foundation.layout.systemBarsPadding | |
| 15 | −import androidx.compose.foundation.layout.width | |
| 16 | −import androidx.compose.foundation.rememberScrollState | |
| 17 | −import androidx.compose.foundation.text.KeyboardOptions | |
| 18 | −import androidx.compose.foundation.verticalScroll | |
| 19 | −import androidx.compose.material.icons.Icons | |
| 20 | −import androidx.compose.material.icons.filled.QrCodeScanner | |
| 21 | −import androidx.compose.material3.Button | |
| 22 | −import androidx.compose.material3.CircularProgressIndicator | |
| 23 | −import androidx.compose.material3.Icon | |
| 24 | −import androidx.compose.material3.MaterialTheme | |
| 25 | −import androidx.compose.material3.OutlinedButton | |
| 26 | −import androidx.compose.material3.OutlinedTextField | |
| 27 | −import androidx.compose.material3.Surface | |
| 28 | −import androidx.compose.material3.Text | |
| 29 | −import androidx.compose.material3.TextButton | |
| 30 | −import androidx.compose.runtime.Composable | |
| 31 | −import androidx.compose.runtime.LaunchedEffect | |
| 32 | −import androidx.compose.runtime.getValue | |
| 33 | −import androidx.compose.runtime.mutableStateOf | |
| 34 | −import androidx.compose.runtime.remember | |
| 35 | −import androidx.compose.runtime.rememberCoroutineScope | |
| 36 | −import androidx.compose.runtime.setValue | |
| 37 | −import androidx.compose.ui.Alignment | |
| 38 | −import androidx.compose.ui.Modifier | |
| 39 | −import androidx.compose.ui.text.input.KeyboardType | |
| 40 | −import androidx.compose.ui.text.input.PasswordVisualTransformation | |
| 41 | −import androidx.compose.ui.unit.dp | |
| 42 | −import com.journeyapps.barcodescanner.CaptureActivity | |
| 43 | −import com.journeyapps.barcodescanner.ScanContract | |
| 44 | −import com.journeyapps.barcodescanner.ScanOptions | |
| 45 | −import kotlinx.coroutines.launch | |
| 46 | −import org.neiam.vigil.app.data.PairLink | |
| 47 | −import org.neiam.vigil.app.data.TokenStore | |
| 48 | −import org.neiam.vigil.app.data.VigilClient | |
| 49 | −import org.neiam.vigil.app.data.coerceToHttps | |
| 50 | −import org.neiam.vigil.app.data.missingScopes | |
| 51 | −import org.neiam.vigil.app.data.normaliseBase | |
| 52 | −import org.neiam.vigil.app.data.parsePair | |
| 53 | −import org.neiam.vigil.app.ui.Wordmark | |
| 54 | −import org.neiam.vigil.app.ui.theme.Corners | |
| 55 | − | |
| 56 | −/** | |
| 57 | − * Pair by the code from the web's Settings → API tokens (a token made with | |
| 58 | − * "Mobile app" ticked): scanned, opened as a vigil://pair link, or typed. | |
| 59 | − * | |
| 60 | − * Nothing is stored until `GET /api/0/` names the token's owner. That reply | |
| 61 | − * also lists the token's scopes: one missing part of the mobile bundle | |
| 62 | − * pairs fine and then comes up empty or forbidden on some screens, so the | |
| 63 | − * gap is named here, and pairing anyway is a choice. | |
| 64 | − */ | |
| 65 | −@Composable | |
| 66 | −fun PairScreen( | |
| 67 | − prefilled: PairLink?, | |
| 68 | − tokens: TokenStore, | |
| 69 | − onPaired: () -> Unit, | |
| 70 | −) { | |
| 71 | − var baseUrl by remember { mutableStateOf(prefilled?.baseUrl ?: "") } | |
| 72 | − var token by remember { mutableStateOf(prefilled?.token ?: "") } | |
| 73 | − var byHand by remember { mutableStateOf(false) } | |
| 74 | − var verifying by remember { mutableStateOf(false) } | |
| 75 | − var error by remember { mutableStateOf<String?>(null) } | |
| 76 | − // Verified, but short of the bundle: who it is, what's missing. | |
| 77 | − var partial by remember { mutableStateOf<Triple<TokenStore.Credentials, String, List<String>>?>(null) } | |
| 78 | − val scope = rememberCoroutineScope() | |
| 79 | − | |
| 80 | − fun verify(given: TokenStore.Credentials) { | |
| 81 | − // Checked against the address it will be stored as. | |
| 82 | − val candidate = given.copy(baseUrl = coerceToHttps(given.baseUrl)) | |
| 83 | − verifying = true | |
| 84 | − error = null | |
| 85 | − partial = null | |
| 86 | − scope.launch { | |
| 87 | − try { | |
| 88 | − val root = VigilClient.authenticated(candidate).root() | |
| 89 | − val user = root.user | |
| 90 | − if (user == null) { | |
| 91 | − error = "The server didn't recognise that token." | |
| 92 | − } else { | |
| 93 | − val missing = missingScopes(root.auth?.scopes.orEmpty()) | |
| 94 | − if (missing.isEmpty()) { | |
| 95 | − tokens.save(candidate) | |
| 96 | − onPaired() | |
| 97 | − } else { | |
| 98 | − partial = Triple(candidate, user.label, missing) | |
| 99 | − } | |
| 100 | − } | |
| 101 | − } catch (e: Exception) { | |
| 102 | − error = VigilClient.describe(e) | |
| 103 | − } finally { | |
| 104 | − verifying = false | |
| 105 | − } | |
| 106 | − } | |
| 107 | − } | |
| 108 | − | |
| 109 | − // Opened from a vigil://pair link: everything is already here. | |
| 110 | − LaunchedEffect(prefilled) { | |
| 111 | − if (prefilled != null) verify(TokenStore.Credentials(prefilled.baseUrl, prefilled.token)) | |
| 112 | − } | |
| 113 | − | |
| 114 | − val scanLauncher = rememberLauncherForActivityResult(ScanContract()) { result -> | |
| 115 | − val raw = result.contents ?: return@rememberLauncherForActivityResult | |
| 116 | − val pair = parsePair(raw) | |
| 117 | − if (pair == null) { | |
| 118 | − error = "That isn't a Vigil pairing code." | |
| 119 | − } else { | |
| 120 | − baseUrl = pair.baseUrl | |
| 121 | − token = pair.token | |
| 122 | − verify(TokenStore.Credentials(pair.baseUrl, pair.token)) | |
| 123 | − } | |
| 124 | − } | |
| 125 | − | |
| 126 | − Column( | |
| 127 | − modifier = Modifier | |
| 128 | − .fillMaxSize() | |
| 129 | − .systemBarsPadding() | |
| 130 | − .imePadding() | |
| 131 | − .verticalScroll(rememberScrollState()) | |
| 132 | − .padding(24.dp), | |
| 133 | − verticalArrangement = Arrangement.spacedBy(16.dp), | |
| 134 | − horizontalAlignment = Alignment.CenterHorizontally, | |
| 135 | − ) { | |
| 136 | − Spacer(Modifier.height(24.dp)) | |
| 137 | − Wordmark() | |
| 138 | − Spacer(Modifier.height(8.dp)) | |
| 139 | − | |
| 140 | − Text( | |
| 141 | − "On the web, open Settings → API tokens, tick Mobile app and create the token. " + | |
| 142 | − "Then scan the code that appears beside it.", | |
| 143 | − style = MaterialTheme.typography.bodyMedium, | |
| 144 | − ) | |
| 145 | − | |
| 146 | − Button( | |
| 147 | − enabled = !verifying, | |
| 148 | − onClick = { | |
| 149 | − scanLauncher.launch( | |
| 150 | − ScanOptions().apply { | |
| 151 | − setDesiredBarcodeFormats(ScanOptions.QR_CODE) | |
| 152 | − setPrompt("Point at the code on Vigil's API tokens page") | |
| 153 | − setBeepEnabled(false) | |
| 154 | − setOrientationLocked(false) | |
| 155 | − captureActivity = CaptureActivity::class.java | |
| 156 | − }, | |
| 157 | − ) | |
| 158 | − }, | |
| 159 | − modifier = Modifier.fillMaxWidth().height(52.dp), | |
| 160 | − ) { | |
| 161 | − Icon(Icons.Default.QrCodeScanner, contentDescription = null, modifier = Modifier.size(20.dp)) | |
| 162 | − Spacer(Modifier.width(10.dp)) | |
| 163 | − Text("Scan code") | |
| 164 | − } | |
| 165 | − | |
| 166 | − if (!byHand) { | |
| 167 | − TextButton(onClick = { byHand = true }) { Text("Enter it by hand instead") } | |
| 168 | − } | |
| 169 | − | |
| 170 | − AnimatedVisibility(visible = byHand) { | |
| 171 | − Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { | |
| 172 | − OutlinedTextField( | |
| 173 | − value = baseUrl, | |
| 174 | − onValueChange = { baseUrl = it }, | |
| 175 | − label = { Text("Server") }, | |
| 176 | − placeholder = { Text("https://vigil.neiam.org") }, | |
| 177 | − singleLine = true, | |
| 178 | − modifier = Modifier.fillMaxWidth(), | |
| 179 | − keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri), | |
| 180 | − ) | |
| 181 | − OutlinedTextField( | |
| 182 | − value = token, | |
| 183 | − onValueChange = { token = it }, | |
| 184 | − label = { Text("API token") }, | |
| 185 | − singleLine = true, | |
| 186 | − visualTransformation = PasswordVisualTransformation(), | |
| 187 | − modifier = Modifier.fillMaxWidth(), | |
| 188 | − keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password), | |
| 189 | − ) | |
| 190 | − OutlinedButton( | |
| 191 | − enabled = !verifying && token.isNotBlank() && normaliseBase(baseUrl) != null, | |
| 192 | − onClick = { verify(TokenStore.Credentials(normaliseBase(baseUrl)!!, token.trim())) }, | |
| 193 | − modifier = Modifier.fillMaxWidth(), | |
| 194 | − ) { Text("Connect") } | |
| 195 | − } | |
| 196 | − } | |
| 197 | − | |
| 198 | − if (verifying) CircularProgressIndicator(modifier = Modifier.size(28.dp)) | |
| 199 | − error?.let { Text(it, color = MaterialTheme.colorScheme.error) } | |
| 200 | − | |
| 201 | − partial?.let { (credentials, who, missing) -> | |
| 202 | − Surface( | |
| 203 | − color = MaterialTheme.colorScheme.surfaceVariant, | |
| 204 | − shape = Corners.card, | |
| 205 | − modifier = Modifier.fillMaxWidth(), | |
| 206 | − ) { | |
| 207 | − Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) { | |
| 208 | − Text("Signed in as $who", color = MaterialTheme.colorScheme.primary) | |
| 209 | − Text( | |
| 210 | − "This token lacks ${missing.joinToString(", ")}, part of the Mobile app " + | |
| 211 | − "bundle, so some screens will be empty or refused. Make a new token " + | |
| 212 | − "with Mobile app ticked, or pair with this one anyway.", | |
| 213 | − style = MaterialTheme.typography.bodySmall, | |
| 214 | − color = MaterialTheme.colorScheme.error, | |
| 215 | − ) | |
| 216 | − OutlinedButton( | |
| 217 | − onClick = { | |
| 218 | − tokens.save(credentials) | |
| 219 | − onPaired() | |
| 220 | − }, | |
| 221 | − modifier = Modifier.fillMaxWidth(), | |
| 222 | − ) { Text("Pair anyway") } | |
| 223 | − } | |
| 224 | − } | |
| 225 | − } | |
| 226 | − } | |
| 227 | −} |
added
android/app/src/main/java/org/neiam/vigil/app/auth/VigilLoginHost.kt
+59
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/AuthLink.kt
+79
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/AuthLinkHandler.kt
+137
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/Autofill.kt
+32
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/HelpScreen.kt
+148
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/LoginApi.kt
+244
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/LoginFlows.kt
+183
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/LoginHost.kt
+111
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/LoginScreen.kt
+308
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/Pkce.kt
+30
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/QrScanner.kt
+70
−0
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/auth/login/Sheets.kt
+333
−0
Click to load diff…
added
android/app/src/test/java/org/neiam/vigil/app/auth/login/AuthLinkTest.kt
+79
−0
Click to load diff…
added
android/app/src/test/java/org/neiam/vigil/app/auth/login/AuthMethodsTest.kt
+80
−0
Click to load diff…
added
android/app/src/test/java/org/neiam/vigil/app/auth/login/LoginApiTest.kt
+44
−0
Click to load diff…
added
android/app/src/test/java/org/neiam/vigil/app/auth/login/PkceTest.kt
+27
−0
Click to load diff…
modified
android/gradle/libs.versions.toml
+3
−0
Click to load diff…
Parents: 83f70ee