Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

signin: a sign-in's address pasted into the Import box finishes that sign-in (LuvLetter)

yetoned0c1afeffd96
diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js
index e320d0330..a0a8aca05 100644
--- a/internal/gui/assets/app.js
+++ b/internal/gui/assets/app.js
@@ -11997,6 +11997,7 @@ let justAdded = ""; // the account that just came in, to greet it
// through
async function startSignIn(agent, risky, site, o = {}) {
if (signingOpen()) api("signin/" + signing.id + "/cancel", {}).catch(() => {});
+ if (signing?.behind) api("signin/" + signing.behind + "/cancel", {}).catch(() => {});
// an account Google may suspend is added only once that is said
if (subOf(agent)?.risk && !risky) {
signing = { agent, state: "risk", forUser: o.user, picked: o.proxy };
@@ -12293,6 +12294,7 @@ function renderStepPlan(sp, id) {
function cancelSignIn() {
if (signing?.id) api("signin/" + signing.id + "/cancel", {}).catch(() => {});
+ if (signing?.behind) api("signin/" + signing.behind + "/cancel", {}).catch(() => {});
signing = null;
renderProviders();
}
@@ -13098,9 +13100,14 @@ function renderCodexAppOut(out) {
// again: the files' text (or what is pasted) goes to magpie, which checks
// each account with the vendor before keeping it, and says what became of
// each. What is read stays here only until it is sent; the box masks it.
+// A sign-in waiting for its browser's address is kept behind the import
+// rather than canceled: that address pasted into the import box finishes
+// it there (LuvLetter, magpie in Docker, pasted it into this box instead of
+// the sign-in's Callback URL field), and closing the box ends it.
function startImport(agent) {
- if (signingOpen()) api("signin/" + signing.id + "/cancel", {}).catch(() => {});
- signing = { agent, state: "import", text: "", files: [] };
+ const behind = signingOpen() && signing.pasteCallback ? signing.id : signing?.behind;
+ if (signingOpen() && signing.id !== behind) api("signin/" + signing.id + "/cancel", {}).catch(() => {});
+ signing = { agent, state: "import", text: "", files: [], behind };
renderProviders();
document.querySelector(".signing.import textarea")?.focus();
}
@@ -13111,14 +13118,15 @@ async function runImport(agent) {
const names = signing.files.map((f) => f.name);
if (signing.text.trim()) { files.push(signing.text); names.push(t("The pasted text")); }
if (!files.length) return;
- signing = { agent, state: "importing", names };
+ const behind = signing.behind;
+ signing = { agent, state: "importing", names, behind };
renderProviders();
try {
const r = await api("signin/import", { agent, files });
if (signing?.agent !== agent) return;
providers = r.providers;
const added = r.results.filter((x) => x.status === "added" || x.status === "updated");
- signing = { agent, state: "imported", results: r.results, names };
+ signing = { agent, state: "imported", results: r.results, names, behind };
delete loginUsage[agent];
const p = providers.providers.find((x) => x.account?.agent === agent);
if (p && added.length) {
@@ -13133,7 +13141,7 @@ async function runImport(agent) {
renderAgents();
} catch (e) {
if (signing?.agent !== agent) return;
- signing = { agent, state: "import", text: "", files: [], error: e.message };
+ signing = { agent, state: "import", text: "", files: [], error: e.message, behind };
renderProviders();
}
}
diff --git a/internal/gui/providers.go b/internal/gui/providers.go
index 8915c4cad..97e233dba 100644
--- a/internal/gui/providers.go
+++ b/internal/gui/providers.go
@@ -2266,10 +2266,20 @@ func providerRoutes(mux *http.ServeMux, w Windows) {
return provider.ImportFactoryKeys(ctx, files)
}
}
- res, err := imp(ctx, in.Agent, in.Files)
- if err != nil {
- fail(rw, err)
- return
+ // the address a sign-in's browser ended on, pasted here rather than
+ // in the sign-in's Callback URL field, finishes that sign-in: it is
+ // no account file, and never goes to the vendor as a token
+ res, rest := provider.PastedSignIns(in.Files)
+ if len(rest) > 0 {
+ more, err := imp(ctx, in.Agent, rest)
+ if err != nil && len(res) == 0 {
+ fail(rw, err)
+ return
+ }
+ if err != nil {
+ more = []provider.ImportedAccount{{Status: "failed", Error: err.Error()}}
+ }
+ res = append(res, more...)
}
agent.SyncCatalogSoon()
writeJSON(rw, struct {
diff --git a/internal/gui/tests/signin-paste-import.test.cjs b/internal/gui/tests/signin-paste-import.test.cjs
new file mode 100644
index 000000000..0d43538d0
--- /dev/null
+++ b/internal/gui/tests/signin-paste-import.test.cjs
@@ -0,0 +1,98 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// LuvLetter on Discord (magpie in Docker, `magpie web`): ChatGPT sent the
+// browser back to http://localhost:1455/auth/callback?code=…, and the address
+// went into the Import box (Import from a file instead…) rather than the
+// sign-in's Callback URL field. The box canceled the sign-in as it opened,
+// so nothing was left for the address to finish, and it was read as a
+// refresh token. Now the sign-in waits behind the box: the address pasted
+// there is posted to the import, which finishes the sign-in, the account is
+// listed as added, and Done ends the sign-in left behind. A sign-in that
+// takes no pasted address is still canceled when the box opens. English
+// and Chinese, Chromium and WebKit; no backend, the API is faked here.
+const assert = require("node:assert/strict");
+const fs = require("node:fs/promises");
+const path = require("node:path");
+const { test } = require("node:test");
+const { chromium, webkit } = require("playwright");
+
+const assets = path.resolve(__dirname, "../assets");
+const LINK = "https://auth.openai.com/oauth/authorize?client_id=app&redirect_uri=http%3A%2F%2Flocalhost%3A1455%2Fauth%2Fcallback&state=st1";
+const BACK = "http://localhost:1455/auth/callback?code=ac_fake&scope=openid&state=st1";
+
+function server(lang, log, paste) {
+ return async (route) => {
+ const url = new URL(route.request().url());
+ const json = (data) => route.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
+ if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
+ if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } });
+ if (url.pathname === "/api/providers") return json({ providers: [{ id: "openai", name: "OpenAI", icon: "openai", preset: "openai", models: [], agents: [], key: { set: true, masked: "sk-…ab12" } }], presets: [], excluded: [], gateway: { running: true, window: true } });
+ if (url.pathname === "/api/plugins") return json({ plugins: [] });
+ if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
+ if (url.pathname === "/api/gateway/trace") return json({ routes: [] });
+ if (url.pathname.endsWith("/cancel")) { log.push("cancel " + url.pathname.split("/")[3]); return route.fulfill({ status: 204 }); }
+ if (url.pathname === "/api/signin/import") {
+ log.push(route.request().postDataJSON());
+ return json({ providers: { providers: [], presets: [], excluded: [] }, results: [{ user: "[email protected]", status: "added", plan: "plus" }] });
+ }
+ if (url.pathname === "/api/signin" || url.pathname === "/api/signin/cx1") return json({ id: "cx1", agent: "codex", state: "waiting", url: LINK, pasteCallback: paste });
+ if (url.pathname.startsWith("/api/")) return json({});
+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
+ try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
+ };
+}
+
+const W = {
+ en: { instead: "Import from a file instead…", import: "Import", done: "Done", added: "Added" },
+ zh: { instead: "改为从文件导入…", import: "导入", done: "完成", added: "已添加" },
+};
+
+async function open(engine, lang, log, paste, t) {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ const page = await (await browser.newContext({ viewport: { width: 900, height: 800 }, reducedMotion: "reduce" })).newPage();
+ t.after(async () => {
+ if (process.env.ARTIFACT_DIR) {
+ await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
+ await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-signin-paste-import-${paste}.png`) });
+ }
+ await browser.close();
+ });
+ page.setDefaultTimeout(5000);
+ const errors = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ await page.route("**/*", server(lang, log, paste));
+ await page.context().route((url) => url.hostname !== "magpie.test", (route) => route.fulfill({ contentType: "text/html", body: "" }));
+ await page.goto("http://magpie.test/?view=providers");
+ await page.locator("#addProvider").click();
+ const sheet = page.locator("#addSheet");
+ await sheet.locator('.tile[data-pick="ChatGPT"]').click();
+ await sheet.locator(".signing").getByRole("button", { name: W[lang].instead }).click();
+ await sheet.locator(".signing.import textarea").waitFor();
+ return { page, sheet, errors };
+}
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const lang of ["en", "zh"]) {
+ test(`${engine} ${lang}: a sign-in's address pasted into the Import box finishes it`, async (t) => {
+ const w = W[lang], log = [];
+ const { page, sheet, errors } = await open(engine, lang, log, true, t);
+ assert.deepEqual(log, [], "the sign-in waits behind the box, not canceled");
+ await sheet.locator(".signing.import textarea").fill(BACK);
+ await sheet.getByRole("button", { name: w.import, exact: true }).click();
+ await sheet.locator(".signing .res.added").getByText("[email protected]").waitFor();
+ assert.deepEqual(log, [{ agent: "codex", files: [BACK] }]);
+ await sheet.getByRole("button", { name: w.done, exact: true }).click();
+ for (let i = 0; i < 40 && log.length < 2; i++) await page.waitForTimeout(50);
+ assert.deepEqual(log.slice(1), ["cancel cx1"], "Done ends the sign-in left behind");
+ assert.deepEqual(errors, []);
+ });
+ }
+ test(`${engine}: a sign-in that takes no address is canceled as the box opens`, async (t) => {
+ const log = [];
+ const { page, errors } = await open(engine, "en", log, false, t);
+ for (let i = 0; i < 40 && !log.length; i++) await page.waitForTimeout(50);
+ assert.deepEqual(log, ["cancel cx1"]);
+ assert.deepEqual(errors, []);
+ });
+}
diff --git a/internal/provider/google_import.go b/internal/provider/google_import.go
index b3faa571c..48959f483 100644
--- a/internal/provider/google_import.go
+++ b/internal/provider/google_import.go
@@ -98,7 +98,7 @@ func parseGoogleImport(agent, data string) ([]googleImport, error) {
if line == "" || strings.HasPrefix(line, "#") {
continue
}
- if strings.ContainsAny(line, " \t") {
+ if strings.ContainsAny(line, " \t") || looksLikeAddress(line) {
add(googleImport{err: "not a refresh token"})
continue
}
diff --git a/internal/provider/login_import.go b/internal/provider/login_import.go
index 6557fcae6..d4ea8c7f2 100644
--- a/internal/provider/login_import.go
+++ b/internal/provider/login_import.go
@@ -98,7 +98,9 @@ func parseLoginImport(agent, data string) ([]loginImport, error) {
if line == "" || strings.HasPrefix(line, "#") {
continue
}
- if strings.ContainsAny(line, " \t") {
+ // an address (a sign-in's callback among them) is never sent to
+ // the vendor as a token
+ if strings.ContainsAny(line, " \t") || looksLikeAddress(line) {
out = append(out, loginImport{err: "not a refresh token"})
continue
}
diff --git a/internal/provider/signin_pasted.go b/internal/provider/signin_pasted.go
new file mode 100644
index 000000000..5a29af0ea
--- /dev/null
+++ b/internal/provider/signin_pasted.go
@@ -0,0 +1,113 @@
+package provider
+
+// A browser sign-in's end pasted where accounts are imported. magpie in
+// Docker or on a server sends the browser back to a localhost address that
+// won't load there, and the sign-in takes that address in its own Callback
+// URL field. LuvLetter (Discord, 10-27) pasted it into the Import box beside
+// it instead, which read the address as a refresh token, sent it to ChatGPT
+// and said the token had been used since. An import now knows such an
+// address, or the code#state Claude's page shows, for what it is: it
+// finishes the sign-in it came from, as the Callback URL field would, and
+// is never read as an account or sent to a vendor as a token.
+
+import (
+ "errors"
+ "net/url"
+ "regexp"
+ "strings"
+)
+
+// errSignInAddressOver is a sign-in's end pasted after its sign-in is over,
+// or for none magpie started.
+var errSignInAddressOver = errors.New("this is the address a sign-in's browser ended on, not an account to import: it finishes only the sign-in that sent the browser there, which isn't waiting any more. Sign in again, and when the page won't load, paste its address into the sign-in's Callback URL field")
+
+// claudePageCode is the code#state Claude's page shows for Claude Code's
+// sign-in, typed back into `claude auth login`.
+var claudePageCode = regexp.MustCompile(`^[A-Za-z0-9_.~-]+#([A-Za-z0-9_.~-]+)$`)
+
+// signInEndState is the OAuth state a pasted text carries when the text is
+// a browser sign-in's end: an http(s) address with a code (or an error) and
+// a state in its query, or Claude's code#state. ok is false for anything
+// else, an account file or a refresh token among them.
+func signInEndState(text string) (state string, ok bool) {
+ s := strings.TrimSpace(text)
+ if s == "" || strings.ContainsAny(s, " \t\r\n") {
+ return "", false
+ }
+ if m := claudePageCode.FindStringSubmatch(s); m != nil {
+ return m[1], true
+ }
+ u, err := url.Parse(s)
+ if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
+ return "", false
+ }
+ q := u.Query()
+ if q.Get("code") == "" && q.Get("error") == "" {
+ return "", false
+ }
+ return q.Get("state"), true
+}
+
+// looksLikeAddress is a line of an import that is a web address: never a
+// refresh token, so never sent to a vendor as one.
+func looksLikeAddress(line string) bool {
+ return strings.Contains(line, "://")
+}
+
+// signInByState finds the sign-in whose vendor page carries state: the
+// one a pasted end of a sign-in belongs to.
+func signInByState(state string) *signInFlow {
+ if state == "" {
+ return nil
+ }
+ signIns.Lock()
+ defer signIns.Unlock()
+ for _, s := range signIns.m {
+ s.mu.Lock()
+ own, link := s.state, s.st.URL
+ s.mu.Unlock()
+ if own == state {
+ return s
+ }
+ if u, err := url.Parse(link); err == nil && link != "" && u.Query().Get("state") == state {
+ return s
+ }
+ }
+ return nil
+}
+
+// PastedSignIns takes out of an import's texts each one that is a browser
+// sign-in's end and finishes the sign-in it belongs to with it, as its
+// Callback URL field does. done says what became of each, in the texts'
+// order; rest are the texts left to import as accounts.
+func PastedSignIns(texts []string) (done []ImportedAccount, rest []string) {
+ for _, text := range texts {
+ state, ok := signInEndState(text)
+ if !ok {
+ rest = append(rest, text)
+ continue
+ }
+ done = append(done, finishPastedSignIn(state, strings.TrimSpace(text)))
+ }
+ return done, rest
+}
+
+func finishPastedSignIn(state, text string) ImportedAccount {
+ s := signInByState(state)
+ if s == nil {
+ return ImportedAccount{Status: "failed", Error: errSignInAddressOver.Error()}
+ }
+ st := s.status()
+ if st.State != "waiting" {
+ return ImportedAccount{Status: "failed", Error: errSignInAddressOver.Error()}
+ }
+ if err := SubmitSignInCallback(st.ID, text); err != nil {
+ return ImportedAccount{Status: "failed", Error: err.Error()}
+ }
+ st = s.status()
+ out := ImportedAccount{User: st.User, Plan: st.Plan, Status: "added"}
+ if st.Again {
+ out.Status = "updated"
+ }
+ return out
+}
diff --git a/internal/provider/signin_pasted_test.go b/internal/provider/signin_pasted_test.go
new file mode 100644
index 000000000..e9fdad284
--- /dev/null
+++ b/internal/provider/signin_pasted_test.go
@@ -0,0 +1,123 @@
+package provider
+
+import (
+ "context"
+ "strings"
+ "testing"
+)
+
+// LuvLetter (Discord, 10-27): magpie in Docker, a ChatGPT sign-in whose
+// browser ended on http://localhost:1455/auth/callback?code=…&state=…, and
+// that address pasted into the Import box beside the sign-in. The box read
+// it as a refresh token, sent it to ChatGPT, and said the token had been
+// used since. Pasted there, it finishes the sign-in it came from, as the
+// sign-in's Callback URL field does, and nothing else is imported.
+func TestImportBoxFinishesThePastedSignIn(t *testing.T) {
+ claudeHome(t)
+ trades := fakeCodexTokens(t, nil)
+
+ st, err := StartSignIn("codex")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer CancelSignIn(st.ID)
+ back := callbackAddress(t, st, "cx", "")
+
+ // another sign-in's address: no sign-in finished, nothing traded, and
+ // the reason says where such an address goes
+ done, rest := PastedSignIns([]string{callbackAddress(t, st, "cx", "not-ours")})
+ if len(rest) != 0 || len(done) != 1 || done[0].Status != "failed" || !strings.Contains(done[0].Error, "Callback URL") {
+ t.Fatalf("done %+v rest %q", done, rest)
+ }
+ if n := trades.Load(); n != 0 {
+ t.Fatalf("%d trades for another sign-in's address", n)
+ }
+
+ // the address, as pasted with a newline, beside an account file
+ file := `{"type":"codex","refresh_token":"r-file"}`
+ done, rest = PastedSignIns([]string{file, " " + back + "\n"})
+ if len(rest) != 1 || rest[0] != file {
+ t.Fatalf("left to import: %q", rest)
+ }
+ if len(done) != 1 || done[0].Status != "added" || done[0].User != "[email protected]" {
+ t.Fatalf("done %+v", done)
+ }
+ if cur, _ := SignInStatus(st.ID); cur.State != "done" {
+ t.Fatalf("sign-in %+v", cur)
+ }
+ if n := trades.Load(); n != 1 {
+ t.Fatalf("%d trades", n)
+ }
+
+ // the same address again: its sign-in is over, so it says so, and the
+ // code is not traded twice
+ done, _ = PastedSignIns([]string{back})
+ if len(done) != 1 || done[0].Status != "failed" || !strings.Contains(done[0].Error, "isn't waiting") {
+ t.Fatalf("again: %+v", done)
+ }
+ if n := trades.Load(); n != 1 {
+ t.Fatalf("%d trades", n)
+ }
+}
+
+// Claude's page shows code#state for Claude Code's sign-in; pasted into the
+// Import box, it is typed into the `claude auth login` waiting for it.
+func TestImportBoxFinishesAPastedClaudeCode(t *testing.T) {
+ claudeHome(t)
+ noAnthropic(t)
+ fakeClaudeLogin(t, fakeClaudeAccount{email: "[email protected]", plan: "pro", refresh: "sk-ant-ort01-paste"}, "", false)
+
+ st, err := StartSignIn("claude")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer CancelSignIn(st.ID)
+ done, rest := PastedSignIns([]string{"the-code#st"})
+ if len(rest) != 0 || len(done) != 1 || done[0].Status != "added" || done[0].User != "[email protected]" {
+ t.Fatalf("done %+v rest %q", done, rest)
+ }
+}
+
+// What isn't a sign-in's end is left to import, and an address among
+// refresh tokens is never sent to the vendor as one.
+func TestImportTellsAddressesFromAccounts(t *testing.T) {
+ for _, s := range []string{
+ `{"tokens":{"refresh_token":"rt_x"}}`,
+ "rt_abc123",
+ "1//0gAbc-def_ghi",
+ "https://example.com/page",
+ "http://localhost:1455/auth/callback?state=s",
+ "a#b c",
+ } {
+ if _, ok := signInEndState(s); ok {
+ t.Errorf("%q read as a sign-in's end", s)
+ }
+ }
+ for _, s := range []string{
+ "http://localhost:1455/auth/callback?code=ac_1&scope=openid&state=s1",
+ "http://127.0.0.1:51121/oauth-callback?state=s1&code=4/0Ab",
+ "http://localhost:1455/auth/callback?error=access_denied&state=s1",
+ "code_1#s1",
+ } {
+ if st, ok := signInEndState(s); !ok || st != "s1" {
+ t.Errorf("%q: state %q, %v", s, st, ok)
+ }
+ }
+
+ claudeHome(t)
+ trades := fakeCodexTokens(t, nil)
+ res, err := ImportLogins(context.Background(), "codex", []string{"http://localhost:1455/auth/callback?code=ac_1&state=s1\n"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(res) != 1 || res[0].Status != "failed" || res[0].Error != "not a refresh token" {
+ t.Fatalf("results %+v", res)
+ }
+ if n := trades.Load(); n != 0 {
+ t.Fatalf("the address went to ChatGPT %d times", n)
+ }
+ gs, err := parseGoogleImport("antigravity", "1//0gAbc\nhttp://127.0.0.1:51121/oauth-callback?code=4/0Ab&state=s1")
+ if err != nil || len(gs) != 2 || gs[0].refreshToken != "1//0gAbc" || gs[1].err != "not a refresh token" {
+ t.Fatalf("google %+v %v", gs, err)
+ }
+}