-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic Foundation for a Keeper Security JSON Importer
- Loading branch information
1 parent
2004c97
commit 9ab5766
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>buttercup-importer</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
</buildSpec> | ||
<natures> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const fs = require("fs/promises"); | ||
const { | ||
Vault, | ||
Entry, | ||
createEntryFacade, | ||
consumeEntryFacade, | ||
} = require("buttercup"); | ||
|
||
const DEFAULT_GROUP = "General"; | ||
|
||
/** | ||
* Importer for Keeper Security vaults | ||
* @memberof module:ButtercupImporter | ||
*/ | ||
class KeeperSecurityImporter { | ||
/** | ||
* Create a new Keeper Security importer | ||
* @param {String} data Raw JSON data of a Keeper Security vault export | ||
*/ | ||
constructor(data) { | ||
this._data = data; | ||
} | ||
|
||
/** | ||
* Export to a Buttercup vault | ||
* @returns {Promise.<Vault>} | ||
* @memberof KeeperSecurityImporter | ||
*/ | ||
export() { | ||
const groups = {}; | ||
return Promise.resolve().then(() => { | ||
const vault = new Vault(); | ||
const ksJson = JSON.parse(this._data); // Parse the new JSON data | ||
|
||
// Create the root group | ||
const rootGroup = vault.createGroup(DEFAULT_GROUP); | ||
groups[null] = rootGroup; | ||
|
||
ksJson.records.forEach((record) => { | ||
if (record.folders) { | ||
var folderPath = record.folders[0].folder; | ||
var folders = folderPath | ||
.split("\\") | ||
.map((folderName) => folderName.trim()); | ||
|
||
var currentGroup = rootGroup; | ||
|
||
for ( | ||
var folderIndex = 0; | ||
folderIndex < folders.length; | ||
folderIndex += 1 | ||
) { | ||
if (groups[folders[folderIndex]] != undefined) { | ||
currentGroup = groups[folders[folderIndex]]; | ||
} else { | ||
currentGroup = currentGroup.createGroup( | ||
folders[folderIndex] | ||
); | ||
|
||
// Section untested due to an odd issue with createEntry not instancing correctly? | ||
const entry = currentGroup.createEntry( | ||
record.title | ||
); | ||
entry.setProperty( | ||
"username", | ||
record.login == null ? "" : record.login | ||
); | ||
entry.setProperty( | ||
"password", | ||
record.password == null ? "" : record.password | ||
); | ||
entry.setProperty( | ||
"URL", | ||
record.login_url == null ? "" : record.login_url | ||
); | ||
|
||
groups[folders[folderIndex]] = currentGroup; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return vault; | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Load an importer from a file | ||
* @param {String} filename The file to load from | ||
* @returns {Promise.<KeeperSecurityImporter>} | ||
* @static | ||
* @memberof KeeperSecurityImporter | ||
*/ | ||
KeeperSecurityImporter.loadFromFile = function (filename) { | ||
return fs | ||
.readFile(filename, "utf8") | ||
.then((data) => new KeeperSecurityImporter(data)); | ||
}; | ||
|
||
module.exports = KeeperSecurityImporter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters