dmenu for bitwarden-cli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env node
  2. const os = require('os')
  3. const path = require('path')
  4. const { exec } = require('child_process')
  5. const minimist = require('minimist')
  6. const menu = require('../src')
  7. const scheduleCleanup = require('../src/schedule-cleanup')
  8. const cachePasswordDefault = 15
  9. const sessionTimeoutDefault = 0
  10. const syncVaultAfterDefault = 0
  11. const urlFilterDefault = null
  12. const stdoutDefault = false
  13. const args = minimist(process.argv.slice(2))
  14. if (args.help) {
  15. console.log(
  16. `Usage: bitwarden-dmenu [options]
  17. The DMENU_PATH environment variable can be used to point to an alternative dmenu implementation. Defaults to 'dmenu'.
  18. Options:
  19. --clear-clipboard Number of seconds to keep selected field in the clipboard.
  20. Defaults to ${cachePasswordDefault}s.
  21. --session-timeout Number of seconds after an unlock that the menu can be accessed
  22. without providing a password again. Defaults to ${sessionTimeoutDefault}s.
  23. --stdout Prints the password & username to stdout
  24. --sync-vault-after Number of seconds allowable between last bitwarden sync and
  25. current time. Defaults to ${syncVaultAfterDefault}s.
  26. --on-error Arbitrary command to run if the program fails. The thrown error
  27. is piped to the given command. Defaults to none.
  28. --url Url to filter by.
  29. --verbose Show extra logs useful for debugging.
  30. `
  31. )
  32. process.exit()
  33. }
  34. const clearClipboardAfter = args['clear-clipboard'] || cachePasswordDefault
  35. const sessionTimeout = args['session-timeout'] || sessionTimeoutDefault
  36. const syncVaultAfter = args['sync-vault-after'] || syncVaultAfterDefault
  37. const onErrorCommand = args['on-error']
  38. const urlFilter = args['url'] || urlFilterDefault
  39. const stdout = args['stdout'] || stdoutDefault
  40. console.debug = args['verbose']
  41. ? (...msgs) => console.log(...msgs, '\n')
  42. : () => {}
  43. const oldestAllowedVaultSync = syncVaultAfter
  44. const saveSession = Boolean(sessionTimeout)
  45. const sessionFile = path.resolve(os.tmpdir(), 'bitwarden-session.txt')
  46. menu({ saveSession, sessionFile, oldestAllowedVaultSync, urlFilter, stdout })
  47. .then(() =>
  48. scheduleCleanup({
  49. lockBitwardenAfter: sessionTimeout,
  50. clearClipboardAfter,
  51. sessionFile,
  52. stdout
  53. })
  54. )
  55. .catch(e => {
  56. console.error(e)
  57. // if something goes wrong, immediately clear the clipboard & lock bitwarden,
  58. // then run error command
  59. scheduleCleanup({
  60. lockBitwardenAfter: 0,
  61. clearClipboardAfter: 0,
  62. sessionFile,
  63. stdout
  64. })
  65. .catch(e => {
  66. // simply log an error with cleanup
  67. console.error(e)
  68. })
  69. .then(() => {
  70. if (onErrorCommand) {
  71. const errorCommand = exec(onErrorCommand)
  72. errorCommand.stdin.write(`'${e.toString()}'`)
  73. errorCommand.stdin.end()
  74. }
  75. })
  76. })