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.

131 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. --stdout Prints the password and username to stdout
  39. --debug Show extra logs useful for debugging.
  40. --debug-unsafe Show debug logs WITHOUT obfuscating your sensitive info. Do not share!
  41. `
  42. )
  43. process.exit()
  44. }
  45. const bwListArgs = args['bw-list-args'] || BW_LIST_ARGS_DEFAULT
  46. const dmenuArgs = args['dmenu-args'] || DMENU_ARGS_DEFAULT
  47. const dmenuPswdArgs = args['dmenu-pswd-args'] || DMENU_PSWD_ARGS_DEFAULT
  48. const unsafeDebug = args['debug-unsafe']
  49. const sessionTimeout = args['session-timeout'] || SESSION_TIMEOUT_DEFAULT
  50. const syncVaultAfter = args['sync-vault-after'] || SYNC_VAULT_AFTER_DEFAULT
  51. const onErrorCommand = args['on-error']
  52. const stdout = args['stdout'] || STDOUT_DEFAULT
  53. // prevent clipboard clearing from locking up process when printing to stdout
  54. const clearClipboardAfter = stdout ? 0 : args['clear-clipboard'] || CACHE_PASSWORD_DEFAULT
  55. console.debug =
  56. args['debug'] || args['debug-unsafe'] ? (...msgs) => console.log(...msgs, '\n') : () => {}
  57. console.info = args['stdout'] ? () => {} : console.info
  58. if (args['debug-unsafe']) obfuscateState.turnOff()
  59. const oldestAllowedVaultSync = syncVaultAfter
  60. const saveSession = Boolean(sessionTimeout)
  61. const sessionFile = path.resolve(os.tmpdir(), 'bitwarden-session.txt')
  62. const dmenuArgsParsed = dmenuArgs ? dmenuArgs.split(/\s+/) : []
  63. const dmenuPswdArgsParsed = ['-p', 'Password:', '-nf', 'black', '-nb', 'black'].concat(
  64. dmenuPswdArgs ? dmenuPswdArgs.split(/\s+/) : []
  65. )
  66. const pipeErrorToCommand = (command, e) =>
  67. new Promise((resolve, reject) => {
  68. const errorCommand = exec(onErrorCommand)
  69. console.log(e)
  70. if (e instanceof CommandError) errorCommand.stdin.write(`'${e.errorMessage}'`)
  71. else errorCommand.stdin.write(`'${e.toString()}'`)
  72. errorCommand.stdin.end()
  73. errorCommand.on('close', status => {
  74. if (status === 0) resolve()
  75. else reject()
  76. })
  77. })
  78. const runBitwardenDmenu = async () => {
  79. try {
  80. await menu({
  81. bwListArgs,
  82. dmenuArgs: dmenuArgsParsed,
  83. dmenuPswdArgs: dmenuPswdArgsParsed,
  84. saveSession,
  85. sessionFile,
  86. stdout,
  87. oldestAllowedVaultSync,
  88. unsafeDebug
  89. })
  90. return { lockBitwardenAfter: sessionTimeout, clearClipboardAfter }
  91. } catch (e) {
  92. if (e instanceof CancelError) {
  93. console.debug('cancelled bitwarden-dmenu early.')
  94. return { lockBitwardenAfter: 0, clearClipboardAfter: null }
  95. } else if (onErrorCommand) await pipeErrorToCommand(onErrorCommand, e)
  96. else {
  97. console.error('an error occurred:')
  98. console.error(e)
  99. }
  100. return { lockBitwardenAfter: 0, clearClipboardAfter: 0 }
  101. }
  102. }
  103. ;(async () => {
  104. const { lockBitwardenAfter, clearClipboardAfter } = await runBitwardenDmenu()
  105. await scheduleCleanup({
  106. lockBitwardenAfter,
  107. clearClipboardAfter,
  108. sessionFile,
  109. stdout
  110. })
  111. })()