1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-14 10:26:19 +01:00
bitwarden-browser/libs/common/src/importers/passwordboss-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

131 lines
4.2 KiB
TypeScript

import { ImportResult } from "../models/domain/import-result";
import { CipherType } from "../vault/enums/cipher-type";
import { CardView } from "../vault/models/view/card.view";
import { FolderView } from "../vault/models/view/folder.view";
import { BaseImporter } from "./base-importer";
import { Importer } from "./importer";
export class PasswordBossJsonImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const results = JSON.parse(data);
if (results == null || results.items == null) {
result.success = false;
return Promise.resolve(result);
}
const foldersMap = new Map<string, string>();
results.folders.forEach((value: any) => {
foldersMap.set(value.id, value.name);
});
const foldersIndexMap = new Map<string, number>();
foldersMap.forEach((val, key) => {
foldersIndexMap.set(key, result.folders.length);
const f = new FolderView();
f.name = val;
result.folders.push(f);
});
results.items.forEach((value: any) => {
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(value.name, "--");
cipher.login.uris = this.makeUriArray(value.login_url);
if (value.folder != null && foldersIndexMap.has(value.folder)) {
result.folderRelationships.push([result.ciphers.length, foldersIndexMap.get(value.folder)]);
}
if (value.identifiers == null) {
return;
}
if (!this.isNullOrWhitespace(value.identifiers.notes)) {
cipher.notes = value.identifiers.notes.split("\\r\\n").join("\n").split("\\n").join("\n");
}
if (value.type === "CreditCard") {
cipher.card = new CardView();
cipher.type = CipherType.Card;
}
for (const property in value.identifiers) {
// eslint-disable-next-line
if (!value.identifiers.hasOwnProperty(property)) {
continue;
}
const valObj = value.identifiers[property];
const val = valObj != null ? valObj.toString() : null;
if (
this.isNullOrWhitespace(val) ||
property === "notes" ||
property === "ignoreItemInSecurityScore"
) {
continue;
}
if (property === "custom_fields") {
valObj.forEach((cf: any) => {
this.processKvp(cipher, cf.name, cf.value);
});
continue;
}
if (cipher.type === CipherType.Card) {
if (property === "cardNumber") {
cipher.card.number = val;
cipher.card.brand = this.getCardBrand(val);
continue;
} else if (property === "nameOnCard") {
cipher.card.cardholderName = val;
continue;
} else if (property === "security_code") {
cipher.card.code = val;
continue;
} else if (property === "expires") {
try {
const expDate = new Date(val);
cipher.card.expYear = expDate.getFullYear().toString();
cipher.card.expMonth = (expDate.getMonth() + 1).toString();
} catch {
// Ignore error
}
continue;
} else if (property === "cardType") {
continue;
}
} else {
if (
(property === "username" || property === "email") &&
this.isNullOrWhitespace(cipher.login.username)
) {
cipher.login.username = val;
continue;
} else if (property === "password") {
cipher.login.password = val;
continue;
} else if (property === "totp") {
cipher.login.totp = val;
continue;
} else if (
(cipher.login.uris == null || cipher.login.uris.length === 0) &&
this.uriFieldNames.indexOf(property) > -1
) {
cipher.login.uris = this.makeUriArray(val);
continue;
}
}
this.processKvp(cipher, property, val);
}
this.convertToNoteIfNeeded(cipher);
this.cleanupCipher(cipher);
result.ciphers.push(cipher);
});
result.success = true;
return Promise.resolve(result);
}
}