bitwarden.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. ;;; bitwarden.el --- Bitwarden command wrapper -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2018 Sean Farley
  3. ;; Author: Sean Farley
  4. ;; URL: https://github.com/seanfarley/emacs-bitwarden
  5. ;; Version: 0.1.3
  6. ;; Created: 2018-09-04
  7. ;; Package-Requires: ((emacs "25.1"))
  8. ;; Keywords: extensions processes bw bitwarden
  9. ;;; License
  10. ;; This program is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This package wraps the bitwarden command-line program.
  22. ;;; Code:
  23. (require 'auth-source)
  24. (require 'json)
  25. (require 'seq)
  26. (require 'subr-x)
  27. (require 'tree-widget)
  28. ;=============================== custom variables ==============================
  29. (defgroup bitwarden nil
  30. "Bitwarden functions and settings."
  31. :group 'external
  32. :tag "bitwarden"
  33. :prefix "bitwarden-")
  34. (defcustom bitwarden-bw-executable (executable-find "bw")
  35. "The bw cli executable used by Bitwarden."
  36. :group 'bitwarden
  37. :type 'string)
  38. (defcustom bitwarden-data-file
  39. (expand-file-name "Bitwarden CLI/data.json"
  40. (cond
  41. ((getenv "BITWARDENCLI_APPDATA_DIR")
  42. (getenv "BITWARDENCLI_APPDATA_DIR"))
  43. ((eq system-type 'darwin)
  44. "~/Library/Application Support")
  45. ((eq system-type 'windows-nt)
  46. (getenv "APPDATA"))
  47. ((getenv "XDG_CONFIG_HOME")
  48. (getenv "XDG_CONFIG_HOME"))
  49. (t
  50. "~/.config")))
  51. "The bw data file used by Bitwarden."
  52. :group 'bitwarden
  53. :type 'string)
  54. (defcustom bitwarden-user nil
  55. "Bitwarden user e-mail."
  56. :group 'bitwarden
  57. :type 'string)
  58. (defcustom bitwarden-automatic-unlock nil
  59. "Optional function to be called to attempt to unlock the vault.
  60. Set this to a function that will evaluate to a password. For
  61. example, this can be the :secret plist from
  62. `auth-source-search'."
  63. :group 'bitwarden
  64. :type 'function)
  65. (defcustom bitwarden-api-secret-key nil
  66. "Optional function to be called to return API secret key.
  67. Set this to a function that will evaluate to a string (the API secret key)."
  68. :group 'bitwarden
  69. :type 'function)
  70. (defcustom bitwarden-api-client-id nil
  71. "Optional function to be called to return the API client id..
  72. Set this to a function that will evaluate to a string (the API client id)."
  73. :group 'bitwarden
  74. :type 'function)
  75. (defconst bitwarden--err-logged-in "you are not logged in")
  76. (defconst bitwarden--err-multiple "more than one result found")
  77. (defconst bitwarden--err-locked "vault is locked")
  78. ;===================================== util ====================================
  79. (defun bitwarden-logged-in-p ()
  80. "Check if `bitwarden-user' is logged in.
  81. Returns nil if not logged in."
  82. (let* ((ret (apply #'bitwarden--raw-runcmd "login" '("--check")))
  83. (exit-code (nth 0 ret)))
  84. (eq exit-code 0)))
  85. (defun bitwarden-unlocked-p ()
  86. "Check if `bitwarden-user' is loged in.
  87. Returns nil if not unlocked."
  88. (let* ((ret (apply #'bitwarden--raw-runcmd "unlock" '("--check")))
  89. (exit-code (nth 0 ret)))
  90. (eq exit-code 0)))
  91. (defun bitwarden--raw-runcmd (cmd &rest args)
  92. "Run bw command CMD with ARGS.
  93. Returns a list with the first element being the exit code and the
  94. second element being the output."
  95. (let ((stderr (get-buffer-create " *bitwarden-bw-stderr*")))
  96. (with-temp-buffer
  97. (list (apply 'call-process
  98. bitwarden-bw-executable
  99. nil ;; input
  100. (list
  101. (current-buffer) ;; stdout
  102. nil) ;; TODO capture stderr
  103. nil ;; re-display buffer
  104. (cons cmd args))
  105. (replace-regexp-in-string "\n$" ""
  106. (buffer-string))))))
  107. (defun bitwarden-runcmd (cmd &rest args)
  108. "Run bw command CMD with ARGS.
  109. This is a wrapper for `bitwarden--raw-runcmd' that also checks
  110. for common errors."
  111. (if (bitwarden-logged-in-p)
  112. (if (bitwarden-unlocked-p)
  113. (let* ((ret (apply #'bitwarden--raw-runcmd cmd args))
  114. (exit-code (nth 0 ret))
  115. (output (nth 1 ret)))
  116. (if (eq exit-code 0)
  117. output
  118. (cond ((string-match "^More than one result was found." output)
  119. bitwarden--err-multiple)
  120. (t nil))))
  121. bitwarden--err-locked)
  122. bitwarden--err-logged-in))
  123. (defun bitwarden--login-proc-filter (proc string print-message)
  124. "Interacts with PROC by sending line-by-line STRING.
  125. If PRINT-MESSAGE is set then messages are printed to minibuffer."
  126. ;; read username if not defined
  127. (when (string-match "^? Email address:" string)
  128. (let ((user (read-string "Bitwarden email: ")))
  129. ;; if we are here then the user forgot to fill in this field so let's do
  130. ;; that now
  131. (setq bitwarden-user user)
  132. (process-send-string proc (concat bitwarden-user "\n"))))
  133. ;; read master password
  134. (when (string-match "^? Master password:.*" string)
  135. (process-send-string
  136. proc (concat (read-passwd "Bitwarden master password: ") "\n")))
  137. ;; check for bad password
  138. (when (string-match "^Username or password is incorrect" string)
  139. (bitwarden--message "incorrect master password" nil print-message))
  140. ;; if trying to unlock, check if logged in
  141. (when (string-match "^You are not logged in" string)
  142. (bitwarden--message "cannot unlock: not logged in" nil print-message))
  143. ;; read the 2fa code
  144. (when (string-match "^? Two-step login code:" string)
  145. (process-send-string
  146. proc (concat (read-passwd "Bitwarden two-step login code: ") "\n")))
  147. ;; check for bad code
  148. (when (string-match "^Login failed" string)
  149. (bitwarden--message "incorrect two-step code" nil print-message))
  150. ;; check for already logged in
  151. (when (string-match "^You are already logged in" string)
  152. (string-match "You are already logged in as \\(.*\\)\\." string)
  153. (bitwarden--message
  154. "already logged in as %s" (match-string 1 string) print-message))
  155. ;; success! now save the BW_SESSION into the environment so spawned processes
  156. ;; inherit it
  157. (when (string-match "^\\(You are logged in\\|Your vault is now unlocked\\)"
  158. string)
  159. ;; set the session env variable so spawned processes inherit
  160. (string-match "export BW_SESSION=\"\\(.*\\)\"" string)
  161. (setenv "BW_SESSION" (match-string 1 string))
  162. (bitwarden--message
  163. "successfully logged in as %s" bitwarden-user print-message)))
  164. (defun bitwarden--raw-unlock (cmd print-message)
  165. "Raw CMD to either unlock a vault or login.
  166. The only difference between unlock and login is just the name of
  167. the command and whether to pass the user.
  168. If PRINT-MESSAGE is set then messages are printed to minibuffer."
  169. (when (get-process "bitwarden")
  170. (delete-process "bitwarden"))
  171. (make-process :name "bitwarden"
  172. :buffer nil
  173. :connection-type 'pipe
  174. :command (append (list bitwarden-bw-executable)
  175. cmd)
  176. :filter (lambda (proc string)
  177. (bitwarden--login-proc-filter
  178. proc string print-message)))
  179. ;; suppress output to the minibuffer when running this programatically
  180. nil)
  181. ;================================= interactive =================================
  182. (defun bitwarden-unlock (&optional print-message)
  183. "Unlock bitwarden vault.
  184. It is not sufficient to check the env variable for BW_SESSION
  185. since that could be set yet could be expired or incorrect.
  186. If run interactively PRINT-MESSAGE gets set and messages are
  187. printed to minibuffer."
  188. (interactive "p")
  189. (let ((pass (if bitwarden-automatic-unlock
  190. (funcall bitwarden-automatic-unlock)
  191. "")))
  192. (bitwarden--raw-unlock (list "unlock" pass) print-message)))
  193. ;;;###autoload
  194. (defun bitwarden-login (&optional print-message)
  195. "Prompt user for password if not logged in.
  196. If run interactively PRINT-MESSAGE gets set and messages are
  197. printed to minibuffer."
  198. (interactive "p")
  199. (if (and bitwarden-api-client-id bitwarden-api-secret-key)
  200. (progn
  201. (setenv "BW_CLIENTID" (funcall bitwarden-api-client-id))
  202. (setenv "BW_CLIENTSECRET" (funcall bitwarden-api-secret-key))
  203. (bitwarden--raw-unlock (list "login") print-message))
  204. (unless bitwarden-user
  205. (setq bitwarden-user (read-string "Bitwarden email: ")))
  206. (let ((pass (if bitwarden-automatic-unlock
  207. (funcall bitwarden-automatic-unlock)
  208. "")))
  209. (bitwarden--raw-unlock (list "login" bitwarden-user pass "--method" "3") print-message))))
  210. (defun bitwarden-lock ()
  211. "Lock the bw vault. Does not ask for confirmation."
  212. (interactive)
  213. (bitwarden-runcmd "lock")
  214. (setenv "BW_SESSION" nil))
  215. (defun bitwarden-logout ()
  216. "Log out bw. Does not ask for confirmation."
  217. (interactive)
  218. (when (bitwarden-logged-in-p)
  219. (bitwarden-runcmd "logout")
  220. (bitwarden-lock)))
  221. (defun bitwarden--message (msg args &optional print-message)
  222. "Print MSG using `message' and `format' with ARGS if non-nil.
  223. PRINT-MESSAGE is an optional parameter to control whether this
  224. method should print at all. If nil then nothing will be printed
  225. at all.
  226. This method will prepend 'Bitwarden: ' before each MSG as a
  227. convenience. Also, return a value of nil so that no strings
  228. are mistaken as a password (e.g. accidentally interpreting
  229. 'Bitwarden: error' as the password when in fact, it was an error
  230. message but happens to be last on the method stack)."
  231. (when print-message
  232. (let ((msg (if args (format msg args) msg)))
  233. (message (concat "Bitwarden: " msg))))
  234. nil)
  235. (defun bitwarden--handle-message (msg &optional print-message)
  236. "Handle return MSG of `bitwarden--auto-cmd'.
  237. Since `bitwarden--auto-cmd' returns a list of (err-code message),
  238. this function exists to handle that. Printing the error message
  239. is entirely dependent on PRINT-MESSAGE (see below for more info
  240. on PRINT-MESSAGE).
  241. If the error code is 0, then print the password based on
  242. PRINT-MESSAGE or just return it.
  243. If the error code is non-zero, then print the message based on
  244. PRINT-MESSAGE and return nil.
  245. PRINT-MESSAGE is an optional parameter to control whether this
  246. method should print at all. If nil then nothing will be printed
  247. at all but password will be returned (e.g. when run
  248. non-interactively)."
  249. (let* ((err (nth 0 msg))
  250. (pass (nth 1 msg)))
  251. (cond
  252. ((eq err 0)
  253. (if print-message
  254. (message "%s" pass)
  255. pass))
  256. (t
  257. (bitwarden--message "%s" pass print-message)
  258. nil))))
  259. (defun bitwarden--auto-cmd (cmd &optional recursive-pass)
  260. "Run Bitwarden CMD and attempt to auto unlock.
  261. If RECURSIVE-PASS is set, then treat this call as a second
  262. attempt after trying to auto-unlock.
  263. Returns a tuple of the error code and the error message or
  264. password if successful."
  265. (let* ((res (or recursive-pass (apply 'bitwarden-runcmd cmd))))
  266. (cond
  267. ((string-match bitwarden--err-locked res)
  268. ;; try to unlock automatically, if possible
  269. (if (not bitwarden-automatic-unlock)
  270. (list 1 (format "error: %s" res))
  271. ;; only attempt a retry once; to prevent infinite recursion
  272. (when (not recursive-pass)
  273. ;; because I don't understand how emacs is asyncronous here nor
  274. ;; how to tell it to wait until the process is done, we do so here
  275. ;; manually
  276. (bitwarden-unlock)
  277. (while (get-process "bitwarden")
  278. (sleep-for 0.1))
  279. (bitwarden--auto-cmd cmd (apply 'bitwarden-runcmd cmd)))))
  280. ((or (string-match bitwarden--err-logged-in res)
  281. (string-match bitwarden--err-multiple res))
  282. (list 2 (format "error: %s" res)))
  283. (t (list 0 res)))))
  284. ;;;###autoload
  285. (defun bitwarden-getpass (account &optional print-message)
  286. "Get password associated with ACCOUNT.
  287. If run interactively PRINT-MESSAGE gets set and password is
  288. printed to minibuffer."
  289. (interactive "Bitwarden account name: \np")
  290. (bitwarden--handle-message
  291. (bitwarden--auto-cmd (list "get" "password" account))
  292. print-message))
  293. ;;;###autoload
  294. (defun bitwarden-search (&optional search-str)
  295. "Search for vault for items containing SEARCH-STR.
  296. Returns a vector of hashtables of the results."
  297. (let* ((args (and search-str (list "--search" search-str)))
  298. (ret (bitwarden--auto-cmd (append (list "list" "items") args)))
  299. (result (bitwarden--handle-message ret)))
  300. (when result
  301. (let* ((json-object-type 'hash-table)
  302. (json-key-type 'string)
  303. (json (json-read-from-string result)))
  304. json))))
  305. (defun bitwarden-folders ()
  306. "List bitwarden folders."
  307. (let* ((ret (bitwarden--auto-cmd (list "list" "folders")))
  308. (result (bitwarden--handle-message ret)))
  309. (when result
  310. (let* ((json-object-type 'hash-table)
  311. (json-key-type 'string)
  312. (json (json-read-from-string result)))
  313. json))))
  314. (defun bitwarden-sync ()
  315. "Sync local store with server."
  316. (interactive)
  317. (let ((res (bitwarden--auto-cmd (list "sync"))))
  318. (message (nth 1 res))))
  319. ;================================= auth-source =================================
  320. (defun bitwarden--users-account-p (account usernames)
  321. "Check if ACCOUNT belongs to any of the given USERNAMES."
  322. (let* ((login (gethash "login" account))
  323. (account-username (when login (gethash "username" login))))
  324. (when account-username
  325. (seq-contains-p usernames account-username #'string=))))
  326. (defun bitwarden--account-login-p (account)
  327. "Check if ACCOUNT has login information."
  328. (gethash "login" account))
  329. (defun bitwarden-auth-source-search (&rest spec)
  330. "Search Bitwarden according to SPEC.
  331. See `auth-source-search' for a description of the plist SPEC."
  332. (let* ((hosts (ensure-list (plist-get spec :host)))
  333. (maxes (ensure-list (plist-get spec :max)))
  334. (users (ensure-list (plist-get spec :user)))
  335. (all-accounts
  336. (if hosts
  337. (seq-mapcat #'bitwarden-search hosts)
  338. (bitwarden-search)))
  339. (login-accounts
  340. (seq-filter #'bitwarden--account-login-p all-accounts))
  341. (users-accounts
  342. (if users
  343. (seq-filter
  344. (lambda (account)
  345. (bitwarden--users-account-p account users))
  346. login-accounts)
  347. login-accounts))
  348. (formatted-results
  349. (seq-map 'bitwarden-auth-source--build-result users-accounts)))
  350. (message "Bitwarden: auth search parameters %s" spec)
  351. (seq-take formatted-results (seq-max maxes))))
  352. (defun bitwarden-auth-source--build-result (account)
  353. "Build a auth-source result for ACCOUNT.
  354. This is meant to be used by `bitwarden-auth-source-search' to format the final
  355. results."
  356. (let* ((host (gethash "name" account))
  357. (login (gethash "login" account)) ;; always present since
  358. ;; `bitwarden-search-filter-username'
  359. ;; tests for it
  360. (user (gethash "username" login))
  361. (pass (gethash "password" login)))
  362. `(:host ,host
  363. :user ,user
  364. :secret (lambda () ,pass))))
  365. (defvar bitwarden-auth-source-backend
  366. (auth-source-backend :type 'bitwarden
  367. :source "." ;; not used
  368. :search-function #'bitwarden-auth-source-search)
  369. "Auth-source backend variable for Bitwarden.")
  370. (defun bitwarden-auth-source-backend-parse (entry)
  371. "Create auth-source backend from ENTRY."
  372. (when (eq entry 'bitwarden)
  373. (auth-source-backend-parse-parameters entry bitwarden-auth-source-backend)))
  374. ;; advice to add custom auth-source function
  375. (if (boundp 'auth-source-backend-parser-functions)
  376. (add-hook 'auth-source-backend-parser-functions
  377. #'bitwarden-auth-source-backend-parse)
  378. (advice-add 'auth-source-backend-parse
  379. :before-until #'bitwarden-auth-source-backend-parse))
  380. ;;;###autoload
  381. (defun bitwarden-auth-source-enable ()
  382. "Enable Bitwarden auth-source by adding it to `auth-sources'."
  383. (interactive)
  384. (add-to-list 'auth-sources 'bitwarden)
  385. (auth-source-forget-all-cached)
  386. (message "Bitwarden: auth-source enabled"))
  387. ;================================= widget utils ================================
  388. (defun bitwarden-list-next ()
  389. "Move to the next item."
  390. (interactive)
  391. (forward-line)
  392. (beginning-of-line)
  393. (widget-forward 1))
  394. (defun bitwarden-list-prev ()
  395. "Move to the previous item."
  396. (interactive)
  397. (widget-backward 2)
  398. (beginning-of-line)
  399. (widget-forward 1))
  400. ;; bitwarden-list-dialog-mode
  401. (defvar bitwarden-list-dialog-mode-map
  402. (let ((map (make-sparse-keymap)))
  403. (set-keymap-parent map widget-keymap)
  404. (define-key map "n" 'bitwarden-list-next)
  405. (define-key map "p" 'bitwarden-list-prev)
  406. (define-key map "q" 'bitwarden-list-cancel-dialog)
  407. map)
  408. "Keymap used in recentf dialogs.")
  409. (define-derived-mode bitwarden-list-dialog-mode nil "bitwarden-list-dialog"
  410. "Major mode of recentf dialogs.
  411. \\{bitwarden-list-dialog-mode-map}"
  412. :syntax-table nil
  413. :abbrev-table nil
  414. (setq truncate-lines t))
  415. (defsubst bitwarden-list-all-get-item-at-pos ()
  416. "Get hashtable from widget at current pos in dialog widget."
  417. (let ((widget (get-char-property (point) 'button)))
  418. (widget-value widget)))
  419. (defsubst bitwarden-list-all-make-spaces (spaces)
  420. "Create a string with SPACES number of whitespaces."
  421. (mapconcat 'identity (make-list spaces " ") ""))
  422. (defsubst bitwarden-pad-to-width (item width)
  423. "Create a string with ITEM padded to WIDTH."
  424. (if (= (length item) width)
  425. item
  426. (if (>= (length item) width)
  427. (concat (substring item 0 (- width 1)) "…")
  428. (concat item (bitwarden-list-all-make-spaces (- width (length item)))))))
  429. ;================================ widget actions ===============================
  430. ;; Dialog settings and actions
  431. (defun bitwarden-list-cancel-dialog (&rest _ignore)
  432. "Cancel the current dialog.
  433. IGNORE arguments."
  434. (interactive)
  435. (kill-buffer (current-buffer))
  436. (bitwarden--message "dialog canceled" nil t))
  437. (defun bitwarden-list-all-kill-ring-save (&optional widget-item)
  438. "Bitwarden `kill-ring-save', insert password to kill ring.
  439. If WIDGET-ITEM is not supplied then look for the widget at the
  440. current point."
  441. (interactive)
  442. (let* ((item (or widget-item
  443. (bitwarden-list-all-get-item-at-pos)))
  444. (type (gethash "type" item))
  445. (login (gethash "login" item)))
  446. (if (not (eq type 1))
  447. (bitwarden--message "error: not a login item" nil t)
  448. (kill-new (gethash "password" login))
  449. (message "Password added to kill ring"))))
  450. (defun bitwarden-list-all-item-action (widget &rest _ignore)
  451. "Do action to element associated with WIDGET's value.
  452. IGNORE other arguments."
  453. (bitwarden-list-all-kill-ring-save (widget-value widget))
  454. (kill-buffer (current-buffer)))
  455. ;=================================== widgets ===================================
  456. (defmacro bitwarden-list-dialog (name &rest forms)
  457. "Show a dialog buffer with NAME, setup with FORMS."
  458. (declare (indent 1) (debug t))
  459. `(with-current-buffer (get-buffer-create ,name)
  460. ;; Cleanup buffer
  461. (let ((inhibit-read-only t)
  462. (ol (overlay-lists)))
  463. (mapc 'delete-overlay (car ol))
  464. (mapc 'delete-overlay (cdr ol))
  465. (erase-buffer))
  466. (bitwarden-list-dialog-mode)
  467. ,@forms
  468. (widget-setup)
  469. (switch-to-buffer (current-buffer))))
  470. (defsubst bitwarden-list-all-make-element (item)
  471. "Create a new cons list from ITEM."
  472. (let* ((folder-id (gethash "folderId" item))
  473. (login-item (gethash "login" item)))
  474. (cons folder-id
  475. (list (cons (concat
  476. (bitwarden-pad-to-width (gethash "name" item) 40)
  477. (bitwarden-pad-to-width
  478. (if login-item (gethash "username" login-item) "")
  479. 32)
  480. (format-time-string
  481. "%Y-%m-%d %T"
  482. (date-to-time (bitwarden-pad-to-width
  483. (gethash "revisionDate" item) 24))))
  484. item)))))
  485. (defun bitwarden-list-all-tree (key val)
  486. "Return a `tree-widget' of folders.
  487. Creates a widget with text KEY and items VAL."
  488. ;; Represent a sub-menu with a tree widget
  489. `(tree-widget
  490. :open t
  491. :match ignore
  492. :node (item :tag ,key
  493. :sample-face bold
  494. :format "%{%t%}\n")
  495. ,@(mapcar 'bitwarden-list-all-item val)))
  496. (defun bitwarden-list-all-item (pass-element)
  497. "Return a widget to display PASS-ELEMENT in a dialog buffer."
  498. ;; Represent a single file with a link widget
  499. `(link :tag ,(car pass-element)
  500. :button-prefix ""
  501. :button-suffix ""
  502. :button-face default
  503. :format "%[%t\n%]"
  504. :help-echo ,(concat "Viewing item " (gethash "id" (cdr pass-element)))
  505. :action bitwarden-list-all-item-action
  506. ,(cdr pass-element)))
  507. (defun bitwarden-list-all-items (items)
  508. "Return a list of widgets to display ITEMS in a dialog buffer."
  509. (let* ((folders (mapcar (lambda (e)
  510. (cons
  511. (gethash "id" e)
  512. (gethash "name" e)))
  513. (bitwarden-folders)))
  514. (hash (make-hash-table :test 'equal)))
  515. ;; create hash table where the keys are the folders and each value is a list
  516. ;; of the password items
  517. (dolist (x (mapcar 'bitwarden-list-all-make-element items))
  518. (let* ((folder-id (car x))
  519. (key (cdr (assoc folder-id folders)))
  520. (val (cdr x))
  521. (klist (gethash key hash)))
  522. (puthash key (append klist val) hash)))
  523. (mapcar (lambda (key)
  524. (bitwarden-list-all-tree key (gethash key hash)))
  525. (sort (hash-table-keys hash) #'string<))))
  526. ;;;###autoload
  527. (defun bitwarden-list-all ()
  528. "Show a dialog, listing all entries associated with `bitwarden-user'.
  529. If optional argument GROUP is given, only entries in GROUP will be listed."
  530. (interactive)
  531. (if (bitwarden-unlocked-p)
  532. (bitwarden-list-dialog "*bitwarden-list*"
  533. ;; Use a L&F that looks like the recentf menu.
  534. (tree-widget-set-theme "folder")
  535. (apply 'widget-create
  536. `(group
  537. :indent 0
  538. :format "%v\n"
  539. ,@(bitwarden-list-all-items
  540. (bitwarden-search))))
  541. (widget-create
  542. 'push-button
  543. :notify 'bitwarden-list-cancel-dialog
  544. "Cancel")
  545. (goto-char (point-min)))
  546. (bitwarden--message "vault not unlocked!" nil t)))
  547. (provide 'bitwarden)
  548. ;;; bitwarden.el ends here