From b5fc67f0f40b214c9074d1d7b84533f895660343 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 4 Oct 2026 11:54:54 +0800 Subject: [PATCH] =?UTF-8?q?kiro=200.1.7:=20a=20Builder=20ID=20sign-in=20is?= =?UTF-8?q?=20asked=20with=20Builder=20ID's=20service=20profile,=20so=20it?= =?UTF-8?q?s=20models=20are=20listed=20and=20served,=20as=20the=20built-in?= =?UTF-8?q?=20(#16,=20YuanZhe-99:=20a=20Kiro=20Student=20account=20moved?= =?UTF-8?q?=20to=20the=20plugin=20was=20offered=20no=20claude-opus-5.5=20o?= =?UTF-8?q?r=20claude-sonnet-5.5,=20which=20the=20built-in=20listed)=20?= =?UTF-8?q?=E2=80=94=20a=20Builder=20ID=20sign-in=20(kiro-cli's,=20the=20K?= =?UTF-8?q?iro=20IDE's=20or=20one=20made=20here)=20names=20no=20profile,?= =?UTF-8?q?=20and=20Kiro=20won't=20list=20one=20for=20it:=20List-Available?= =?UTF-8?q?-Profiles=20answers=20403=20"AWS=20Builder=20ID=20is=20not=20su?= =?UTF-8?q?pported=20for=20this=20operation"=20in=20both=20regions,=20so?= =?UTF-8?q?=20profileOf=20threw,=20provider.models=20fell=20back=20to=20Au?= =?UTF-8?q?to=20alone,=20and=20usage=20and=20chat=20failed.=20magpie's=20b?= =?UTF-8?q?uilt-in=20has=20asked=20such=20a=20sign-in=20with=20the=20servi?= =?UTF-8?q?ce=20profile=20kiro-cli=20and=20the=20IDE=20use=20(arn:aws:code?= =?UTF-8?q?whisperer:us-east-1:638616132270:profile/AAAACCCCXXXX)=20since?= =?UTF-8?q?=20magpie=2040102f74=20(yetone/magpie#422);=20the=20plugin=20no?= =?UTF-8?q?w=20does=20as=20it=20does:=20kiro-cli's=20odic=20sign-in=20with?= =?UTF-8?q?=20Builder=20ID's=20start=20URL=20or=20none,=20the=20IDE's=20wi?= =?UTF-8?q?th=20provider=20BuilderId,=20and=20one=20signed=20in=20here=20(?= =?UTF-8?q?or=20moved=20from=20the=20built-in)=20with=20loginProvider=20Bu?= =?UTF-8?q?ilderId=20take=20it=20without=20asking,=20and=20an=20AWS=20sign?= =?UTF-8?q?-in=20whose=20profile=20listing=20is=20that=20403=20takes=20it?= =?UTF-8?q?=20too.=20Identity=20Center=20sign-ins=20still=20have=20their?= =?UTF-8?q?=20profile=20listed.=20builderid.test.mjs=20(fake=20management?= =?UTF-8?q?=20and=20runtime=20API:=20one=20signed=20in=20here=20lists=20cl?= =?UTF-8?q?aude-opus-5.5=20and=20chats=20under=20the=20service=20profile,?= =?UTF-8?q?=20the=20403=20found,=20Identity=20Center=20unchanged,=20kiro-c?= =?UTF-8?q?li's=20with=20and=20without=20a=20start=20URL,=20the=20IDE's)?= =?UTF-8?q?=20fails=204=20of=205=20on=200.1.6=20and=20passes;=20bun=20test?= =?UTF-8?q?=20(28=20pass)=20and=20bun=20scripts/check.mjs=20kiro=20pass=20?= =?UTF-8?q?under=20a=20temp=20HOME.=20Checked=20for=20real=20with=20a=20Go?= =?UTF-8?q?ogle=20sign-in=20(profile=20listed,=20models=20listed,=20claude?= =?UTF-8?q?-sonnet-4.5=20answers):=20unchanged.=20Not=20tried=20with=20a?= =?UTF-8?q?=20real=20Builder=20ID=20or=20Kiro=20Student=20account=20(none?= =?UTF-8?q?=20here).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/kiro/builderid.test.mjs | 148 +++++++++++++++++++++++++++++++ packages/kiro/index.mjs | 21 ++++- packages/kiro/package.json | 2 +- 3 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 packages/kiro/builderid.test.mjs diff --git a/packages/kiro/builderid.test.mjs b/packages/kiro/builderid.test.mjs new file mode 100644 index 0000000..c813f40 --- /dev/null +++ b/packages/kiro/builderid.test.mjs @@ -0,0 +1,148 @@ +// A Builder ID sign-in (kiro-cli's, the Kiro IDE's or one made here) has no +// profile of its own, and Kiro won't list one for it; as the built-in +// (magpie 40102f74, #422), it is asked with the service profile kiro-cli and +// the IDE use for Builder ID, so its own models are listed and served +// (magpie-community/plugins#16: a Kiro Student account was offered Auto +// alone, where the built-in listed claude-opus-5.5 and claude-sonnet-5.5). +import { test, expect, afterAll, beforeAll, afterEach } from "bun:test" +import { homedir, tmpdir } from "node:os" +import { mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs" +import { join } from "node:path" +import { Database } from "bun:sqlite" + +let plugin +beforeAll(async () => { + // Bun reads HOME once, at start: run as HOME=$(mktemp -d) bun test, so + // no real sign-in (~/.kiro, ~/.aws, kiro-cli's) is ever read or written + if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test") + ;({ KiroAuthPlugin: plugin } = await import("./index.mjs")) +}) +const offline = async () => { throw new Error("no network in tests") } +const real = globalThis.fetch +globalThis.fetch = offline +const cliDir = () => + process.platform === "darwin" ? join(homedir(), "Library", "Application Support", "kiro-cli") : join(homedir(), ".local", "share", "kiro-cli") +const ideDir = () => join(homedir(), ".aws", "sso", "cache") +const clean = () => { + rmSync(cliDir(), { recursive: true, force: true }) + rmSync(ideDir(), { recursive: true, force: true }) +} +afterAll(() => { + globalThis.fetch = real + clean() +}) +afterEach(() => { + globalThis.fetch = offline + clean() +}) + +const BUILDER_ID = "arn:aws:codewhisperer:us-east-1:638616132270:profile/AAAACCCCXXXX" +const IDC = "arn:aws:codewhisperer:us-east-1:222222222222:profile/IDC" +const later = () => Date.now() + 60 * 60_000 +const STUDENT = [ + { modelId: "auto", modelName: "auto", tokenLimits: { maxInputTokens: 1000000, maxOutputTokens: 64000 } }, + { modelId: "claude-opus-5.5", modelName: "claude-opus-5.5", tokenLimits: { maxInputTokens: 1000000, maxOutputTokens: 128000 } }, + { modelId: "claude-sonnet-5.5", modelName: "claude-sonnet-5.5", tokenLimits: { maxInputTokens: 1000000, maxOutputTokens: 64000 } }, +] + +// kiro is Kiro's API as a Builder ID sees it: no profile listed for it (a +// 403, or the profiles given), the models listed only under its service +// profile, and a chat refused with any other. +function kiro({ profiles } = {}) { + const seen = { profiles: 0, listedWith: [], chatWith: [] } + globalThis.fetch = async (input, init = {}) => { + const url = new URL(String(input)) + if (url.pathname === "/List-Available-Profiles") { + seen.profiles++ + if (profiles) return Response.json({ profiles }) + return new Response(JSON.stringify({ message: "AWS Builder ID is not supported for this operation", reason: null }), { status: 403 }) + } + if (url.pathname === "/List-Available-Models") { + const p = url.searchParams.get("profileArn") + seen.listedWith.push(p) + if (p === BUILDER_ID || p === IDC) return Response.json({ defaultModel: { modelId: "auto" }, models: STUDENT }) + return new Response(JSON.stringify({ message: "profileArn is required for this request." }), { status: 400 }) + } + if (url.pathname === "/generateAssistantResponse") { + const b = JSON.parse(init.body) + seen.chatWith.push([b.profileArn, b.conversationState.currentMessage.userInputMessage.modelId]) + return new Response(JSON.stringify({ message: "stop here" }), { status: 500 }) + } + throw new Error("unexpected " + url) + } + return seen +} + +const hooks = () => plugin({ client: { auth: { set: async () => {} } } }) +const ask = async (h, auth, model) => { + const l = await h.auth.loader(async () => auth) + return l.fetch("https://kiro.invalid/v1/messages", { method: "POST", body: JSON.stringify({ model, messages: [{ role: "user", content: "hi" }] }) }) +} + +test("a Builder ID signed in here lists and serves its own models under Builder ID's profile", async () => { + const seen = kiro() + const h = await hooks() + const auth = { type: "oauth", access: "t", refresh: "r", expires: later(), method: "idc", loginProvider: "BuilderId", region: "us-east-1", + profileArn: "", clientId: "c", clientSecret: "s", accountId: "student@example.edu" } + const list = await h.provider.models({ models: { auto: {} } }, { auth }) + expect(Object.keys(list)).toEqual(["auto", "claude-opus-5.5", "claude-sonnet-5.5"]) + await ask(h, auth, "claude-opus-5.5") + expect(seen.profiles).toBe(0) + expect(seen.listedWith).toEqual([BUILDER_ID]) + expect(seen.chatWith).toEqual([[BUILDER_ID, "claude-opus-5.5"]]) +}) + +test("an AWS sign-in whose profiles Kiro won't list for Builder ID takes Builder ID's profile", async () => { + const seen = kiro() + const h = await hooks() + // moved from the built-in without its provider said + const auth = { type: "oauth", access: "t", refresh: "r", expires: later(), method: "idc", region: "us-east-1", profileArn: "", + clientId: "c", clientSecret: "s", accountId: "student@example.edu" } + const list = await h.provider.models({ models: { auto: {} } }, { auth }) + expect(Object.keys(list)).toContain("claude-opus-5.5") + expect(seen.profiles).toBe(1) + expect(seen.listedWith).toEqual([BUILDER_ID]) +}) + +test("Identity Center still has its profile listed", async () => { + const seen = kiro({ profiles: [{ arn: IDC }] }) + const h = await hooks() + const auth = { type: "oauth", access: "t", refresh: "r", expires: later(), method: "idc", loginProvider: "Enterprise", region: "us-east-1", + profileArn: "", clientId: "c", clientSecret: "s", accountId: "dev@corp.example" } + await h.provider.models({ models: { auto: {} } }, { auth }) + expect(seen.profiles).toBe(1) + expect(seen.listedWith).toEqual([IDC]) +}) + +test("kiro-cli's Builder ID sign-in (its start URL, or none) is asked with Builder ID's profile", async () => { + for (const start of ["https://view.awsapps.com/start", undefined]) { + mkdirSync(cliDir(), { recursive: true }) + const db = new Database(join(cliDir(), "data.sqlite3"), { create: true }) + db.exec("CREATE TABLE auth_kv (key TEXT PRIMARY KEY, value TEXT)") + const token = { access_token: "t", refresh_token: "r", expires_at: new Date(later()).toISOString(), region: "us-east-1", + ...(start ? { start_url: start } : {}) } + db.prepare("INSERT INTO auth_kv VALUES (?, ?)").run("kirocli:odic:token", JSON.stringify(token)) + db.prepare("INSERT INTO auth_kv VALUES (?, ?)").run("kirocli:odic:device-registration", JSON.stringify({ client_id: "c", client_secret: "s" })) + db.close() + const seen = kiro() + const h = await hooks() + const list = await h.provider.models({ models: { auto: {} } }, { auth: { type: "oauth", source: "kiro-cli", access: "", refresh: "", expires: 0 } }) + expect(Object.keys(list)).toContain("claude-sonnet-5.5") + expect(seen.profiles).toBe(0) + expect(seen.listedWith).toEqual([BUILDER_ID]) + clean() + } +}) + +test("the Kiro IDE's Builder ID sign-in is asked with Builder ID's profile", async () => { + mkdirSync(ideDir(), { recursive: true }) + writeFileSync(join(ideDir(), "kiro-auth-token.json"), JSON.stringify({ accessToken: "t", refreshToken: "r", + expiresAt: new Date(later()).toISOString(), authMethod: "IdC", provider: "BuilderId", region: "us-east-1", clientIdHash: "h" })) + writeFileSync(join(ideDir(), "h.json"), JSON.stringify({ clientId: "c", clientSecret: "s" })) + const seen = kiro() + const h = await hooks() + const list = await h.provider.models({ models: { auto: {} } }, { auth: { type: "oauth", source: "kiro-ide", access: "", refresh: "", expires: 0 } }) + expect(Object.keys(list)).toContain("claude-opus-5.5") + expect(seen.profiles).toBe(0) + expect(seen.listedWith).toEqual([BUILDER_ID]) +}) diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index e85bc70..d0e04f0 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -43,6 +43,13 @@ const SCOPES = ["codewhisperer:completions", "codewhisperer:analysis", "codewhis const IDE_UA = "KiroIDE-1.1.70-" + createHash("sha256").update("magpie:" + hostname()).digest("hex") const DESKTOP_UA = "Kiro-Desktop/0.2.13 (darwin; arm64)" const SIGN_IN_TIMEOUT = 10 * 60 * 1000 +// A Builder ID sign-in has no profile of its own, and Kiro won't list one +// for it (List-Available-Profiles is a 403, "AWS Builder ID is not supported +// for this operation"); the Kiro IDE and kiro-cli name this service profile +// for it instead, so its models, usage and chat are asked with it too. +const BUILDER_ID_START = "https://view.awsapps.com/start" +const BUILDER_ID_PROFILE = "arn:aws:codewhisperer:us-east-1:638616132270:profile/AAAACCCCXXXX" +const isBuilderID = (provider) => String(provider ?? "").toLowerCase() === "builderid" const env = (k) => process.env[k] ?? "" const home = () => env("HOME") || homedir() @@ -118,6 +125,8 @@ async function readCLI() { if (kind === "social") c.method = "social" else if (kind === "odic") { c.method = "idc" + // as kiro-cli tells them apart: Builder ID's start URL, or none + c.builderID = !m.start_url || m.start_url === BUILDER_ID_START const reg = get("kirocli:odic:device-registration") if (reg) [c.clientId, c.clientSecret] = [reg.client_id ?? "", reg.client_secret ?? ""] } else { @@ -145,7 +154,7 @@ async function readIDE() { } if (!t?.accessToken) return null const c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: time(t.expiresAt), region: t.region || "us-east-1", - profile: t.profileArn ?? "", method: "idc", idePath: path } + profile: t.profileArn ?? "", method: "idc", idePath: path, builderID: isBuilderID(t.provider) } if (t.clientIdHash) { try { const reg = JSON.parse(await readFile(join(ideDir(), t.clientIdHash + ".json"), "utf8")) @@ -289,6 +298,7 @@ class KiroError extends Error { } catch {} super(msg ? `Kiro: ${msg} (${status})` : `Kiro: ${STATUS_TEXT[status] ?? ""}`) this.status = status + this.body = String(body ?? "") } } @@ -309,7 +319,7 @@ async function managementCall(region, a, method, query, body) { } // profileOf finds the profile a sign-in that doesn't name one uses: an API -// key's own, or the first Kiro lists in either of its regions. +// key's own, Builder ID's, or the first Kiro lists in either of its regions. async function profileOf(c) { if (c.method === "apikey") { const res = await fetch(management("us-east-1") + "/", { @@ -327,6 +337,7 @@ async function profileOf(c) { if (!arn) throw new Error("Kiro didn't say which profile the API key is for") return arn } + if (c.builderID) return BUILDER_ID_PROFILE let last for (const region of ["us-east-1", "eu-central-1"]) { try { @@ -334,6 +345,8 @@ async function profileOf(c) { const arn = (out?.profiles ?? []).find((p) => p?.arn)?.arn if (arn) return arn } catch (e) { + // a Builder ID sign-in the token didn't say was one + if (c.method === "idc" && e?.status === 403 && e.body.includes("Builder ID")) return BUILDER_ID_PROFILE last = e } } @@ -396,7 +409,7 @@ async function credOf(auth) { if (!auth.access) return null return { access: auth.access, refresh: auth.refresh ?? "", expires: auth.expires ?? 0, method: auth.method || "social", region: auth.region || "us-east-1", profile: auth.profileArn ?? "", clientId: auth.clientId ?? "", clientSecret: auth.clientSecret ?? "", - own: true } + builderID: isBuilderID(auth.loginProvider), own: true } } // Account holds the credentials in use, refreshing them when near their @@ -1275,7 +1288,7 @@ async function browserSignIn() { grantType: "authorization_code", redirectUri: awsRedirect, code: q.get("code"), codeVerifier: aws.verifier }) if (!t.accessToken) throw new Error("AWS sent back no token") c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: expiry(t.expiresIn), method: "idc", loginProvider: aws.provider, - region: aws.region, clientId: aws.clientId, clientSecret: aws.clientSecret, profile: "" } + region: aws.region, clientId: aws.clientId, clientSecret: aws.clientSecret, profile: "", builderID: isBuilderID(aws.provider) } } else if (q.get("state") !== state) { // not ours: someone else's page, or a stale tab return html(200, page(false, "This link isn't from this sign-in", "Start it again.")) diff --git a/packages/kiro/package.json b/packages/kiro/package.json index 1110bef..ed1cba7 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.6", + "version": "0.1.7", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./index.mjs",