Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(web): add Slack OAuth state helpers
Richie McIlroyb9a75c948120
diff --git a/apps/web/__tests__/unit/slack-oauth-state.test.ts b/apps/web/__tests__/unit/slack-oauth-state.test.tsnew file mode 100644index 000000000..a3977a327--- /dev/null+++ b/apps/web/__tests__/unit/slack-oauth-state.test.ts@@ -0,0 +1,59 @@+import { describe, expect, it } from "vitest";+import {+ createSlackOAuthState,+ verifySlackOAuthState,+} from "@/lib/slack/oauth-state";++describe("Slack OAuth state", () => {+ const secret = "client-secret";+ const now = 1720000000000;++ it("round-trips a signed, time-bounded state", () => {+ const state = createSlackOAuthState({+ organizationId: "org123",+ userId: "user123",+ secret,+ now,+ nonce: "nonce123",+ });++ expect(verifySlackOAuthState({ state, secret, now })).toMatchObject({+ v: 1,+ organizationId: "org123",+ userId: "user123",+ issuedAt: 1720000000,+ nonce: "nonce123",+ });+ });++ it("rejects tampering, expiry, and states from the future", () => {+ const state = createSlackOAuthState({+ organizationId: "org123",+ userId: "user123",+ secret,+ now,+ nonce: "nonce123",+ });+ expect(+ verifySlackOAuthState({+ state: `${state}x`,+ secret,+ now,+ }),+ ).toBeNull();+ expect(+ verifySlackOAuthState({+ state,+ secret,+ now: now + 601_000,+ }),+ ).toBeNull();+ expect(+ verifySlackOAuthState({+ state,+ secret,+ now: now - 61_000,+ }),+ ).toBeNull();+ });+});diff --git a/apps/web/lib/slack/oauth-state.ts b/apps/web/lib/slack/oauth-state.tsnew file mode 100644index 000000000..ad2882828--- /dev/null+++ b/apps/web/lib/slack/oauth-state.ts@@ -0,0 +1,100 @@+import { createHmac, randomUUID, timingSafeEqual } from "node:crypto";++const STATE_VERSION = 1;+const STATE_TTL_SECONDS = 60 * 10;+const FUTURE_SKEW_SECONDS = 60;++type SlackOAuthStatePayload = {+ v: typeof STATE_VERSION;+ organizationId: string;+ userId: string;+ issuedAt: number;+ nonce: string;+};++const signState = (payload: string, secret: string) =>+ createHmac("sha256", secret).update(payload).digest("base64url");++export const createSlackOAuthState = ({+ organizationId,+ userId,+ secret,+ now = Date.now(),+ nonce = randomUUID(),+}: {+ organizationId: string;+ userId: string;+ secret: string;+ now?: number;+ nonce?: string;+}) => {+ const encoded = Buffer.from(+ JSON.stringify({+ v: STATE_VERSION,+ organizationId,+ userId,+ issuedAt: Math.floor(now / 1000),+ nonce,+ } satisfies SlackOAuthStatePayload),+ ).toString("base64url");+ return `${encoded}.${signState(encoded, secret)}`;+};++const isPayload = (value: unknown): value is SlackOAuthStatePayload => {+ if (!value || typeof value !== "object") return false;+ const payload = value as Partial<SlackOAuthStatePayload>;+ return (+ payload.v === STATE_VERSION &&+ typeof payload.organizationId === "string" &&+ payload.organizationId.length > 0 &&+ payload.organizationId.length <= 128 &&+ typeof payload.userId === "string" &&+ payload.userId.length > 0 &&+ payload.userId.length <= 128 &&+ typeof payload.issuedAt === "number" &&+ Number.isSafeInteger(payload.issuedAt) &&+ typeof payload.nonce === "string" &&+ payload.nonce.length > 0 &&+ payload.nonce.length <= 128+ );+};++export const verifySlackOAuthState = ({+ state,+ secret,+ now = Date.now(),+}: {+ state: string;+ secret: string;+ now?: number;+}): SlackOAuthStatePayload | null => {+ const [encoded, signature, extra] = state.split(".");+ if (!encoded || !signature || extra) return null;+ const expected = signState(encoded, secret);+ const expectedBuffer = Buffer.from(expected);+ const signatureBuffer = Buffer.from(signature);+ if (+ expectedBuffer.length !== signatureBuffer.length ||+ !timingSafeEqual(expectedBuffer, signatureBuffer)+ ) {+ return null;+ }++ let payload: unknown;+ try {+ payload = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8"));+ } catch {+ return null;+ }+ if (!isPayload(payload)) return null;++ const nowSeconds = Math.floor(now / 1000);+ if (+ payload.issuedAt > nowSeconds + FUTURE_SKEW_SECONDS ||+ nowSeconds - payload.issuedAt > STATE_TTL_SECONDS+ ) {+ return null;+ }++ return payload;+};