feat(server): use Clerk's device authorization grant for headless connect login #11794
Mergedjuliusmarmingemerged 4 commits into
main fromrefs/pull/11794/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/cloud/CliTokenManager.ts line 349OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -322,3 +331,19 @@331+) {332+ const httpClient = yield* HttpClient.HttpClient;333+ const params = {334+ grant_type: DEVICE_CODE_GRANT_TYPE,335+ device_code: deviceCode,322336 client_id: metadata.clientId,323- code_verifier: verifier,324- });337+ };338+ let interval = initialInterval;339+ while (true) {340+ yield* Effect.sleep(interval);341+ const response = yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe(342+ HttpClientRequest.bodyUrlParams(params),343+ httpClient.execute,344+ Effect.map(Option.some),345+ Effect.catchIf(isTransportError, () => Effect.succeedNone),346+ );347+ // Transport failures and upstream 5xx are transient while the device code348+ // is still valid; the next tick retries.349+ if (Option.isNone(response) || response.value.status >= 500) continue;apps/server/src/cloud/CliTokenManager.ts lines 349 to 349OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -322,3 +331,19 @@331+) {332+ const httpClient = yield* HttpClient.HttpClient;333+ const params = {334+ grant_type: DEVICE_CODE_GRANT_TYPE,335+ device_code: deviceCode,322336 client_id: metadata.clientId,323- code_verifier: verifier,324- });337+ };338+ let interval = initialInterval;339+ while (true) {340+ yield* Effect.sleep(interval);341+ const response = yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe(342+ HttpClientRequest.bodyUrlParams(params),343+ httpClient.execute,344+ Effect.map(Option.some),345+ Effect.catchIf(isTransportError, () => Effect.succeedNone),346+ );347+ // Transport failures and upstream 5xx are transient while the device code348+ // is still valid; the next tick retries.349+ if (Option.isNone(response) || response.value.status >= 500) continue;
- apps/server/src/cloud/CliTokenManager.ts line 392Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -325,2 +366,21 @@366+ cause: failure.error_description ?? failure.error,367+ });368+ }369+ }325370 });326371372+/**373+ * OAuth device authorization grant for machines without a local browser374+ * (SSH). Clerk issues a short user code; the user approves it on Clerk's375+ * hosted device page from any browser while this process polls the token376+ * endpoint. Nothing is typed into the terminal and no redirect URI is377+ * involved, so the hosted app plays no part in this flow.378+ */379+export const deviceAuthorizationLogin = Effect.fn("cloud.cli_token.device_authorization_login")(380+ function* <E, R>(showPrompt: (prompt: DeviceAuthorizationPrompt) => Effect.Effect<void, E, R>) {381+ const metadata = yield* cloudCliOAuthConfig;382+ const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);383+ const authorization = yield* HttpClientRequest.post(metadata.deviceAuthorizationEndpoint).pipe(384+ HttpClientRequest.bodyUrlParams({385+ client_id: metadata.clientId,386+ scope: metadata.scopes.join(" "),docs/operations/connect-setup.md lines 42 to 46Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -39,8 +39,8 @@3939 In Clerk's OAuth applications settings:40404141 Create a public OAuth application for the T3 CLI, using authorization-code exchange with PKCE.42-Allow both redirect URIs: `http://127.0.0.1:34338/callback` and43- `https://app.t3.codes/connect/callback`. A custom `T3CODE_HOSTED_APP_URL` needs its own44- `/connect/callback` URL. Headless and SSH authorization depend on the hosted redirect.42+Allow the redirect URI `http://127.0.0.1:34338/callback`.4543 Enable the `openid`, `profile`, and `email` scopes.46-Set `T3CODE_CLERK_CLI_OAUTH_CLIENT_ID` to the generated public client ID in local and release44+Enable **Device authorization grant** on the application. Headless and SSH authorization use45+ it, and Clerk only advertises the device endpoint once it is on. The feature is in beta and46+ Clerk enables it per account on request.
juliusmarminge merged thisdc0869b
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
9d4bb55· PR result:93a9202· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.