Skip to content

Commit 3229f11

Browse files
feat(server): use Clerk's device authorization grant for headless connect login
Headless and SSH `t3 connect` used a custom out-of-band flow: the CLI printed a hosted /connect URL, the browser landed on /connect/callback, and the user pasted a code.state blob back into the terminal. Clerk now supports the OAuth device authorization grant (RFC 8628), which needs no redirect URI, no hosted callback page, and no typing in the terminal. The CLI now requests a device code, prints Clerk's verification link and user code, and polls the token endpoint honoring interval, slow_down, expiry, and denial. The hosted /connect/callback route and the code-blob helpers are removed; /connect is loopback-only. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 549d182 commit 3229f11

16 files changed

Lines changed: 405 additions & 554 deletions

‎apps/server/src/cli/connect.ts‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import * as Layer from "effect/Layer";
2020
import * as Option from "effect/Option";
2121
import * as References from "effect/References";
2222
import * as Schema from "effect/Schema";
23-
import * as Terminal from "effect/Terminal";
2423
import { Command, Flag, GlobalFlag, Prompt } from "effect/unstable/cli";
2524
import {
2625
FetchHttpClient,
@@ -64,13 +63,14 @@ const jsonFlag = Flag.boolean("json").pipe(
6463
const isCloudCliTokenManagerError = Schema.is(CliTokenManager.CloudCliTokenManagerError);
6564

6665
const headlessFlag = Flag.boolean("headless").pipe(
67-
Flag.withDescription("Authorize without a local browser using out-of-band OAuth."),
66+
Flag.withDescription("Authorize without a local browser using the OAuth device flow."),
6867
Flag.withDefault(false),
6968
);
7069

7170
/**
7271
* Inside an SSH session there is no local browser to complete the loopback
73-
* OAuth callback, so out-of-band OAuth is the only flow that can work.
72+
* OAuth callback, so the device authorization grant is the only flow that
73+
* can work.
7474
*/
7575
export const headlessSessionConfig = Config.all({
7676
sshConnection: Config.string("SSH_CONNECTION").pipe(Config.option),
@@ -79,20 +79,21 @@ export const headlessSessionConfig = Config.all({
7979
Config.map(({ sshConnection, sshTty }) => Option.isSome(sshConnection) || Option.isSome(sshTty)),
8080
);
8181

82-
const promptForOutOfBandOAuthCode = Effect.fn("cloud.cli.prompt_for_out_of_band_oauth_code")(
83-
function* ({ authorizeUrl, validate }: CliTokenManager.OutOfBandOAuthPromptInput) {
84-
yield* Console.log(formatHeadlessAuthorizationPrompt(authorizeUrl));
85-
return yield* Prompt.run(Prompt.text({ message: "Authorization code", validate }));
86-
},
87-
);
82+
const showDeviceAuthorizationPrompt = (prompt: CliTokenManager.DeviceAuthorizationPrompt) =>
83+
Console.log(formatDeviceAuthorizationPrompt(prompt));
8884

89-
function formatHeadlessAuthorizationPrompt(authorizeUrl: string): string {
85+
function formatDeviceAuthorizationPrompt(
86+
prompt: CliTokenManager.DeviceAuthorizationPrompt,
87+
): string {
88+
const minutes = Math.max(1, Math.round(Duration.toMinutes(prompt.expiresIn)));
9089
return [
9190
"Headless authorization",
9291
"Open this URL on a device with a browser:",
93-
` ${authorizeUrl}`,
92+
` ${prompt.verificationUriComplete ?? prompt.verificationUri}`,
93+
"",
94+
`Confirm this code when asked: ${prompt.userCode}`,
9495
"",
95-
"After signing in, return here and enter the code shown in your browser.",
96+
`Waiting for approval (expires in ${minutes} min). Press Ctrl+C to cancel.`,
9697
].join("\n");
9798
}
9899

@@ -110,7 +111,7 @@ const authorizeCli = Effect.fn("cloud.cli.authorize")(function* (options: {
110111
yield* Console.log("\nHeadless mode enabled. A new authorization link is ready below.");
111112
}
112113
// A stored credential whose refresh fails (revoked, expired grant) must
113-
// fall through to a fresh out-of-band authorization, not dead-end the command.
114+
// fall through to a fresh device authorization, not dead-end the command.
114115
const existing = yield* tokens.getExisting.pipe(
115116
Effect.catchTag("CloudCliCredentialRefreshError", () =>
116117
Console.log(
@@ -121,13 +122,11 @@ const authorizeCli = Effect.fn("cloud.cli.authorize")(function* (options: {
121122
if (Option.isSome(existing)) {
122123
return existing.value.identity ?? null;
123124
}
124-
const { token, identity } = yield* CliTokenManager.outOfBandOAuthLogin(
125-
promptForOutOfBandOAuthCode,
125+
const { token, identity } = yield* CliTokenManager.deviceAuthorizationLogin(
126+
showDeviceAuthorizationPrompt,
126127
).pipe(
127128
Effect.mapError((cause) =>
128-
// Ctrl-C / EOF at the prompt is a QuitError; let it propagate so the CLI
129-
// cancels quietly instead of dumping an authorization error.
130-
Terminal.isQuitError(cause) || isCloudCliTokenManagerError(cause)
129+
isCloudCliTokenManagerError(cause)
131130
? cause
132131
: new CliTokenManager.CloudCliAuthorizationError({ cause }),
133132
),

0 commit comments

Comments
 (0)