Fix Kiro existing web session desktop auth

This commit is contained in:
QLHazyCoder
2026-05-19 14:49:46 +08:00
parent 56fcbbdc30
commit 6f88486a7d
6 changed files with 452 additions and 17 deletions
@@ -85,9 +85,125 @@ test('kiro desktop authorize runner uses a shared 3-minute page-load timeout bud
test('kiro desktop authorization is gated by completed Kiro Web sign-in', () => {
const source = fs.readFileSync('background/kiro/desktop-authorize-runner.js', 'utf8');
assert.match(source, /runtimeState\.register\?\.status/);
assert.match(source, /runtimeState\.webAuth\?\.status/);
assert.match(source, /restoreKiroWebSessionFromOpenTabs/);
assert.match(source, /GET_KIRO_REGISTER_PAGE_STATE/);
assert.match(source, /Kiro Web 登录态尚未建立/);
assert.match(source, /已检测到 Kiro Web 登录态/);
});
test('executeKiroStartDesktopAuthorize restores existing Kiro Web session before desktop auth', async () => {
const api = loadDesktopAuthorizeRunnerApi();
let currentState = {
kiroRuntime: {
session: {},
register: {},
webAuth: {},
desktopAuth: {},
upload: {},
},
};
const setStateCalls = [];
const logs = [];
let completedPayload = null;
let openedAuthorizeUrl = '';
let registerReadySource = '';
let desktopRegisteredTabId = null;
const runner = api.createKiroDesktopAuthorizeRunner({
addLog: async (message, level) => {
logs.push({ message, level });
},
chrome: {
tabs: {
get: async (tabId) => ({
id: tabId,
status: 'complete',
url: tabId === 77
? 'https://app.kiro.dev/settings/account'
: 'https://oidc.us-east-1.amazonaws.com/authorize',
}),
query: async (queryInfo) => {
if (Array.isArray(queryInfo.url)) {
return [{
id: 77,
status: 'complete',
url: 'https://app.kiro.dev/settings/account',
}];
}
return [];
},
update: async () => ({}),
},
webNavigation: {
onBeforeNavigate: { addListener: () => {} },
onCommitted: { addListener: () => {} },
},
webRequest: {
onBeforeRequest: { addListener: () => {} },
},
},
completeNodeFromBackground: async (_nodeId, payload) => {
completedPayload = payload;
},
ensureContentScriptReadyOnTab: async (source, tabId, options = {}) => {
assert.equal(tabId, 77);
registerReadySource = source;
assert.equal(options.injectSource, 'kiro-register-page');
},
fetchImpl: async () => ({
ok: true,
text: async () => JSON.stringify({
clientId: 'restored-client-id',
clientSecret: 'restored-client-secret',
}),
}),
getState: async () => currentState,
getTabId: async () => null,
isTabAlive: async () => false,
KIRO_REGISTER_INJECT_FILES: ['content/kiro/register-page.js'],
registerTab: async (source, tabId) => {
if (source === 'kiro-desktop-authorize') {
desktopRegisteredTabId = tabId;
}
},
reuseOrCreateTab: async (_source, url) => {
openedAuthorizeUrl = url;
return 88;
},
sendToContentScriptResilient: async (source, message) => {
assert.equal(source, 'kiro-register-page');
assert.equal(message.type, 'GET_KIRO_REGISTER_PAGE_STATE');
return {
ok: true,
state: 'kiro_web_signed_in',
url: 'https://app.kiro.dev/settings/account',
accountEmail: 'restored@duck.com',
};
},
setState: async (patch) => {
setStateCalls.push(patch);
currentState = { ...currentState, ...patch };
},
sleepWithStop: async () => {},
throwIfStopped: () => {},
waitForTabStableComplete: async () => ({ status: 'complete' }),
});
await runner.executeKiroStartDesktopAuthorize(currentState);
assert.equal(registerReadySource, 'kiro-register-page');
assert.equal(desktopRegisteredTabId, 88);
assert.match(openedAuthorizeUrl, /client_id=restored-client-id/);
assert.equal(currentState.email, 'restored@duck.com');
assert.equal(currentState.kiroRuntime.register.email, 'restored@duck.com');
assert.equal(currentState.kiroRuntime.register.status, 'completed');
assert.equal(currentState.kiroRuntime.webAuth.status, 'signed_in');
assert.equal(completedPayload?.kiroRuntime?.desktopAuth?.clientId, 'restored-client-id');
assert.equal(
logs.some(({ message }) => message.includes('检测到已有 Kiro Web 登录态,已恢复账号 restored@duck.com')),
true
);
assert.equal(setStateCalls.length >= 2, true);
});
test('executeKiroCompleteDesktopAuthorize finishes from callback page without waiting for tracker replay', async () => {
+55 -3
View File
@@ -17,6 +17,18 @@ const OPENAI_NODE_IDS = [
'platform-verify',
];
const KIRO_NODE_IDS = [
'kiro-open-register-page',
'kiro-submit-email',
'kiro-submit-name',
'kiro-submit-verification-code',
'kiro-submit-password',
'kiro-complete-register-consent',
'kiro-start-desktop-authorize',
'kiro-complete-desktop-authorize',
'kiro-upload-credential',
];
function extractFunction(name) {
const markers = [`async function ${name}(`, `function ${name}(`];
const start = markers
@@ -61,7 +73,7 @@ function extractFunction(name) {
return source.slice(start, end);
}
function createApi() {
function createApi(nodeIds = OPENAI_NODE_IDS) {
const bundle = [
extractFunction('isStepDoneStatus'),
extractFunction('skipNode'),
@@ -70,7 +82,7 @@ function createApi() {
return new Function(`
const DEFAULT_STATE = { nodeStatuses: {} };
function getNodeIdsForState() {
return ${JSON.stringify(OPENAI_NODE_IDS)};
return ${JSON.stringify(nodeIds)};
}
function normalizeStatusMapForNodes(statuses = {}) {
return { ...statuses };
@@ -80,7 +92,7 @@ function isNodeExecutionAllowedForState() {
return true;
}
function getExecutionAllowedNodeIdsForState() {
return ${JSON.stringify(OPENAI_NODE_IDS)};
return ${JSON.stringify(nodeIds)};
}
${bundle}
return { skipNode };
@@ -174,3 +186,43 @@ test('skipNode from open-chatgpt skips only unfinished linked signup nodes', asy
true
);
});
test('skipNode cascades from kiro open-register through the whole register branch', async () => {
const statuses = Object.fromEntries(KIRO_NODE_IDS.map((nodeId) => [nodeId, 'pending']));
const events = {
setNodeStatusCalls: [],
logs: [],
};
const api = createApi(KIRO_NODE_IDS);
globalThis.ensureManualInteractionAllowed = async () => ({
nodeStatuses: { ...statuses },
});
globalThis.getState = async () => ({
nodeStatuses: { ...statuses },
});
globalThis.setNodeStatus = async (nodeId, status) => {
events.setNodeStatusCalls.push({ nodeId, status });
statuses[nodeId] = status;
};
globalThis.addLog = async (message, level) => {
events.logs.push({ message, level });
};
const result = await api.skipNode('kiro-open-register-page');
assert.deepStrictEqual(result, { ok: true, nodeId: 'kiro-open-register-page', status: 'skipped' });
assert.deepStrictEqual(events.setNodeStatusCalls, [
{ nodeId: 'kiro-open-register-page', status: 'skipped' },
{ nodeId: 'kiro-submit-email', status: 'skipped' },
{ nodeId: 'kiro-submit-name', status: 'skipped' },
{ nodeId: 'kiro-submit-verification-code', status: 'skipped' },
{ nodeId: 'kiro-submit-password', status: 'skipped' },
{ nodeId: 'kiro-complete-register-consent', status: 'skipped' },
]);
assert.equal(events.logs[0]?.message, '节点 kiro-open-register-page 已跳过');
assert.equal(
events.logs[1]?.message,
'节点 kiro-open-register-page 已跳过,节点 kiro-submit-email、kiro-submit-name、kiro-submit-verification-code、kiro-submit-password、kiro-complete-register-consent 也已同时跳过。'
);
});
@@ -90,3 +90,18 @@ test('kiro register content treats Kiro web success callback as signed in', () =
assert.equal(detected.state, 'kiro_web_signed_in');
});
test('kiro register content extracts signed-in account email from Kiro account page text', () => {
const harness = createHarness({
href: 'https://app.kiro.dev/settings/account',
hostname: 'app.kiro.dev',
title: 'Account',
bodyText: 'Account Email scrap-aged-quirk@duck.com support@kiro.dev',
});
const detected = harness.detectKiroRegisterPageState();
assert.equal(detected.state, 'kiro_web_signed_in');
assert.equal(detected.accountEmail, 'scrap-aged-quirk@duck.com');
assert.equal(detected.email, 'scrap-aged-quirk@duck.com');
});