Created
February 5, 2016 18:57
-
-
Save andrewjmead/50760760ec3109661ae5 to your computer and use it in GitHub Desktop.
Raistlin - Password Manager Fixed
This file contains hidden or 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
var storage = require('node-persist'); | |
var crypto = require('crypto-js'); | |
storage.initSync(); | |
var accounts = []; | |
var argv = require('yargs').command('create', 'Create a new entry in the password manager', function(yargs) { | |
yargs.options({ | |
accountName: { | |
demand: true, | |
type: 'string', | |
alias: 'a', | |
description: 'Enter account name here i.e. facebook or email.' | |
}, | |
username: { | |
demand: true, | |
alias: 'u', | |
type: 'string', | |
description: 'Enter the username for this account here.' | |
}, | |
password: { | |
demand: true, | |
alias: 'p', | |
type: 'string', | |
description: 'Enter your password here' | |
}, | |
masterPassword:{ | |
demand: true, | |
alias: 'm', | |
type: 'string', | |
description: 'A master password which allows access to all account information' | |
} | |
}) | |
}).help('help') | |
.command('get', 'Here is where you will retrieve your account information', function(yargs) { | |
yargs.options({ | |
accountName: { | |
demand: true, | |
alias: 'a', | |
type: 'string', | |
description: 'Use this command followed by the account name to retrieve your account information.' | |
}, | |
masterPassword:{ | |
demand: true, | |
alias: 'm', | |
type: 'string', | |
description: 'A master password which allows access to all account information' | |
} | |
}); | |
}).help('help') | |
.argv; | |
var command = argv._[0]; | |
function saveAccounts (accounts, masterPassword) { | |
// encrypt accounts | |
var encryptedAccounts = crypto.AES.encrypt(JSON.stringify(accounts), masterPassword); | |
// setItemSync | |
storage.setItemSync('accounts', encryptedAccounts.toString()); | |
// return accounts | |
return accounts; | |
} | |
function getAccounts (masterPassword) { | |
// use getItemSync to fetch accounts | |
var encryptedAccount = storage.getItemSync('accounts'); | |
var accounts = []; | |
// decrypt | |
if (typeof encryptedAccount !== 'undefined') { | |
var bytes = crypto.AES.decrypt(encryptedAccount, masterPassword); | |
accounts = JSON.parse(bytes.toString(crypto.enc.Utf8)); | |
} | |
// return accounts array | |
return accounts; | |
} | |
function createAcount(account, masterPassword) { | |
accounts = getAccounts(masterPassword); | |
accounts.push(account); | |
saveAccounts(accounts, masterPassword); | |
return account; | |
} | |
function getAccount(accountName, masterPassword) { | |
var accounts = getAccounts(masterPassword); | |
var matchedAccount; | |
accounts.forEach(function(account) { | |
if (account.name === accountName) { | |
matchedAccount = account; | |
} | |
}); | |
console.log(matchedAccount); | |
} | |
if (command === 'create' && typeof argv.accountName !== 'undefined' && typeof argv.username !== 'undefined' && typeof argv.password !== 'undefined') { | |
createAcount({ | |
name: argv.accountName, | |
username: argv.username, | |
password: argv.password | |
}, argv.masterPassword); | |
} else if (command === 'get' && typeof argv.accountName !== 'undefined') { | |
getAccount(argv.accountName, argv.masterPassword); | |
} else if (command === 'get' && typeof argv.accountName === 'undefined') { | |
console.log('That account does not exist in the password manager.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment