Skip to content

Commit 362b093

Browse files
only21milclaude
andcommitted
fix(auth): accept a typed pairing code regardless of letter case
Pairing codes are issued as 12 uppercase characters, but the server matched them byte for byte, so a code typed in lowercase (the mobile Code field does not auto-capitalize) was rejected with auth_invalid. Fold a credential to uppercase before the pairing-link lookup when it has the issued code format. Seeded and rotating desktop bootstrap credentials are still matched exactly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fb99076 commit 362b093

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎apps/server/src/auth/PairingGrantStore.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
9393
}).pipe(Effect.provide(layerPairingGrantStore())),
9494
);
9595

96+
it.effect("accepts a one-time token typed in lowercase", () =>
97+
Effect.gen(function* () {
98+
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
99+
const issued = yield* bootstrapCredentials.issueOneTimeToken();
100+
const first = yield* bootstrapCredentials.consume(issued.credential.toLowerCase());
101+
const second = yield* Effect.flip(bootstrapCredentials.consume(issued.credential));
102+
103+
expect(first.method).toBe("one-time-token");
104+
expect(second._tag).toBe("UnknownBootstrapCredentialError");
105+
}).pipe(Effect.provide(layerPairingGrantStore())),
106+
);
107+
96108
it.effect("atomically consumes a one-time token when multiple requests race", () =>
97109
Effect.gen(function* () {
98110
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
@@ -176,6 +188,23 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
176188
),
177189
);
178190

191+
it.effect("matches a seeded desktop bootstrap credential exactly", () =>
192+
Effect.gen(function* () {
193+
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
194+
const exact = yield* bootstrapCredentials.consume("abcdefghjkmn");
195+
const otherCase = yield* Effect.flip(bootstrapCredentials.consume("ABCDEFGHJKMN"));
196+
197+
expect(exact.method).toBe("desktop-bootstrap");
198+
expect(otherCase._tag).toBe("UnknownBootstrapCredentialError");
199+
}).pipe(
200+
Effect.provide(
201+
layerPairingGrantStore({
202+
desktopBootstrapToken: "abcdefghjkmn",
203+
}),
204+
),
205+
),
206+
);
207+
179208
it.effect("reports seeded desktop bootstrap credentials as expired after their ttl", () =>
180209
Effect.gen(function* () {
181210
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;

‎apps/server/src/auth/PairingGrantStore.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ const PAIRING_TOKEN_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
253253
const PAIRING_TOKEN_LENGTH = 12;
254254
const PAIRING_TOKEN_REJECTION_LIMIT =
255255
Math.floor(256 / PAIRING_TOKEN_ALPHABET.length) * PAIRING_TOKEN_ALPHABET.length;
256+
const PAIRING_TOKEN_PATTERN = new RegExp(
257+
`^[${PAIRING_TOKEN_ALPHABET}]{${PAIRING_TOKEN_LENGTH}}$`,
258+
"i",
259+
);
256260

257261
export const make = Effect.gen(function* () {
258262
const crypto = yield* Crypto.Crypto;
@@ -538,12 +542,19 @@ export const make = Effect.gen(function* () {
538542
return yield* seededResult.error;
539543
}
540544

545+
// Pairing codes are issued in uppercase, but a phone keyboard types
546+
// them in lowercase. Only codes in the issued format are folded, so
547+
// other credentials still have to match exactly.
548+
const pairingCredential = PAIRING_TOKEN_PATTERN.test(credential)
549+
? credential.toUpperCase()
550+
: credential;
551+
541552
// The scope check is part of the UPDATE's WHERE clause so a rejected
542553
// request cannot consume a one-time link. The re-check below only
543554
// explains why nothing matched.
544555
const consumed = yield* pairingLinks
545556
.consumeAvailable({
546-
credential,
557+
credential: pairingCredential,
547558
proofKeyThumbprint: input?.proofKeyThumbprint ?? null,
548559
...(input?.requestedScopes !== undefined
549560
? { requestedScopes: input.requestedScopes }
@@ -568,7 +579,7 @@ export const make = Effect.gen(function* () {
568579
}
569580

570581
const matching = yield* pairingLinks
571-
.getByCredential({ credential })
582+
.getByCredential({ credential: pairingCredential })
572583
.pipe(Effect.mapError((cause) => new BootstrapCredentialLookupError({ cause })));
573584
if (Option.isNone(matching)) {
574585
return yield* new UnknownBootstrapCredentialError({});

0 commit comments

Comments
 (0)