Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

121 lignes
3.5 KiB

  1. #!/usr/bin/env node
  2. 'use strict';
  3. const clipboardy = require('clipboardy');
  4. const meow = require('meow');
  5. const LessPass = require('lesspass');
  6. const read = require('read');
  7. const chalk = require('chalk');
  8. const helpMessage = `
  9. Usage
  10. $ lesspass <site> <login> [masterPassword] [options]
  11. Options
  12. -l add lowercase in password
  13. -u add uppercase in password
  14. -d add digits in password
  15. -s add symbols in password
  16. --no-lowercase remove lowercase from password
  17. --no-uppercase remove uppercase from password
  18. --no-digits remove digits from password
  19. --no-symbols remove symbols from password
  20. --length, -L int (default 16)
  21. --counter, -c int (default 1)
  22. --clipboard, -C copy generated password to clipboard rather than displaying it.
  23. Need pbcopy (OSX), xsel (Linux) or clip (Windows).
  24. Examples
  25. # no symbols
  26. $ lesspass lesspass.com contact@lesspass.com password --no-symbols
  27. OlfK63bmUhqrGODR
  28. # no symbols shortcut
  29. $ lesspass lesspass.com contact@lesspass.com password -lud
  30. OlfK63bmUhqrGODR
  31. # only digits and length of 8
  32. $ lesspass lesspass.com contact@lesspass.com -d -L8
  33. master password:
  34. 75837019`;
  35. const cli = meow(helpMessage, {
  36. alias: {L: 'length', c: 'counter', C: 'clipboard'},
  37. boolean: ['l', 'u', 'd', 's', 'C', 'clipboard']
  38. });
  39. function calcPassword(site, login, masterPassword, passwordProfile) {
  40. LessPass.generatePassword(site, login, masterPassword, passwordProfile)
  41. .then(function(generatedPassword) {
  42. if (passwordProfile.clipboard) {
  43. clipboardy.write(generatedPassword)
  44. .then(() => {
  45. console.log('Copied to clipboard');
  46. process.exit();
  47. }).catch(err => {
  48. console.error(chalk.red('Copy failed.'));
  49. console.error(err.message);
  50. process.exit(1);
  51. });
  52. } else {
  53. console.log(generatedPassword);
  54. process.exit();
  55. }
  56. });
  57. }
  58. function hasNoShortOption(options) {
  59. return !(['l', 'u', 'd', 's'].some(function(shortOption) {
  60. return typeof options[shortOption] !== 'undefined' && options[shortOption];
  61. }));
  62. }
  63. function getOptionBoolean(options, optionString) {
  64. let shortOption = optionString.substring(0, 1);
  65. if (options[shortOption]) {
  66. return true;
  67. }
  68. if (typeof options[optionString] === 'undefined') {
  69. return hasNoShortOption(options);
  70. }
  71. return options[optionString]
  72. }
  73. const lowercase = getOptionBoolean(cli.flags, 'lowercase');
  74. const uppercase = getOptionBoolean(cli.flags, 'uppercase');
  75. const symbols = getOptionBoolean(cli.flags, 'symbols');
  76. const digits = getOptionBoolean(cli.flags, 'digits');
  77. const passwordProfile = {
  78. lowercase: lowercase,
  79. uppercase: uppercase,
  80. symbols: symbols,
  81. numbers: digits,
  82. clipboard: cli.flags.clipboard || false,
  83. length: cli.flags.length || 16,
  84. counter: cli.flags.counter || 1
  85. };
  86. const site = cli.input[0];
  87. const login = cli.input[1];
  88. if (typeof site === 'undefined' && typeof login === 'undefined') {
  89. console.log(chalk.red('site or login cannot be empty'));
  90. console.log('type lesspass --help');
  91. process.exit(-1);
  92. }
  93. if (cli.input.length === 3) {
  94. const masterPassword = cli.input[2];
  95. calcPassword(site, login, masterPassword, passwordProfile)
  96. } else {
  97. read({prompt: 'master password: ', silent: true}, function(er, password) {
  98. calcPassword(site, login, password, passwordProfile)
  99. });
  100. }