Open History to see the last commit
1import { readHashParams } from "./remote.ts";
2
3const CONNECT_AUTH_STATE_PARAM = "state";
4const CONNECT_AUTH_CHALLENGE_PARAM = "challenge";
5const CONNECT_AUTH_CODE_SEPARATOR = ".";
6
7const CONNECT_AUTHORIZE_PATH = "/connect";
8const CONNECT_CALLBACK_PATH = "/connect/callback";
9
10/**
11 * The CLI prints URLs against this origin and the web bundle uses it to
12 * decide whether it is the hosted deployment — the two must agree, so the
13 * default lives here.
14 */
15export const DEFAULT_HOSTED_APP_URL = "https://app.t3.codes";
16
17/**
18 * Requested at authorize time by the hosted page and honored by the CLI's
19 * token exchange; keep both sides on this single definition.
20 */
21export const CONNECT_OAUTH_SCOPES = ["openid", "profile", "email"] as const;
22
23export interface ConnectAuthorizeRequest {
24 readonly state: string;
25 readonly challenge: string;
26}
27
28/**
29 * The URL a headless CLI prints for the user to open on a machine with a
30 * browser. `state` and `code_challenge` ride the fragment so they never reach
31 * the hosted app's server or CDN logs; neither is a secret.
32 */
33export function buildConnectAuthorizeRequestUrl(input: {
34 readonly hostedAppUrl: string;
35 readonly state: string;
36 readonly challenge: string;
37}): string {
38 const url = new URL(CONNECT_AUTHORIZE_PATH, input.hostedAppUrl);
39 url.hash = new URLSearchParams([
40 [CONNECT_AUTH_STATE_PARAM, input.state],
41 [CONNECT_AUTH_CHALLENGE_PARAM, input.challenge],
42 ]).toString();
43 return url.toString();
44}
45
46export function readConnectAuthorizeRequest(url: URL): ConnectAuthorizeRequest | null {
47 const params = readHashParams(url);
48 const state = params.get(CONNECT_AUTH_STATE_PARAM)?.trim() ?? "";
49 const challenge = params.get(CONNECT_AUTH_CHALLENGE_PARAM)?.trim() ?? "";
50 if (!state || !challenge) {
51 return null;
52 }
53 return { state, challenge };
54}
55
56export function connectCallbackUrl(hostedAppUrl: string): string {
57 return new URL(CONNECT_CALLBACK_PATH, hostedAppUrl).toString();
58}
59
60export function buildConnectClerkAuthorizeUrl(input: {
61 readonly authorizationEndpoint: string;
62 readonly clientId: string;
63 readonly redirectUri: string;
64 readonly scopes: ReadonlyArray<string>;
65 readonly state: string;
66 readonly challenge: string;
67}): string {
68 const url = new URL(input.authorizationEndpoint);
69 url.searchParams.set("client_id", input.clientId);
70 url.searchParams.set("redirect_uri", input.redirectUri);
71 url.searchParams.set("response_type", "code");
72 url.searchParams.set("scope", input.scopes.join(" "));
73 url.searchParams.set("state", input.state);
74 url.searchParams.set("code_challenge", input.challenge);
75 url.searchParams.set("code_challenge_method", "S256");
76 return url.toString();
77}
78
79export interface ConnectAuthCode {
80 readonly code: string;
81 readonly state: string;
82}
83
84/**
85 * The single blob the hosted callback page displays and the CLI accepts.
86 * Bundling `state` with the authorization code lets the CLI keep the loopback
87 * flow's CSRF check without any backend: it verifies the returned state
88 * matches the one it generated. Clerk authorization codes and the CLI's
89 * base64url states never contain ".".
90 */
91export function encodeConnectAuthCode(input: ConnectAuthCode): string {
92 return `${input.code}${CONNECT_AUTH_CODE_SEPARATOR}${input.state}`;
93}
94
95/**
96 * Validates an out-of-band authorization code against the state of the request this process
97 * generated. Returns the parsed code or a user-facing error message; both
98 * the prompt's live validation and the authoritative post-prompt check go
99 * through here so they cannot drift.
100 */
101export function checkConnectAuthCode(
102 blob: string,
103 expectedState: string,
104): ConnectAuthCode | string {
105 const parsed = parseConnectAuthCode(blob);
106 if (parsed === null) {
107 return "That does not look like a T3 Connect code. Copy the full code.";
108 }
109 if (parsed.state !== expectedState) {
110 return "That code belongs to a different connect request. Open the URL above and try again.";
111 }
112 return parsed;
113}
114
115export function parseConnectAuthCode(blob: string): ConnectAuthCode | null {
116 const trimmed = blob.trim();
117 const separatorIndex = trimmed.lastIndexOf(CONNECT_AUTH_CODE_SEPARATOR);
118 if (separatorIndex <= 0 || separatorIndex === trimmed.length - 1) {
119 return null;
120 }
121 const code = trimmed.slice(0, separatorIndex);
122 const state = trimmed.slice(separatorIndex + 1);
123 return { code, state };
124}