Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

connectAuth.test.ts

104 lines3.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 buildConnectAuthorizeRequestUrl,
5 buildConnectClerkAuthorizeUrl,
6 connectCallbackUrl,
7 connectLoopbackRedirectUri,
8 encodeConnectAuthCode,
9 parseConnectAuthCode,
10 readConnectAuthorizeRequest,
11} from "./connectAuth.ts";
12​
13describe("connectAuth", () => {
14 it("round-trips state and challenge through the authorize URL fragment", () => {
15 const url = buildConnectAuthorizeRequestUrl({
16 hostedAppUrl: "https://app.t3.codes",
17 state: "q7mK9xV2pL4nR8sT6wYzAQ",
18 challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
19 });
20 const parsed = new URL(url);
21​
22 expect(parsed.origin).toBe("https://app.t3.codes");
23 expect(parsed.pathname).toBe("/connect");
24 expect(parsed.search).toBe("");
25 expect(readConnectAuthorizeRequest(parsed)).toEqual({
26 state: "q7mK9xV2pL4nR8sT6wYzAQ",
27 challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
28 });
29 });
30​
31 it("rejects authorize requests missing state or challenge", () => {
32 expect(readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect"))).toBeNull();
33 expect(
34 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#state=abc")),
35 ).toBeNull();
36 expect(
37 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#challenge=abc")),
38 ).toBeNull();
39 });
40​
41 it("round-trips the loopback port through the authorize URL fragment", () => {
42 const url = buildConnectAuthorizeRequestUrl({
43 hostedAppUrl: "https://app.t3.codes",
44 state: "state-1",
45 challenge: "challenge-1",
46 loopbackPort: 34338,
47 });
48​
49 expect(readConnectAuthorizeRequest(new URL(url))).toEqual({
50 state: "state-1",
51 challenge: "challenge-1",
52 loopbackPort: 34338,
53 });
54 expect(connectLoopbackRedirectUri(34338)).toBe("http://127.0.0.1:34338/callback");
55 });
56​
57 it("rejects authorize requests whose loopback port is corrupted", () => {
58 for (const port of ["", "abc", "-1", "0", "65536", "34338x", "34 38"]) {
59 const url = new URL(
60 `https://app.t3.codes/connect#state=state-1&challenge=challenge-1&port=${encodeURIComponent(port)}`,
61 );
62 expect(readConnectAuthorizeRequest(url), port).toBeNull();
63 }
64 });
65​
66 it("builds a PKCE authorize URL against the Clerk endpoint", () => {
67 const url = new URL(
68 buildConnectClerkAuthorizeUrl({
69 authorizationEndpoint: "https://clerk.t3.codes/oauth/authorize",
70 clientId: "oauthapp_123",
71 redirectUri: connectCallbackUrl("https://app.t3.codes"),
72 scopes: ["openid", "profile", "email"],
73 state: "state-1",
74 challenge: "challenge-1",
75 }),
76 );
77​
78 expect(url.origin).toBe("https://clerk.t3.codes");
79 expect(url.pathname).toBe("/oauth/authorize");
80 expect(url.searchParams.get("client_id")).toBe("oauthapp_123");
81 expect(url.searchParams.get("redirect_uri")).toBe("https://app.t3.codes/connect/callback");
82 expect(url.searchParams.get("response_type")).toBe("code");
83 expect(url.searchParams.get("scope")).toBe("openid profile email");
84 expect(url.searchParams.get("state")).toBe("state-1");
85 expect(url.searchParams.get("code_challenge")).toBe("challenge-1");
86 expect(url.searchParams.get("code_challenge_method")).toBe("S256");
87 });
88​
89 it("round-trips the out-of-band authorization code and preserves dots inside it", () => {
90 const blob = encodeConnectAuthCode({ code: "az9.code.chunk", state: "state-uuid" });
91 expect(parseConnectAuthCode(blob)).toEqual({ code: "az9.code.chunk", state: "state-uuid" });
92 expect(parseConnectAuthCode(` ${blob}\n`)).toEqual({
93 code: "az9.code.chunk",
94 state: "state-uuid",
95 });
96 });
97​
98 it("rejects malformed out-of-band authorization codes", () => {
99 expect(parseConnectAuthCode("")).toBeNull();
100 expect(parseConnectAuthCode("no-separator")).toBeNull();
101 expect(parseConnectAuthCode(".leading")).toBeNull();
102 expect(parseConnectAuthCode("trailing.")).toBeNull();
103 });
104});