Skip to content

Commit 2ddc8f0

Browse files
committed
fix(mobile): accept a pairing code however it was typed
1 parent 2f486ab commit 2ddc8f0

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎apps/mobile/src/features/connection/ConnectionsNewRouteScreen.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CameraView, useCameraPermissions } from "expo-camera";
22
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
33
import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
4+
import { normalizePairingCode } from "@t3tools/shared/remote";
45
import { AsyncResult } from "effect/unstable/reactivity";
56
import { useCallback, useEffect, useRef, useState } from "react";
67
import { Alert, Linking, Platform, ScrollView, View } from "react-native";
@@ -78,7 +79,10 @@ export function ConnectionsNewRouteScreen({
7879
}, []);
7980

8081
const handleCodeChange = useCallback((value: string) => {
81-
setCodeInput(value);
82+
// Only what the user types is repaired. A scanned or deep-linked code is
83+
// written straight to state and never reaches this handler, which matters
84+
// because those can carry a hosted token from a different alphabet.
85+
setCodeInput(normalizePairingCode(value));
8286
}, []);
8387

8488
const openScanner = useCallback(async () => {

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ import { Button } from "../ui/button";
8686
import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty";
8787
import { AnimatedHeight } from "../AnimatedHeight";
8888
import { Textarea } from "../ui/textarea";
89-
import { getPairingTokenFromUrl, setPairingTokenOnUrl } from "../../pairingUrl";
89+
import {
90+
getPairingTokenFromUrl,
91+
normalizePairingCode,
92+
setPairingTokenOnUrl,
93+
} from "../../pairingUrl";
9094
import { readHostedPairingRequest } from "../../hostedPairing";
9195
import {
9296
createServerPairingCredential,
@@ -2423,7 +2427,9 @@ export function ConnectionsSettings() {
24232427
<span className="mb-1.5 block text-xs font-medium text-foreground">Pairing code</span>
24242428
<Input
24252429
value={savedBackendPairingCode}
2426-
onChange={(event) => setSavedBackendPairingCode(event.target.value)}
2430+
onChange={(event) =>
2431+
setSavedBackendPairingCode(normalizePairingCode(event.target.value))
2432+
}
24272433
placeholder="PAIRCODE"
24282434
disabled={isAddingSavedBackend}
24292435
spellCheck={false}

‎apps/web/src/pairingUrl.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {
22
getPairingTokenFromUrl,
3+
normalizePairingCode,
34
setPairingTokenOnUrl,
45
stripPairingTokenFromUrl,
56
} from "@t3tools/shared/remote";

‎packages/shared/src/remote.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
RemoteBackendUrlMissingError,
66
RemotePairingTokenMissingError,
77
RemotePairingUrlInvalidError,
8+
normalizePairingCode,
89
resolveRemotePairingTarget,
910
} from "./remote.ts";
1011

@@ -200,6 +201,29 @@ describe("remote", () => {
200201
);
201202
});
202203

204+
it("repairs a hand-typed pairing code whatever spacing or case it arrived in", () => {
205+
for (const typed of ["abcd2345efgh", "ABCD-2345-EFGH", "abcd 2345 efgh", " AbCd-2345 EfGh "]) {
206+
expect(normalizePairingCode(typed.trim())).toBe("ABCD2345EFGH");
207+
}
208+
});
209+
210+
it("does not normalize a token that came out of a pairing URL", () => {
211+
// Hosted tokens are issued elsewhere, are not drawn from the uppercase
212+
// pairing alphabet, and reach resolveRemotePairingTarget through the same
213+
// pairingCode field - so the normalizer must stay out of this path.
214+
expect(
215+
resolveRemotePairingTarget({
216+
pairingUrl: "https://remote.example.com/pair#token=MiXeD-case_token",
217+
}).credential,
218+
).toBe("MiXeD-case_token");
219+
expect(
220+
resolveRemotePairingTarget({
221+
host: "https://remote.example.com",
222+
pairingCode: "MiXeD-case_token",
223+
}).credential,
224+
).toBe("MiXeD-case_token");
225+
});
226+
203227
it("preserves URL parsing causes with their input source", () => {
204228
let pairingUrlError: unknown;
205229
try {

‎packages/shared/src/remote.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ export const readHostedPairingRequest = (url: URL): HostedPairingRequest | null
185185
};
186186
};
187187

188+
/**
189+
* Repairs a pairing code a person typed. Issued codes only use
190+
* `PAIRING_TOKEN_ALPHABET` (uppercase, no separators), so this cannot alter a
191+
* real one. Call it where someone types and nowhere else: a pasted URL reaches
192+
* `resolveRemotePairingTarget` through the same `pairingCode` field, and a
193+
* hosted token has its own alphabet that normalizing would corrupt.
194+
*/
195+
export const normalizePairingCode = (code: string): string =>
196+
code.replace(/[\s-]+/g, "").toUpperCase();
197+
188198
export const resolveRemotePairingTarget = (input: {
189199
readonly pairingUrl?: string;
190200
readonly host?: string;

0 commit comments

Comments
 (0)