2018-06-23 15:27:30 +02:00
|
|
|
import { BaseImporter } from './baseImporter';
|
|
|
|
import { Importer } from './importer';
|
|
|
|
|
|
|
|
import { ImportResult } from '../models/domain/importResult';
|
|
|
|
|
|
|
|
import { CardView } from '../models/view/cardView';
|
|
|
|
import { CipherView } from '../models/view/cipherView';
|
|
|
|
import { FolderView } from '../models/view/folderView';
|
|
|
|
import { IdentityView } from '../models/view/identityView';
|
|
|
|
import { LoginView } from '../models/view/loginView';
|
|
|
|
import { SecureNoteView } from '../models/view/secureNoteView';
|
|
|
|
|
|
|
|
import { CipherType } from '../enums/cipherType';
|
|
|
|
import { SecureNoteType } from '../enums/secureNoteType';
|
|
|
|
|
|
|
|
export class LastPassCsvImporter extends BaseImporter implements Importer {
|
2020-12-05 03:05:11 +01:00
|
|
|
parse(data: string): Promise<ImportResult> {
|
2018-06-23 15:27:30 +02:00
|
|
|
const result = new ImportResult();
|
|
|
|
const results = this.parseCsv(data, true);
|
|
|
|
if (results == null) {
|
|
|
|
result.success = false;
|
2020-12-05 03:05:11 +01:00
|
|
|
return Promise.resolve(result);
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 20:46:23 +02:00
|
|
|
results.forEach((value, index) => {
|
2018-06-23 15:27:30 +02:00
|
|
|
const cipherIndex = result.ciphers.length;
|
2018-07-05 23:29:35 +02:00
|
|
|
let folderIndex = result.folders.length;
|
2019-01-24 18:04:42 +01:00
|
|
|
let grouping = value.grouping;
|
|
|
|
if (grouping != null) {
|
2019-02-22 21:06:29 +01:00
|
|
|
grouping = grouping.replace(/\\/g, '/').replace(/[\x00-\x1F\x7F-\x9F]/g, '');
|
2019-01-24 18:04:42 +01:00
|
|
|
}
|
|
|
|
const hasFolder = this.getValueOrDefault(grouping, '(none)') !== '(none)';
|
2018-06-23 15:27:30 +02:00
|
|
|
let addFolder = hasFolder;
|
|
|
|
|
|
|
|
if (hasFolder) {
|
|
|
|
for (let i = 0; i < result.folders.length; i++) {
|
2019-01-24 18:04:42 +01:00
|
|
|
if (result.folders[i].name === grouping) {
|
2018-06-23 15:27:30 +02:00
|
|
|
addFolder = false;
|
|
|
|
folderIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 01:00:00 +02:00
|
|
|
const cipher = this.buildBaseCipher(value);
|
2018-06-23 15:27:30 +02:00
|
|
|
if (cipher.type === CipherType.Login) {
|
|
|
|
cipher.notes = this.getValueOrDefault(value.extra);
|
|
|
|
cipher.login = new LoginView();
|
|
|
|
cipher.login.uris = this.makeUriArray(value.url);
|
|
|
|
cipher.login.username = this.getValueOrDefault(value.username);
|
|
|
|
cipher.login.password = this.getValueOrDefault(value.password);
|
|
|
|
} else if (cipher.type === CipherType.SecureNote) {
|
|
|
|
this.parseSecureNote(value, cipher);
|
|
|
|
} else if (cipher.type === CipherType.Card) {
|
|
|
|
cipher.card = this.parseCard(value);
|
|
|
|
cipher.notes = this.getValueOrDefault(value.notes);
|
|
|
|
} else if (cipher.type === CipherType.Identity) {
|
|
|
|
cipher.identity = this.parseIdentity(value);
|
|
|
|
cipher.notes = this.getValueOrDefault(value.notes);
|
|
|
|
if (!this.isNullOrWhitespace(value.ccnum)) {
|
|
|
|
// there is a card on this identity too
|
2018-07-06 01:00:00 +02:00
|
|
|
const cardCipher = this.buildBaseCipher(value);
|
2018-06-23 15:27:30 +02:00
|
|
|
cardCipher.identity = null;
|
|
|
|
cardCipher.type = CipherType.Card;
|
|
|
|
cardCipher.card = this.parseCard(value);
|
|
|
|
result.ciphers.push(cardCipher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ciphers.push(cipher);
|
|
|
|
|
|
|
|
if (addFolder) {
|
|
|
|
const f = new FolderView();
|
2019-01-24 18:04:42 +01:00
|
|
|
f.name = grouping;
|
2018-06-23 15:27:30 +02:00
|
|
|
result.folders.push(f);
|
|
|
|
}
|
|
|
|
if (hasFolder) {
|
2018-07-10 22:38:43 +02:00
|
|
|
result.folderRelationships.push([cipherIndex, folderIndex]);
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-06 01:00:00 +02:00
|
|
|
if (this.organization) {
|
2018-07-05 23:29:35 +02:00
|
|
|
this.moveFoldersToCollections(result);
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:46:23 +02:00
|
|
|
result.success = true;
|
2020-12-05 03:05:11 +01:00
|
|
|
return Promise.resolve(result);
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 01:00:00 +02:00
|
|
|
private buildBaseCipher(value: any) {
|
2018-06-23 15:27:30 +02:00
|
|
|
const cipher = new CipherView();
|
|
|
|
if (value.hasOwnProperty('profilename') && value.hasOwnProperty('profilelanguage')) {
|
|
|
|
// form fill
|
|
|
|
cipher.favorite = false;
|
|
|
|
cipher.name = this.getValueOrDefault(value.profilename, '--');
|
|
|
|
cipher.type = CipherType.Card;
|
|
|
|
|
|
|
|
if (!this.isNullOrWhitespace(value.title) || !this.isNullOrWhitespace(value.firstname) ||
|
|
|
|
!this.isNullOrWhitespace(value.lastname) || !this.isNullOrWhitespace(value.address1) ||
|
|
|
|
!this.isNullOrWhitespace(value.phone) || !this.isNullOrWhitespace(value.username) ||
|
|
|
|
!this.isNullOrWhitespace(value.email)) {
|
|
|
|
cipher.type = CipherType.Identity;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// site or secure note
|
2018-07-06 01:00:00 +02:00
|
|
|
cipher.favorite = !this.organization && this.getValueOrDefault(value.fav, '0') === '1';
|
2018-06-23 15:27:30 +02:00
|
|
|
cipher.name = this.getValueOrDefault(value.name, '--');
|
|
|
|
cipher.type = value.url === 'http://sn' ? CipherType.SecureNote : CipherType.Login;
|
|
|
|
}
|
|
|
|
return cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseCard(value: any): CardView {
|
|
|
|
const card = new CardView();
|
|
|
|
card.cardholderName = this.getValueOrDefault(value.ccname);
|
|
|
|
card.number = this.getValueOrDefault(value.ccnum);
|
|
|
|
card.code = this.getValueOrDefault(value.cccsc);
|
|
|
|
card.brand = this.getCardBrand(value.ccnum);
|
|
|
|
|
|
|
|
if (!this.isNullOrWhitespace(value.ccexp) && value.ccexp.indexOf('-') > -1) {
|
|
|
|
const ccexpParts = (value.ccexp as string).split('-');
|
|
|
|
if (ccexpParts.length > 1) {
|
|
|
|
card.expYear = ccexpParts[0];
|
|
|
|
card.expMonth = ccexpParts[1];
|
|
|
|
if (card.expMonth.length === 2 && card.expMonth[0] === '0') {
|
|
|
|
card.expMonth = card.expMonth[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseIdentity(value: any): IdentityView {
|
|
|
|
const identity = new IdentityView();
|
|
|
|
identity.title = this.getValueOrDefault(value.title);
|
|
|
|
identity.firstName = this.getValueOrDefault(value.firstname);
|
|
|
|
identity.middleName = this.getValueOrDefault(value.middlename);
|
|
|
|
identity.lastName = this.getValueOrDefault(value.lastname);
|
|
|
|
identity.username = this.getValueOrDefault(value.username);
|
|
|
|
identity.company = this.getValueOrDefault(value.company);
|
|
|
|
identity.ssn = this.getValueOrDefault(value.ssn);
|
|
|
|
identity.address1 = this.getValueOrDefault(value.address1);
|
|
|
|
identity.address2 = this.getValueOrDefault(value.address2);
|
|
|
|
identity.address3 = this.getValueOrDefault(value.address3);
|
|
|
|
identity.city = this.getValueOrDefault(value.city);
|
|
|
|
identity.state = this.getValueOrDefault(value.state);
|
|
|
|
identity.postalCode = this.getValueOrDefault(value.zip);
|
|
|
|
identity.country = this.getValueOrDefault(value.country);
|
|
|
|
identity.email = this.getValueOrDefault(value.email);
|
|
|
|
identity.phone = this.getValueOrDefault(value.phone);
|
|
|
|
|
|
|
|
if (!this.isNullOrWhitespace(identity.title)) {
|
|
|
|
identity.title = identity.title.charAt(0).toUpperCase() + identity.title.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseSecureNote(value: any, cipher: CipherView) {
|
|
|
|
const extraParts = this.splitNewLine(value.extra);
|
|
|
|
let processedNote = false;
|
|
|
|
|
|
|
|
if (extraParts.length) {
|
|
|
|
const typeParts = extraParts[0].split(':');
|
|
|
|
if (typeParts.length > 1 && typeParts[0] === 'NoteType' &&
|
|
|
|
(typeParts[1] === 'Credit Card' || typeParts[1] === 'Address')) {
|
|
|
|
if (typeParts[1] === 'Credit Card') {
|
2020-02-06 21:03:55 +01:00
|
|
|
const mappedData = this.parseSecureNoteMapping<CardView>(cipher, extraParts, {
|
2018-06-23 15:27:30 +02:00
|
|
|
'Number': 'number',
|
|
|
|
'Name on Card': 'cardholderName',
|
|
|
|
'Security Code': 'code',
|
2020-02-06 17:24:18 +01:00
|
|
|
// LP provides date in a format like 'June,2020'
|
|
|
|
// Store in expMonth, then parse and modify
|
|
|
|
'Expiration Date': 'expMonth',
|
2018-06-23 15:27:30 +02:00
|
|
|
});
|
2020-02-06 17:24:18 +01:00
|
|
|
|
2020-02-06 21:03:55 +01:00
|
|
|
if (this.isNullOrWhitespace(mappedData.expMonth) || mappedData.expMonth === ',') {
|
2020-02-06 17:24:18 +01:00
|
|
|
// No expiration data
|
2020-02-06 21:28:17 +01:00
|
|
|
mappedData.expMonth = undefined;
|
2020-02-06 17:24:18 +01:00
|
|
|
} else {
|
2020-02-06 21:03:55 +01:00
|
|
|
const [monthString, year] = mappedData.expMonth.split(',');
|
2020-02-06 17:24:18 +01:00
|
|
|
// Parse month name into number
|
|
|
|
if (!this.isNullOrWhitespace(monthString)) {
|
2020-02-06 21:03:55 +01:00
|
|
|
const month = new Date(Date.parse(monthString.trim() + ' 1, 2012')).getMonth() + 1;
|
|
|
|
if (isNaN(month)) {
|
2020-02-06 21:28:17 +01:00
|
|
|
mappedData.expMonth = undefined;
|
2020-02-06 21:03:55 +01:00
|
|
|
} else {
|
|
|
|
mappedData.expMonth = month.toString();
|
|
|
|
}
|
2020-02-06 17:24:18 +01:00
|
|
|
} else {
|
2020-02-06 21:28:17 +01:00
|
|
|
mappedData.expMonth = undefined;
|
2020-02-06 17:24:18 +01:00
|
|
|
}
|
|
|
|
if (!this.isNullOrWhitespace(year)) {
|
2020-02-06 21:03:55 +01:00
|
|
|
mappedData.expYear = year;
|
2020-02-06 17:24:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 15:27:30 +02:00
|
|
|
cipher.type = CipherType.Card;
|
2020-02-06 21:03:55 +01:00
|
|
|
cipher.card = mappedData;
|
2018-06-23 15:27:30 +02:00
|
|
|
} else if (typeParts[1] === 'Address') {
|
2020-02-06 21:03:55 +01:00
|
|
|
const mappedData = this.parseSecureNoteMapping<IdentityView>(cipher, extraParts, {
|
2018-06-23 15:27:30 +02:00
|
|
|
'Title': 'title',
|
|
|
|
'First Name': 'firstName',
|
|
|
|
'Last Name': 'lastName',
|
|
|
|
'Middle Name': 'middleName',
|
|
|
|
'Company': 'company',
|
|
|
|
'Address 1': 'address1',
|
|
|
|
'Address 2': 'address2',
|
|
|
|
'Address 3': 'address3',
|
|
|
|
'City / Town': 'city',
|
|
|
|
'State': 'state',
|
|
|
|
'Zip / Postal Code': 'postalCode',
|
|
|
|
'Country': 'country',
|
|
|
|
'Email Address': 'email',
|
|
|
|
'Username': 'username',
|
|
|
|
});
|
|
|
|
cipher.type = CipherType.Identity;
|
2020-02-06 21:03:55 +01:00
|
|
|
cipher.identity = mappedData;
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
processedNote = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!processedNote) {
|
|
|
|
cipher.secureNote = new SecureNoteView();
|
|
|
|
cipher.secureNote.type = SecureNoteType.Generic;
|
|
|
|
cipher.notes = this.getValueOrDefault(value.extra);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 21:03:55 +01:00
|
|
|
private parseSecureNoteMapping<T>(cipher: CipherView, extraParts: string[], map: any): T {
|
2018-06-23 15:27:30 +02:00
|
|
|
const dataObj: any = {};
|
|
|
|
|
2019-04-16 05:02:14 +02:00
|
|
|
let processingNotes = false;
|
2021-02-04 16:49:23 +01:00
|
|
|
extraParts.forEach(extraPart => {
|
2019-01-19 05:42:03 +01:00
|
|
|
let key: string = null;
|
|
|
|
let val: string = null;
|
2019-04-16 05:02:14 +02:00
|
|
|
if (!processingNotes) {
|
|
|
|
if (this.isNullOrWhitespace(extraPart)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const colonIndex = extraPart.indexOf(':');
|
|
|
|
if (colonIndex === -1) {
|
|
|
|
key = extraPart;
|
|
|
|
} else {
|
|
|
|
key = extraPart.substring(0, colonIndex);
|
|
|
|
if (extraPart.length > colonIndex) {
|
|
|
|
val = extraPart.substring(colonIndex + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.isNullOrWhitespace(key) || this.isNullOrWhitespace(val) || key === 'NoteType') {
|
|
|
|
return;
|
2019-01-19 05:42:03 +01:00
|
|
|
}
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 05:02:14 +02:00
|
|
|
if (processingNotes) {
|
2020-02-06 21:03:55 +01:00
|
|
|
cipher.notes += ('\n' + extraPart);
|
2019-04-16 05:02:14 +02:00
|
|
|
} else if (key === 'Notes') {
|
2020-02-06 21:03:55 +01:00
|
|
|
if (!this.isNullOrWhitespace(cipher.notes)) {
|
|
|
|
cipher.notes += ('\n' + val);
|
2018-06-23 15:27:30 +02:00
|
|
|
} else {
|
2020-02-06 21:03:55 +01:00
|
|
|
cipher.notes = val;
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
2019-04-16 05:02:14 +02:00
|
|
|
processingNotes = true;
|
2019-01-19 05:42:03 +01:00
|
|
|
} else if (map.hasOwnProperty(key)) {
|
|
|
|
dataObj[map[key]] = val;
|
2018-06-23 15:27:30 +02:00
|
|
|
} else {
|
2020-02-06 21:03:55 +01:00
|
|
|
this.processKvp(cipher, key, val);
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-06 21:03:55 +01:00
|
|
|
return dataObj;
|
2018-06-23 15:27:30 +02:00
|
|
|
}
|
|
|
|
}
|