From 3a34d3b174a1cad8eac9fe950c814336902b4cab Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 23 Jul 2018 12:04:34 -0400 Subject: [PATCH] gnome json importer --- src/importers/gnomeJsonImporter.ts | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/importers/gnomeJsonImporter.ts diff --git a/src/importers/gnomeJsonImporter.ts b/src/importers/gnomeJsonImporter.ts new file mode 100644 index 00000000000..be62e8cfbef --- /dev/null +++ b/src/importers/gnomeJsonImporter.ts @@ -0,0 +1,60 @@ +import { BaseImporter } from './baseImporter'; +import { Importer } from './importer'; + +import { ImportResult } from '../models/domain/importResult'; + +export class GnomeJsonImporter extends BaseImporter implements Importer { + parse(data: string): ImportResult { + const result = new ImportResult(); + const results = JSON.parse(data); + if (results == null || Object.keys(results).length === 0) { + result.success = false; + return result; + } + + for (const keyRing in results) { + if (!results.hasOwnProperty(keyRing) || this.isNullOrWhitespace(keyRing) || + results[keyRing].length === 0) { + continue; + } + + results[keyRing].forEach((value: any) => { + if (this.isNullOrWhitespace(value.display_name) || value.display_name.indexOf('http') !== 0) { + return; + } + + this.processFolder(result, keyRing); + const cipher = this.initLoginCipher(); + cipher.name = value.display_name.replace('http://', '').replace('https://', ''); + if (cipher.name.length > 30) { + cipher.name = cipher.name.substring(0, 30); + } + cipher.login.password = this.getValueOrDefault(value.secret); + cipher.login.uris = this.makeUriArray(value.display_name); + + if (value.attributes != null) { + cipher.login.username = value.attributes != null ? + this.getValueOrDefault(value.attributes.username_value) : null; + for (const attr in value.attributes) { + if (!value.attributes.hasOwnProperty(attr) || attr === 'username_value' || + attr === 'xdg:schema') { + continue; + } + this.processKvp(cipher, attr, value.attributes[attr]); + } + } + + this.convertToNoteIfNeeded(cipher); + this.cleanupCipher(cipher); + result.ciphers.push(cipher); + }); + } + + if (this.organization) { + this.moveFoldersToCollections(result); + } + + result.success = true; + return result; + } +}