1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 17:53:39 +00:00
Files
browser/libs/common/src/importers/enpass/enpass-json-importer.ts
Robyn MacCallum 7ebedbecfb [SG-998] and [SG-999] Vault and Autofill team refactor (#4542)
* Move DeprecatedVaultFilterService to vault folder

* [libs] move VaultItemsComponent

* [libs] move AddEditComponent

* [libs] move AddEditCustomFields

* [libs] move attachmentsComponent

* [libs] folderAddEditComponent

* [libs] IconComponent

* [libs] PasswordRepormptComponent

* [libs] PremiumComponent

* [libs] ViewCustomFieldsComponent

* [libs] ViewComponent

* [libs] PasswordRepromptService

* [libs] Move FolderService and FolderApiService abstractions

* [libs] FolderService imports

* [libs] PasswordHistoryComponent

* [libs] move Sync and SyncNotifier abstractions

* [libs] SyncService imports

* [libs] fix file casing for passwordReprompt abstraction

* [libs] SyncNotifier import fix

* [libs] CipherServiceAbstraction

* [libs] PasswordRepromptService abstraction

* [libs] Fix file casing for angular passwordReprompt service

* [libs] fix file casing for SyncNotifierService

* [libs] CipherRepromptType

* [libs] rename CipherRepromptType

* [libs] CipherType

* [libs] Rename CipherType

* [libs] CipherData

* [libs] FolderData

* [libs] PasswordHistoryData

* [libs] AttachmentData

* [libs] CardData

* [libs] FieldData

* [libs] IdentityData

* [libs] LocalData

* [libs] LoginData

* [libs] SecureNoteData

* [libs] LoginUriData

* [libs] Domain classes

* [libs] SecureNote

* [libs] Request models

* [libs] Response models

* [libs] View part 1

* [libs] Views part 2

* [libs] Move folder services

* [libs] Views fixes

* [libs] Move sync services

* [libs] cipher service

* [libs] Types

* [libs] Sync file casing

* [libs] Fix folder service import

* [libs] Move spec files

* [libs] casing fixes on spec files

* [browser] Autofill background, clipboard, commands

* [browser] Fix ContextMenusBackground casing

* [browser] Rename fix

* [browser] Autofill content

* [browser] autofill.js

* [libs] enpass importer spec fix

* [browser] autofill models

* [browser] autofill manifest path updates

* [browser] Autofill notification files

* [browser] autofill services

* [browser] Fix file casing

* [browser] Vault popup loose components

* [browser] Vault components

* [browser] Manifest fixes

* [browser] Vault services

* [cli] vault commands and models

* [browser] File capitilization fixes

* [desktop] Vault components and services

* [web] vault loose components

* [web] Vault components

* [browser] Fix misc-utils import

* [libs] Fix psono spec imports

* [fix] Add comments to address lint rules
2023-01-31 16:08:37 -05:00

207 lines
7.0 KiB
TypeScript

import { FieldType } from "../../enums/fieldType";
import { ImportResult } from "../../models/domain/import-result";
import { CipherType } from "../../vault/enums/cipher-type";
import { CardView } from "../../vault/models/view/card.view";
import { CipherView } from "../../vault/models/view/cipher.view";
import { FolderView } from "../../vault/models/view/folder.view";
import { BaseImporter } from "../base-importer";
import { Importer } from "../importer";
import { EnpassJsonFile, EnpassFolder, EnpassField } from "./types/enpass-json-type";
type EnpassFolderTreeItem = EnpassFolder & { children: EnpassFolderTreeItem[] };
const androidUrlRegex = new RegExp("androidapp://.*==@", "g");
export class EnpassJsonImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const results: EnpassJsonFile = JSON.parse(data);
if (results == null || results.items == null || results.items.length === 0) {
result.success = false;
return Promise.resolve(result);
}
const foldersMap = new Map<string, string>();
const foldersIndexMap = new Map<string, number>();
const folderTree = this.buildFolderTree(results.folders);
this.flattenFolderTree(null, folderTree, foldersMap);
foldersMap.forEach((val, key) => {
foldersIndexMap.set(key, result.folders.length);
const f = new FolderView();
f.name = val;
result.folders.push(f);
});
results.items.forEach((item) => {
if (item.folders != null && item.folders.length > 0 && foldersIndexMap.has(item.folders[0])) {
result.folderRelationships.push([
result.ciphers.length,
foldersIndexMap.get(item.folders[0]),
]);
}
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(item.title);
cipher.favorite = item.favorite > 0;
if (item.template_type != null && item.fields != null && item.fields.length > 0) {
if (
item.template_type.indexOf("login.") === 0 ||
item.template_type.indexOf("password.") === 0
) {
this.processLogin(cipher, item.fields);
} else if (item.template_type.indexOf("creditcard.") === 0) {
this.processCard(cipher, item.fields);
} else if (
item.template_type.indexOf("identity.") < 0 &&
item.fields.some((f) => f.type === "password" && !this.isNullOrWhitespace(f.value))
) {
this.processLogin(cipher, item.fields);
} else {
this.processNote(cipher, item.fields);
}
}
cipher.notes += "\n" + this.getValueOrDefault(item.note, "");
this.convertToNoteIfNeeded(cipher);
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
});
result.success = true;
return Promise.resolve(result);
}
private processLogin(cipher: CipherView, fields: EnpassField[]) {
const urls: string[] = [];
fields.forEach((field) => {
if (this.isNullOrWhitespace(field.value) || field.type === "section") {
return;
}
if (
(field.type === "username" || field.type === "email") &&
this.isNullOrWhitespace(cipher.login.username)
) {
cipher.login.username = field.value;
} else if (field.type === "password" && this.isNullOrWhitespace(cipher.login.password)) {
cipher.login.password = field.value;
} else if (field.type === "totp" && this.isNullOrWhitespace(cipher.login.totp)) {
cipher.login.totp = field.value;
} else if (field.type === "url") {
urls.push(field.value);
} else if (field.type === ".Android#") {
let cleanedValue = field.value.startsWith("androidapp://")
? field.value
: "androidapp://" + field.value;
cleanedValue = cleanedValue.replace("android://", "");
cleanedValue = cleanedValue.replace(androidUrlRegex, "androidapp://");
urls.push(cleanedValue);
} else {
this.processKvp(
cipher,
field.label,
field.value,
field.sensitive === 1 ? FieldType.Hidden : FieldType.Text
);
}
});
cipher.login.uris = this.makeUriArray(urls);
}
private processCard(cipher: CipherView, fields: EnpassField[]) {
cipher.card = new CardView();
cipher.type = CipherType.Card;
fields.forEach((field) => {
if (
this.isNullOrWhitespace(field.value) ||
field.type === "section" ||
field.type === "ccType"
) {
return;
}
if (field.type === "ccName" && this.isNullOrWhitespace(cipher.card.cardholderName)) {
cipher.card.cardholderName = field.value;
} else if (field.type === "ccNumber" && this.isNullOrWhitespace(cipher.card.number)) {
cipher.card.number = field.value;
cipher.card.brand = this.getCardBrand(cipher.card.number);
} else if (field.type === "ccCvc" && this.isNullOrWhitespace(cipher.card.code)) {
cipher.card.code = field.value;
} else if (field.type === "ccExpiry" && this.isNullOrWhitespace(cipher.card.expYear)) {
if (!this.setCardExpiration(cipher, field.value)) {
this.processKvp(
cipher,
field.label,
field.value,
field.sensitive === 1 ? FieldType.Hidden : FieldType.Text
);
}
} else {
this.processKvp(
cipher,
field.label,
field.value,
field.sensitive === 1 ? FieldType.Hidden : FieldType.Text
);
}
});
}
private processNote(cipher: CipherView, fields: EnpassField[]) {
fields.forEach((field) => {
if (this.isNullOrWhitespace(field.value) || field.type === "section") {
return;
}
this.processKvp(
cipher,
field.label,
field.value,
field.sensitive === 1 ? FieldType.Hidden : FieldType.Text
);
});
}
private buildFolderTree(folders: EnpassFolder[]): EnpassFolderTreeItem[] {
if (folders == null) {
return [];
}
const folderTree: EnpassFolderTreeItem[] = [];
const map = new Map<string, EnpassFolderTreeItem>([]);
folders.forEach((obj: EnpassFolderTreeItem) => {
map.set(obj.uuid, obj);
obj.children = [];
});
folders.forEach((obj: EnpassFolderTreeItem) => {
if (obj.parent_uuid != null && obj.parent_uuid !== "" && map.has(obj.parent_uuid)) {
map.get(obj.parent_uuid).children.push(obj);
} else {
folderTree.push(obj);
}
});
return folderTree;
}
private flattenFolderTree(
titlePrefix: string,
tree: EnpassFolderTreeItem[],
map: Map<string, string>
) {
if (tree == null) {
return;
}
tree.forEach((f) => {
if (f.title != null && f.title.trim() !== "") {
let title = f.title.trim();
if (titlePrefix != null && titlePrefix.trim() !== "") {
title = titlePrefix + "/" + title;
}
map.set(f.uuid, title);
if (f.children != null && f.children.length !== 0) {
this.flattenFolderTree(title, f.children, map);
}
}
});
}
}