feat: enhance URI handling and TOTP field extraction in import functions

This commit is contained in:
shuaiplus
2026-04-25 16:35:35 +08:00
parent 15ee922777
commit 4a63c077f5
4 changed files with 126 additions and 27 deletions
+66 -9
View File
@@ -19,6 +19,71 @@ export function normalizeUri(raw: string): string | null {
return s.slice(0, 1000);
}
export function normalizeUriList(rawUris: string[]): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const raw of rawUris) {
const uri = normalizeUri(raw);
if (!uri) continue;
const key = uri.toLowerCase();
if (seen.has(key)) continue;
seen.add(key);
out.push(uri);
}
return out;
}
export function setLoginUris(login: Record<string, unknown>, rawUris: string[]): void {
const uris = normalizeUriList(rawUris);
login.uris = uris.length ? uris.map((uri) => ({ uri, match: null })) : null;
}
export function addLoginUri(login: Record<string, unknown>, rawUri: string): void {
const existing = Array.isArray(login.uris)
? login.uris.map((entry) => txt((entry as Record<string, unknown>)?.uri)).filter(Boolean)
: [];
setLoginUris(login, [...existing, rawUri]);
}
export function isTotpFieldName(raw: unknown): boolean {
const name = txt(raw).toLowerCase().replace(/[\s_-]+/g, '');
if (!name) return false;
return [
'totp',
'totpuri',
'otp',
'otpuri',
'otpurl',
'otpauth',
'onetimepassword',
'onetimepasscode',
'2fa',
'twofactor',
'twofactorauthentication',
'authenticator',
'verificationcode',
].includes(name);
}
export function extractTotpValue(raw: unknown): string {
if (raw === null || raw === undefined) return '';
if (typeof raw === 'string' || typeof raw === 'number' || typeof raw === 'boolean') return txt(raw);
if (Array.isArray(raw)) {
for (const item of raw) {
const value = extractTotpValue(item);
if (value) return value;
}
return '';
}
if (typeof raw !== 'object') return '';
const obj = raw as Record<string, unknown>;
for (const key of ['totpUri', 'otpAuth', 'otpauth', 'uri', 'url', 'secret', 'totp', 'otp', 'value', 'code']) {
const value = extractTotpValue(obj[key]);
if (value) return value;
}
return '';
}
export function parseSerializedUris(raw: string): string[] {
const source = txt(raw);
if (!source) return [];
@@ -38,15 +103,7 @@ export function parseSerializedUris(raw: string): string[] {
.filter(Boolean)
: [source];
const seen = new Set<string>();
const uris: string[] = [];
for (const part of parts) {
const normalized = normalizeUri(part);
if (!normalized || seen.has(normalized)) continue;
seen.add(normalized);
uris.push(normalized);
}
return uris;
return normalizeUriList(parts);
}
export function nameFromUrl(raw: string): string | null {