Skip to content

Instantly share code, notes, and snippets.

@oscarkuo
Created September 4, 2016 05:08
Show Gist options
  • Save oscarkuo/a17ed6ea5ce522ef7f5681d4724b6a00 to your computer and use it in GitHub Desktop.
Save oscarkuo/a17ed6ea5ce522ef7f5681d4724b6a00 to your computer and use it in GitHub Desktop.
Recursively map entries in a keepass database using keepass.io
const path = require('path');
const kpio = require('keepass.io');
const db = new kpio.Database();
const dbPath = path.join(__dirname, 'TestDB_Password1234.kdbx');
db.addCredential(new kpio.Credentials.Password('Password1234'));
const findGetFieldValue = (array, key) => {
const f = array.find(f => f.Key === key);
if (f === undefined) return null;
return key === 'Password' ? f.Value._ : f.Value;
}
const isEntryExpired = (entry) => {
const expiryTime = new Date(entry.Times.ExpiryTime);
const expires = entry.Times.Expires;
return (expires === 'True' && !isNaN(expiryTime))
? Date.now() > expiryTime
: false;
};
const getTreeEntries = (api, groups, prefixes) => {
if (groups === undefined) return [];
let entries = [];
groups.forEach(group => {
const prefixesNew = [ ...prefixes, group.Name ];
entries.push(...api.getEntries(group.UUID).map(e => ({
tags: e.Tags.length > 0 ? e.Tags.split(';') : [],
title: [ ...prefixesNew, findGetFieldValue(e.String, 'Title') ],
expired: isEntryExpired(e),
username: findGetFieldValue(e.String, 'UserName'),
password: findGetFieldValue(e.String, 'Password'),
})));
entries.push(...getTreeEntries(api, group.Groups, prefixesNew));
});
return entries;
};
db.loadFile(dbPath, function(err) {
if (err) throw err;
const api = db.getBasicApi();
console.log(JSON.stringify(getTreeEntries(api, api.getGroupTree()[0].Groups, ''), null, 2));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment