Skip to content

Commit 2ef38b2

Browse files
fix(app): accept a pasted redirect URL for provider sign-in (#63315)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
1 parent 7b65261 commit 2ef38b2

84 files changed

Lines changed: 547 additions & 144 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/app/src/providers/connect/controller.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ export function createProviderDialogController(options: {
397397
return
398398
}
399399

400+
if (effect.type === "paste") {
401+
void serverSDK.api.integration.oauth
402+
.complete({ integrationID, attemptID: effect.signIn.authorization.attemptID, location, code: effect.value })
403+
.then(
404+
() => result({ type: "paste.accepted", attempt: effect.attempt }),
405+
(error) =>
406+
result({ type: "paste.rejected", attempt: effect.attempt, error: formatServerError(error, language.t) }),
407+
)
408+
409+
return
410+
}
411+
400412
if (effect.type === "sync") return void sync()
401413

402414
void refresh(effect, integrationID, result)

‎packages/app/src/providers/connect/dialog.tsx‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Checkbox } from "@opencode/ui/checkbox"
77
import { Loader } from "@opencode/ui/loader"
88
import { TextField } from "@opencode/ui/text-field"
99
import { DialogBody, DialogHeader, DialogTitle, DialogTitleGroup, Dialog } from "@opencode/ui/dialog"
10+
import { RedirectPasteForm } from "@opencode/ui/redirect-paste"
1011
import { TextInput } from "@opencode/ui/text-input"
1112
import { type Component, createMemo, createUniqueId, For, type JSX, Match, onMount, Show, Switch } from "solid-js"
1213
import { createStore } from "solid-js/store"
@@ -443,11 +444,10 @@ function ProviderSteps(props: { provider: string; controller: ProviderDialogCont
443444
const userCode = new URL(authorization.url).searchParams.get("user_code")
444445

445446
if (userCode) return userCode
447+
// Device sign-ins put the code after a colon ("Enter code: ABCD-1234"); other instructions are not a code.
446448
const instructions = authorization.instructions
447449

448450
if (instructions?.includes(":")) return instructions.split(":").pop()?.trim()
449-
450-
return instructions
451451
})
452452

453453
// Asks one visible field at a time, like the TUI, so `when` conditions see every earlier answer.
@@ -913,7 +913,14 @@ function ProviderSteps(props: { provider: string; controller: ProviderDialogCont
913913
function OAuthAutoView() {
914914
return (
915915
<div class="flex flex-col gap-5 px-3 text-[13px] font-[440] leading-5 tracking-[-0.04px] text-v2-text-text-muted">
916-
<div>{language.t("provider.connect.oauth.auto.description", { provider: name() })}</div>
916+
<div>
917+
{language.t(
918+
connect().paste
919+
? "provider.connect.oauth.auto.paste.description"
920+
: "provider.connect.oauth.auto.description",
921+
{ provider: name() },
922+
)}
923+
</div>
917924
<StatusRow>{language.t("provider.connect.status.waiting")}</StatusRow>
918925
<div class="flex flex-wrap items-center gap-2">
919926
<Button
@@ -936,6 +943,18 @@ function ProviderSteps(props: { provider: string; controller: ProviderDialogCont
936943
/>
937944
)}
938945
</Show>
946+
<Show when={connect().paste}>
947+
{(paste) => (
948+
<RedirectPasteForm
949+
value={paste().value}
950+
error={paste().error}
951+
locked={paste().locked}
952+
submitting={paste().submitting}
953+
onInput={(value) => props.controller.input({ type: "paste.changed", value })}
954+
onSubmit={() => props.controller.input({ type: "paste.submitted" })}
955+
/>
956+
)}
957+
</Show>
939958
</div>
940959
)
941960
}

‎packages/app/src/providers/connect/machine.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,129 @@ test("an unreachable OAuth status resumes the same attempt, and Back from it can
292292
])
293293
})
294294

295+
// The browser can't reach a remote server's callback, so the user pastes the URL it landed on instead.
296+
function waitingWithPaste() {
297+
const starting = replay([loaded([oauth]), { type: "method.selected", index: 0 }])
298+
const start = starting.effects.find((effect) => effect.type === "start")
299+
300+
if (start?.type !== "start") throw new Error("expected a start effect")
301+
302+
return {
303+
attempt: start.attempt,
304+
state: transitionProviderConnect(starting.state, {
305+
type: "oauth.started",
306+
attempt: start.attempt,
307+
authorization: { ...authorization("auto"), paste: true },
308+
}).state,
309+
}
310+
}
311+
312+
test("a browser sign-in offers a paste box only when its attempt takes a pasted redirect", () => {
313+
expect(selectProviderConnectView(waitingWithPaste().state).paste).toEqual({
314+
value: "",
315+
error: undefined,
316+
locked: false,
317+
submitting: false,
318+
})
319+
320+
const plain = replay([
321+
loaded([oauth]),
322+
{ type: "method.selected", index: 0 },
323+
{ type: "oauth.started", attempt: 1, authorization: authorization("auto") },
324+
])
325+
326+
expect(selectProviderConnectView(plain.state).paste).toBeUndefined()
327+
expect(transitionProviderConnect(plain.state, { type: "paste.changed", value: "http://x" }).state).toBe(plain.state)
328+
})
329+
330+
test("a rejected paste shows the server error and keeps polling the same attempt", () => {
331+
const waiting = waitingWithPaste()
332+
const empty = transitionProviderConnect(waiting.state, { type: "paste.submitted" })
333+
334+
expect(empty.effects).toEqual([])
335+
expect(selectProviderConnectView(empty.state).paste?.error).toEqual({ type: "required" })
336+
337+
const typed = transitionProviderConnect(empty.state, {
338+
type: "paste.changed",
339+
value: " http://127.0.0.1:1/callback?code=c&state=s ",
340+
})
341+
342+
expect(selectProviderConnectView(typed.state).paste?.error).toBeUndefined()
343+
344+
const sent = transitionProviderConnect(typed.state, { type: "paste.submitted" })
345+
346+
expect(sent.effects).toMatchObject([
347+
{ type: "paste", attempt: waiting.attempt, value: "http://127.0.0.1:1/callback?code=c&state=s" },
348+
])
349+
// The field takes no edits or second submit while the first is in flight.
350+
expect(selectProviderConnectView(sent.state).paste).toMatchObject({ locked: true, submitting: true })
351+
expect(transitionProviderConnect(sent.state, { type: "paste.submitted" }).effects).toEqual([])
352+
expect(transitionProviderConnect(sent.state, { type: "paste.changed", value: "again" }).state).toBe(sent.state)
353+
354+
const rejected = transitionProviderConnect(sent.state, {
355+
type: "paste.rejected",
356+
attempt: waiting.attempt,
357+
error: "This URL belongs to a different sign-in",
358+
})
359+
360+
expect(selectProviderConnectView(rejected.state)).toMatchObject({
361+
screen: "browser",
362+
paste: {
363+
value: " http://127.0.0.1:1/callback?code=c&state=s ",
364+
error: { type: "rejected", message: "This URL belongs to a different sign-in" },
365+
locked: false,
366+
},
367+
})
368+
369+
// Polling still decides how the sign-in ends.
370+
const done = transitionProviderConnect(rejected.state, {
371+
type: "polled",
372+
attempt: waiting.attempt,
373+
outcome: { status: "complete" },
374+
})
375+
376+
expect(done.state.status).toBe("refreshing")
377+
})
378+
379+
test("an accepted paste stays locked while polling reports how the sign-in ends", () => {
380+
const waiting = waitingWithPaste()
381+
const typed = transitionProviderConnect(waiting.state, { type: "paste.changed", value: "http://x" })
382+
const sent = transitionProviderConnect(typed.state, { type: "paste.submitted" })
383+
const accepted = transitionProviderConnect(sent.state, { type: "paste.accepted", attempt: waiting.attempt })
384+
385+
expect(selectProviderConnectView(accepted.state).paste).toEqual({
386+
value: "http://x",
387+
error: undefined,
388+
locked: true,
389+
submitting: false,
390+
})
391+
expect(transitionProviderConnect(accepted.state, { type: "paste.submitted" }).effects).toEqual([])
392+
})
393+
394+
test("a paste result after Back is ignored, and a resumed sign-in starts editable", () => {
395+
const waiting = waitingWithPaste()
396+
const typed = transitionProviderConnect(waiting.state, { type: "paste.changed", value: "http://x" })
397+
const sent = transitionProviderConnect(typed.state, { type: "paste.submitted" })
398+
const back = transitionProviderConnect(sent.state, { type: "back" })
399+
400+
expect(transitionProviderConnect(back.state, { type: "paste.accepted", attempt: waiting.attempt }).state).toBe(
401+
back.state,
402+
)
403+
404+
const failed = transitionProviderConnect(sent.state, {
405+
type: "polled",
406+
attempt: waiting.attempt,
407+
outcome: { status: "unreachable", error: "offline" },
408+
})
409+
410+
const resumed = transitionProviderConnect(failed.state, { type: "retry" })
411+
412+
expect(selectProviderConnectView(resumed.state).paste).toMatchObject({ value: "http://x", locked: false })
413+
expect(
414+
transitionProviderConnect(resumed.state, { type: "paste.rejected", attempt: waiting.attempt, error: "late" }).state,
415+
).toBe(resumed.state)
416+
})
417+
295418
test("a failed refresh retries the refresh, not the sign-in", () => {
296419
const sent = submittingKey()
297420
const stored = transitionProviderConnect(sent.state, { type: "submit.succeeded", attempt: sent.attempt })

‎packages/app/src/providers/connect/machine.ts‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export type ProviderSignIn =
3333
readonly method: Extract<ProviderConnectMethod, { type: "oauth" }>
3434
readonly authorization: ProviderAuthorization
3535
readonly browserFailed: boolean
36+
/** Set when the attempt also takes the redirect URL pasted from a browser that cannot reach the server. */
37+
readonly paste?: ProviderPaste
3638
}
3739
| {
3840
readonly kind: "code"
@@ -55,6 +57,13 @@ export type ProviderPolledSignIn = Extract<ProviderSignIn, { kind: "browser" | "
5557

5658
export type ProviderEntryError = { readonly type: "required" } | { readonly type: "rejected"; readonly message: string }
5759

60+
/** The pasted redirect URL of a browser sign-in. The browser's own callback still completes the sign-in meanwhile. */
61+
export type ProviderPaste =
62+
| { readonly status: "editing"; readonly value: string; readonly error?: ProviderEntryError }
63+
| { readonly status: "submitting"; readonly value: string }
64+
/** The server took the URL and finishes the sign-in; polling reports how it ends. */
65+
| { readonly status: "accepted"; readonly value: string }
66+
5867
export type ProviderRetry =
5968
| { readonly type: "start" }
6069
| { readonly type: "poll"; readonly signIn: Extract<ProviderSignIn, { kind: "browser" }> }
@@ -110,6 +119,11 @@ export type ProviderConnectEvent =
110119
readonly opened: boolean
111120
}
112121
| { readonly type: "entry.submitted"; readonly value: string }
122+
| { readonly type: "paste.changed"; readonly value: string }
123+
| { readonly type: "paste.submitted" }
124+
/** The server took the pasted URL; the sign-in still settles through polling. */
125+
| { readonly type: "paste.accepted"; readonly attempt: number }
126+
| { readonly type: "paste.rejected"; readonly attempt: number; readonly error: string }
113127
| { readonly type: "oauth.started"; readonly attempt: number; readonly authorization: ProviderAuthorization }
114128
| { readonly type: "command.started"; readonly attempt: number; readonly command: ProviderCommandAttempt }
115129
| { readonly type: "external.saved"; readonly attempt: number }
@@ -152,6 +166,12 @@ export type ProviderConnectEffect =
152166
readonly value: string
153167
readonly answer?: FormAnswer
154168
}
169+
| {
170+
readonly type: "paste"
171+
readonly attempt: number
172+
readonly signIn: Extract<ProviderSignIn, { kind: "browser" }>
173+
readonly value: string
174+
}
155175
| { readonly type: "refresh"; readonly attempt: number; readonly method: ProviderConnectMethod }
156176
/** Reloads the catalogs without reporting back, for a credential saved after the dialog moved on. */
157177
| { readonly type: "sync" }
@@ -333,6 +353,7 @@ export function selectProviderConnectView(state: ProviderConnectState) {
333353
commandOutput: signIn?.kind === "command" ? signIn.output : "",
334354
form: state.status === "form" ? selectProviderFormView(state.form) : undefined,
335355
entryError: state.status === "entering" ? state.error : undefined,
356+
paste: state.status === "waiting" && signIn?.kind === "browser" && signIn.paste ? pasteView(signIn.paste) : undefined,
336357
// A typed secret is sent and the catalogs are refreshing: the form takes no second submit.
337358
submitting: state.status === "submitting" || state.status === "refreshing",
338359
error: state.status === "failed" ? state.error : undefined,
@@ -452,6 +473,14 @@ function polled(
452473
state: Extract<ProviderConnectState, { status: "waiting" }>,
453474
event: ProviderConnectEvent,
454475
): ProviderConnectTransition {
476+
if (
477+
event.type === "paste.changed" ||
478+
event.type === "paste.submitted" ||
479+
event.type === "paste.accepted" ||
480+
event.type === "paste.rejected"
481+
)
482+
return pasted(state, event)
483+
455484
if (event.type !== "polled") return unchanged(state)
456485
const outcome = event.outcome
457486

@@ -473,15 +502,69 @@ function polled(
473502
return unchanged({ ...selection(state, state.attempt), status: "failed", error: outcome.error, retry })
474503
}
475504

505+
// A paste only hands the server the URL: polling keeps running and reports how the sign-in ends.
506+
function pasted(
507+
state: Extract<ProviderConnectState, { status: "waiting" }>,
508+
event: Extract<
509+
ProviderConnectEvent,
510+
{ type: "paste.changed" | "paste.submitted" | "paste.accepted" | "paste.rejected" }
511+
>,
512+
): ProviderConnectTransition {
513+
const signIn = state.signIn
514+
515+
if (signIn.kind !== "browser" || !signIn.paste) return unchanged(state)
516+
const paste = signIn.paste
517+
const set = (next: ProviderPaste) => ({ ...state, signIn: { ...signIn, paste: next } })
518+
519+
if (event.type === "paste.changed") {
520+
if (paste.status !== "editing" || (event.value === paste.value && !paste.error)) return unchanged(state)
521+
522+
return unchanged(set({ status: "editing", value: event.value }))
523+
}
524+
525+
if (event.type === "paste.submitted") {
526+
if (paste.status !== "editing") return unchanged(state)
527+
const value = paste.value.trim()
528+
529+
if (!value) return unchanged(set({ status: "editing", value: paste.value, error: { type: "required" } }))
530+
531+
return {
532+
state: set({ status: "submitting", value: paste.value }),
533+
effects: [{ type: "paste", attempt: state.attempt, signIn, value }],
534+
}
535+
}
536+
537+
if (paste.status !== "submitting") return unchanged(state)
538+
539+
if (event.type === "paste.accepted") return unchanged(set({ status: "accepted", value: paste.value }))
540+
541+
return unchanged(set({ status: "editing", value: paste.value, error: { type: "rejected", message: event.error } }))
542+
}
543+
544+
function pasteView(paste: ProviderPaste) {
545+
return {
546+
value: paste.value,
547+
error: paste.status === "editing" ? paste.error : undefined,
548+
locked: paste.status !== "editing",
549+
submitting: paste.status === "submitting",
550+
}
551+
}
552+
476553
function poll(
477554
state: Extract<ProviderConnectState, { status: "starting" | "failed" }>,
478555
signIn: ProviderPolledSignIn,
479556
): ProviderConnectTransition {
480557
const attempt = state.status === "starting" ? state.attempt : state.attempt + 1
481558

559+
// A paste in flight when polling failed reports to the old attempt, so a resumed sign-in starts editable too.
560+
const next: ProviderPolledSignIn =
561+
signIn.kind === "browser" && signIn.authorization.paste
562+
? { ...signIn, paste: { status: "editing", value: signIn.paste?.value ?? "" } }
563+
: signIn
564+
482565
return {
483-
state: { ...selection(state, attempt), status: "waiting", signIn },
484-
effects: [{ type: "poll", attempt, signIn, delay: false }],
566+
state: { ...selection(state, attempt), status: "waiting", signIn: next },
567+
effects: [{ type: "poll", attempt, signIn: next, delay: false }],
485568
}
486569
}
487570

0 commit comments

Comments
 (0)