Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. console.log('Starting password manager.');
  2.  
  3. var crypto = require('crypto-js');
  4. var storage = require('node-persist'); // integrates node-persist module into this application
  5. storage.initSync(); // prepares the computer to write and save variables
  6.  
  7. /*
  8. var retrievedAccount = storage.getItemSync('account');
  9. console.log("Account is ", retrievedAccount);
  10. */
  11.  
  12. var argv = require('yargs')
  13. .command('create','Creates a user account', function(yargs){
  14. yargs.options({
  15. name: {
  16. demand: true,
  17. alias: 'n',
  18. type: 'string',
  19. description: 'name eg Facebook or Twitter'
  20. },
  21. username: {
  22. demand: true,
  23. alias: 'u',
  24. type: 'string',
  25. description: 'username eg user12! or user12@gmail.com'
  26. },
  27. password: {
  28. demand: true,
  29. alias: 'p',
  30. type: 'string',
  31. description: 'password for account access'
  32. },
  33. masterPassword:{
  34. demand: true,
  35. alias: 'm',
  36. type: 'string',
  37. description: 'Master password is required for encryption / decryption'
  38. }
  39. }).help('help');
  40. })
  41. .command('get', 'get accounts with a matching name', function(yargs){
  42. yargs.options({
  43. name: {
  44. demand: true,
  45. alias: 'n',
  46. type: 'string',
  47. description: 'Specify an account name like Twitter, Facebook etc'
  48. },
  49. masterPassword:{
  50. demand: true,
  51. alias: 'm',
  52. type: 'string',
  53. description: 'Master password is required for encryption / decryption'
  54. }
  55. }).help('help');
  56. })
  57. .help('help')
  58. .argv;
  59.  
  60. var command = argv._[0];
  61. //console.log('From create function: account = ' , argv);
  62.  
  63. /*
  64. - Create a function called 'create'
  65. - The create function will require 3 arguments
  66. - 1) --name (as in Twitter, Facebook etc)
  67. - 2) --username (like user12 or an email address)
  68. - 3) --password
  69.  
  70. - Create a function called 'get'
  71. - requires one argument ie --name
  72.  
  73. Example
  74. account.name = 'Facebook'
  75. account.username = 'User21'
  76. account.password = 'Passwrd 123!'
  77. */
  78.  
  79. function getAccounts(masterPassword){
  80. var accounts = [];
  81.  
  82. // use getItemSync to fetch accounts
  83. var encryptedAccount = storage.getItemSync('accounts');
  84. // DECRYPT
  85. if (typeof accounts !== 'undefined'){
  86. var bytes = crypto.AES.decrypt(encryptedAccount,masterPassword);
  87. var accounts = JSON.parse(bytes.toString(crypto.enc.Utf8));
  88. }
  89.  
  90. // return accounts array
  91. return accounts;
  92. }
  93.  
  94. function saveAccounts(accounts, masterPassword){
  95. // encrypt accounts
  96. var encryptedAccounts = crypto.AES.encrypt(JSON.stringify(accounts), masterPassword);
  97.  
  98. // setItemSync ie saving encrypted Accounts
  99. storage.setItemSync('accounts', encryptedAccounts.toString());
  100. console.log('Account saved.');
  101. // return accounts
  102. return accounts;
  103. }
  104.  
  105. function createAccount(account, masterPassword){
  106. var accounts = getAccounts(masterPassword);
  107.  
  108. // push on new account
  109. accounts.push(account);
  110.  
  111. // save the account
  112. saveAccounts(accounts, masterPassword);
  113.  
  114. // return account
  115. return account;
  116. }
  117.  
  118. function getAccount(accountName){
  119. var accounts = getAccounts(masterPassword);
  120. var matchedAccount;
  121.  
  122. accounts.forEach(function(account){
  123. if(account.name === accountName) {
  124. matchedAccount = account;
  125. }
  126. });
  127. return matchedAccount;
  128. }
  129.  
  130. if(command === 'create'){
  131. //console.log("Coming from 1st if statement ie command === !!!!");
  132. var createdAccount = createAccount({
  133. name: argv.name,
  134. username: argv.username,
  135. password: argv.password
  136. }, argv.masterPassword);
  137. console.log("createdAccount datatype : " + typeof createdAccount);
  138. //console.log("Account created: " );
  139. } else if( command === 'get'){
  140. var fetchedAccount = getAccounts(argv.masterPassword);
  141.  
  142. if(typeof fetchedAccount === 'undefined'){
  143. console.log("Account not found.");
  144. } else {
  145. console.log('Account found');
  146. console.log(fetchedAccount);
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement