emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/base32 94f41a945e 028/103: Implement auto-copying of TOTP


From: ELPA Syncer
Subject: [nongnu] elpa/base32 94f41a945e 028/103: Implement auto-copying of TOTP tokens
Date: Sat, 10 Feb 2024 13:00:02 -0500 (EST)

branch: elpa/base32
commit 94f41a945ee5d21b2b18c3fd8015b58800efdff8
Author: Vivek Das Mohapatra <vivek@collabora.co.uk>
Commit: Vivek Das Mohapatra <vivek@collabora.co.uk>

    Implement auto-copying of TOTP tokens
    
    Allow user-configurable paste buffers (see totp-auto-copy-password)
    which will be automatically and continuously updated to hold the
    current value of the last interactively generated TOTP token until
    the TOTP buffer is closed or the user copies some other content manually.
---
 totp.el | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/totp.el b/totp.el
index bf037b7b2a..c7ec9098de 100644
--- a/totp.el
+++ b/totp.el
@@ -66,11 +66,28 @@ a secret if you ever re-import it."
   :group 'totp
   :type  'boolean)
 
+(defcustom totp-auto-copy-password nil
+  "If set then ‘totp’ and other interactive functions that generate
+TOTP tokens will automatically copy the generated token into the selected
+copy/paste backends, ready for pasting (eg into other applications or buffers).
+The behaviour is as follows:
+ - When the token is generated, it is placed in the selected copy areas
+ - If the copy area still contains the previous value when the token
+   expires and is regenerated, it is replaced with the new value."
+  :group 'totp
+  :type '(choice
+          (const :tag "Off" nil)
+          (set :tag "Choose Copy Method(s)"
+           (const :tag "Primary (middle-click etc)"  PRIMARY)
+           (const :tag "Clipboard (Paste, c-y, V-v)" CLIPBOARD)
+           (const :tag "Secondary"                   SECONDARY))))
+
 (defcustom totp-auth-sources nil
   "Serves the same purpose as ‘auth-sources’, but for the TOTP
 package. If unset (the default) this will be initialised to a list
 consisting of the contents of ‘auth-sources’ with the freedesktop
 secrets service login session prepended to it, if it is available."
+  :group 'totp
   :type `(repeat :tag "Authentication Sources"
                  (choice
                   (string :tag "Just a file")
@@ -481,6 +498,18 @@ and EXPIRY is the seconds after the epoch when the TOTP 
expires."
 (defvar totp-display-label  nil)
 (defvar totp-display-expiry nil)
 (defvar totp-display-secret nil)
+(defvar totp-display-oldpwd nil)
+
+(defun totp-update-paste-buffers (old new)
+  "For each copy/paste buffer selected by ‘totp-auto-copy-password’
+update the contents to password NEW (if it contains password OLD,
+or if OLD is unset)."
+  ;;(message "totp-update-paste-buffers %S (%S)" old new 
totp-auto-copy-password)
+  (mapc (lambda (type &optional ok)
+          (with-demoted-errors "gui get/set selection error: %S"
+            (setq ok (if old (equal old (gui-get-selection type)) t))
+            (if ok (gui-set-selection type (or new "")))))
+        totp-auto-copy-password))
 
 (defun totp-cancel-this-timer ()
   "Cancel the timer whose callback this is called from."
@@ -518,6 +547,10 @@ and EXPIRY is the seconds after the epoch when the TOTP 
expires."
                   token               (nth 0 otp)
                   totp-display-ttl    (nth 1 otp)
                   totp-display-expiry (nth 2 otp)))
+        ;; update the copy/paste buffers if necessary:
+        (totp-update-paste-buffers totp-display-oldpwd token)
+        (setq totp-display-oldpwd token)
+        ;; display the current token
         (insert (format "TOTP %s [%02ds]: %s\n"
                         totp-display-label totp-display-ttl token)))
     (totp-cancel-this-timer)))
@@ -531,9 +564,11 @@ and EXPIRY is the seconds after the epoch when the TOTP 
expires."
     (mapc 'make-local-variable '(totp-display-ttl
                                  totp-display-label
                                  totp-display-expiry
+                                 totp-display-oldpwd
                                  totp-display-secret))
     (setq totp-display-label  label
           totp-display-secret (cdr (assq :secret secret))
+          totp-display-oldpwd nil
           totp-display-ttl    nil
           totp-display-expiry nil)
     (pop-to-buffer ui-buffer)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]