Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. var storage = require('node-persist');
  2. var crypto = require('crypto-js');
  3.  
  4. storage.initSync();
  5. var accounts = [];
  6. var argv = require('yargs').command('create', 'Create a new entry in the password manager', function(yargs) {
  7. yargs.options({
  8. accountName: {
  9. demand: true,
  10. type: 'string',
  11. alias: 'a',
  12. description: 'Enter account name here i.e. facebook or email.'
  13. },
  14. username: {
  15. demand: true,
  16. alias: 'u',
  17. type: 'string',
  18. description: 'Enter the username for this account here.'
  19. },
  20. password: {
  21. demand: true,
  22. alias: 'p',
  23. type: 'string',
  24. description: 'Enter your password here'
  25. },
  26. masterPassword:{
  27. demand: true,
  28. alias: 'm',
  29. type: 'string',
  30. description: 'A master password which allows access to all account information'
  31. }
  32. })
  33. }).help('help')
  34. .command('get', 'Here is where you will retrieve your account information', function(yargs) {
  35. yargs.options({
  36. accountName: {
  37. demand: true,
  38. alias: 'a',
  39. type: 'string',
  40. description: 'Use this command followed by the account name to retrieve your account information.'
  41. },
  42. masterPassword:{
  43. demand: true,
  44. alias: 'm',
  45. type: 'string',
  46. description: 'A master password which allows access to all account information'
  47. }
  48. });
  49. }).help('help')
  50. .argv;
  51.  
  52. var command = argv._[0];
  53.  
  54. function saveAccounts (accounts, masterPassword) {
  55.  
  56. // encrypt accounts
  57. var encryptedAccounts = crypto.AES.encrypt(JSON.stringify(accounts), masterPassword);
  58.  
  59. // setItemSync
  60. storage.setItemSync('accounts', encryptedAccounts.toString());
  61.  
  62. // return accounts
  63. return accounts;
  64. }
  65.  
  66. function getAccounts (masterPassword) {
  67.  
  68. // use getItemSync to fetch accounts
  69. var encryptedAccount = storage.getItemSync('accounts');
  70. var accounts = [];
  71.  
  72. // decrypt
  73. if (typeof encryptedAccount !== 'undefined') {
  74. var bytes = crypto.AES.decrypt(encryptedAccount, masterPassword);
  75. accounts = JSON.parse(bytes.toString(crypto.enc.Utf8));
  76. }
  77.  
  78. // return accounts array
  79. return accounts;
  80. }
  81.  
  82. function createAcount(account, masterPassword) {
  83. accounts = getAccounts(masterPassword);
  84. accounts.push(account);
  85. saveAccounts(accounts, masterPassword);
  86. return account;
  87. }
  88.  
  89. function getAccount(accountName, masterPassword) {
  90. var accounts = getAccounts(masterPassword);
  91. var matchedAccount;
  92. accounts.forEach(function(account) {
  93. if (account.name === accountName) {
  94. matchedAccount = account;
  95. }
  96. });
  97. console.log(matchedAccount);
  98. }
  99.  
  100. if (command === 'create' && typeof argv.accountName !== 'undefined' && typeof argv.username !== 'undefined' && typeof argv.password !== 'undefined') {
  101. createAcount({
  102. name: argv.accountName,
  103. username: argv.username,
  104. password: argv.password
  105. }, argv.masterPassword);
  106.  
  107. } else if (command === 'get' && typeof argv.accountName !== 'undefined') {
  108. getAccount(argv.accountName, argv.masterPassword);
  109. } else if (command === 'get' && typeof argv.accountName === 'undefined') {
  110. console.log('That account does not exist in the password manager.');
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement