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
+76
View File
@@ -9,6 +9,13 @@ const KIRO_ALLOW_ACCESS_TEXT_PATTERN = /allow access|允许访问/i;
const KIRO_SUCCESS_TEXT_PATTERN = /authorization successful|you may now close this window|you are now signed in|授权成功|可以关闭此窗口|已登录/i;
const KIRO_CLOUDFRONT_403_TEXT_PATTERN = /403 error|the request could not be satisfied|generated by cloudfront/i;
const KIRO_AWS_REQUEST_ERROR_TEXT_PATTERN = /sorry,\s*there was an error processing your request\.?\s*please try again\.?|抱歉,处理您的请求时出错。请重试。?/i;
const KIRO_EMAIL_TEXT_PATTERN = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/gi;
const KIRO_NON_ACCOUNT_EMAIL_DOMAINS = new Set([
'amazon.com',
'aws.amazon.com',
'example.com',
'kiro.dev',
]);
const KIRO_EMAIL_INPUT_SELECTOR = [
'input[placeholder="username@example.com"]',
@@ -67,6 +74,58 @@ function getKiroPageText() {
.trim();
}
function normalizeKiroEmailCandidate(value = '') {
const matched = String(value || '').match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/i);
return matched ? matched[0].trim().toLowerCase() : '';
}
function isLikelyKiroAccountEmail(value = '') {
const normalized = normalizeKiroEmailCandidate(value);
if (!normalized) {
return false;
}
const [, domain = ''] = normalized.split('@');
const normalizedDomain = domain.toLowerCase();
if (KIRO_NON_ACCOUNT_EMAIL_DOMAINS.has(normalizedDomain)) {
return false;
}
return !normalized.startsWith('no-reply@')
&& !normalized.startsWith('noreply@')
&& !normalized.startsWith('support@')
&& !normalizedDomain.endsWith('.amazon.com')
&& !normalizedDomain.endsWith('.amazonaws.com')
&& !normalizedDomain.endsWith('.kiro.dev')
&& !normalizedDomain.endsWith('.signin.aws')
&& !normalizedDomain.endsWith('.profile.aws');
}
function extractFirstKiroAccountEmailFromText(text = '') {
const matches = String(text || '').match(KIRO_EMAIL_TEXT_PATTERN) || [];
return matches.map((entry) => normalizeKiroEmailCandidate(entry)).find(isLikelyKiroAccountEmail) || '';
}
function findKiroSignedInAccountEmail(pageText = '') {
const selector = [
'input[type="email"]',
'input[name*="email" i]',
'[data-testid*="email" i]',
'[aria-label*="email" i]',
'[title*="email" i]',
].join(', ');
for (const element of collectVisibleElements(selector)) {
const candidate = extractFirstKiroAccountEmailFromText([
element?.value,
element?.textContent,
element?.getAttribute?.('aria-label'),
element?.getAttribute?.('title'),
].filter(Boolean).join(' '));
if (candidate) {
return candidate;
}
}
return extractFirstKiroAccountEmailFromText(pageText);
}
function collectVisibleElements(selector) {
return Array.from(document.querySelectorAll(selector))
.filter((element) => isVisibleKiroElement(element));
@@ -309,9 +368,12 @@ function detectKiroRegisterPageState() {
};
}
if (isKiroWebSignedInPage(pageText)) {
const accountEmail = findKiroSignedInAccountEmail(pageText);
return {
state: 'kiro_web_signed_in',
url: currentUrl,
accountEmail,
email: accountEmail,
};
}
}
@@ -642,6 +704,19 @@ async function confirmKiroRegisterConsent(payload = {}) {
async function handleKiroRegisterCommand(message) {
switch (message.type) {
case 'GET_KIRO_REGISTER_PAGE_STATE': {
const detected = detectKiroRegisterPageState();
return {
state: detected?.state || '',
url: detected?.url || location.href,
accountEmail: detected?.accountEmail || '',
email: detected?.email || detected?.accountEmail || '',
fatalMessage: detected?.fatalMessage || '',
authorizationActionKind: detected?.authorizationActionKind || '',
authorizationActionText: detected?.authorizationActionText || '',
actionText: detected?.actionText || '',
};
}
case 'ENSURE_KIRO_PAGE_STATE':
return ensureKiroRegisterPageState(message.payload || {});
case 'ENSURE_KIRO_STATE_CHANGE':
@@ -679,6 +754,7 @@ if (document.documentElement.getAttribute(KIRO_REGISTER_PAGE_LISTENER_SENTINEL)
if (
message.type === 'ENSURE_KIRO_PAGE_STATE'
|| message.type === 'ENSURE_KIRO_STATE_CHANGE'
|| message.type === 'GET_KIRO_REGISTER_PAGE_STATE'
|| message.type === 'EXECUTE_NODE'
) {
resetStopState();