-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtotp-widget.el
91 lines (76 loc) · 2.97 KB
/
totp-widget.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
;;; totp-widget.el --- Time-based One-time Password (TOTP): Widget -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Jürgen Hötzel
;; Author: Jürgen Hötzel <juergen@hoetzel.info>
;; Version: 0.1
;; URL: https://github.com/juergenhoetzel/emacs-totp
;; Keywords: tools pass password
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Google Authenticator like widget
;;; Code:
(require 'totp)
(require 'widget)
(require 'wid-edit)
(defvar totp-widget-progress nil)
(defvar totp-widget-timer nil)
(defun totp-widget--elapsed ()
"Return pin code elapsed time."
(mod (truncate (time-to-seconds)) 30))
(defun totp-widget--cancel-timer ()
"Cancel widget redisplay timer."
(when totp-widget-timer
(cancel-timer totp-widget-timer)
(setf totp-widget-timer nil)))
;;;###autoload
(defun totp-widget ()
"Create a widget for `totp-accounts'."
(interactive)
(totp-widget--cancel-timer)
(when (called-interactively-p 'interactive)
(switch-to-buffer "*TOTP Accounts*"))
(with-current-buffer "*TOTP Accounts*"
(kill-all-local-variables)
(make-local-variable 'totp-repeat)
(let ((inhibit-read-only t))
(erase-buffer))
(remove-overlays)
(widget-insert "Current TOTP accounts:\n\n")
(widget-insert "[")
;; FIXME: This should be read_only
(setf totp-widget-progress
(widget-create 'editable-field
:size 30
:format "%v" ; Text after the field!
(make-string (totp-widget--elapsed) ?#)))
(widget-insert "]\n")
(dolist (account (totp-accounts))
(let ((secret (plist-get (totp--auth-info account) :secret)))
(when (functionp secret)
(setq secret (funcall secret)))
(widget-insert (format "\n%s: " account))
(widget-create 'push-button :notify (lambda (&rest _)
(totp-copy-pin-as-kill account)
(message "Code for %s copied to kill-ring" account))
(totp secret))))
(widget-insert "\n")
(use-local-map widget-keymap)
(widget-setup)
;; FIXME: Prevent race condition
(setf totp-widget-timer (run-with-timer 1 0.7 (lambda ()
(let ((elapsed (totp-widget--elapsed)))
(if (zerop elapsed)
(totp-widget)
(save-excursion
(widget-value-set totp-widget-progress (make-string elapsed ?#))))))))
(add-hook 'kill-buffer-hook #'totp-widget--cancel-timer nil t)))
(provide 'totp-widget)
;;; totp-widget.el ends here