@inworld/cli 2.1.0 → 2.2.0
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/dist/cli/inworld-mcp.js +206 -6
- package/dist/index.js +210 -7
- package/package.json +1 -1
package/dist/cli/inworld-mcp.js
CHANGED
|
@@ -62525,7 +62525,7 @@ var init_package = __esm({
|
|
|
62525
62525
|
"package.json"() {
|
|
62526
62526
|
package_default = {
|
|
62527
62527
|
name: "@inworld/cli",
|
|
62528
|
-
version: "2.
|
|
62528
|
+
version: "2.2.0",
|
|
62529
62529
|
type: "module",
|
|
62530
62530
|
main: "dist/index.js",
|
|
62531
62531
|
bin: {
|
|
@@ -62821,14 +62821,14 @@ async function startLoopbackReceiver(options) {
|
|
|
62821
62821
|
});
|
|
62822
62822
|
let claimed = false;
|
|
62823
62823
|
let ended = false;
|
|
62824
|
-
let
|
|
62824
|
+
let failure2;
|
|
62825
62825
|
const redemption = new AbortController();
|
|
62826
62826
|
const end = (outcome) => {
|
|
62827
62827
|
if (ended) return false;
|
|
62828
62828
|
ended = true;
|
|
62829
62829
|
claimed = true;
|
|
62830
62830
|
if (outcome instanceof Error) {
|
|
62831
|
-
|
|
62831
|
+
failure2 = outcome;
|
|
62832
62832
|
redemption.abort();
|
|
62833
62833
|
settle.reject(outcome);
|
|
62834
62834
|
} else {
|
|
@@ -62845,11 +62845,11 @@ async function startLoopbackReceiver(options) {
|
|
|
62845
62845
|
// connection is reset, and `answer` finds it gone.
|
|
62846
62846
|
(tokens) => {
|
|
62847
62847
|
end(tokens);
|
|
62848
|
-
answer(signInResultUrl(options.pageUrl,
|
|
62848
|
+
answer(signInResultUrl(options.pageUrl, failure2?.message));
|
|
62849
62849
|
},
|
|
62850
62850
|
(error2) => {
|
|
62851
62851
|
end(error2);
|
|
62852
|
-
answer(signInResultUrl(options.pageUrl,
|
|
62852
|
+
answer(signInResultUrl(options.pageUrl, failure2?.message));
|
|
62853
62853
|
}
|
|
62854
62854
|
);
|
|
62855
62855
|
};
|
|
@@ -65281,6 +65281,203 @@ var init_schemas4 = __esm({
|
|
|
65281
65281
|
}
|
|
65282
65282
|
});
|
|
65283
65283
|
|
|
65284
|
+
// src/mcp/auth/second_factor.ts
|
|
65285
|
+
function askHidden(prompt, input = process.stdin, output = process.stdout) {
|
|
65286
|
+
return new Promise((resolve2, reject) => {
|
|
65287
|
+
const raw = Boolean(input.isTTY && input.setRawMode);
|
|
65288
|
+
let typed = "";
|
|
65289
|
+
const finish = (settle) => {
|
|
65290
|
+
input.removeListener("data", onData);
|
|
65291
|
+
input.removeListener("end", onEnd);
|
|
65292
|
+
if (raw) input.setRawMode?.(false);
|
|
65293
|
+
input.pause();
|
|
65294
|
+
output.write("\n");
|
|
65295
|
+
settle();
|
|
65296
|
+
};
|
|
65297
|
+
const onData = (chunk) => {
|
|
65298
|
+
for (const ch of chunk.toString()) {
|
|
65299
|
+
if (ch === "\r" || ch === "\n") return finish(() => resolve2(typed));
|
|
65300
|
+
if (ch === "") {
|
|
65301
|
+
return finish(() => reject(new Error("Sign-in cancelled.")));
|
|
65302
|
+
}
|
|
65303
|
+
if (ch === "" && !typed) {
|
|
65304
|
+
return finish(() => reject(new Error(INPUT_ENDED_MESSAGE)));
|
|
65305
|
+
}
|
|
65306
|
+
if (ch === "\x7F" || ch === "\b") typed = typed.slice(0, -1);
|
|
65307
|
+
else typed += ch;
|
|
65308
|
+
}
|
|
65309
|
+
};
|
|
65310
|
+
const onEnd = () => finish(() => reject(new Error(INPUT_ENDED_MESSAGE)));
|
|
65311
|
+
if (raw) input.setRawMode?.(true);
|
|
65312
|
+
output.write(prompt);
|
|
65313
|
+
input.on("data", onData);
|
|
65314
|
+
input.on("end", onEnd);
|
|
65315
|
+
input.resume();
|
|
65316
|
+
});
|
|
65317
|
+
}
|
|
65318
|
+
function maskEmail(address) {
|
|
65319
|
+
const at = address.lastIndexOf("@");
|
|
65320
|
+
if (at < 1) return "your email address";
|
|
65321
|
+
return `${address[0]}***${address.slice(at)}`;
|
|
65322
|
+
}
|
|
65323
|
+
async function gotrueJson(url, init) {
|
|
65324
|
+
const res = await fetch(url, {
|
|
65325
|
+
method: init.method,
|
|
65326
|
+
headers: {
|
|
65327
|
+
Authorization: `Bearer ${init.accessToken}`,
|
|
65328
|
+
"Content-Type": "application/json",
|
|
65329
|
+
[CLIENT_INFO_HEADER]: CLIENT_INFO
|
|
65330
|
+
},
|
|
65331
|
+
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
65332
|
+
});
|
|
65333
|
+
const text = await res.text();
|
|
65334
|
+
let data = {};
|
|
65335
|
+
try {
|
|
65336
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
65337
|
+
if (parsed && typeof parsed === "object") {
|
|
65338
|
+
data = parsed;
|
|
65339
|
+
}
|
|
65340
|
+
} catch {
|
|
65341
|
+
}
|
|
65342
|
+
return { status: res.status, data };
|
|
65343
|
+
}
|
|
65344
|
+
function failure(step, status, data) {
|
|
65345
|
+
return new Error(
|
|
65346
|
+
`${step} failed: ${gotrueErrorMessage(data, `HTTP ${status}`)}`
|
|
65347
|
+
);
|
|
65348
|
+
}
|
|
65349
|
+
function signedInIndependently(accessToken) {
|
|
65350
|
+
const amr = decodeJwtPayload(accessToken)?.amr;
|
|
65351
|
+
return Array.isArray(amr) && amr.some(
|
|
65352
|
+
(entry) => typeof entry === "object" && entry !== null && INDEPENDENT_FIRST_FACTORS.has(
|
|
65353
|
+
String(entry.method)
|
|
65354
|
+
)
|
|
65355
|
+
);
|
|
65356
|
+
}
|
|
65357
|
+
function chooseFactor(factors, accessToken) {
|
|
65358
|
+
const verified = factors.filter((f) => f.status === "verified");
|
|
65359
|
+
if (verified.length === 0) return null;
|
|
65360
|
+
const owed = verified.filter((f) => f.factor_type !== "recovery_code");
|
|
65361
|
+
const totp = owed.find((f) => f.factor_type === "totp");
|
|
65362
|
+
if (totp) return totp;
|
|
65363
|
+
const email2 = owed.find((f) => f.factor_type === "email");
|
|
65364
|
+
const onlyEmail = verified.every((f) => f.factor_type === "email");
|
|
65365
|
+
if (email2 && (onlyEmail || signedInIndependently(accessToken))) return email2;
|
|
65366
|
+
throw new Error(UNSUPPORTED_MESSAGE);
|
|
65367
|
+
}
|
|
65368
|
+
async function stepUpSecondFactor(source, tokens, io) {
|
|
65369
|
+
if (decodeJwtPayload(tokens.idToken)?.aal !== "aal1") return tokens;
|
|
65370
|
+
const accessToken = tokens.idToken;
|
|
65371
|
+
const user = await gotrueJson(`${source.authUrl}/user`, {
|
|
65372
|
+
method: "GET",
|
|
65373
|
+
accessToken
|
|
65374
|
+
});
|
|
65375
|
+
if (!succeeded(user.status)) {
|
|
65376
|
+
throw failure(
|
|
65377
|
+
"Reading the account's second factors",
|
|
65378
|
+
user.status,
|
|
65379
|
+
user.data
|
|
65380
|
+
);
|
|
65381
|
+
}
|
|
65382
|
+
const factor = chooseFactor(
|
|
65383
|
+
Array.isArray(user.data.factors) ? user.data.factors : [],
|
|
65384
|
+
accessToken
|
|
65385
|
+
);
|
|
65386
|
+
if (!factor) return tokens;
|
|
65387
|
+
const interactive = io.interactive ?? (Boolean(process.stdin.isTTY) && Boolean(process.stdout.isTTY));
|
|
65388
|
+
if (!interactive) throw new Error(NO_TERMINAL_MESSAGE);
|
|
65389
|
+
const ask = io.ask ?? ((prompt2) => askHidden(prompt2));
|
|
65390
|
+
const address = typeof user.data.email === "string" ? user.data.email : tokens.email;
|
|
65391
|
+
const prompt = factor.factor_type === "email" ? `Enter the code we emailed to ${address ? maskEmail(address) : "your email address"}: ` : "Enter the 6-digit code from your authenticator app: ";
|
|
65392
|
+
const factorUrl = `${source.authUrl}/factors/${encodeURIComponent(factor.id)}`;
|
|
65393
|
+
io.print("This account has a second factor.");
|
|
65394
|
+
let wrongCodes = 0;
|
|
65395
|
+
for (let challenges = 0; challenges < MAX_CHALLENGES; challenges++) {
|
|
65396
|
+
const challenge = await gotrueJson(`${factorUrl}/challenge`, {
|
|
65397
|
+
method: "POST",
|
|
65398
|
+
accessToken,
|
|
65399
|
+
body: {}
|
|
65400
|
+
});
|
|
65401
|
+
const challengeId = challenge.data.id;
|
|
65402
|
+
if (!succeeded(challenge.status) || typeof challengeId !== "string") {
|
|
65403
|
+
throw failure(
|
|
65404
|
+
"Starting the second-factor challenge",
|
|
65405
|
+
challenge.status,
|
|
65406
|
+
challenge.data
|
|
65407
|
+
);
|
|
65408
|
+
}
|
|
65409
|
+
if (factor.factor_type === "email") io.print("We emailed you a code.");
|
|
65410
|
+
for (; ; ) {
|
|
65411
|
+
const code = (await ask(prompt)).replace(/\s+/g, "");
|
|
65412
|
+
if (!/^\d+$/.test(code)) {
|
|
65413
|
+
io.print("The code is digits only. Try again.");
|
|
65414
|
+
continue;
|
|
65415
|
+
}
|
|
65416
|
+
const verified = await gotrueJson(`${factorUrl}/verify`, {
|
|
65417
|
+
method: "POST",
|
|
65418
|
+
accessToken,
|
|
65419
|
+
body: { challenge_id: challengeId, code }
|
|
65420
|
+
});
|
|
65421
|
+
if (succeeded(verified.status)) {
|
|
65422
|
+
const { access_token, refresh_token } = verified.data;
|
|
65423
|
+
if (typeof access_token !== "string" || typeof refresh_token !== "string") {
|
|
65424
|
+
throw new Error(
|
|
65425
|
+
"Second-factor verification: response missing access or refresh token"
|
|
65426
|
+
);
|
|
65427
|
+
}
|
|
65428
|
+
const verifiedUser = verified.data.user;
|
|
65429
|
+
return {
|
|
65430
|
+
idToken: access_token,
|
|
65431
|
+
refreshToken: refresh_token,
|
|
65432
|
+
email: verifiedUser?.email ?? tokens.email
|
|
65433
|
+
};
|
|
65434
|
+
}
|
|
65435
|
+
if (verified.data.error_code === "mfa_challenge_expired") {
|
|
65436
|
+
io.print("That code expired. Sending a new one.");
|
|
65437
|
+
break;
|
|
65438
|
+
}
|
|
65439
|
+
if (isWrongCode(verified.status, verified.data)) {
|
|
65440
|
+
if (++wrongCodes >= MAX_CODE_ATTEMPTS) {
|
|
65441
|
+
throw new Error(
|
|
65442
|
+
"Too many incorrect codes. Run `inworld login` to try again."
|
|
65443
|
+
);
|
|
65444
|
+
}
|
|
65445
|
+
io.print("That code is not correct. Try again.");
|
|
65446
|
+
continue;
|
|
65447
|
+
}
|
|
65448
|
+
throw failure(
|
|
65449
|
+
"Second-factor verification",
|
|
65450
|
+
verified.status,
|
|
65451
|
+
verified.data
|
|
65452
|
+
);
|
|
65453
|
+
}
|
|
65454
|
+
}
|
|
65455
|
+
throw new Error(
|
|
65456
|
+
"The second-factor code expired too many times. Run `inworld login` to try again."
|
|
65457
|
+
);
|
|
65458
|
+
}
|
|
65459
|
+
var MAX_CODE_ATTEMPTS, MAX_CHALLENGES, NO_TERMINAL_MESSAGE, UNSUPPORTED_MESSAGE, INPUT_ENDED_MESSAGE, succeeded, isWrongCode, INDEPENDENT_FIRST_FACTORS;
|
|
65460
|
+
var init_second_factor = __esm({
|
|
65461
|
+
"src/mcp/auth/second_factor.ts"() {
|
|
65462
|
+
"use strict";
|
|
65463
|
+
init_client_info();
|
|
65464
|
+
init_identity();
|
|
65465
|
+
MAX_CODE_ATTEMPTS = 5;
|
|
65466
|
+
MAX_CHALLENGES = 5;
|
|
65467
|
+
NO_TERMINAL_MESSAGE = "This account needs a second-factor code; run `inworld login` in an interactive terminal.";
|
|
65468
|
+
UNSUPPORTED_MESSAGE = "This account's second factor cannot be completed in the CLI after this sign-in. Set up an authenticator app in your Inworld account settings and run `inworld login` again, or contact support.";
|
|
65469
|
+
INPUT_ENDED_MESSAGE = "Input ended before a code was entered.";
|
|
65470
|
+
succeeded = (status) => status >= 200 && status < 300;
|
|
65471
|
+
isWrongCode = (status, data) => data.error_code === "mfa_verification_failed" || status === 422 && !data.error_code;
|
|
65472
|
+
INDEPENDENT_FIRST_FACTORS = /* @__PURE__ */ new Set([
|
|
65473
|
+
"password",
|
|
65474
|
+
"sso/saml",
|
|
65475
|
+
"passkey",
|
|
65476
|
+
"web3"
|
|
65477
|
+
]);
|
|
65478
|
+
}
|
|
65479
|
+
});
|
|
65480
|
+
|
|
65284
65481
|
// src/mcp/auth/browser.ts
|
|
65285
65482
|
import { execFile } from "node:child_process";
|
|
65286
65483
|
import { promisify as promisify2 } from "node:util";
|
|
@@ -65392,7 +65589,9 @@ async function runLogin() {
|
|
|
65392
65589
|
print: (line) => console.error(line),
|
|
65393
65590
|
output: process.stdout
|
|
65394
65591
|
});
|
|
65395
|
-
const tokens = await signIn.done
|
|
65592
|
+
const tokens = await stepUpSecondFactor(source, await signIn.done, {
|
|
65593
|
+
print: (line) => console.error(line)
|
|
65594
|
+
});
|
|
65396
65595
|
console.error();
|
|
65397
65596
|
console.error("Exchanging for Inworld session...");
|
|
65398
65597
|
const exchanged = await exchangeIdentityToken(tokens.idToken);
|
|
@@ -65433,6 +65632,7 @@ var init_login = __esm({
|
|
|
65433
65632
|
init_exchange();
|
|
65434
65633
|
init_identity();
|
|
65435
65634
|
init_paths();
|
|
65635
|
+
init_second_factor();
|
|
65436
65636
|
init_session();
|
|
65437
65637
|
init_sign_in_prompt();
|
|
65438
65638
|
}
|
package/dist/index.js
CHANGED
|
@@ -19724,7 +19724,7 @@ var init_package = __esm({
|
|
|
19724
19724
|
"package.json"() {
|
|
19725
19725
|
package_default = {
|
|
19726
19726
|
name: "@inworld/cli",
|
|
19727
|
-
version: "2.
|
|
19727
|
+
version: "2.2.0",
|
|
19728
19728
|
type: "module",
|
|
19729
19729
|
main: "dist/index.js",
|
|
19730
19730
|
bin: {
|
|
@@ -20082,14 +20082,14 @@ async function startLoopbackReceiver(options) {
|
|
|
20082
20082
|
});
|
|
20083
20083
|
let claimed = false;
|
|
20084
20084
|
let ended = false;
|
|
20085
|
-
let
|
|
20085
|
+
let failure2;
|
|
20086
20086
|
const redemption = new AbortController();
|
|
20087
20087
|
const end = (outcome) => {
|
|
20088
20088
|
if (ended) return false;
|
|
20089
20089
|
ended = true;
|
|
20090
20090
|
claimed = true;
|
|
20091
20091
|
if (outcome instanceof Error) {
|
|
20092
|
-
|
|
20092
|
+
failure2 = outcome;
|
|
20093
20093
|
redemption.abort();
|
|
20094
20094
|
settle.reject(outcome);
|
|
20095
20095
|
} else {
|
|
@@ -20106,11 +20106,11 @@ async function startLoopbackReceiver(options) {
|
|
|
20106
20106
|
// connection is reset, and `answer` finds it gone.
|
|
20107
20107
|
(tokens) => {
|
|
20108
20108
|
end(tokens);
|
|
20109
|
-
answer(signInResultUrl(options.pageUrl,
|
|
20109
|
+
answer(signInResultUrl(options.pageUrl, failure2?.message));
|
|
20110
20110
|
},
|
|
20111
20111
|
(error2) => {
|
|
20112
20112
|
end(error2);
|
|
20113
|
-
answer(signInResultUrl(options.pageUrl,
|
|
20113
|
+
answer(signInResultUrl(options.pageUrl, failure2?.message));
|
|
20114
20114
|
}
|
|
20115
20115
|
);
|
|
20116
20116
|
};
|
|
@@ -23949,6 +23949,203 @@ var require_cross_spawn = __commonJS({
|
|
|
23949
23949
|
}
|
|
23950
23950
|
});
|
|
23951
23951
|
|
|
23952
|
+
// src/mcp/auth/second_factor.ts
|
|
23953
|
+
function askHidden(prompt, input = process.stdin, output = process.stdout) {
|
|
23954
|
+
return new Promise((resolve6, reject) => {
|
|
23955
|
+
const raw = Boolean(input.isTTY && input.setRawMode);
|
|
23956
|
+
let typed = "";
|
|
23957
|
+
const finish = (settle) => {
|
|
23958
|
+
input.removeListener("data", onData);
|
|
23959
|
+
input.removeListener("end", onEnd);
|
|
23960
|
+
if (raw) input.setRawMode?.(false);
|
|
23961
|
+
input.pause();
|
|
23962
|
+
output.write("\n");
|
|
23963
|
+
settle();
|
|
23964
|
+
};
|
|
23965
|
+
const onData = (chunk) => {
|
|
23966
|
+
for (const ch of chunk.toString()) {
|
|
23967
|
+
if (ch === "\r" || ch === "\n") return finish(() => resolve6(typed));
|
|
23968
|
+
if (ch === "") {
|
|
23969
|
+
return finish(() => reject(new Error("Sign-in cancelled.")));
|
|
23970
|
+
}
|
|
23971
|
+
if (ch === "" && !typed) {
|
|
23972
|
+
return finish(() => reject(new Error(INPUT_ENDED_MESSAGE)));
|
|
23973
|
+
}
|
|
23974
|
+
if (ch === "\x7F" || ch === "\b") typed = typed.slice(0, -1);
|
|
23975
|
+
else typed += ch;
|
|
23976
|
+
}
|
|
23977
|
+
};
|
|
23978
|
+
const onEnd = () => finish(() => reject(new Error(INPUT_ENDED_MESSAGE)));
|
|
23979
|
+
if (raw) input.setRawMode?.(true);
|
|
23980
|
+
output.write(prompt);
|
|
23981
|
+
input.on("data", onData);
|
|
23982
|
+
input.on("end", onEnd);
|
|
23983
|
+
input.resume();
|
|
23984
|
+
});
|
|
23985
|
+
}
|
|
23986
|
+
function maskEmail(address) {
|
|
23987
|
+
const at = address.lastIndexOf("@");
|
|
23988
|
+
if (at < 1) return "your email address";
|
|
23989
|
+
return `${address[0]}***${address.slice(at)}`;
|
|
23990
|
+
}
|
|
23991
|
+
async function gotrueJson(url, init) {
|
|
23992
|
+
const res = await fetch(url, {
|
|
23993
|
+
method: init.method,
|
|
23994
|
+
headers: {
|
|
23995
|
+
Authorization: `Bearer ${init.accessToken}`,
|
|
23996
|
+
"Content-Type": "application/json",
|
|
23997
|
+
[CLIENT_INFO_HEADER]: CLIENT_INFO
|
|
23998
|
+
},
|
|
23999
|
+
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
24000
|
+
});
|
|
24001
|
+
const text = await res.text();
|
|
24002
|
+
let data = {};
|
|
24003
|
+
try {
|
|
24004
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
24005
|
+
if (parsed && typeof parsed === "object") {
|
|
24006
|
+
data = parsed;
|
|
24007
|
+
}
|
|
24008
|
+
} catch {
|
|
24009
|
+
}
|
|
24010
|
+
return { status: res.status, data };
|
|
24011
|
+
}
|
|
24012
|
+
function failure(step, status, data) {
|
|
24013
|
+
return new Error(
|
|
24014
|
+
`${step} failed: ${gotrueErrorMessage(data, `HTTP ${status}`)}`
|
|
24015
|
+
);
|
|
24016
|
+
}
|
|
24017
|
+
function signedInIndependently(accessToken) {
|
|
24018
|
+
const amr = decodeJwtPayload(accessToken)?.amr;
|
|
24019
|
+
return Array.isArray(amr) && amr.some(
|
|
24020
|
+
(entry) => typeof entry === "object" && entry !== null && INDEPENDENT_FIRST_FACTORS.has(
|
|
24021
|
+
String(entry.method)
|
|
24022
|
+
)
|
|
24023
|
+
);
|
|
24024
|
+
}
|
|
24025
|
+
function chooseFactor(factors, accessToken) {
|
|
24026
|
+
const verified = factors.filter((f) => f.status === "verified");
|
|
24027
|
+
if (verified.length === 0) return null;
|
|
24028
|
+
const owed = verified.filter((f) => f.factor_type !== "recovery_code");
|
|
24029
|
+
const totp = owed.find((f) => f.factor_type === "totp");
|
|
24030
|
+
if (totp) return totp;
|
|
24031
|
+
const email2 = owed.find((f) => f.factor_type === "email");
|
|
24032
|
+
const onlyEmail = verified.every((f) => f.factor_type === "email");
|
|
24033
|
+
if (email2 && (onlyEmail || signedInIndependently(accessToken))) return email2;
|
|
24034
|
+
throw new Error(UNSUPPORTED_MESSAGE);
|
|
24035
|
+
}
|
|
24036
|
+
async function stepUpSecondFactor(source, tokens, io) {
|
|
24037
|
+
if (decodeJwtPayload(tokens.idToken)?.aal !== "aal1") return tokens;
|
|
24038
|
+
const accessToken = tokens.idToken;
|
|
24039
|
+
const user = await gotrueJson(`${source.authUrl}/user`, {
|
|
24040
|
+
method: "GET",
|
|
24041
|
+
accessToken
|
|
24042
|
+
});
|
|
24043
|
+
if (!succeeded(user.status)) {
|
|
24044
|
+
throw failure(
|
|
24045
|
+
"Reading the account's second factors",
|
|
24046
|
+
user.status,
|
|
24047
|
+
user.data
|
|
24048
|
+
);
|
|
24049
|
+
}
|
|
24050
|
+
const factor = chooseFactor(
|
|
24051
|
+
Array.isArray(user.data.factors) ? user.data.factors : [],
|
|
24052
|
+
accessToken
|
|
24053
|
+
);
|
|
24054
|
+
if (!factor) return tokens;
|
|
24055
|
+
const interactive = io.interactive ?? (Boolean(process.stdin.isTTY) && Boolean(process.stdout.isTTY));
|
|
24056
|
+
if (!interactive) throw new Error(NO_TERMINAL_MESSAGE);
|
|
24057
|
+
const ask = io.ask ?? ((prompt2) => askHidden(prompt2));
|
|
24058
|
+
const address = typeof user.data.email === "string" ? user.data.email : tokens.email;
|
|
24059
|
+
const prompt = factor.factor_type === "email" ? `Enter the code we emailed to ${address ? maskEmail(address) : "your email address"}: ` : "Enter the 6-digit code from your authenticator app: ";
|
|
24060
|
+
const factorUrl = `${source.authUrl}/factors/${encodeURIComponent(factor.id)}`;
|
|
24061
|
+
io.print("This account has a second factor.");
|
|
24062
|
+
let wrongCodes = 0;
|
|
24063
|
+
for (let challenges = 0; challenges < MAX_CHALLENGES; challenges++) {
|
|
24064
|
+
const challenge = await gotrueJson(`${factorUrl}/challenge`, {
|
|
24065
|
+
method: "POST",
|
|
24066
|
+
accessToken,
|
|
24067
|
+
body: {}
|
|
24068
|
+
});
|
|
24069
|
+
const challengeId = challenge.data.id;
|
|
24070
|
+
if (!succeeded(challenge.status) || typeof challengeId !== "string") {
|
|
24071
|
+
throw failure(
|
|
24072
|
+
"Starting the second-factor challenge",
|
|
24073
|
+
challenge.status,
|
|
24074
|
+
challenge.data
|
|
24075
|
+
);
|
|
24076
|
+
}
|
|
24077
|
+
if (factor.factor_type === "email") io.print("We emailed you a code.");
|
|
24078
|
+
for (; ; ) {
|
|
24079
|
+
const code = (await ask(prompt)).replace(/\s+/g, "");
|
|
24080
|
+
if (!/^\d+$/.test(code)) {
|
|
24081
|
+
io.print("The code is digits only. Try again.");
|
|
24082
|
+
continue;
|
|
24083
|
+
}
|
|
24084
|
+
const verified = await gotrueJson(`${factorUrl}/verify`, {
|
|
24085
|
+
method: "POST",
|
|
24086
|
+
accessToken,
|
|
24087
|
+
body: { challenge_id: challengeId, code }
|
|
24088
|
+
});
|
|
24089
|
+
if (succeeded(verified.status)) {
|
|
24090
|
+
const { access_token, refresh_token } = verified.data;
|
|
24091
|
+
if (typeof access_token !== "string" || typeof refresh_token !== "string") {
|
|
24092
|
+
throw new Error(
|
|
24093
|
+
"Second-factor verification: response missing access or refresh token"
|
|
24094
|
+
);
|
|
24095
|
+
}
|
|
24096
|
+
const verifiedUser = verified.data.user;
|
|
24097
|
+
return {
|
|
24098
|
+
idToken: access_token,
|
|
24099
|
+
refreshToken: refresh_token,
|
|
24100
|
+
email: verifiedUser?.email ?? tokens.email
|
|
24101
|
+
};
|
|
24102
|
+
}
|
|
24103
|
+
if (verified.data.error_code === "mfa_challenge_expired") {
|
|
24104
|
+
io.print("That code expired. Sending a new one.");
|
|
24105
|
+
break;
|
|
24106
|
+
}
|
|
24107
|
+
if (isWrongCode(verified.status, verified.data)) {
|
|
24108
|
+
if (++wrongCodes >= MAX_CODE_ATTEMPTS) {
|
|
24109
|
+
throw new Error(
|
|
24110
|
+
"Too many incorrect codes. Run `inworld login` to try again."
|
|
24111
|
+
);
|
|
24112
|
+
}
|
|
24113
|
+
io.print("That code is not correct. Try again.");
|
|
24114
|
+
continue;
|
|
24115
|
+
}
|
|
24116
|
+
throw failure(
|
|
24117
|
+
"Second-factor verification",
|
|
24118
|
+
verified.status,
|
|
24119
|
+
verified.data
|
|
24120
|
+
);
|
|
24121
|
+
}
|
|
24122
|
+
}
|
|
24123
|
+
throw new Error(
|
|
24124
|
+
"The second-factor code expired too many times. Run `inworld login` to try again."
|
|
24125
|
+
);
|
|
24126
|
+
}
|
|
24127
|
+
var MAX_CODE_ATTEMPTS, MAX_CHALLENGES, NO_TERMINAL_MESSAGE, UNSUPPORTED_MESSAGE, INPUT_ENDED_MESSAGE, succeeded, isWrongCode, INDEPENDENT_FIRST_FACTORS;
|
|
24128
|
+
var init_second_factor = __esm({
|
|
24129
|
+
"src/mcp/auth/second_factor.ts"() {
|
|
24130
|
+
"use strict";
|
|
24131
|
+
init_client_info();
|
|
24132
|
+
init_identity();
|
|
24133
|
+
MAX_CODE_ATTEMPTS = 5;
|
|
24134
|
+
MAX_CHALLENGES = 5;
|
|
24135
|
+
NO_TERMINAL_MESSAGE = "This account needs a second-factor code; run `inworld login` in an interactive terminal.";
|
|
24136
|
+
UNSUPPORTED_MESSAGE = "This account's second factor cannot be completed in the CLI after this sign-in. Set up an authenticator app in your Inworld account settings and run `inworld login` again, or contact support.";
|
|
24137
|
+
INPUT_ENDED_MESSAGE = "Input ended before a code was entered.";
|
|
24138
|
+
succeeded = (status) => status >= 200 && status < 300;
|
|
24139
|
+
isWrongCode = (status, data) => data.error_code === "mfa_verification_failed" || status === 422 && !data.error_code;
|
|
24140
|
+
INDEPENDENT_FIRST_FACTORS = /* @__PURE__ */ new Set([
|
|
24141
|
+
"password",
|
|
24142
|
+
"sso/saml",
|
|
24143
|
+
"passkey",
|
|
24144
|
+
"web3"
|
|
24145
|
+
]);
|
|
24146
|
+
}
|
|
24147
|
+
});
|
|
24148
|
+
|
|
23952
24149
|
// src/mcp/auth/browser.ts
|
|
23953
24150
|
import { execFile } from "node:child_process";
|
|
23954
24151
|
import { promisify as promisify4 } from "node:util";
|
|
@@ -73051,7 +73248,9 @@ async function runLogin() {
|
|
|
73051
73248
|
print: (line) => console.error(line),
|
|
73052
73249
|
output: process.stdout
|
|
73053
73250
|
});
|
|
73054
|
-
const tokens = await signIn2.done
|
|
73251
|
+
const tokens = await stepUpSecondFactor(source, await signIn2.done, {
|
|
73252
|
+
print: (line) => console.error(line)
|
|
73253
|
+
});
|
|
73055
73254
|
console.error();
|
|
73056
73255
|
console.error("Exchanging for Inworld session...");
|
|
73057
73256
|
const exchanged = await exchangeIdentityToken(tokens.idToken);
|
|
@@ -73092,6 +73291,7 @@ var init_login = __esm({
|
|
|
73092
73291
|
init_exchange();
|
|
73093
73292
|
init_identity();
|
|
73094
73293
|
init_paths();
|
|
73294
|
+
init_second_factor();
|
|
73095
73295
|
init_session();
|
|
73096
73296
|
init_sign_in_prompt();
|
|
73097
73297
|
}
|
|
@@ -84236,6 +84436,7 @@ var audiobookCommand = new Command("audiobook").description("Audiobook generatio
|
|
|
84236
84436
|
// src/commands/auth/login_command.ts
|
|
84237
84437
|
init_constants5();
|
|
84238
84438
|
init_identity();
|
|
84439
|
+
init_second_factor();
|
|
84239
84440
|
init_sign_in_prompt();
|
|
84240
84441
|
|
|
84241
84442
|
// src/services/workspace_service.ts
|
|
@@ -84652,7 +84853,9 @@ var signIn = async (source, provider) => {
|
|
|
84652
84853
|
printError,
|
|
84653
84854
|
output: process.stdout
|
|
84654
84855
|
});
|
|
84655
|
-
return browserSignIn.done
|
|
84856
|
+
return stepUpSecondFactor(source, await browserSignIn.done, {
|
|
84857
|
+
print
|
|
84858
|
+
});
|
|
84656
84859
|
};
|
|
84657
84860
|
var loginAction = async (options) => {
|
|
84658
84861
|
try {
|