Skip to content

Commit

Permalink
feat: use shorter channel tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
horsefacts committed Aug 19, 2024
1 parent ada822b commit c8122d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/relay/src/channels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("channel store", () => {
describe("open", () => {
test("opens a channel and returns the token", async () => {
const channel = await channels.open();
expect(channel._unsafeUnwrap()).toMatch(/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/);
expect(channel._unsafeUnwrap()).toMatch(/[2-9A-HJ-NP-Z]{8}/);
});
});

Expand Down
4 changes: 2 additions & 2 deletions apps/relay/src/channels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Redis } from "ioredis";
import { ResultAsync, err, ok } from "neverthrow";
import { randomUUID } from "crypto";
import { RelayAsyncResult, RelayError } from "./errors";
import { generateChannelToken } from "./tokens";

interface ChannelStoreOpts {
redisUrl: string;
Expand All @@ -18,7 +18,7 @@ export class ChannelStore<T> {
}

async open(state?: T): RelayAsyncResult<string> {
const channelToken = randomUUID();
const channelToken = generateChannelToken();
return ResultAsync.fromPromise(
this.redis.set(channelToken, JSON.stringify(state ?? {}), "EX", this.ttl),
(err) => new RelayError("unavailable", err as Error),
Expand Down
4 changes: 2 additions & 2 deletions apps/relay/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("relay server", () => {

expect(response.status).toBe(201);
const { channelToken, url, connectUri, nonce, ...rest } = response.data;
expect(channelToken).toMatch(/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/);
expect(channelToken).toMatch(/[2-9A-HJ-NP-Z]{8}/);
expect(url).toMatch("https://warpcast.com/~/sign-in-with-farcaster");
expect(url).toBe(connectUri);
expect(rest).toStrictEqual({});
Expand Down Expand Up @@ -106,7 +106,7 @@ describe("relay server", () => {
expect(params.get("expirationTime")).toBe(expirationTime);
expect(params.get("requestId")).toBe(requestId);
expect(params.get("redirectUrl")).toBe(redirectUrl);
expect(channelToken).toMatch(/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/);
expect(channelToken).toMatch(/[2-9A-HJ-NP-Z]{8}/);
expect(nonce).toBe(customNonce);
expect(url).toBe(connectUri);
expect(rest).toStrictEqual({});
Expand Down
16 changes: 16 additions & 0 deletions apps/relay/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import crypto from "crypto";

const ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";

export function generateChannelToken(length = 8): string {
const bytes = crypto.randomBytes(length);
let id = "";
for (let i = 0; i < length; i++) {
const byte = bytes[i];
if (byte === undefined) {
throw new Error("Error generating channel token");
}
id += ALPHABET[byte % ALPHABET.length];
}
return id;
}

0 comments on commit c8122d1

Please sign in to comment.