A major mode for password-store
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.
 
 

682 lines
24 KiB

  1. ;;; pass.el --- Major mode for password-store.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2019 Nicolas Petton & Damien Cassou
  3. ;; Author: Nicolas Petton <petton.nicolas@gmail.com>
  4. ;; Damien Cassou <damien@cassou.me>
  5. ;; Version: 2.0
  6. ;; GIT: https://github.com/NicolasPetton/pass
  7. ;; Package-Requires: ((emacs "25") (password-store "2.1.0") (password-store-otp "0.1.5") (f "0.17"))
  8. ;; Created: 09 Jun 2015
  9. ;; Keywords: password-store, password, keychain
  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. ;; Major mode for password-store.el
  22. ;;; Code:
  23. (require 'password-store)
  24. (require 'imenu)
  25. (require 'button)
  26. (require 'f)
  27. (require 'subr-x)
  28. (defgroup pass '()
  29. "Major mode for password-store."
  30. :group 'password-store)
  31. (defcustom pass-show-keybindings t
  32. "Whether the keybindings should be displayed in the pass buffer."
  33. :group 'pass
  34. :type 'boolean)
  35. (defcustom pass-username-field "username"
  36. "Field name used in the files to indicate an username."
  37. :group 'pass
  38. :type 'string)
  39. (defvar pass-buffer-name "*Password-Store*"
  40. "Name of the pass buffer.")
  41. (defvar pass-mode-map
  42. (let ((map (make-sparse-keymap)))
  43. (define-key map (kbd "n") #'pass-next-entry)
  44. (define-key map (kbd "p") #'pass-prev-entry)
  45. (define-key map (kbd "M-n") #'pass-next-directory)
  46. (define-key map (kbd "M-p") #'pass-prev-directory)
  47. (define-key map (kbd "k") #'pass-kill)
  48. (define-key map (kbd "s") #'isearch-forward)
  49. (define-key map (kbd "?") #'describe-mode)
  50. (define-key map (kbd "g") #'pass-update-buffer)
  51. (define-key map (kbd "i") #'pass-insert)
  52. (define-key map (kbd "I") #'pass-insert-generated)
  53. (define-key map (kbd "j") #'pass-goto-entry)
  54. (define-key map (kbd "w") #'pass-copy)
  55. (define-key map (kbd "b") #'pass-copy-username)
  56. (define-key map (kbd "f") #'pass-copy-field)
  57. (define-key map (kbd "u") #'pass-copy-url)
  58. (define-key map (kbd "U") #'pass-browse-url)
  59. (define-key map (kbd "v") #'pass-view)
  60. (define-key map (kbd "r") #'pass-rename)
  61. (define-key map (kbd "o") #'pass-otp-options)
  62. (define-key map (kbd "RET") #'pass-view)
  63. (define-key map (kbd "q") #'pass-quit)
  64. map)
  65. "Keymap for `pass-mode'.")
  66. (defface pass-mode-header-face '((t . (:inherit font-lock-keyword-face)))
  67. "Face for displaying the header of the pass buffer."
  68. :group 'pass)
  69. (defface pass-mode-entry-face '((t . ()))
  70. "Face for displaying pass entry names."
  71. :group 'pass)
  72. (defface pass-mode-directory-face '((t . (:inherit
  73. font-lock-function-name-face
  74. :weight
  75. bold)))
  76. "Face for displaying password-store directory names."
  77. :group 'pass)
  78. (define-derived-mode pass-mode fundamental-mode "Password-Store"
  79. "Major mode for editing password-stores.
  80. \\{pass-mode-map}"
  81. (setq default-directory (password-store-dir))
  82. (setq imenu-prev-index-position-function
  83. 'pass--imenu-prev-index-position-function)
  84. (setq imenu-extract-index-name-function
  85. 'pass--imenu-extract-index-name-function)
  86. (read-only-mode))
  87. (defun pass--imenu-prev-index-position-function ()
  88. "Move point to previous line in current buffer.
  89. This function is used as a value for
  90. `imenu-prev-index-position-function'."
  91. (pass-prev-entry)
  92. (not (bobp)))
  93. (defun pass--imenu-extract-index-name-function ()
  94. "Return imenu name for pass entry at point.
  95. This function is used as a value for
  96. `imenu-extract-index-name-function'."
  97. (pass-entry-at-point))
  98. (defun pass-setup-buffer ()
  99. "Setup the password-store buffer."
  100. (pass-mode)
  101. (pass-update-buffer))
  102. ;;;###autoload
  103. (defun pass ()
  104. "Open the password-store buffer."
  105. (interactive)
  106. (if (get-buffer pass-buffer-name)
  107. (progn
  108. (switch-to-buffer pass-buffer-name)
  109. (pass-update-buffer))
  110. (let ((buf (get-buffer-create pass-buffer-name)))
  111. (pop-to-buffer buf)
  112. (pass-setup-buffer))))
  113. (defmacro pass--with-writable-buffer (&rest body)
  114. "Evaluate BODY as if the current buffer was not in `read-only-mode'."
  115. (declare (indent 0) (debug t))
  116. `(let ((inhibit-read-only t))
  117. ,@body))
  118. (defmacro pass--save-point (&rest body)
  119. "Evaluate BODY and restore the point.
  120. Similar to `save-excursion' but only restore the point."
  121. (declare (indent 0) (debug t))
  122. (let ((point (make-symbol "point")))
  123. `(let ((,point (point)))
  124. ,@body
  125. (goto-char (min ,point (point-max))))))
  126. (defun pass-quit ()
  127. "Kill the buffer quitting the window."
  128. (interactive)
  129. (when (y-or-n-p "Kill all pass entry buffers? ")
  130. (dolist (buf (buffer-list))
  131. (with-current-buffer buf
  132. (when (eq major-mode 'pass-view-mode)
  133. (kill-buffer buf)))))
  134. (quit-window t))
  135. (defun pass-next-entry ()
  136. "Move point to the next entry found."
  137. (interactive)
  138. (pass--goto-next #'pass-entry-at-point))
  139. (defun pass-prev-entry ()
  140. "Move point to the previous entry."
  141. (interactive)
  142. (pass--goto-prev #'pass-entry-at-point))
  143. (defun pass--target-entry-pos (target)
  144. "Return position of TARGET entry or nil if it doesn't exist."
  145. (save-excursion
  146. (goto-char (point-min))
  147. (pass--goto-next (lambda () (equal (pass-entry-at-point) target)))
  148. (unless (eobp)
  149. (point))))
  150. (defun pass-goto-entry (entry)
  151. "Move point to ENTRY and return its position.
  152. If ENTRY doesn't exist in the buffer, then preserve point and return nil.
  153. Note that ENTRY must reflect its deep level in the directory structure;
  154. for instance, an entry `bar' inside the subdirectory `foo', must be
  155. specified as `foo/bar'.
  156. When called interactively, prompt users with completion using
  157. all entries in the pass buffer."
  158. (interactive
  159. (list (completing-read "Jump to entry: "
  160. (password-store-list) nil 'match)))
  161. (let ((entry-pos (pass--target-entry-pos entry)))
  162. (if entry-pos
  163. (goto-char entry-pos)
  164. (message "No entry matches %s in current directory" entry)
  165. nil)))
  166. (defun pass-next-directory ()
  167. "Move point to the next directory found."
  168. (interactive)
  169. (pass--goto-next #'pass-directory-at-point))
  170. (defun pass-prev-directory ()
  171. "Move point to the previous directory."
  172. (interactive)
  173. (pass--goto-prev #'pass-directory-at-point))
  174. (defmacro pass--with-closest-entry (varname &rest body)
  175. "Bound VARNAME to the closest entry before point and evaluate BODY."
  176. (declare (indent 1) (debug t))
  177. `(let ((,varname (pass-closest-entry)))
  178. (if ,varname
  179. (progn ,@body)
  180. (message "No entry at point"))))
  181. (defun pass-rename (new-name)
  182. "Rename the entry at point to NEW-NAME."
  183. (interactive (list (read-string "Rename entry to: " (pass-closest-entry))))
  184. (pass--with-closest-entry entry
  185. (password-store-rename entry new-name)
  186. (pass-update-buffer)))
  187. (defun pass-kill ()
  188. "Remove the entry at point."
  189. (interactive)
  190. (pass--with-closest-entry entry
  191. (when (yes-or-no-p (format "Do you want remove the entry %s? " entry))
  192. (password-store-remove entry)
  193. (pass-update-buffer))))
  194. (defun pass-update-buffer ()
  195. "Update the current buffer contents."
  196. (interactive)
  197. (pass--save-point
  198. (pass--with-writable-buffer
  199. (delete-region (point-min) (point-max))
  200. (pass-display-data))))
  201. (defun pass-insert (arg)
  202. "Insert an entry to the password-store.
  203. The password is read from user input.
  204. When called with a prefix argument ARG, visit the entry file."
  205. (interactive "P")
  206. (if arg
  207. (pass-insert-multiline)
  208. (progn
  209. (call-interactively #'password-store-insert)
  210. (pass-update-buffer))))
  211. (defun pass-insert-multiline ()
  212. "This function behaves similarly to `pass -m'.
  213. It creates an empty entry file, and visit it."
  214. (let ((entry (format "%s.gpg" (read-string "Password entry: ")))
  215. (default-directory (password-store-dir)))
  216. (make-directory (file-name-directory entry) t)
  217. (find-file (expand-file-name entry (password-store-dir)))))
  218. (defun pass-insert-generated ()
  219. "Insert an entry to the password-store.
  220. Use a generated password instead of reading the password from
  221. user input."
  222. (interactive)
  223. (call-interactively #'password-store-generate)
  224. (pass-update-buffer))
  225. (defun pass-view ()
  226. "Visit the entry at point."
  227. (interactive)
  228. (pass--with-closest-entry entry
  229. (find-file (concat (f-join (password-store-dir) entry) ".gpg"))))
  230. (defun pass-copy ()
  231. "Add the password of entry at point to kill ring."
  232. (interactive)
  233. (pass--with-closest-entry entry
  234. (password-store-copy entry)))
  235. (defun pass--copy-field (field)
  236. "Add FIELD of entry at point to kill ring."
  237. (pass--with-closest-entry entry
  238. (let* ((inhibit-message t)
  239. (parsed-entries (password-store-parse-entry entry)))
  240. (unless (assoc field parsed-entries)
  241. (user-error "Field `%s' not in %s" field entry))
  242. (password-store-copy-field entry field))))
  243. (defun pass-copy-field (field)
  244. "Add FIELD of entry at point to kill ring.
  245. When called interactively, prompt users for field with completion
  246. using all fields in the entry."
  247. (interactive
  248. (pass--with-closest-entry entry
  249. (list (password-store-read-field entry))))
  250. (if (equal field "secret") (pass-copy)
  251. (pass--copy-field field)))
  252. (defun pass-copy-username ()
  253. "Add username of entry at point to kill ring."
  254. (interactive)
  255. (pass--copy-field pass-username-field))
  256. (defun pass-copy-url ()
  257. "Add url of entry at point to kill ring."
  258. (interactive)
  259. (pass--copy-field password-store-url-field))
  260. (defun pass-display-data ()
  261. "Display the password-store data into the current buffer."
  262. (let ((items (pass--tree)))
  263. (pass-display-header)
  264. (pass-display-item items)))
  265. (defun pass-display-header ()
  266. "Display the header in to the current buffer."
  267. (insert "Password-store directory:")
  268. (put-text-property (point-at-bol) (point) 'face 'pass-mode-header-face)
  269. (insert " ")
  270. (pass--display-keybindings-toggle)
  271. (insert "\n\n")
  272. (when pass-show-keybindings
  273. (pass--display-keybindings '((pass-copy . "Copy password")
  274. (pass-copy-username . "Copy username")
  275. (pass-copy-url . "Copy url")))
  276. (insert "\n")
  277. (pass--display-keybindings '((pass-copy-field . "Copy field")
  278. (pass-goto-entry . "Jump to Entry")
  279. (pass-browse-url . "Browse url")))
  280. (insert "\n")
  281. (pass--display-keybindings '((pass-insert . "Insert")
  282. (pass-next-entry . "Next")
  283. (pass-update-buffer . "Update")))
  284. (insert "\n")
  285. (pass--display-keybindings '((pass-insert-generated . "Generate")
  286. (pass-prev-entry . "Previous")
  287. (pass-otp-options . "OTP Support")))
  288. (insert "\n")
  289. (pass--display-keybindings '((pass-rename . "Rename")
  290. (pass-next-directory . "Next dir")
  291. (pass-view . "View entry")))
  292. (insert "\n")
  293. (pass--display-keybindings '((pass-kill . "Delete")
  294. (pass-prev-directory . "Previous dir")
  295. (describe-mode . "Help")))
  296. (newline)
  297. (newline)))
  298. (defun pass--display-keybindings-toggle ()
  299. "Display a button to toggle whether keybindings should be displayed."
  300. (let ((label (if pass-show-keybindings
  301. "[Hide keybindings]"
  302. "[Show keybindings]")))
  303. (insert-button label 'action #'pass--toggle-display-keybindings)))
  304. (defun pass--toggle-display-keybindings (&rest _)
  305. "Toggle displaying the keybindings and update the buffer."
  306. (setq pass-show-keybindings (not pass-show-keybindings))
  307. (put pass-show-keybindings
  308. 'customized-value
  309. (list (custom-quote (symbol-value pass-show-keybindings))))
  310. (pass-update-buffer))
  311. (defun pass--display-keybindings (bindings)
  312. "Display the keybinding in each item of BINDINGS.
  313. BINDINGS is an alist of bindings."
  314. (mapc (lambda (pair)
  315. (pass--display-keybinding (car pair) (cdr pair)))
  316. bindings))
  317. (defun pass--display-keybinding (command label)
  318. "Insert the associated keybinding for COMMAND with LABEL."
  319. (insert (format "%8s %-13s \t "
  320. (format "%s"
  321. (propertize (substitute-command-keys
  322. (format "<\\[%s]>" (symbol-name command)))
  323. 'face 'font-lock-constant-face))
  324. label)))
  325. (defun pass-display-item (item &optional indent-level)
  326. "Display the directory or entry ITEM into the current buffer.
  327. If INDENT-LEVEL is specified, add enough spaces before displaying
  328. ITEM."
  329. (unless indent-level (setq indent-level 0))
  330. (let ((directory (listp item)))
  331. (pass-display-item-prefix indent-level)
  332. (if directory
  333. (pass-display-directory item indent-level)
  334. (pass-display-entry item))))
  335. (defun pass-display-entry (entry)
  336. "Display the password-store entry ENTRY into the current buffer."
  337. (let ((entry-name (f-filename entry)))
  338. (insert entry-name)
  339. (add-text-properties (point-at-bol) (point)
  340. `(face pass-mode-entry-face pass-entry ,entry))
  341. (newline)))
  342. (defun pass-display-directory (directory indent-level)
  343. "Display the directory DIRECTORY into the current buffer.
  344. DIRECTORY is a list, its CAR being the name of the directory and its CDR
  345. the entries of the directory. Add enough spaces so that each entry is
  346. indented according to INDENT-LEVEL."
  347. (let ((name (car directory))
  348. (items (cdr directory)))
  349. (insert name)
  350. (add-text-properties (point-at-bol) (point)
  351. `(face pass-mode-directory-face pass-directory ,name))
  352. (newline)
  353. (dolist (item items)
  354. (pass-display-item item (1+ indent-level)))))
  355. (defun pass-display-item-prefix (indent-level)
  356. "Display some indenting text according to INDENT-LEVEL."
  357. (dotimes (_ (max 0 (* (1- indent-level) 4)))
  358. (insert " "))
  359. (unless (zerop indent-level)
  360. (insert "├── ")))
  361. (defun pass-entry-at-point ()
  362. "Return the `pass-entry' property at point."
  363. (get-text-property (point-at-eol) 'pass-entry))
  364. (defun pass-directory-at-point ()
  365. "Return the `pass-directory' property at point."
  366. (get-text-property (point) 'pass-directory))
  367. (defun pass-closest-entry ()
  368. "Return the closest entry in the current buffer, looking backward."
  369. (save-excursion
  370. (or (pass-entry-at-point)
  371. (unless (bolp)
  372. (forward-line -1)
  373. (pass-closest-entry)))))
  374. (defun pass-otp-options (option)
  375. "Dispatch otp actions depending on user OPTION input.
  376. Display help message with OTP functionality options."
  377. (interactive
  378. (list
  379. (read-char-choice "[i] Insert, [a] Append, [s] Append from screenshot, [o] Copy token, [u] Copy URI, or [C-g] to abort: "
  380. '(?i ?a ?s ?o ?u))))
  381. (unless (require 'password-store-otp nil t)
  382. (user-error "You cannot do this without installing `password-store-otp' first"))
  383. (pcase option
  384. (?i (pass-otp-insert))
  385. (?a (pass-otp-append))
  386. (?s (pass-otp-from-screenshot))
  387. (?o (pass-otp-token-copy))
  388. (?u (pass-otp-uri-copy))))
  389. (defun pass-otp-token-copy ()
  390. "Add OTP Token from closest entry to kill ring."
  391. (interactive)
  392. (pass--with-closest-entry entry
  393. (password-store-otp-token-copy entry)))
  394. (defun pass-otp-uri-copy ()
  395. "Add OTP URI from closest entry to kill ring."
  396. (interactive)
  397. (pass--with-closest-entry entry
  398. (password-store-otp-uri-copy entry)))
  399. (defun pass-otp-insert ()
  400. "Insert an OTP URI entry to the password-store.
  401. The password is read from user input."
  402. (interactive)
  403. (call-interactively #'password-store-otp-insert)
  404. (pass-update-buffer))
  405. (defun pass-otp-append ()
  406. "Append an OTP URI to an existing entry in the password-store.
  407. The password is read from user input."
  408. (interactive)
  409. (pass--with-closest-entry entry
  410. (password-store-otp-append entry (read-passwd "OTP URI: " t)))
  411. (pass-update-buffer))
  412. (defun pass-otp-from-screenshot ()
  413. "Append an OTP URI taken from a screenshot to an existing entry in the password-store."
  414. (interactive)
  415. (pass--with-closest-entry entry
  416. (password-store-otp-append-from-image entry))
  417. (pass-update-buffer))
  418. (defun pass--goto-next (pred)
  419. "Move point to the next match of PRED."
  420. (forward-line)
  421. (while (not (or (eobp) (funcall pred)))
  422. (forward-line)))
  423. (defun pass--goto-prev (pred)
  424. "Move point to the previous match of PRED."
  425. (forward-line -1)
  426. (while (not (or (bobp) (funcall pred)))
  427. (forward-line -1)))
  428. (defun pass--tree (&optional subdir)
  429. "Return a tree of all entries in SUBDIR.
  430. If SUBDIR is nil, return the entries of `(password-store-dir)'."
  431. (unless subdir (setq subdir ""))
  432. (let ((path (f-join (password-store-dir) subdir)))
  433. (if (f-directory? path)
  434. (unless (string= (f-filename subdir) ".git")
  435. (cons (f-filename path)
  436. (delq nil
  437. (mapcar 'pass--tree
  438. (f-entries path)))))
  439. (when (and (equal (f-ext path) "gpg")
  440. (not (backup-file-name-p path)))
  441. (password-store--file-to-entry path)))))
  442. ;;; major mode for viewing entries
  443. (defvar pass-view-mask "·············"
  444. "Mask used to hide passwords.")
  445. (defvar pass-view-mode-map
  446. (let ((map (make-sparse-keymap)))
  447. (define-key map (kbd "C-c C-c") #'pass-view-toggle-password)
  448. (define-key map (kbd "C-c C-w") #'pass-view-copy-password)
  449. map))
  450. (make-variable-buffer-local 'pass-view-mode-map)
  451. (defun pass-view-entry-name (&optional buffer)
  452. "Return the entry name for BUFFER.
  453. This function only works when `pass-view-mode' is enabled."
  454. (with-current-buffer (or buffer (current-buffer))
  455. (when (eq major-mode 'pass-view-mode)
  456. (f-no-ext (replace-regexp-in-string
  457. (format "^%s/" (f-expand (password-store-dir)))
  458. ""
  459. buffer-file-name)))))
  460. (defun pass-view-toggle-password ()
  461. "Enable or disable password hiding."
  462. (interactive)
  463. (save-excursion
  464. (goto-char (point-min))
  465. (let ((buf-modified (buffer-modified-p)))
  466. (if (string= (get-text-property (point) 'display)
  467. pass-view-mask)
  468. (pass-view-unmask-password)
  469. (pass-view-mask-password))
  470. (set-buffer-modified-p buf-modified))))
  471. (defun pass-view-copy-password ()
  472. "Copy the password of the entry in the current buffer."
  473. (interactive)
  474. (save-excursion
  475. (goto-char (point-min))
  476. (copy-region-as-kill (point) (line-end-position))
  477. (message "Password copied to kill ring.")))
  478. (defun pass-view-mask-password ()
  479. "Mask the password of the current buffer."
  480. (let ((inhibit-read-only t))
  481. (save-excursion
  482. (goto-char (point-min))
  483. (set-text-properties (point-min) (line-end-position)
  484. `(display ,pass-view-mask)))))
  485. (defun pass-view-unmask-password ()
  486. "Show the password in the current buffer."
  487. (save-excursion
  488. (goto-char (point-min))
  489. (remove-text-properties (point-min) (line-end-position)
  490. '(display nil))))
  491. (defun pass-view-copy-token ()
  492. "Copy current `pass-view' buffer's OTP token into clipboard."
  493. (interactive)
  494. (when-let (entry-name (pass-view-entry-name))
  495. (password-store-otp-token-copy entry-name)))
  496. (defun pass-view-qrcode ()
  497. "Open a new buffer that displays a QR Code for the current entry."
  498. (interactive)
  499. (when-let (entry-name (pass-view-entry-name))
  500. (let ((qrcode-buffer (get-buffer-create "*pass-view-qrcode*")))
  501. (with-current-buffer qrcode-buffer
  502. (fundamental-mode) ;; Return buffer *back* to fundamental, in case it isn't already.
  503. (erase-buffer)
  504. (insert (password-store-otp-qrcode entry-name "SVG"))
  505. (image-mode)
  506. (local-set-key (kbd "q") 'kill-this-buffer))
  507. (switch-to-buffer-other-window qrcode-buffer))))
  508. (defun pass-view--otp-remaining-secs ()
  509. "Return a string with the remaining countdown base 30."
  510. (let* ((base 30)
  511. (remaining (- base (% (truncate (time-to-seconds (current-time)))
  512. base)))
  513. (remaining-str (number-to-string remaining)))
  514. (if (< remaining 10) ;; leftpad-ing
  515. (concat "0" remaining-str)
  516. remaining-str)))
  517. (defun pass-view--set-otp-header (token remaining-secs)
  518. "Display OTP TOKEN and REMAINING-SECS in Header Line."
  519. (let ((otp-data (concat (propertize " " 'display '((space :align-to 0)))
  520. (propertize "OTP: " 'face 'pass-mode-header-face)
  521. token " - " remaining-secs "s remaining"))
  522. (key-binding (concat (propertize (substitute-command-keys
  523. (format "<\\[%s]>" "pass-view-copy-token"))
  524. 'face 'font-lock-constant-face)
  525. " Copy token")))
  526. (setq header-line-format (concat otp-data " " key-binding))
  527. (force-mode-line-update)))
  528. (defun pass-view--has-otp-p ()
  529. "Return t-ish value if there's an OTP URI in the current buffer.
  530. nil otherwise."
  531. (save-excursion
  532. (goto-char (point-min))
  533. (search-forward "otpauth://" nil t)))
  534. (defun pass-view--otp-counter (buffer &optional last-token force-create)
  535. "Reload BUFFER's OTP token and countdown, using LAST-TOKEN if any, and if FORCE-CREATE, build Header Line from scratch."
  536. (when (buffer-live-p buffer)
  537. (with-current-buffer buffer
  538. (when (or header-line-format force-create)
  539. (let* ((remaining-secs (pass-view--otp-remaining-secs))
  540. (token (if (or (not last-token)
  541. (string= remaining-secs "30"))
  542. (password-store-otp-token (pass-view-entry-name buffer))
  543. last-token)))
  544. (pass-view--set-otp-header token remaining-secs)
  545. (run-at-time 1 nil #'pass-view--otp-counter buffer token))))))
  546. (defun pass-view--prepare-otp ()
  547. "Start an OTP token/remaining secs counter in current buffer.
  548. This function also binds a couple of handy OTP related key-bindings to
  549. `pass-mode-map'."
  550. (when (and (require 'password-store-otp nil t)
  551. (pass-view--has-otp-p))
  552. ;; Build OTP counter
  553. (pass-view--otp-counter (current-buffer) nil t)
  554. ;; Rebuild header after saving.
  555. (add-hook 'after-save-hook
  556. #'(lambda ()
  557. (if (pass-view--has-otp-p)
  558. (pass-view--otp-counter (current-buffer) nil t)
  559. ;; Remove header line
  560. (setq header-line-format nil)))
  561. t t)
  562. ;; Define a couple of OTP helper shortcuts
  563. (define-key pass-view-mode-map (kbd "C-c C-o") #'pass-view-copy-token)
  564. (define-key pass-view-mode-map (kbd "C-c C-q") #'pass-view-qrcode)))
  565. (defun pass-browse-url ()
  566. "Browse URL in entry at point."
  567. (interactive)
  568. (pass--with-closest-entry entry
  569. (password-store-url entry)))
  570. (defvar pass-view-font-lock-keywords '("^[^\s\n]+:" . 'font-lock-keyword-face)
  571. "Font lock keywords for ‘pass-view-mode’.")
  572. (define-derived-mode pass-view-mode nil "Pass-View"
  573. "Major mode for viewing password-store entries.
  574. \\{pass-view-mode-map}"
  575. (pass-view-toggle-password)
  576. (pass-view--prepare-otp)
  577. (setq-local font-lock-defaults '(pass-view-font-lock-keywords t))
  578. (font-lock-mode 1)
  579. (message
  580. (substitute-command-keys
  581. "Press <\\[pass-view-toggle-password]> to display & edit the password")))
  582. (add-to-list 'auto-mode-alist
  583. (cons
  584. (format "%s/.*\\.gpg\\'"
  585. (expand-file-name (password-store-dir)))
  586. 'pass-view-mode))
  587. (provide 'pass)
  588. ;;; pass.el ends here