bitwarden-estensione-browser/libs/common/src/importers/keepass2XmlImporter.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.3 KiB
TypeScript
Raw Normal View History

import { FieldType } from "../enums/fieldType";
2018-07-10 23:51:55 +02:00
import { ImportResult } from "../models/domain/importResult";
import { FolderView } from "../models/view/folderView";
2022-02-22 15:39:11 +01:00
import { BaseImporter } from "./baseImporter";
import { Importer } from "./importer";
2018-07-10 23:51:55 +02:00
export class KeePass2XmlImporter extends BaseImporter implements Importer {
result = new ImportResult();
parse(data: string): Promise<ImportResult> {
2018-07-10 23:51:55 +02:00
const doc = this.parseXml(data);
if (doc == null) {
this.result.success = false;
return Promise.resolve(this.result);
2018-07-10 23:51:55 +02:00
}
const rootGroup = doc.querySelector("KeePassFile > Root > Group");
if (rootGroup == null) {
this.result.errorMessage = "Missing `KeePassFile > Root > Group` node.";
this.result.success = false;
return Promise.resolve(this.result);
2018-07-10 23:51:55 +02:00
}
this.traverse(rootGroup, true, "");
2018-07-10 23:57:59 +02:00
if (this.organization) {
this.moveFoldersToCollections(this.result);
}
2018-07-10 23:51:55 +02:00
this.result.success = true;
return Promise.resolve(this.result);
2021-12-16 13:36:21 +01:00
}
2018-07-10 23:51:55 +02:00
traverse(node: Element, isRootNode: boolean, groupPrefixName: string) {
const folderIndex = this.result.folders.length;
let groupName = groupPrefixName;
2021-12-16 13:36:21 +01:00
2018-07-10 23:51:55 +02:00
if (!isRootNode) {
if (groupName !== "") {
2018-07-23 23:37:32 +02:00
groupName += "/";
2021-12-16 13:36:21 +01:00
}
2018-07-10 23:51:55 +02:00
const nameEl = this.querySelectorDirectChild(node, "Name");
groupName += nameEl == null ? "-" : nameEl.textContent;
const folder = new FolderView();
folder.name = groupName;
this.result.folders.push(folder);
}
this.querySelectorAllDirectChild(node, "Entry").forEach((entry) => {
const cipherIndex = this.result.ciphers.length;
2021-12-16 13:36:21 +01:00
2018-07-10 23:51:55 +02:00
const cipher = this.initLoginCipher();
this.querySelectorAllDirectChild(entry, "String").forEach((entryString) => {
const valueEl = this.querySelectorDirectChild(entryString, "Value");
const value = valueEl != null ? valueEl.textContent : null;
if (this.isNullOrWhitespace(value)) {
2021-12-16 13:36:21 +01:00
return;
}
2018-07-10 23:51:55 +02:00
const keyEl = this.querySelectorDirectChild(entryString, "Key");
const key = keyEl != null ? keyEl.textContent : null;
2021-12-16 13:36:21 +01:00
2018-07-10 23:51:55 +02:00
if (key === "URL") {
cipher.login.uris = this.makeUriArray(value);
} else if (key === "UserName") {
cipher.login.username = value;
} else if (key === "Password") {
cipher.login.password = value;
2020-07-20 21:00:33 +02:00
} else if (key === "otp") {
cipher.login.totp = value.replace("key=", "");
2018-07-10 23:51:55 +02:00
} else if (key === "Title") {
cipher.name = value;
} else if (key === "Notes") {
cipher.notes += value + "\n";
2021-12-16 13:36:21 +01:00
} else {
2018-07-10 23:51:55 +02:00
let type = FieldType.Text;
2019-11-26 14:34:45 +01:00
const attrs = valueEl.attributes as any;
2021-12-16 13:36:21 +01:00
if (
2019-11-26 14:34:45 +01:00
attrs.length > 0 &&
2018-07-10 23:51:55 +02:00
attrs.ProtectInMemory != null &&
2019-11-26 14:34:45 +01:00
attrs.ProtectInMemory.value === "True"
2021-12-16 13:36:21 +01:00
) {
2018-07-10 23:51:55 +02:00
type = FieldType.Hidden;
2021-12-16 13:36:21 +01:00
}
2018-07-10 23:51:55 +02:00
this.processKvp(cipher, key, value, type);
}
2021-12-16 13:36:21 +01:00
});
2018-07-10 23:51:55 +02:00
this.cleanupCipher(cipher);
2018-07-10 23:51:55 +02:00
this.result.ciphers.push(cipher);
2021-12-16 13:36:21 +01:00
2018-07-10 23:51:55 +02:00
if (!isRootNode) {
this.result.folderRelationships.push([cipherIndex, folderIndex]);
}
});
2021-12-16 13:36:21 +01:00
this.querySelectorAllDirectChild(node, "Group").forEach((group) => {
2018-07-10 23:51:55 +02:00
this.traverse(group, false, groupName);
});
}
}