@magpie-community/opencode-kiro-auth 0.1.39 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/index.mjs +183 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -75,6 +75,55 @@ them, are under "Other ways to sign in…".
|
|
|
75
75
|
us-east-1, as kiro-cli uses it. Its models are listed with the control
|
|
76
76
|
plane's `ListAvailableModels` and its credits with `GetUsageLimits`, and
|
|
77
77
|
no profile is named in its calls, as kiro-cli names none.
|
|
78
|
+
- **Import accounts from a file** (magpie only, `auth.import`): a JSON file,
|
|
79
|
+
or JSON pasted in, of sign-ins made elsewhere, one account or a list:
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
[
|
|
83
|
+
{ "email": "user@gmail.com", "refreshToken": "…", "provider": "Google" },
|
|
84
|
+
{ "refreshToken": "…", "provider": "Github" },
|
|
85
|
+
{ "refreshToken": "…", "clientId": "…", "clientSecret": "…", "provider": "BuilderId" },
|
|
86
|
+
{ "refreshToken": "…", "clientId": "…", "clientSecret": "…", "provider": "Enterprise", "region": "eu-central-1" }
|
|
87
|
+
]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- `provider` is the way it was signed in to: `Google` and `Github` are
|
|
91
|
+
renewed through Kiro's auth service in us-east-1; `BuilderId` and
|
|
92
|
+
`Enterprise` (IAM Identity Center) through AWS's OIDC in `region`
|
|
93
|
+
(blank: us-east-1), with the `clientId` and `clientSecret` registered
|
|
94
|
+
for it. With no `provider`, `authMethod` (`social`, `IdC`) or `auth`
|
|
95
|
+
(`Social`, `IdC`) says it, and else a sign-in with a client is AWS's.
|
|
96
|
+
- Also read: Kiro Account Manager's export (`{ accounts: [{ email,
|
|
97
|
+
provider, credentials: { refreshToken, … } }] }`), kiro2api's
|
|
98
|
+
`KIRO_AUTH_TOKEN` list, the Kiro IDE's `kiro-auth-token.json` (choose its
|
|
99
|
+
`<clientIdHash>.json` with it for a Builder ID or Identity Center
|
|
100
|
+
sign-in), kiro-cli's tokens (`access_token`, `refresh_token`, …; one with
|
|
101
|
+
a `start_url` or `scopes` is AWS's, renewed with the
|
|
102
|
+
`kirocli:odic:device-registration` client chosen with it) and this
|
|
103
|
+
plugin's own entries in `plugin-auth.json` or OpenCode's `auth.json`.
|
|
104
|
+
Other providers' entries there (keys not `kiro` or `kiro#…`) are passed
|
|
105
|
+
over, so none of their tokens is sent to Kiro.
|
|
106
|
+
- Each sign-in is renewed first unless its access token has more than 2
|
|
107
|
+
minutes left (`accessToken` with `expiresAt`), as the Kiro IDE renews
|
|
108
|
+
it. Kiro may give a new refresh token and stop taking the old one, so
|
|
109
|
+
the tool the file came from may have to sign in again.
|
|
110
|
+
- It is named by the email Kiro has for it (the file's `email` when Kiro
|
|
111
|
+
names none) and kept exactly as a browser sign-in of that way: `email
|
|
112
|
+
(Google)`, with its `uid`, region, profile ARN and plan. One with no
|
|
113
|
+
email either is `Kiro account <8 hex>`, from its file's token, so two
|
|
114
|
+
such stay two accounts. Importing an account signed in already replaces
|
|
115
|
+
it.
|
|
116
|
+
- A file's `profileArn` is kept only in the shape Kiro gives one
|
|
117
|
+
(`arn:aws:codewhisperer:<region>:<12 digits>:profile/<id>`); any other
|
|
118
|
+
is dropped and Kiro is asked for the profile. As for every sign-in
|
|
119
|
+
(the IDE's file, kiro-cli's, a saved one), the API's region is taken
|
|
120
|
+
from the profile only when it is one of Kiro's (us-east-1,
|
|
121
|
+
eu-central-1), and a region that isn't an AWS region's shape is refused
|
|
122
|
+
before anything is sent, so no file decides where a token goes.
|
|
123
|
+
- A sign-in Kiro refuses, one of an unknown `provider`, an AWS one without
|
|
124
|
+
its client, and a company's own identity provider (`ExternalIdp`) are
|
|
125
|
+
listed as not imported, with why; the others are imported. One sign-in
|
|
126
|
+
twice is renewed once. At most 100 are taken at a time.
|
|
78
127
|
|
|
79
128
|
A browser sign-in is kept where OpenCode keeps sign-ins (`auth.json`; in
|
|
80
129
|
magpie, `plugin-auth.json`) as an `oauth` entry. The entry holds the
|
package/index.mjs
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
// kiro-cli's or the IDE's own sign-in can be used instead: it is read where
|
|
10
10
|
// they keep it each time, refreshed by kiro-cli first, and a token
|
|
11
11
|
// refreshed here is written back, so they go on with it. A Kiro API key
|
|
12
|
-
// (ksk_…) signs in too
|
|
12
|
+
// (ksk_…) signs in too, and in magpie sign-ins made elsewhere are imported
|
|
13
|
+
// from a file by their refresh token (auth.import).
|
|
13
14
|
//
|
|
14
15
|
// Kiro has no API OpenCode speaks: the models are declared on Anthropic's
|
|
15
16
|
// Messages, and the fetch here sends each request whole to Kiro's own API
|
|
@@ -44,15 +45,30 @@ const WORDS = {
|
|
|
44
45
|
own: { zh: "Kiro CLI 或 Kiro IDE 已登录的账号", "zh-TW": "Kiro CLI 或 Kiro IDE 已登入的帳號", ja: "Kiro CLI または Kiro IDE のサインイン", de: "Anmeldung von Kiro CLI oder Kiro IDE" },
|
|
45
46
|
key: { zh: "Kiro API Key(ksk_…)", "zh-TW": "Kiro API Key(ksk_…)", ja: "Kiro API キー(ksk_…)", de: "Kiro-API-Schlüssel (ksk_…)" },
|
|
46
47
|
}
|
|
48
|
+
// IMPORT_FROM says what magpie's "Import accounts from a file…" takes
|
|
49
|
+
const IMPORT_FROM = {
|
|
50
|
+
en: "Choose a JSON file of Kiro sign-ins, or paste it: one account or a list, each with its refreshToken and the way it signed in (provider: Google, Github, BuilderId or Enterprise; Builder ID and Identity Center also need clientId, clientSecret and region), such as the Kiro IDE's kiro-auth-token.json. Each is renewed with Kiro before it is added, so the tool it came from may have to sign in again.",
|
|
51
|
+
froms: {
|
|
52
|
+
zh: "选择 Kiro 登录凭证的 JSON 文件,或直接粘贴:一个账号或一组账号,每个含 refreshToken 和登录方式(provider:Google、Github、BuilderId 或 Enterprise;Builder ID 和 IAM Identity Center 还需要 clientId、clientSecret 和 region),例如 Kiro IDE 的 kiro-auth-token.json。每个账号先向 Kiro 续期再添加,来源工具可能需要重新登录。",
|
|
53
|
+
"zh-TW": "選擇 Kiro 登入憑證的 JSON 檔,或直接貼上:一個帳號或一組帳號,每個含 refreshToken 和登入方式(provider:Google、Github、BuilderId 或 Enterprise;Builder ID 和 IAM Identity Center 還需要 clientId、clientSecret 和 region),例如 Kiro IDE 的 kiro-auth-token.json。每個帳號先向 Kiro 續期再加入,來源工具可能需要重新登入。",
|
|
54
|
+
ja: "Kiro のサインイン情報の JSON ファイルを選ぶか貼り付けてください。1 アカウントまたはリストで、各アカウントに refreshToken とサインイン方法(provider:Google、Github、BuilderId、Enterprise。Builder ID と IAM Identity Center は clientId、clientSecret、region も必要)を含めます。Kiro IDE の kiro-auth-token.json もそのまま使えます。各アカウントは追加前に Kiro で更新されるため、元のツールは再サインインが必要になることがあります。",
|
|
55
|
+
de: "Wähle eine JSON-Datei mit Kiro-Anmeldungen oder füge sie ein: ein Konto oder eine Liste, jedes mit refreshToken und Anmeldeart (provider: Google, Github, BuilderId oder Enterprise; Builder ID und IAM Identity Center brauchen außerdem clientId, clientSecret und region), etwa die kiro-auth-token.json der Kiro IDE. Jedes Konto wird vor dem Hinzufügen bei Kiro erneuert, daher muss sich das Tool, aus dem es stammt, eventuell neu anmelden.",
|
|
56
|
+
},
|
|
57
|
+
}
|
|
47
58
|
const MESSAGES = "@ai-sdk/anthropic"
|
|
48
59
|
const BASE = "https://runtime.us-east-1.kiro.dev" // what OpenCode is given; the fetch sends each request to the account's region
|
|
49
60
|
|
|
50
61
|
const PORTAL = "https://app.kiro.dev"
|
|
51
62
|
const AUTH_SERVICE = "https://prod.us-east-1.auth.desktop.kiro.dev"
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
63
|
+
// a region read from a file goes into these host names: one that isn't a region's shape is refused
|
|
64
|
+
const inHost = (region) => {
|
|
65
|
+
if (!AWS_REGION.test(String(region))) throw new Error(`${JSON.stringify(region)} isn't an AWS region`)
|
|
66
|
+
return region
|
|
67
|
+
}
|
|
68
|
+
const oidc = (region) => `https://oidc.${inHost(region)}.amazonaws.com`
|
|
69
|
+
const runtime = (region) => `https://runtime.${inHost(region)}.kiro.dev`
|
|
70
|
+
const management = (region) => `https://management.${inHost(region)}.kiro.dev`
|
|
71
|
+
const refreshURL = (region) => `https://prod.${inHost(region)}.auth.desktop.kiro.dev/refreshToken`
|
|
56
72
|
// the ports Kiro's sign-in page sends the browser back to, the IDE's
|
|
57
73
|
const CALLBACK_PORTS = [3128, 4649, 6588, 8008, 9091, 49153, 50153, 51153, 52153, 53153]
|
|
58
74
|
// what the IDE asks AWS for
|
|
@@ -165,7 +181,7 @@ async function readCLI() {
|
|
|
165
181
|
const m = get(key)
|
|
166
182
|
if (!m?.access_token) continue
|
|
167
183
|
const c = { access: m.access_token, refresh: m.refresh_token ?? "", expires: time(m.expires_at), region: m.region || "us-east-1",
|
|
168
|
-
profile: m.profile_arn
|
|
184
|
+
profile: arnOf(m.profile_arn), dbKey: key }
|
|
169
185
|
if (kind === "social") c.method = "social"
|
|
170
186
|
else if (kind === "odic") {
|
|
171
187
|
c.method = "idc"
|
|
@@ -198,7 +214,7 @@ async function readIDE() {
|
|
|
198
214
|
}
|
|
199
215
|
if (!t?.accessToken) return null
|
|
200
216
|
const c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: time(t.expiresAt), region: t.region || "us-east-1",
|
|
201
|
-
profile: t.profileArn
|
|
217
|
+
profile: arnOf(t.profileArn), method: "idc", idePath: path, builderID: isBuilderID(t.provider) }
|
|
202
218
|
if (t.clientIdHash) {
|
|
203
219
|
try {
|
|
204
220
|
const reg = JSON.parse(await readFile(join(ideDir(), t.clientIdHash + ".json"), "utf8"))
|
|
@@ -334,12 +350,17 @@ function authHeaders(a) {
|
|
|
334
350
|
const tokenType = (c) => (c.method === "apikey" ? "API_KEY" : c.method === "external-idp" ? "EXTERNAL_IDP" : "")
|
|
335
351
|
|
|
336
352
|
// region is where the account's API is: the region its profile is in, else
|
|
337
|
-
// the nearer of Kiro's two to where it signed in.
|
|
353
|
+
// the nearer of Kiro's two to where it signed in. It goes into the API's host
|
|
354
|
+
// name, so only Kiro's own regions are taken from a profile.
|
|
338
355
|
function regionOf(profile, signedIn) {
|
|
339
356
|
const parts = String(profile ?? "").split(":")
|
|
340
|
-
if (parts.length > 4 && parts[3]) return parts[3]
|
|
357
|
+
if (parts.length > 4 && CREDIT_REGIONS.includes(parts[3])) return parts[3]
|
|
341
358
|
return String(signedIn ?? "").startsWith("eu-") ? "eu-central-1" : "us-east-1"
|
|
342
359
|
}
|
|
360
|
+
// arnOf is a profile ARN as Kiro gives one, else "": one read from a file
|
|
361
|
+
// that isn't is left for profileOf to ask Kiro for
|
|
362
|
+
const PROFILE_ARN = /^arn:aws:codewhisperer:[a-z]{2}(?:-[a-z]+)+-\d{1,2}:\d{12}:profile\/[A-Za-z0-9_-]+$/
|
|
363
|
+
const arnOf = (s) => (PROFILE_ARN.test(String(s ?? "").trim()) ? String(s).trim() : "")
|
|
343
364
|
|
|
344
365
|
// STATUS_TEXT is Go's http.StatusText for the answers Kiro gives.
|
|
345
366
|
const STATUS_TEXT = { 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 408: "Request Timeout",
|
|
@@ -688,7 +709,7 @@ async function credOf(auth) {
|
|
|
688
709
|
if (auth.source === "kiro") return (await readCLI()) ?? (await readIDE())
|
|
689
710
|
if (!auth.access) return null
|
|
690
711
|
return { access: auth.access, refresh: auth.refresh ?? "", expires: auth.expires ?? 0, method: auth.method || "social",
|
|
691
|
-
region: auth.region || "us-east-1", profile: auth.profileArn
|
|
712
|
+
region: auth.region || "us-east-1", profile: arnOf(auth.profileArn), clientId: auth.clientId ?? "", clientSecret: auth.clientSecret ?? "",
|
|
692
713
|
builderID: isBuilderID(auth.loginProvider), own: true,
|
|
693
714
|
...(auth.profiles && typeof auth.profiles === "object" && !Array.isArray(auth.profiles) ? { profiles: { ...auth.profiles } } : {}) }
|
|
694
715
|
}
|
|
@@ -2978,6 +2999,17 @@ function httpsURL(s) {
|
|
|
2978
2999
|
}
|
|
2979
3000
|
}
|
|
2980
3001
|
|
|
3002
|
+
// signedIn is the account a sign-in keeps, whether made in the browser or
|
|
3003
|
+
// brought in from a file: named by the email Kiro has for it (else the one
|
|
3004
|
+
// given), with the way it was signed in to.
|
|
3005
|
+
function signedIn(c, who, email = who.email, nameless = "Kiro account") {
|
|
3006
|
+
return { type: "success", provider: ID, refresh: c.refresh, access: c.access, expires: c.expires,
|
|
3007
|
+
accountId: email ? nameOf(email, c.loginProvider) : nameless,
|
|
3008
|
+
...(uidOf(c.loginProvider) ? { uid: uidOf(c.loginProvider) } : {}),
|
|
3009
|
+
method: c.method, loginProvider: c.loginProvider, region: c.region, profileArn: who.profile,
|
|
3010
|
+
...(c.clientId ? { clientId: c.clientId, clientSecret: c.clientSecret } : {}), ...(who.plan ? { plan: who.plan } : {}) }
|
|
3011
|
+
}
|
|
3012
|
+
|
|
2981
3013
|
// whoIs names a new sign-in with the email Kiro has for it, and its plan.
|
|
2982
3014
|
async function whoIs(c) {
|
|
2983
3015
|
try {
|
|
@@ -3152,13 +3184,10 @@ async function browserSignIn(inputs = {}) {
|
|
|
3152
3184
|
// still busy while it is named: a second paste is told it is finishing
|
|
3153
3185
|
const who = await whoIs(c)
|
|
3154
3186
|
if (stage === "over") return over()
|
|
3155
|
-
const
|
|
3187
|
+
const done = signedIn(c, who)
|
|
3156
3188
|
stage = "over"
|
|
3157
|
-
finish(
|
|
3158
|
-
|
|
3159
|
-
method: c.method, loginProvider: c.loginProvider, region: c.region, profileArn: who.profile,
|
|
3160
|
-
...(c.clientId ? { clientId: c.clientId, clientSecret: c.clientSecret } : {}), ...(who.plan ? { plan: who.plan } : {}) })
|
|
3161
|
-
html(200, page(true, "You're signed in", `${user} is signed in. You can close this tab.`))
|
|
3189
|
+
finish(done)
|
|
3190
|
+
html(200, page(true, "You're signed in", `${done.accountId} is signed in. You can close this tab.`))
|
|
3162
3191
|
})
|
|
3163
3192
|
|
|
3164
3193
|
const timer = setTimeout(() => {
|
|
@@ -3226,6 +3255,140 @@ async function ownSignIn() {
|
|
|
3226
3255
|
}
|
|
3227
3256
|
}
|
|
3228
3257
|
|
|
3258
|
+
// ---- accounts from a file (magpie's auth.import) ------------------------------------
|
|
3259
|
+
//
|
|
3260
|
+
// A file of sign-ins made elsewhere: Kiro Account Manager's ([{ email,
|
|
3261
|
+
// refreshToken, provider }], or its export with the tokens under
|
|
3262
|
+
// credentials), kiro2api's ({ auth: "Social" | "IdC", refreshToken }), the
|
|
3263
|
+
// Kiro IDE's kiro-auth-token.json (with its <clientIdHash>.json client),
|
|
3264
|
+
// kiro-cli's tokens, or this plugin's own entry. Each is kept as the browser
|
|
3265
|
+
// sign-in keeps it (signedIn), renewed first the way its kind is renewed.
|
|
3266
|
+
|
|
3267
|
+
const IMPORT_MOST = 100 // what magpie keeps of one import
|
|
3268
|
+
const IMPORT_AT_ONCE = 6
|
|
3269
|
+
// the ways a sign-in is named in those files, as loginProvider says them
|
|
3270
|
+
const IMPORT_WAYS = { google: "Google", github: "Github", builderid: "BuilderId", awsbuilderid: "BuilderId", enterprise: "Enterprise",
|
|
3271
|
+
idc: "Enterprise", awsidc: "Enterprise", identitycenter: "Enterprise", iamidentitycenter: "Enterprise", internal: "Internal" }
|
|
3272
|
+
const squash = (s) => s.toLowerCase().replace(/[^a-z]/g, "")
|
|
3273
|
+
const word = (v) => (typeof v === "string" ? v.trim() : "")
|
|
3274
|
+
const pick = (o, ...keys) => keys.map((k) => word(o[k])).find(Boolean) ?? ""
|
|
3275
|
+
// an expiry as ISO time, local "YYYY/MM/DD HH:MM:SS", Unix seconds or ms
|
|
3276
|
+
const whenOf = (v) => (typeof v === "number" ? (v <= 0 ? 0 : v < 1e11 ? v * 1000 : v) : time(v))
|
|
3277
|
+
const emailIn = (s) => /^([^\s()]+@[^\s()]+)(?: \([^()]*\))?$/.exec(s)?.[1] ?? ""
|
|
3278
|
+
const EXTERNAL_IDP = "A company's own identity provider can't be signed in to here yet; sign in with `kiro-cli login`, and use Kiro CLI's sign-in"
|
|
3279
|
+
const NO_CLIENT = "an AWS Builder ID or IAM Identity Center sign-in is renewed with the client it was registered with: add its clientId and clientSecret, or choose the Kiro IDE's registration file (~/.aws/sso/cache/<clientIdHash>.json) with it"
|
|
3280
|
+
const REFUSED = "Kiro refused its refresh token: it has expired, been revoked, or been replaced by a newer one where it came from; sign in there again and export it once more"
|
|
3281
|
+
const REFUSED_IDC = "AWS refused its refresh token or its client: one has expired or been revoked; sign in again where it came from and export it once more"
|
|
3282
|
+
|
|
3283
|
+
// entryOf reads one sign-in of a file: { c, email } as refresh() takes it,
|
|
3284
|
+
// or { error, email }.
|
|
3285
|
+
function entryOf(o) {
|
|
3286
|
+
const said = pick(o, "provider", "loginProvider", "idp", "loginMethod", "login_option")
|
|
3287
|
+
const how = squash(pick(o, "authMethod", "auth_method", "auth", "method"))
|
|
3288
|
+
const email = emailIn(pick(o, "email", "accountId"))
|
|
3289
|
+
if (squash(said) === "externalidp" || how === "externalidp") return { error: EXTERNAL_IDP, email }
|
|
3290
|
+
let loginProvider = IMPORT_WAYS[squash(said)] ?? ""
|
|
3291
|
+
if (said && !loginProvider) return { error: `Kiro has no sign-in with ${JSON.stringify(said)}; provider is Google, Github, BuilderId or Enterprise`, email }
|
|
3292
|
+
const clientId = pick(o, "clientId", "client_id")
|
|
3293
|
+
const clientSecret = pick(o, "clientSecret", "client_secret")
|
|
3294
|
+
const start = pick(o, "startUrl", "start_url", "issuerUrl", "issuer_url")
|
|
3295
|
+
// kiro-cli's AWS sign-in names no provider and keeps its client apart, but has a start URL or scopes
|
|
3296
|
+
const aws = how === "idc" || how === "builderid" || !!clientId || !!start || Array.isArray(o.scopes)
|
|
3297
|
+
const social = loginProvider ? loginProvider === "Google" || loginProvider === "Github" : how === "social" || !aws
|
|
3298
|
+
if (!social && !loginProvider) {
|
|
3299
|
+
loginProvider = how === "builderid" || start === BUILDER_ID_START ? "BuilderId" : start ? "Enterprise" : ""
|
|
3300
|
+
}
|
|
3301
|
+
// Kiro's auth service renews every Google and GitHub sign-in in us-east-1
|
|
3302
|
+
const region = social ? "us-east-1" : pick(o, "idcRegion", "idc_region", "authRegion", "region") || "us-east-1"
|
|
3303
|
+
if (!AWS_REGION.test(region)) return { error: `${JSON.stringify(region)} isn't an AWS region`, email }
|
|
3304
|
+
return { email, c: { access: pick(o, "accessToken", "access_token", "access"), refresh: pick(o, "refreshToken", "refresh_token", "refresh"),
|
|
3305
|
+
expires: whenOf(o.expiresAt ?? o.expires_at ?? o.expires), method: social ? "social" : "idc", loginProvider, region,
|
|
3306
|
+
profile: arnOf(pick(o, "profileArn", "profile_arn")), clientId, clientSecret, builderID: isBuilderID(loginProvider) } }
|
|
3307
|
+
}
|
|
3308
|
+
|
|
3309
|
+
// signInsIn are the sign-ins a file's text holds; the clients registered
|
|
3310
|
+
// with AWS it holds (the Kiro IDE's <clientIdHash>.json) go into regs.
|
|
3311
|
+
function signInsIn(text, regs) {
|
|
3312
|
+
let v
|
|
3313
|
+
try {
|
|
3314
|
+
v = JSON.parse(text)
|
|
3315
|
+
} catch {
|
|
3316
|
+
return [{ error: "not JSON" }]
|
|
3317
|
+
}
|
|
3318
|
+
const out = []
|
|
3319
|
+
const seen = new Set()
|
|
3320
|
+
const walk = (o, depth) => {
|
|
3321
|
+
if (!o || typeof o !== "object" || depth > 4 || seen.has(o)) return
|
|
3322
|
+
seen.add(o)
|
|
3323
|
+
if (Array.isArray(o)) return o.forEach((x) => walk(x, depth + 1))
|
|
3324
|
+
const t = o.credentials && typeof o.credentials === "object" && !Array.isArray(o.credentials) ? { ...o, ...o.credentials } : o
|
|
3325
|
+
if (pick(t, "refreshToken", "refresh_token", "refresh", "accessToken", "access_token", "access")) return out.push(entryOf(t))
|
|
3326
|
+
const clientId = pick(t, "clientId", "client_id")
|
|
3327
|
+
const clientSecret = pick(t, "clientSecret", "client_secret")
|
|
3328
|
+
if (clientId && clientSecret) return regs.push({ clientId, clientSecret })
|
|
3329
|
+
// a sign-in store (magpie's plugin-auth.json, OpenCode's auth.json) keeps other providers' too: only Kiro's are taken
|
|
3330
|
+
for (const [k, x] of Object.entries(o)) if (!(x && (x.type === "oauth" || x.type === "api") && !/^kiro(#|$)/i.test(k))) walk(x, depth + 1)
|
|
3331
|
+
}
|
|
3332
|
+
walk(v, 0)
|
|
3333
|
+
return out.length || regs.length ? out : [{ error: "no Kiro sign-in in it (no refreshToken)" }]
|
|
3334
|
+
}
|
|
3335
|
+
|
|
3336
|
+
// importOne checks a sign-in with Kiro as signing in does: renewed unless
|
|
3337
|
+
// its access token still has time (as the Kiro IDE renews it), then named
|
|
3338
|
+
// by the email Kiro has for it.
|
|
3339
|
+
async function importOne(e) {
|
|
3340
|
+
const failed = (error) => ({ type: "failed", error, ...(e.email ? { accountId: nameOf(e.email, e.c?.loginProvider) } : {}) })
|
|
3341
|
+
if (e.error) return failed(e.error)
|
|
3342
|
+
let c = e.c
|
|
3343
|
+
if (c.method === "idc" && !(c.clientId && c.clientSecret)) return failed(NO_CLIENT)
|
|
3344
|
+
try {
|
|
3345
|
+
let renewed = false
|
|
3346
|
+
if (!c.access || !(c.expires - Date.now() > EARLY_MS)) {
|
|
3347
|
+
if (!c.refresh) return failed("its access token has ended, and it has no refreshToken to renew it with")
|
|
3348
|
+
c = await refresh(c)
|
|
3349
|
+
renewed = true
|
|
3350
|
+
}
|
|
3351
|
+
let who = await whoIs(c)
|
|
3352
|
+
// an access token Kiro didn't answer to: renewed, and asked again
|
|
3353
|
+
if (!who.email && !renewed && c.refresh) {
|
|
3354
|
+
c = await refresh(c)
|
|
3355
|
+
who = await whoIs(c)
|
|
3356
|
+
}
|
|
3357
|
+
// one Kiro names no one is still its own account: called by its file's token, so two such aren't kept as one
|
|
3358
|
+
const tag = createHash("sha256").update(e.c.refresh || e.c.access).digest("hex").slice(0, 8)
|
|
3359
|
+
return signedIn(c, who, who.email || e.email, `Kiro account ${tag}`)
|
|
3360
|
+
} catch (err) {
|
|
3361
|
+
return failed(err?.gone ? (c.method === "idc" ? REFUSED_IDC : REFUSED) : err?.message ?? String(err))
|
|
3362
|
+
}
|
|
3363
|
+
}
|
|
3364
|
+
|
|
3365
|
+
async function importSignIns(texts, signal) {
|
|
3366
|
+
const regs = []
|
|
3367
|
+
const found = texts.flatMap((t) => signInsIn(t, regs))
|
|
3368
|
+
if (regs.length === 1) for (const e of found) if (e.c?.method === "idc" && !e.c.clientId) Object.assign(e.c, regs[0])
|
|
3369
|
+
// one sign-in twice: renewing it twice would spend its refresh token
|
|
3370
|
+
const tokens = new Set()
|
|
3371
|
+
const list = found.filter((e) => {
|
|
3372
|
+
const k = e.c?.refresh || e.c?.access
|
|
3373
|
+
if (!k) return true
|
|
3374
|
+
if (tokens.has(k)) return false
|
|
3375
|
+
tokens.add(k)
|
|
3376
|
+
return true
|
|
3377
|
+
})
|
|
3378
|
+
const left = list.length > IMPORT_MOST ? list.splice(IMPORT_MOST - 1).length : 0
|
|
3379
|
+
const results = []
|
|
3380
|
+
let next = 0
|
|
3381
|
+
const work = async () => {
|
|
3382
|
+
while (next < list.length && !signal?.aborted) {
|
|
3383
|
+
const i = next++
|
|
3384
|
+
results[i] = await importOne(list[i])
|
|
3385
|
+
}
|
|
3386
|
+
}
|
|
3387
|
+
await Promise.all(Array.from({ length: Math.min(IMPORT_AT_ONCE, list.length) }, work))
|
|
3388
|
+
if (left) results.push({ type: "failed", error: `${left} more sign-ins weren't imported: at most ${IMPORT_MOST} are taken at a time; import the rest on their own` })
|
|
3389
|
+
return results.filter(Boolean)
|
|
3390
|
+
}
|
|
3391
|
+
|
|
3229
3392
|
// ---- the plugin ------------------------------------------------------------------------
|
|
3230
3393
|
|
|
3231
3394
|
async function bodyOf(input, init) {
|
|
@@ -3306,6 +3469,10 @@ export async function KiroAuthPlugin({ client, magpie } = {}) {
|
|
|
3306
3469
|
return { error: e.message, signIn: e.gone ? "expired" : "kept" }
|
|
3307
3470
|
}
|
|
3308
3471
|
},
|
|
3472
|
+
// magpie's "Import accounts from a file…" (OpenCode ignores it)
|
|
3473
|
+
importFrom: IMPORT_FROM.en,
|
|
3474
|
+
importFroms: IMPORT_FROM.froms,
|
|
3475
|
+
import: (texts, { signal } = {}) => importSignIns(Array.isArray(texts) ? texts : [texts], signal),
|
|
3309
3476
|
// labels, messages and hints are magpie's own fields: the same words
|
|
3310
3477
|
// by the window's language (OpenCode shows the English)
|
|
3311
3478
|
// magpie's switches for the provider (#2038): shown in its editor, read
|