|
16 | 16 | // `updated_at`, nothing else) |
17 | 17 | // --------------------------------------------------------------------------- |
18 | 18 |
|
| 19 | +import { createHmac } from "node:crypto"; |
19 | 20 | import { readFileSync } from "node:fs"; |
20 | 21 |
|
21 | 22 | import { describe, expect, it } from "@effect/vitest"; |
22 | 23 | import { eq } from "drizzle-orm"; |
23 | | -import { Effect, Layer, Option } from "effect"; |
| 24 | +import { Effect, Layer, Option, Schema } from "effect"; |
24 | 25 | import { HttpServerResponse } from "effect/unstable/http"; |
25 | 26 |
|
26 | 27 | import { |
@@ -456,6 +457,90 @@ describe("moved dashboard location", () => { |
456 | 457 | }); |
457 | 458 | }); |
458 | 459 |
|
| 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 | + |
459 | 544 | // --------------------------------------------------------------------------- |
460 | 545 | // Migration 0022 and the role v2 writes the table with. |
461 | 546 | // --------------------------------------------------------------------------- |
|
0 commit comments