Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/server/src/auth/PairingGrantStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
}).pipe(Effect.provide(layerPairingGrantStore())),
);

it.effect("accepts a one-time token typed in lowercase", () =>
Effect.gen(function* () {
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
const issued = yield* bootstrapCredentials.issueOneTimeToken();
const first = yield* bootstrapCredentials.consume(issued.credential.toLowerCase());
const second = yield* Effect.flip(bootstrapCredentials.consume(issued.credential));

expect(first.method).toBe("one-time-token");
expect(second._tag).toBe("UnknownBootstrapCredentialError");
}).pipe(Effect.provide(layerPairingGrantStore())),
);

it.effect("atomically consumes a one-time token when multiple requests race", () =>
Effect.gen(function* () {
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
Expand Down Expand Up @@ -176,6 +188,23 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
),
);

it.effect("matches a seeded desktop bootstrap credential exactly", () =>
Effect.gen(function* () {
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
const exact = yield* bootstrapCredentials.consume("abcdefghjkmn");
const otherCase = yield* Effect.flip(bootstrapCredentials.consume("ABCDEFGHJKMN"));

expect(exact.method).toBe("desktop-bootstrap");
expect(otherCase._tag).toBe("UnknownBootstrapCredentialError");
}).pipe(
Effect.provide(
layerPairingGrantStore({
desktopBootstrapToken: "abcdefghjkmn",
}),
),
),
);

it.effect("reports seeded desktop bootstrap credentials as expired after their ttl", () =>
Effect.gen(function* () {
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
Expand Down
15 changes: 13 additions & 2 deletions apps/server/src/auth/PairingGrantStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ const PAIRING_TOKEN_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
const PAIRING_TOKEN_LENGTH = 12;
const PAIRING_TOKEN_REJECTION_LIMIT =
Math.floor(256 / PAIRING_TOKEN_ALPHABET.length) * PAIRING_TOKEN_ALPHABET.length;
const PAIRING_TOKEN_PATTERN = new RegExp(
`^[${PAIRING_TOKEN_ALPHABET}]{${PAIRING_TOKEN_LENGTH}}$`,
"i",
);

export const make = Effect.gen(function* () {
const crypto = yield* Crypto.Crypto;
Expand Down Expand Up @@ -538,12 +542,19 @@ export const make = Effect.gen(function* () {
return yield* seededResult.error;
}

// Pairing codes are issued in uppercase, but a phone keyboard types
// them in lowercase. Only codes in the issued format are folded, so
// other credentials still have to match exactly.
const pairingCredential = PAIRING_TOKEN_PATTERN.test(credential)
? credential.toUpperCase()
: credential;

// The scope check is part of the UPDATE's WHERE clause so a rejected
// request cannot consume a one-time link. The re-check below only
// explains why nothing matched.
const consumed = yield* pairingLinks
.consumeAvailable({
credential,
credential: pairingCredential,
proofKeyThumbprint: input?.proofKeyThumbprint ?? null,
...(input?.requestedScopes !== undefined
? { requestedScopes: input.requestedScopes }
Expand All @@ -568,7 +579,7 @@ export const make = Effect.gen(function* () {
}

const matching = yield* pairingLinks
.getByCredential({ credential })
.getByCredential({ credential: pairingCredential })
.pipe(Effect.mapError((cause) => new BootstrapCredentialLookupError({ cause })));
if (Option.isNone(matching)) {
return yield* new UnknownBootstrapCredentialError({});
Expand Down
Loading