1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 02:33:46 +00:00
Files
browser/libs/common/src/importers/fsecure/fsecure-fsk-importer.ts
Daniel James Smith d6acc77ba7 [PS-1755] BEEEP: FSecure FSK-Importer improvements (#3877)
* Move FsecureFskImporter into separate folder

* Add types for exported fsk file

* Add new testdata and rewrite existing tests

* Fix #2801 - Use type instead of style property to differentiate between cipher types

* Add setting cipher.favorite

* Remove unmapped property autofillAndroid

* Re-naming files due to new naming convention

Renamed added or changed files of this PR
Fixed all imports
Removed items from the whitelist

* Extract method refactor

Move logic inside of parse loop into parseEntry
Extract handling of Entries of type Login into handleLoginEntry
Extract handling of Entries of type CreditCard into handleCreditCardEntry

* Simplify folder structure

Use vendor name importer folder
Rename /importers/fsecureImporters to /importers/fsecure
Move fsecure-fsk-types.ts out of the types folder into the fsecure-folder
Delete types folder
Fix all the imports

* Move spec and test-data to fsecure importer

* Fix broken import after merge master

* Use the new FSecureFskImporter

Must have messed up during the last merge:
Delete old importer and spec
Fix import of FSecureFskImporter in import.service
2022-12-22 16:59:46 +01:00

80 lines
2.6 KiB
TypeScript

import { CipherType } from "../../enums/cipherType";
import { ImportResult } from "../../models/domain/import-result";
import { CardView } from "../../models/view/card.view";
import { CipherView } from "../../models/view/cipher.view";
import { BaseImporter } from "../base-importer";
import { Importer } from "../importer";
import { FskEntry, FskEntryTypesEnum, FskFile } from "./fsecure-fsk-types";
export class FSecureFskImporter extends BaseImporter implements Importer {
parse(data: string): Promise<ImportResult> {
const result = new ImportResult();
const results: FskFile = JSON.parse(data);
if (results == null || results.data == null) {
result.success = false;
return Promise.resolve(result);
}
for (const key in results.data) {
// eslint-disable-next-line
if (!results.data.hasOwnProperty(key)) {
continue;
}
const value = results.data[key];
const cipher = this.parseEntry(value);
result.ciphers.push(cipher);
}
result.success = true;
return Promise.resolve(result);
}
private parseEntry(entry: FskEntry): CipherView {
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(entry.service);
cipher.notes = this.getValueOrDefault(entry.notes);
cipher.favorite = entry.favorite > 0;
switch (entry.type) {
case FskEntryTypesEnum.Login:
this.handleLoginEntry(entry, cipher);
break;
case FskEntryTypesEnum.CreditCard:
this.handleCreditCardEntry(entry, cipher);
break;
default:
return;
break;
}
this.convertToNoteIfNeeded(cipher);
this.cleanupCipher(cipher);
return cipher;
}
private handleLoginEntry(entry: FskEntry, cipher: CipherView) {
cipher.login.username = this.getValueOrDefault(entry.username);
cipher.login.password = this.getValueOrDefault(entry.password);
cipher.login.uris = this.makeUriArray(entry.url);
}
private handleCreditCardEntry(entry: FskEntry, cipher: CipherView) {
cipher.type = CipherType.Card;
cipher.card = new CardView();
cipher.card.cardholderName = this.getValueOrDefault(entry.username);
cipher.card.number = this.getValueOrDefault(entry.creditNumber);
cipher.card.brand = this.getCardBrand(cipher.card.number);
cipher.card.code = this.getValueOrDefault(entry.creditCvv);
if (!this.isNullOrWhitespace(entry.creditExpiry)) {
if (!this.setCardExpiration(cipher, entry.creditExpiry)) {
this.processKvp(cipher, "Expiration", entry.creditExpiry);
}
}
if (!this.isNullOrWhitespace(entry.password)) {
this.processKvp(cipher, "PIN", entry.password);
}
}
}