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.

129 lines
4.6 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
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 obfuscateState = require('../src/util/obfuscate')
  8. const { scheduleCleanup } = require('../src/schedule-cleanup')
  9. const { CancelError, CommandError } = require('../src/util/error')
  10. const BW_LIST_ARGS_DEFAULT = ''
  11. const CACHE_PASSWORD_DEFAULT = 15
  12. const DMENU_ARGS_DEFAULT = ''
  13. const DMENU_PSWD_ARGS_DEFAULT = ''
  14. const SESSION_TIMEOUT_DEFAULT = 0
  15. const SYNC_VAULT_AFTER_DEFAULT = 0
  16. const STDOUT_DEFAULT = false
  17. const args = minimist(process.argv.slice(2))
  18. if (args.help) {
  19. console.log(
  20. `Usage: bitwarden-dmenu [options]
  21. The DMENU_PATH environment variable can be used to point to an alternative dmenu implementation. Defaults to 'dmenu'.
  22. Options:
  23. --bw-list-args Arbitrary arguments to pass to bitwarden's 'list' command
  24. Defaults to nothing.
  25. --clear-clipboard Number of seconds to keep selected field in the clipboard.
  26. Defaults to ${CACHE_PASSWORD_DEFAULT}s.
  27. --dmenu-args Sets arbitrary arguments to pass to dmenu
  28. Defaults to nothing.
  29. --dmenu-pswd-args Sets arbitrary arguments to pass to the dmenu password prompt
  30. Defaults to nothing.
  31. --session-timeout Number of seconds after an unlock that the menu can be accessed
  32. without providing a password again. Defaults to ${SESSION_TIMEOUT_DEFAULT}s.
  33. --stdout Prints the password and username to stdout
  34. --sync-vault-after Number of seconds allowable between last bitwarden sync and
  35. current time. Defaults to ${SYNC_VAULT_AFTER_DEFAULT}s.
  36. --on-error Arbitrary command to run if the program fails. The thrown error
  37. is piped to the given command. Defaults to none.
  38. --debug Show extra logs useful for debugging.
  39. --debug-unsafe Show debug logs WITHOUT obfuscating your sensitive info. Do not share!
  40. `
  41. )
  42. process.exit()
  43. }
  44. const bwListArgs = args['bw-list-args'] || BW_LIST_ARGS_DEFAULT
  45. const dmenuArgs = args['dmenu-args'] || DMENU_ARGS_DEFAULT
  46. const dmenuPswdArgs = args['dmenu-pswd-args'] || DMENU_PSWD_ARGS_DEFAULT
  47. const unsafeDebug = args['debug-unsafe']
  48. const sessionTimeout = args['session-timeout'] || SESSION_TIMEOUT_DEFAULT
  49. const syncVaultAfter = args['sync-vault-after'] || SYNC_VAULT_AFTER_DEFAULT
  50. const onErrorCommand = args['on-error']
  51. const stdout = args['stdout'] || STDOUT_DEFAULT
  52. // prevent clipboard clearing from locking up process when printing to stdout
  53. const clearClipboardAfter = stdout ? 0 : args['clear-clipboard'] || CACHE_PASSWORD_DEFAULT
  54. console.debug =
  55. args['debug'] || args['debug-unsafe'] ? (...msgs) => console.log(...msgs, '\n') : () => {}
  56. console.info = args['stdout'] ? () => {} : console.info
  57. if (args['debug-unsafe']) obfuscateState.turnOff()
  58. const oldestAllowedVaultSync = syncVaultAfter
  59. const saveSession = Boolean(sessionTimeout)
  60. const sessionFile = path.resolve(os.tmpdir(), 'bitwarden-session.txt')
  61. const dmenuArgsParsed = dmenuArgs ? dmenuArgs.split(/\s+/) : []
  62. const dmenuPswdArgsParsed = ['-p', 'Password:', '-nf', 'black', '-nb', 'black'].concat(
  63. dmenuPswdArgs ? dmenuPswdArgs.split(/\s+/) : []
  64. )
  65. const pipeErrorToCommand = (command, e) =>
  66. new Promise((resolve, reject) => {
  67. const errorCommand = exec(onErrorCommand)
  68. console.log(e)
  69. if (e instanceof CommandError) errorCommand.stdin.write(`'${e.errorMessage}'`)
  70. else errorCommand.stdin.write(`'${e.toString()}'`)
  71. errorCommand.stdin.end()
  72. errorCommand.on('close', status => {
  73. if (status === 0) resolve()
  74. else reject()
  75. })
  76. })
  77. const runBitwardenDmenu = async () => {
  78. try {
  79. await menu({
  80. bwListArgs,
  81. dmenuArgs: dmenuArgsParsed,
  82. dmenuPswdArgs: dmenuPswdArgsParsed,
  83. saveSession,
  84. sessionFile,
  85. stdout,
  86. oldestAllowedVaultSync,
  87. unsafeDebug
  88. })
  89. return { lockBitwardenAfter: sessionTimeout, clearClipboardAfter }
  90. } catch (e) {
  91. if (e instanceof CancelError) {
  92. console.debug('cancelled bitwarden-dmenu early.')
  93. return { lockBitwardenAfter: 0, clearClipboardAfter: null }
  94. } else if (onErrorCommand) await pipeErrorToCommand(onErrorCommand, e)
  95. else {
  96. console.error('an error occurred:')
  97. console.error(e)
  98. }
  99. return { lockBitwardenAfter: 0, clearClipboardAfter: 0 }
  100. }
  101. }
  102. ;(async () => {
  103. const { lockBitwardenAfter, clearClipboardAfter } = await runBitwardenDmenu()
  104. await scheduleCleanup({
  105. lockBitwardenAfter,
  106. clearClipboardAfter,
  107. sessionFile,
  108. stdout
  109. })
  110. })()