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

connectAuth.ts

86 lines3.0 KB

1import { readHashParams } from "./remote.ts";
2​
3const CONNECT_AUTH_STATE_PARAM = "state";
4const CONNECT_AUTH_CHALLENGE_PARAM = "challenge";
5const CONNECT_AUTH_PORT_PARAM = "port";
6const CONNECT_LOOPBACK_CALLBACK_PATH = "/callback";
7​
8const CONNECT_AUTHORIZE_PATH = "/connect";
9​
10export const DEFAULT_HOSTED_APP_URL = "https://app.t3.codes";
11​
12export const CONNECT_OAUTH_SCOPES = ["openid", "profile", "email", "offline_access"] as const;
13​
14export interface ConnectAuthorizeRequest {
15 readonly state: string;
16 readonly challenge: string;
17 readonly loopbackPort: number;
19​
20/**
21 * The CLI routes through the hosted /connect page rather than hitting Clerk's
22 * /oauth/authorize directly: a signed-out browser sent straight to
23 * /oauth/authorize goes through Clerk's sign-in redirect, which does not
24 * reliably preserve the authorize query parameters (state, response_type,
25 * code_challenge).
26 */
27export function buildConnectAuthorizeRequestUrl(input: {
28 readonly hostedAppUrl: string;
29 readonly state: string;
30 readonly challenge: string;
31 readonly loopbackPort: number;
32}): string {
33 const url = new URL(CONNECT_AUTHORIZE_PATH, input.hostedAppUrl);
34 url.hash = new URLSearchParams([
35 [CONNECT_AUTH_STATE_PARAM, input.state],
36 [CONNECT_AUTH_CHALLENGE_PARAM, input.challenge],
37 [CONNECT_AUTH_PORT_PARAM, String(input.loopbackPort)],
38 ]).toString();
39 return url.toString();
41​
42export function readConnectAuthorizeRequest(url: URL): ConnectAuthorizeRequest | null {
43 const params = readHashParams(url);
44 const state = params.get(CONNECT_AUTH_STATE_PARAM)?.trim() ?? "";
45 const challenge = params.get(CONNECT_AUTH_CHALLENGE_PARAM)?.trim() ?? "";
46 const loopbackPort = parseLoopbackPort(params.get(CONNECT_AUTH_PORT_PARAM)?.trim() ?? "");
47 if (!state || !challenge || loopbackPort === null) {
48 return null;
49 }
50 return { state, challenge, loopbackPort };
52​
53function parseLoopbackPort(value: string): number | null {
54 if (!/^\d{1,5}$/.test(value)) {
55 return null;
56 }
57 const port = Number(value);
58 return port >= 1 && port <= 65535 ? port : null;
60​
61/**
62 * Redirect URI for the CLI's local callback listener. Must stay in sync with
63 * the redirect URI registered on the Clerk CLI OAuth application.
64 */
65export function connectLoopbackRedirectUri(port: number): string {
66 return `http://127.0.0.1:${port}${CONNECT_LOOPBACK_CALLBACK_PATH}`;
68​
69export function buildConnectClerkAuthorizeUrl(input: {
70 readonly authorizationEndpoint: string;
71 readonly clientId: string;
72 readonly redirectUri: string;
73 readonly scopes: ReadonlyArray<string>;
74 readonly state: string;
75 readonly challenge: string;
76}): string {
77 const url = new URL(input.authorizationEndpoint);
78 url.searchParams.set("client_id", input.clientId);
79 url.searchParams.set("redirect_uri", input.redirectUri);
80 url.searchParams.set("response_type", "code");
81 url.searchParams.set("scope", input.scopes.join(" "));
82 url.searchParams.set("state", input.state);
83 url.searchParams.set("code_challenge", input.challenge);
84 url.searchParams.set("code_challenge_method", "S256");
85 return url.toString();