Skip to content

Commit 7957647

Browse files
authored
Test the v2 session handoff redirect (#2357)
1 parent b0d1089 commit 7957647

1 file changed

Lines changed: 86 additions & 1 deletion

File tree

‎apps/cloud/src/org/move-to-v2.node.test.ts‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
// `updated_at`, nothing else)
1717
// ---------------------------------------------------------------------------
1818

19+
import { createHmac } from "node:crypto";
1920
import { readFileSync } from "node:fs";
2021

2122
import { describe, expect, it } from "@effect/vitest";
2223
import { eq } from "drizzle-orm";
23-
import { Effect, Layer, Option } from "effect";
24+
import { Effect, Layer, Option, Schema } from "effect";
2425
import { HttpServerResponse } from "effect/unstable/http";
2526

2627
import {
@@ -456,6 +457,90 @@ describe("moved dashboard location", () => {
456457
});
457458
});
458459

460+
// ---------------------------------------------------------------------------
461+
// The session handoff the redirect carries when V2_SESSION_HANDOFF_SECRET is set.
462+
// ---------------------------------------------------------------------------
463+
464+
const decodeClaims = Schema.decodeUnknownSync(
465+
Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
466+
);
467+
468+
describe("moved dashboard session handoff", () => {
469+
const v2Origin = new URL("https://app.example.test");
470+
const secret = "synthetic-handoff-secret-0123456789abcdef";
471+
const handoff = { secret, user: "user_synthetic_member" };
472+
const member = () => Effect.succeed(true);
473+
const stranger = () => Effect.succeed(false);
474+
const locate = (
475+
target: { readonly slug: string } | { readonly organizationId: string },
476+
options: {
477+
readonly isMember?: (organizationId: string) => Effect.Effect<boolean>;
478+
readonly handoff?: typeof handoff;
479+
} = { handoff },
480+
) =>
481+
run(
482+
movedDashboardLocation({
483+
target,
484+
v2Origin,
485+
isMember: options.isMember ?? member,
486+
handoff: options.handoff,
487+
}),
488+
);
489+
490+
/** The claims of a handoff URL's token, once its signature checks out with `secret`. */
491+
const verifiedClaims = (location: string | null) => {
492+
expect(location).not.toBeNull();
493+
const url = new URL(location ?? "");
494+
expect(`${url.origin}${url.pathname}`).toBe("https://app.example.test/api/v1-handoff");
495+
expect([...url.searchParams.keys()]).toEqual(["token"]);
496+
const [prefix, payload, signature, ...rest] = (url.searchParams.get("token") ?? "").split(".");
497+
expect(prefix).toBe("v1hnd");
498+
expect(rest).toEqual([]);
499+
const expected = createHmac("sha256", secret).update(`v1hnd.${payload}`).digest("base64url");
500+
expect(signature).toBe(expected);
501+
return decodeClaims(Buffer.from(payload ?? "", "base64url").toString("utf8"));
502+
};
503+
504+
it("sends a member of a moved organization to v2's handoff with a signed, minute-long token", async () => {
505+
const moved = await seedOrg("moved", { id: "v2-org", slug: "acme-team" });
506+
const before = Math.floor(Date.now() / 1000);
507+
const bySlug = verifiedClaims(await locate({ slug: moved.slug }));
508+
const bySession = verifiedClaims(await locate({ organizationId: moved.id }));
509+
expect(bySlug).toMatchObject({
510+
v: 1,
511+
aud: "v2-session-handoff",
512+
user: "user_synthetic_member",
513+
organization: moved.id,
514+
slug: "acme-team",
515+
});
516+
expect(Object.keys(bySlug).sort()).toEqual(
517+
["aud", "exp", "iat", "jti", "organization", "slug", "user", "v"].sort(),
518+
);
519+
expect(bySlug.iat).toBeGreaterThanOrEqual(before);
520+
expect(bySlug.iat).toBeLessThanOrEqual(Math.floor(Date.now() / 1000));
521+
expect(Number(bySlug.exp) - Number(bySlug.iat)).toBe(60);
522+
expect(typeof bySlug.jti === "string" && bySlug.jti.length >= 16).toBe(true);
523+
// Every redirect carries its own token.
524+
expect(bySession).toMatchObject({ organization: moved.id, slug: "acme-team" });
525+
expect(bySession.jti).not.toBe(bySlug.jti);
526+
});
527+
528+
it("gives no token to anyone who is not a member, or for an organization that has not moved", async () => {
529+
const moved = await seedOrg("moved", { id: "v2-org", slug: "acme-team" });
530+
const moving = await seedOrg("moving");
531+
expect(await locate({ slug: moved.slug }, { isMember: stranger, handoff })).toBeNull();
532+
expect(await locate({ organizationId: moved.id }, { isMember: stranger, handoff })).toBeNull();
533+
expect(await locate({ slug: moving.slug })).toBeNull();
534+
});
535+
536+
it("keeps the plain redirect without the secret, or without a v2 slug to open", async () => {
537+
const moved = await seedOrg("moved", { id: "v2-org", slug: "acme-team" });
538+
const slugless = await seedOrg("moved", { id: "v2-org", slug: null });
539+
expect(await locate({ slug: moved.slug }, {})).toBe("https://app.example.test/org/acme-team");
540+
expect(await locate({ slug: slugless.slug })).toBe("https://app.example.test/");
541+
});
542+
});
543+
459544
// ---------------------------------------------------------------------------
460545
// Migration 0022 and the role v2 writes the table with.
461546
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)