Open History to see the last commit
1import { CONNECT_OAUTH_SCOPES, DEFAULT_HOSTED_APP_URL } from "@t3tools/shared/connectAuth";
2import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
3import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
4import * as Config from "effect/Config";
5import * as ConfigProvider from "effect/ConfigProvider";
6import * as Effect from "effect/Effect";
7import * as Schema from "effect/Schema";
8import * as SchemaIssue from "effect/SchemaIssue";
9
10declare const __T3CODE_BUILD_RELAY_URL__: string | undefined;
11declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
12declare const __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: string | undefined;
13declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: string | undefined;
14declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: string | undefined;
15declare const __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: string | undefined;
16
17const CLOUD_CLI_OAUTH_REDIRECT_URI = "http://127.0.0.1:34338/callback";
18const CLOUD_CLI_OAUTH_SCOPES = CONNECT_OAUTH_SCOPES;
19
20function validateRelayUrl(value: string) {
21 const relayUrl = normalizeSecureRelayUrl(value);
22 return relayUrl === null
23 ? Effect.fail(
24 new Config.ConfigError(
25 new Schema.SchemaError(
26 new SchemaIssue.InvalidValue({
27 message: "Relay URL must be a secure absolute HTTPS origin.",
28 }),
29 ),
30 ),
31 )
32 : Effect.succeed(relayUrl);
33}
34
35function readBuildTimeValue(value: string | undefined): string {
36 return typeof value === "undefined" ? "" : value.trim();
37}
38
39function normalizeSecureUrl(value: string): string | null {
40 try {
41 const url = new URL(value);
42 return url.protocol === "https:" ? url.toString() : null;
43 } catch {
44 return null;
45 }
46}
47
48export const buildTimeRelayUrl =
49 typeof __T3CODE_BUILD_RELAY_URL__ === "undefined"
50 ? ""
51 : (normalizeSecureRelayUrl(__T3CODE_BUILD_RELAY_URL__) ?? "");
52export const buildTimeClerkPublishableKey = readBuildTimeValue(
53 typeof __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__ === "undefined"
54 ? undefined
55 : __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__,
56);
57export const buildTimeClerkCliOAuthClientId = readBuildTimeValue(
58 typeof __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__ === "undefined"
59 ? undefined
60 : __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__,
61);
62export const buildTimeRelayClientTracing = {
63 tracesUrl: readBuildTimeValue(
64 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__ === "undefined"
65 ? undefined
66 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__,
67 ),
68 tracesDataset: readBuildTimeValue(
69 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__ === "undefined"
70 ? undefined
71 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__,
72 ),
73 tracesToken: readBuildTimeValue(
74 typeof __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__ === "undefined"
75 ? undefined
76 : __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__,
77 ),
78} as const;
79
80export function resolveRelayClientTracingConfig(
81 env: Readonly<Record<string, string | undefined>> = process.env,
82 fallback = buildTimeRelayClientTracing,
83) {
84 const tracesUrl = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() || fallback.tracesUrl;
85 const tracesDataset =
86 env.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() || fallback.tracesDataset;
87 const tracesToken = env.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() || fallback.tracesToken;
88 const normalizedTracesUrl = normalizeSecureUrl(tracesUrl);
89 return normalizedTracesUrl && tracesDataset && tracesToken
90 ? { tracesUrl: normalizedTracesUrl, tracesDataset, tracesToken }
91 : null;
92}
93
94export function makeRelayUrlConfig(fallback = buildTimeRelayUrl) {
95 const runtimeConfig = Config.nonEmptyString("T3CODE_RELAY_URL");
96 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
97 Config.mapOrFail(validateRelayUrl),
98 );
99}
100
101export const relayUrlConfig = makeRelayUrlConfig();
102
103/**
104 * Hosted app origin used for out-of-band OAuth on headless
105 * machines. Overridable so staging/nightly builds can point their CLIs at a
106 * matching hosted deployment.
107 */
108export const hostedAppUrlConfig = makePublicValueConfig(
109 "T3CODE_HOSTED_APP_URL",
110 DEFAULT_HOSTED_APP_URL,
111).pipe(Config.mapOrFail(validateHostedAppUrl));
112
113function validateHostedAppUrl(value: string) {
114 try {
115 const url = new URL(value);
116 const isLoopbackHttp =
117 url.protocol === "http:" &&
118 (url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]");
119 if (
120 (url.protocol !== "https:" && !isLoopbackHttp) ||
121 url.pathname !== "/" ||
122 url.search !== "" ||
123 url.hash !== ""
124 ) {
125 throw new Error("invalid hosted app origin");
126 }
127 return Effect.succeed(url.origin);
128 } catch {
129 return Effect.fail(
130 new Config.ConfigError(
131 new Schema.SchemaError(
132 new SchemaIssue.InvalidValue({
133 message: "Hosted app URL must be an absolute HTTPS origin (or HTTP loopback origin).",
134 }),
135 ),
136 ),
137 );
138 }
139}
140
141function makePublicValueConfig(name: string, fallback: string) {
142 const runtimeConfig = Config.nonEmptyString(name);
143 return (fallback ? runtimeConfig.pipe(Config.withDefault(fallback)) : runtimeConfig).pipe(
144 Config.map((value) => value.trim()),
145 );
146}
147
148export interface CloudCliOAuthConfig {
149 readonly authorizationEndpoint: string;
150 readonly tokenEndpoint: string;
151 readonly clientId: string;
152 readonly redirectUri: string;
153 readonly scopes: typeof CLOUD_CLI_OAUTH_SCOPES;
154}
155
156export function makeCloudCliOAuthConfig({
157 clerkPublishableKeyFallback = buildTimeClerkPublishableKey,
158 clerkCliOAuthClientIdFallback = buildTimeClerkCliOAuthClientId,
159}: {
160 readonly clerkPublishableKeyFallback?: string;
161 readonly clerkCliOAuthClientIdFallback?: string;
162} = {}) {
163 return Config.all({
164 clerkPublishableKey: makePublicValueConfig(
165 "T3CODE_CLERK_PUBLISHABLE_KEY",
166 clerkPublishableKeyFallback,
167 ),
168 clientId: makePublicValueConfig(
169 "T3CODE_CLERK_CLI_OAUTH_CLIENT_ID",
170 clerkCliOAuthClientIdFallback,
171 ),
172 }).pipe(
173 Config.mapOrFail(({ clerkPublishableKey, clientId }) =>
174 Effect.try({
175 try: () => clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey),
176 catch: (cause) =>
177 new Config.ConfigError(
178 new ConfigProvider.SourceError({
179 message: "Failed to derive Clerk Frontend API URL from the publishable key.",
180 cause,
181 }),
182 ),
183 }).pipe(
184 Effect.map(
185 (clerkFrontendApiUrl) =>
186 ({
187 authorizationEndpoint: `${clerkFrontendApiUrl}/oauth/authorize`,
188 tokenEndpoint: `${clerkFrontendApiUrl}/oauth/token`,
189 clientId,
190 redirectUri: CLOUD_CLI_OAUTH_REDIRECT_URI,
191 scopes: CLOUD_CLI_OAUTH_SCOPES,
192 }) satisfies CloudCliOAuthConfig,
193 ),
194 ),
195 ),
196 );
197}
198
199export const cloudCliOAuthConfig = makeCloudCliOAuthConfig();
200
201export const hasCloudPublicConfig = Boolean(
202 (normalizeSecureRelayUrl(process.env.T3CODE_RELAY_URL ?? "") ?? buildTimeRelayUrl) &&
203 (process.env.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() || buildTimeClerkPublishableKey) &&
204 (process.env.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || buildTimeClerkCliOAuthClientId),
205);