Colota - Android GPS Location tracker
refactor(connection): turn the certificate actions into rows (#874)
Max Dietrich1f355d41dcdc
diff --git a/apps/docs/docs/configuration/mtls.md b/apps/docs/docs/configuration/mtls.mdindex 38300efc..9ee3d4aa 100644--- a/apps/docs/docs/configuration/mtls.md+++ b/apps/docs/docs/configuration/mtls.md@@ -18,7 +18,7 @@ If your cert is already installed in Android's KeyChain (via Android Settings ->1. Open Colota -> Settings -> Connection -> **Client certificate**2. Tap **Pick from device certificates**-3. Android's system dialog appears. Select your cert.+3. Android's system dialog appears. Select your cert. With no certificate installed, Android closes the dialog at once and Colota says nothing was picked.4. The screen now reads **Valid** with the expiry date and "device credential store", plus the subject and issuer.Colota only remembers which cert you picked - the key stays in the device credential store, survives reinstalling Colota and is never backed up.diff --git a/apps/mobile/src/components/features/settings/ConnectionSettings.tsx b/apps/mobile/src/components/features/settings/ConnectionSettings.tsxindex 73e1176f..00760312 100644--- a/apps/mobile/src/components/features/settings/ConnectionSettings.tsx+++ b/apps/mobile/src/components/features/settings/ConnectionSettings.tsx@@ -4,7 +4,7 @@*/import React, { useState, useCallback, useEffect } from "react"-import { Text, StyleSheet, View, ActivityIndicator, Keyboard } from "react-native"+import { StyleSheet, View, ActivityIndicator, Keyboard } from "react-native"import {Braces,CircleAlert,@@ -24,7 +24,6 @@ import NativeLocationService from "../../../services/NativeLocationService"import { endpointCarriesKey, endpointExample, isEndpointAllowed } from "../../../utils/settingsValidation"import { isTraccarJsonFormat, isOverlandFormat } from "../../../utils/apiPayload"import { ensureLocalNetworkPermission } from "../../../services/LocationServicePermission"-import { fontSizes, fonts, lineHeights } from "../../../styles/typography"import { useTheme } from "../../../hooks/useTheme"import { space } from "../../../constants"import { logger } from "../../../utils/logger"@@ -275,10 +274,6 @@ export function ConnectionSettings({return (<View>- <Text style={[styles.intro, { color: colors.textSecondary }]}>- Where locations are sent and how the server knows it is you. Changes apply at once.- </Text>-<SectionTitle>Server</SectionTitle><Card rows><StateLine@@ -336,9 +331,7 @@ export function ConnectionSettings({loading={test?.kind === "testing"}testID="test-connection-btn"/>- <FieldMessage>- {testBlocker ?? "Sends your latest recorded location to this endpoint with your credentials."}- </FieldMessage>+ {testBlocker ? <FieldMessage>{testBlocker}</FieldMessage> : null}</View>{test?.kind === "testing" && (@@ -402,12 +395,6 @@ export function ConnectionSettings({}const styles = StyleSheet.create({- intro: {- fontSize: fontSizes.body,- ...fonts.regular,- lineHeight: lineHeights.body,- marginBottom: space.lg- },groupTop: {marginTop: space.xl},diff --git a/apps/mobile/src/components/features/settings/MtlsSection.tsx b/apps/mobile/src/components/features/settings/MtlsSection.tsxindex 0d33ac86..34d15b4e 100644--- a/apps/mobile/src/components/features/settings/MtlsSection.tsx+++ b/apps/mobile/src/components/features/settings/MtlsSection.tsx@@ -4,11 +4,18 @@*/import React, { useState, useCallback, useEffect } from "react"-import { Text, StyleSheet, View, ActivityIndicator } from "react-native"-import { CircleAlert, ShieldCheck, TriangleAlert, type LucideIcon } from "lucide-react-native"+import { StyleSheet, View, ActivityIndicator } from "react-native"+import {+ CircleAlert,+ Download,+ FileKey,+ KeyRound,+ ShieldCheck,+ TriangleAlert,+ type LucideIcon+} from "lucide-react-native"import { useTheme } from "../../../hooks/useTheme"-import { fonts, fontSizes, lineHeights } from "../../../styles/typography"-import { SectionTitle, Card, Divider, Button, FieldMessage, TextField, StateLine, StatRow } from "../../index"+import { SectionTitle, Card, Divider, Button, FieldMessage, ListItem, TextField, StateLine, StatRow } from "../../index"import NativeLocationService from "../../../services/NativeLocationService"import { showChoice, showConfirm } from "../../../services/modalService"import { ClientCertInfoResult } from "../../../types/global"@@ -32,9 +39,11 @@ function errMsg(map: Record<string, string>, err: any, fallback: string): stringtype ImportState ={ kind: "idle" } | { kind: "picked"; b64: string; password: string; importing: boolean; error: string | null }-const PICK_LINE = "Key stays in the device credential store, survives reinstalling Colota, never backed up."-const IMPORT_LINE =- "Key moves into the Android Keystore and never leaves the device. The file password is used once and discarded. Not backed up, import again after a restore."+const PICK_LINE = "Key stays in the device's credential store, survives a reinstall, never backed up."+const IMPORT_LINE = "Key moves into the Android Keystore and the file password is discarded. Not backed up."+// Android reports an empty picker and a cancelled one the same way, so the line covers both.+const NO_PICK_LINE =+ "No certificate was picked. If the list was empty, install one in Android settings under Encryption & credentials first."export function MtlsSection() {const { colors } = useTheme()@@ -42,6 +51,7 @@ export function MtlsSection() {const [caInfo, setCaInfo] = useState<ClientCertInfoResult | null>(null)const [importState, setImportState] = useState<ImportState>({ kind: "idle" })const [clientPickError, setClientPickError] = useState<string | null>(null)+ const [pickNote, setPickNote] = useState<string | null>(null)const [caError, setCaError] = useState<string | null>(null)const refresh = useCallback(async () => {@@ -94,9 +104,13 @@ export function MtlsSection() {const handlePickKeyChain = useCallback(async () => {setClientPickError(null)+ setPickNote(null)try {const result = await NativeLocationService.pickKeyChainCert()- if (!result) return+ if (!result) {+ setPickNote(NO_PICK_LINE)+ return+ }await refresh()} catch (err: any) {logger.error("[MtlsSection] pickKeyChainCert failed:", err)@@ -106,6 +120,7 @@ export function MtlsSection() {const handlePickFile = useCallback(async () => {setClientPickError(null)+ setPickNote(null)try {const b64 = await NativeLocationService.pickClientCertFile()if (!b64) return@@ -211,20 +226,45 @@ export function MtlsSection() {removeTestID="remove-cert-btn"/>) : (- <View style={styles.block}>- <Text style={[styles.description, { color: colors.textSecondary }]}>- None. Only for a server that asks for one.- </Text>- <View>- <Button variant="secondary" onPress={handlePickKeyChain} title="Pick from device certificates" />- <FieldMessage>{PICK_LINE}</FieldMessage>- </View>- <View>- <Button variant="secondary" onPress={handlePickFile} title="Import .p12 / .pfx" />- <FieldMessage>{IMPORT_LINE}</FieldMessage>- {clientPickError && <FieldMessage variant="error">{clientPickError}</FieldMessage>}- </View>- </View>+ <>+ <StateLine+ icon={ShieldCheck}+ iconColor={colors.textSecondary}+ label="None"+ caption="Only for a server that asks for one"+ testID="certificate-state"+ />+ <Divider tight />+ <ListItem+ testID="pick-keychain-row"+ icon={KeyRound}+ label="Pick from device certificates"+ sub={PICK_LINE}+ subLines={2}+ accessibilityHint="Opens the system certificate picker"+ onPress={handlePickKeyChain}+ />+ <Divider tight inset />+ <ListItem+ testID="import-p12-row"+ icon={FileKey}+ trailingIcon={Download}+ label="Import .p12 / .pfx"+ sub={IMPORT_LINE}+ subLines={2}+ accessibilityHint="Opens the file picker"+ onPress={handlePickFile}+ />+ {clientPickError ? (+ <View style={styles.rowError}>+ <FieldMessage variant="error">{clientPickError}</FieldMessage>+ </View>+ ) : pickNote ? (+ <View style={styles.rowError}>+ <FieldMessage>{pickNote}</FieldMessage>+ </View>+ ) : null}+ </>)}</Card>@@ -237,19 +277,34 @@ export function MtlsSection() {onReplace={handlePickServerCa}onRemove={handleClearServerCa}removeTestID="remove-ca-btn"+ note="Encrypted on this device and included in encrypted backups."/>) : (- <View style={styles.block}>- <Text style={[styles.description, { color: colors.textSecondary }]}>- None. Public CAs such as Let's Encrypt work without it. Add one only for a private or self-signed CA.- Certificates you installed on the device are not used.- </Text>- <View>- <Button variant="secondary" onPress={handlePickServerCa} title="Import CA (.crt / .pem)" />- <FieldMessage>Encrypted on this device and included in encrypted backups.</FieldMessage>- {caError && <FieldMessage variant="error">{caError}</FieldMessage>}- </View>- </View>+ <>+ <StateLine+ icon={ShieldCheck}+ iconColor={colors.textSecondary}+ label="None"+ caption="Not needed for a public certificate"+ testID="ca-state"+ />+ <Divider tight />+ <ListItem+ testID="import-ca-row"+ icon={FileKey}+ trailingIcon={Download}+ label="Import CA (.crt / .pem)"+ sub="For a private or self-signed CA. CAs you installed in Android settings are not used."+ subLines={2}+ accessibilityHint="Opens the file picker"+ onPress={handlePickServerCa}+ />+ {caError ? (+ <View style={styles.rowError}>+ <FieldMessage variant="error">{caError}</FieldMessage>+ </View>+ ) : null}+ </>)}</Card></>@@ -270,7 +325,8 @@ function CertificateCard({issuer,onReplace,onRemove,- removeTestID+ removeTestID,+ note}: {state: CertificateStatesubject?: string@@ -278,6 +334,7 @@ function CertificateCard({onReplace: () => voidonRemove: () => voidremoveTestID: string+ note?: string}) {const { colors } = useTheme()const tone =@@ -301,6 +358,7 @@ function CertificateCard({<View style={styles.details}>{subject ? <StatRow label="Subject" value={shortenDn(subject)} /> : null}{issuer ? <StatRow label="Issuer" value={shortenDn(issuer)} /> : null}+ {note ? <FieldMessage>{note}</FieldMessage> : null}<View style={styles.buttonRow}><Button style={styles.flex1} variant="ghost" title="Replace" onPress={onReplace} /><Button style={styles.flex1} variant="danger" title="Remove" onPress={onRemove} testID={removeTestID} />@@ -333,15 +391,13 @@ const styles = StyleSheet.create({paddingBottom: space.lg,gap: space.lg},+ rowError: {+ paddingBottom: space.lg+ },details: {paddingTop: space.md,paddingBottom: space.lg},- description: {- fontSize: fontSizes.description,- ...fonts.regular,- lineHeight: lineHeights.description- },buttonRow: {flexDirection: "row",gap: space.md,diff --git a/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx b/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsxindex 2fbbfb05..2f311913 100644--- a/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx+++ b/apps/mobile/src/components/features/settings/__tests__/ConnectionSettings.test.tsx@@ -253,10 +253,12 @@ describe("ConnectionSettings", () => {expect(getByText("Needs one recorded location to send. Start tracking first.")).toBeTruthy()})- it("says what it sends while enabled", () => {- const { getByText } = renderComponent()+ it("shows no line under Test connection while it can run", () => {+ const { queryByText, getByTestId } = renderComponent()- expect(getByText("Sends your latest recorded location to this endpoint with your credentials.")).toBeTruthy()+ expect(getByTestId("test-connection-btn").props.accessibilityState.disabled).toBe(false)+ expect(queryByText(/^Needs one recorded location/)).toBeNull()+ expect(queryByText(/^Sends your latest recorded location/)).toBeNull()})it("reports Reachable with the code and the time, and persists nothing itself", async () => {diff --git a/apps/mobile/src/components/features/settings/__tests__/MtlsSection.test.tsx b/apps/mobile/src/components/features/settings/__tests__/MtlsSection.test.tsxindex 4efd4e36..044b5089 100644--- a/apps/mobile/src/components/features/settings/__tests__/MtlsSection.test.tsx+++ b/apps/mobile/src/components/features/settings/__tests__/MtlsSection.test.tsx@@ -18,6 +18,13 @@ jest.mock("../../../index", () => {R.createElement(Text, null, title)),FieldMessage: ({ children }: any) => R.createElement(Text, null, children),+ ListItem: ({ label, sub, onPress, testID }: any) =>+ R.createElement(+ Pressable,+ { onPress, testID, accessibilityRole: "button" },+ R.createElement(Text, null, label),+ R.createElement(Text, null, sub)+ ),StateLine: ({ label, caption, testID }: any) =>R.createElement(View, { testID }, R.createElement(Text, null, label), R.createElement(Text, null, caption)),StatRow: ({ label, value }: any) =>@@ -100,19 +107,23 @@ describe("MtlsSection", () => {expect(await findByText("Pick from device certificates")).toBeTruthy()expect(- getByText("Key stays in the device credential store, survives reinstalling Colota, never backed up.")+ getByText("Key stays in the device's credential store, survives a reinstall, never backed up.")).toBeTruthy()expect(getByText("Import .p12 / .pfx")).toBeTruthy()- expect(getByText(/^Key moves into the Android Keystore/)).toBeTruthy()- expect(getByText("None. Only for a server that asks for one.")).toBeTruthy()+ expect(+ getByText("Key moves into the Android Keystore and the file password is discarded. Not backed up.")+ ).toBeTruthy()+ expect(getByText("Only for a server that asks for one")).toBeTruthy()})- it("says when a CA is needed at all and where it lives", async () => {+ it("says when a CA is needed at all", async () => {const { findByText, getByText } = render(<MtlsSection />)expect(await findByText("Import CA (.crt / .pem)")).toBeTruthy()- expect(getByText(/^None\. Public CAs such as Let's Encrypt work without it\./)).toBeTruthy()- expect(getByText("Encrypted on this device and included in encrypted backups.")).toBeTruthy()+ expect(getByText("Not needed for a public certificate")).toBeTruthy()+ expect(+ getByText("For a private or self-signed CA. CAs you installed in Android settings are not used.")+ ).toBeTruthy()})})@@ -134,6 +145,7 @@ describe("MtlsSection", () => {fireEvent.press(await findByText("Pick from device certificates"))await waitFor(() => expect(mockPickKeyChainCert).toHaveBeenCalled())expect(mockGetClientCertInfo).toHaveBeenCalledTimes(1)+ expect(await findByText(/^No certificate was picked\. If the list was empty/)).toBeTruthy()})})@@ -259,6 +271,7 @@ describe("MtlsSection", () => {)await waitFor(() => expect(mockClearServerCa).toHaveBeenCalled())expect(getByText("Valid")).toBeTruthy()+ expect(getByText("Encrypted on this device and included in encrypted backups.")).toBeTruthy()})it("names the consequence of an expired CA", async () => {diff --git a/apps/mobile/src/screens/MtlsSettingsScreen.tsx b/apps/mobile/src/screens/MtlsSettingsScreen.tsxindex 2202a727..48c7fad2 100644--- a/apps/mobile/src/screens/MtlsSettingsScreen.tsx+++ b/apps/mobile/src/screens/MtlsSettingsScreen.tsx@@ -4,17 +4,13 @@*/import React from "react"-import { Text, StyleSheet, View, ScrollView } from "react-native"+import { StyleSheet, ScrollView } from "react-native"import { ScreenProps } from "../types/global"-import { useTheme } from "../hooks/useTheme"-import { fontSizes, fonts, lineHeights } from "../styles/typography"import { Container } from "../components"import { MtlsSection } from "../components/features/settings/MtlsSection"import { space } from "../constants"export function MtlsSettingsScreen({}: ScreenProps) {- const { colors } = useTheme()-return (<Container><ScrollView@@ -22,13 +18,6 @@ export function MtlsSettingsScreen({}: ScreenProps) {keyboardShouldPersistTaps="handled"showsVerticalScrollIndicator={false}>- <View style={styles.header}>- <Text style={[styles.subtitle, { color: colors.textSecondary }]}>- For a server or reverse proxy that requires mutual TLS. Not needed for tokens or passwords. The private key- never leaves this device.- </Text>- </View>-<MtlsSection /></ScrollView></Container>@@ -40,13 +29,5 @@ const styles = StyleSheet.create({paddingHorizontal: space.lg,paddingTop: space.lg,paddingBottom: space.xxl- },- header: {- marginBottom: space.lg- },- subtitle: {- fontSize: fontSizes.body,- ...fonts.regular,- lineHeight: lineHeights.body}})