feat: add headless t3 connect setup for SSH hosts (#3749)

Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: codex <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-07-19 18:59:23 +02:00
1 parent 53e3c98a55
commit 7a820abfdd
35 files changed
+3018 -145

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import {
buildConnectCliClerkAuthorizeUrl,
hasConnectCliAuthConfig,
readConnectCliCallbackResult,
} from "./connectCliAuth";
// Any pk_test_* key decodes to <base64 hostname>.clerk.accounts.dev.
const TEST_PUBLISHABLE_KEY = `pk_test_${btoa("witty-mole-42.clerk.accounts.dev$")}`;
describe("connectCliAuth", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("requires both the publishable key and the CLI OAuth client id", () => {
vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.com");
expect(hasConnectCliAuthConfig()).toBe(false);
vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");
expect(hasConnectCliAuthConfig()).toBe(true);
});
it("builds the Clerk authorize URL with the configured hosted origin's callback", () => {
vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");
vi.stubEnv("VITE_HOSTED_APP_URL", "https://nightly.app.t3.codes");
const authorizeUrl = buildConnectCliClerkAuthorizeUrl({
state: "state-1",
challenge: "challenge-1",
});
expect(authorizeUrl).not.toBeNull();
const url = new URL(authorizeUrl!);
expect(url.hostname).toBe("witty-mole-42.clerk.accounts.dev");
expect(url.pathname).toBe("/oauth/authorize");
expect(url.searchParams.get("redirect_uri")).toBe(
"https://nightly.app.t3.codes/connect/callback",
);
expect(url.searchParams.get("state")).toBe("state-1");
expect(url.searchParams.get("code_challenge")).toBe("challenge-1");
expect(url.searchParams.get("code_challenge_method")).toBe("S256");
});
it("returns null when the CLI OAuth client id is not configured", () => {
vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);
expect(
buildConnectCliClerkAuthorizeUrl({ state: "state-1", challenge: "challenge-1" }),
).toBeNull();
});
it("reads the code and state Clerk echoes back to the callback", () => {
expect(
readConnectCliCallbackResult(
new URL("https://app.t3.codes/connect/callback?code=abc&state=state-1"),
),
).toEqual({ code: "abc", state: "state-1" });
expect(
readConnectCliCallbackResult(new URL("https://app.t3.codes/connect/callback?code=abc")),
).toBeNull();
expect(
readConnectCliCallbackResult(new URL("https://app.t3.codes/connect/callback?state=s")),
).toBeNull();
});
});
+91
View File
@@ -0,0 +1,91 @@
import {
buildConnectClerkAuthorizeUrl,
connectCallbackUrl,
CONNECT_OAUTH_SCOPES,
type ConnectAuthorizeRequest,
} from "@t3tools/shared/connectAuth";
import { clerkFrontendApiUrlFromPublishableKey } from "@t3tools/shared/relayAuth";
import { configuredHostedAppUrl, isHostedStaticApp } from "../hostedPairing";
import { hasCloudPublicConfig, resolveCloudPublicConfig, trimNonEmpty } from "./publicConfig";
const CONNECT_CLI_AUTH_STATE_STORAGE_KEY = "t3code-connect-cli-auth-state";
export function resolveConnectCliOAuthClientId(): string | null {
return trimNonEmpty(import.meta.env.VITE_CLERK_CLI_OAUTH_CLIENT_ID as string | undefined);
}
export function hasConnectCliAuthConfig(): boolean {
return Boolean(
resolveCloudPublicConfig().clerkPublishableKey && resolveConnectCliOAuthClientId(),
);
}
/**
* Gate for the /connect routes: the CLI handshake only exists on the hosted
* deployment (the same bundle ships inside local instances) and needs the
* Clerk CLI OAuth client configured at build time.
*/
export function connectCliAuthRoutesEnabled(): boolean {
return isHostedStaticApp() && hasCloudPublicConfig() && hasConnectCliAuthConfig();
}
/**
* Builds the Clerk authorize URL for a CLI-initiated connect request. The
* state is mirrored into sessionStorage so the callback page can verify the
* response matches a request this browser actually started.
*/
export function buildConnectCliClerkAuthorizeUrl(request: ConnectAuthorizeRequest): string | null {
const { clerkPublishableKey } = resolveCloudPublicConfig();
const clientId = resolveConnectCliOAuthClientId();
if (!clerkPublishableKey || !clientId) {
return null;
}
return buildConnectClerkAuthorizeUrl({
authorizationEndpoint: `${clerkFrontendApiUrlFromPublishableKey(clerkPublishableKey)}/oauth/authorize`,
clientId,
redirectUri: connectCallbackUrl(configuredHostedAppUrl()),
scopes: CONNECT_OAUTH_SCOPES,
state: request.state,
challenge: request.challenge,
});
}
export function rememberConnectCliAuthState(state: string): void {
try {
window.sessionStorage.setItem(CONNECT_CLI_AUTH_STATE_STORAGE_KEY, state);
} catch {
// Session storage can be unavailable (e.g. blocked). The callback page
// then falls back to trusting the state Clerk echoed back.
}
}
/**
* Read-only on purpose: this runs during render, where a removal would be
* consumed by React's double-invoked/discarded renders (StrictMode) and
* silently disable the state check. The value is not a secret and is
* overwritten by the next /connect visit.
*/
export function readConnectCliAuthState(): string | null {
try {
return window.sessionStorage.getItem(CONNECT_CLI_AUTH_STATE_STORAGE_KEY);
} catch {
return null;
}
}
export interface ConnectCliCallbackResult {
readonly code: string;
readonly state: string;
}
export function readConnectCliCallbackResult(
url: URL = new URL(window.location.href),
): ConnectCliCallbackResult | null {
const code = url.searchParams.get("code")?.trim() ?? "";
const state = url.searchParams.get("state")?.trim() ?? "";
if (!code || !state) {
return null;
}
return { code, state };
}
+1 -1
View File
@@ -24,7 +24,7 @@ export interface CloudPublicConfig {
};
}
function trimNonEmpty(value: string | undefined): string | null {
export function trimNonEmpty(value: string | undefined): string | null {
return value?.trim() || null;
}