Created
March 11, 2016 15:30
-
-
Save andrewjmead/759e518661ce613ec334 to your computer and use it in GitHub Desktop.
Kyle Password Manager
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
| console.log('Password Manager started'); | |
| var storage = require('node-persist'); | |
| var crypto = require('crypto-js'); | |
| var argv = require('yargs') | |
| .command('create', 'Create a new account', function(yargs){ | |
| return yargs.options({ | |
| name: { | |
| demand: true, | |
| alias: 'n', | |
| description: 'The account name', | |
| type: 'string' | |
| }, | |
| username: { | |
| demand: true, | |
| alias: 'u', | |
| description: 'The account\'s username', | |
| type: 'string' | |
| }, | |
| password: { | |
| demand: true, | |
| alias: 'p', | |
| description: 'The account\'s password', | |
| type: 'string' | |
| }, | |
| masterPassword: { | |
| demand: true, | |
| alias: 'm', | |
| description: 'Your password manager password', | |
| type: 'string' | |
| } | |
| }).help('help'); | |
| }) | |
| .command('get', 'Retrieve an account', function(yargs){ | |
| return yargs.options({ | |
| name: { | |
| demand: true, | |
| alias: 'n', | |
| description: 'The name of the account', | |
| type: 'string' | |
| }, | |
| masterPassword: { | |
| demand: true, | |
| alias: 'm', | |
| description: 'Your password manager password', | |
| type: 'string' | |
| } | |
| }).help('help'); | |
| }) | |
| .argv; | |
| //Initialize persist storage | |
| storage.initSync(); | |
| //Command line options | |
| function getAccounts(masterPassword){ | |
| //Fetch the accounts object | |
| var accountsEncrypted = storage.getItemSync('accounts'); | |
| var accounts = []; | |
| if(typeof(accountsEncrypted) !== 'undefined'){ | |
| //Decrypt | |
| var bytes = crypto.AES.decrypt(accountsEncrypted, masterPassword); | |
| var accountsDecryptedJSON = bytes.toString(crypto.enc.Utf8); | |
| var accounts = JSON.parse(accountsDecryptedJSON); | |
| } | |
| //return | |
| return accounts | |
| } | |
| function saveAccounts(accounts, masterPassword){ | |
| var accountsEncrypted = crypto.AES.encrypt(accounts, masterPassword); | |
| storage.setItemSync('accounts', accountsEncrypted.toString()); | |
| } | |
| function createAccount(account, masterPassword){ | |
| var accounts = getAccounts(masterPassword); | |
| accounts.push(account); | |
| accounts = JSON.stringify(accounts); | |
| 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; | |
| } | |
| }); | |
| return matchedAccount; | |
| } | |
| //Run what the command line options say to | |
| var command = argv._[0]; | |
| if(command === 'create'){ | |
| var newAccount = createAccount({ | |
| name: argv.name, | |
| username: argv.username, | |
| password: argv.password, | |
| }, argv.masterPassword); | |
| console.log(newAccount); | |
| } else if(command === 'get'){ | |
| var foundAccount = getAccount(argv.name, argv.masterPassword); | |
| if(typeof foundAccount === 'undefined'){ | |
| console.log('Account not found.') | |
| } else { | |
| console.log(foundAccount); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment