diff --git a/src/importers/baseImporter.ts b/src/importers/baseImporter.ts index 77dd6d3c0d..535f663753 100644 --- a/src/importers/baseImporter.ts +++ b/src/importers/baseImporter.ts @@ -8,6 +8,7 @@ import { LoginUriView } from '../models/view/loginUriView'; import { Utils } from '../misc/utils'; import { FieldView } from '../models/view/fieldView'; +import { FolderView } from '../models/view/folderView'; import { LoginView } from '../models/view/loginView'; import { CipherType } from '../enums/cipherType'; @@ -287,4 +288,29 @@ export abstract class BaseImporter { cipher.fields.push(field); } } + + protected processFolder(result: ImportResult, folderName: string) { + let folderIndex = result.folders.length; + const hasFolder = !this.isNullOrWhitespace(folderName); + let addFolder = hasFolder; + + if (hasFolder) { + for (let i = 0; i < result.folders.length; i++) { + if (result.folders[i].name === folderName) { + addFolder = false; + folderIndex = i; + break; + } + } + } + + if (addFolder) { + const f = new FolderView(); + f.name = folderName; + result.folders.push(f); + } + if (hasFolder) { + result.folderRelationships.push([result.ciphers.length, folderIndex]); + } + } } diff --git a/src/importers/passwordSafeXmlImporter.ts b/src/importers/passwordSafeXmlImporter.ts new file mode 100644 index 0000000000..ff799217b6 --- /dev/null +++ b/src/importers/passwordSafeXmlImporter.ts @@ -0,0 +1,62 @@ +import { BaseImporter } from './baseImporter'; +import { Importer } from './importer'; + +import { ImportResult } from '../models/domain/importResult'; + +export class PasswordSafeXmlImporter extends BaseImporter implements Importer { + parse(data: string): ImportResult { + const result = new ImportResult(); + const doc = this.parseXml(data); + if (doc == null) { + result.success = false; + return result; + } + + const passwordSafe = doc.querySelector('passwordsafe'); + if (passwordSafe == null) { + result.errorMessage = 'Missing `passwordsafe` node.'; + result.success = false; + return result; + } + + const notesDelimiter = passwordSafe.getAttribute('delimiter'); + const entries = doc.querySelectorAll('passwordsafe > entry'); + Array.from(entries).forEach((entry) => { + const group = this.querySelectorDirectChild(entry, 'group'); + const groupText = group != null && !this.isNullOrWhitespace(group.textContent) ? + group.textContent.split('.').join(' > ') : null; + this.processFolder(result, groupText); + + const title = this.querySelectorDirectChild(entry, 'title'); + const username = this.querySelectorDirectChild(entry, 'username'); + const email = this.querySelectorDirectChild(entry, 'email'); + const password = this.querySelectorDirectChild(entry, 'password'); + const url = this.querySelectorDirectChild(entry, 'url'); + const notes = this.querySelectorDirectChild(entry, 'notes'); + const cipher = this.initLoginCipher(); + cipher.name = title != null ? this.getValueOrDefault(title.textContent, '--') : '--'; + cipher.notes = notes != null ? + this.getValueOrDefault(notes.textContent, '').split(notesDelimiter).join('\n') : null; + cipher.login.username = username != null ? this.getValueOrDefault(username.textContent) : null; + cipher.login.password = password != null ? this.getValueOrDefault(password.textContent) : null; + cipher.login.uris = url != null ? this.makeUriArray(url.textContent) : null; + + if (this.isNullOrWhitespace(cipher.login.username) && email != null) { + cipher.login.username = this.getValueOrDefault(email.textContent); + } else if (email != null && !this.isNullOrWhitespace(email.textContent)) { + cipher.notes = this.isNullOrWhitespace(cipher.notes) ? 'Email: ' + email.textContent + : (cipher.notes + '\n' + 'Email: ' + email.textContent); + } + + this.cleanupCipher(cipher); + result.ciphers.push(cipher); + }); + + if (this.organization) { + this.moveFoldersToCollections(result); + } + + result.success = true; + return result; + } +} diff --git a/src/models/domain/domain.ts b/src/models/domain/domain.ts index 8eed0f5699..f5d27b0340 100644 --- a/src/models/domain/domain.ts +++ b/src/models/domain/domain.ts @@ -1,4 +1,4 @@ -import { CipherString } from '../domain/cipherString'; +import { CipherString } from './cipherString'; import { View } from '../view/view';