]> git.eshelyaron.com Git - emacs.git/commitdiff
Add `auth-info-password' and use it instead of ad hoc code
authorSam Steingold <sds@gnu.org>
Thu, 6 Jan 2022 19:42:10 +0000 (14:42 -0500)
committerSam Steingold <sds@gnu.org>
Fri, 7 Jan 2022 19:48:59 +0000 (14:48 -0500)
* lisp/auth-source.el (auth-info-password):
  Extract from `auth-source-pick-first-password'.
(auth-source-pick-first-password, auth-source-secrets-create)
(auth-source-user-and-password): Use `auth-info-password'.
* lisp/erc/erc-services.el (erc-nickserv-get-password):
  Use `auth-source-pick-first-password'.
* lisp/erc/erc.el (erc-open, erc-server-join-channel): Likewise.
* lisp/gnus/mail-source.el (mail-source-set-1): Add a comment.
* lisp/gnus/nnimap.el (nnimap-credentials): Use `auth-info-password'.
* lisp/gnus/nntp.el (nntp-send-authinfo): Likewise.
* lisp/mail/rmail.el (rmail-get-remote-password): Likewise.
* lisp/mail/smtpmail.el (smtpmail-try-auth-methods): Likewise.
* lisp/net/sieve-manage.el (sieve-sasl-auth): Likewise.
* lisp/net/tramp.el (tramp-read-passwd): Likewise.
* lisp/net/rcirc.el (rcirc): Likewise (fixes a bug: the possibility
  that password might be a function was not handled).

lisp/auth-source.el
lisp/erc/erc-services.el
lisp/erc/erc.el
lisp/gnus/mail-source.el
lisp/gnus/nnimap.el
lisp/gnus/nntp.el
lisp/mail/rmail.el
lisp/mail/smtpmail.el
lisp/net/rcirc.el
lisp/net/sieve-manage.el
lisp/net/tramp.el

index 80c220561a6607e9d00f463295ab57863f2642ba..046a685d744cd5161ca309b16e6d7e3b2f6b159e 100644 (file)
@@ -853,15 +853,17 @@ while \(:host t) would find all host entries."
               (cl-return 'no)))
           'no))))
 
-(defun auth-source-pick-first-password (&rest spec)
-  "Pick the first secret found from applying SPEC to `auth-source-search'."
-  (let* ((result (nth 0 (apply #'auth-source-search (plist-put spec :max 1))))
-         (secret (plist-get result :secret)))
-
+(defun auth-info-password (auth-info)
+  "Return the :secret password from the AUTH-INFO."
+  (let ((secret (plist-get auth-info :secret)))
     (if (functionp secret)
         (funcall secret)
       secret)))
 
+(defun auth-source-pick-first-password (&rest spec)
+  "Pick the first secret found from applying SPEC to `auth-source-search'."
+  (auth-info-password (car (apply #'auth-source-search (plist-put spec :max 1)))))
+
 (defun auth-source-format-prompt (prompt alist)
   "Format PROMPT using %x (for any character x) specifiers in ALIST.
 Remove trailing \": \"."
@@ -1800,10 +1802,9 @@ authentication tokens:
       (plist-put
        artificial
        :save-function
-       (let* ((collection collection)
-              (item (plist-get artificial :label))
-              (secret (plist-get artificial :secret))
-              (secret (if (functionp secret) (funcall secret) secret)))
+       (let ((collection collection)
+             (item (plist-get artificial :label))
+             (secret (auth-info-password artificial)))
          (lambda ()
           (auth-source-secrets-saver collection item secret args)))))
 
@@ -2410,9 +2411,7 @@ MODE can be \"login\" or \"password\"."
                         :require '(:user :secret)
                         :create nil))))
          (user (plist-get auth-info :user))
-         (password (plist-get auth-info :secret)))
-    (when (functionp password)
-      (setq password (funcall password)))
+         (password (auth-info-password auth-info)))
     (list user password auth-info)))
 
 ;;; Tiny mode for editing .netrc/.authinfo modes (that basically just
index dcd786411f2209411acdb4ae24f83a66b22742e2..4b3ca7d23f761b8ad853309c66faf70806dabfb4 100644 (file)
@@ -444,15 +444,12 @@ it returns nil."
                           (cl-second (assoc network
                                             erc-nickserv-passwords)))))
             (when erc-use-auth-source-for-nickserv-password
-              (let ((secret (cl-first (auth-source-search
-                                       :max 1 :require '(:secret)
-                                       :host server
-                                       ;; Ensure a string for :port
-                                       :port (format "%s" port)
-                                       :user nick))))
-                (when secret
-                  (let ((passwd (plist-get secret :secret)))
-                    (if (functionp passwd) (funcall passwd) passwd)))))
+              (auth-source-pick-first-password
+               :require '(:secret)
+               :host server
+               ;; Ensure a string for :port
+               :port (format "%s" port)
+               :user nick))
             (when erc-prompt-for-nickserv-password
               (read-passwd
                (format "NickServ password for %s on %s (RET to cancel): "
index 24f476257116d94fb29da31a0c0beb6e041a0ab8..5faeda9a132304064f5f5dd883d5794df9cfdf12 100644 (file)
@@ -2062,19 +2062,12 @@ Returns the buffer for the given server or channel."
     ;; password stuff
     (setq erc-session-password
           (or passwd
-              (let ((secret
-                     (plist-get
-                      (nth 0
-                           (auth-source-search :host server
-                                               :max 1
-                                               :user nick
-                                               ;; secrets.el wouldn’t accept a number
-                                               :port (if (numberp port) (number-to-string port) port)
-                                               :require '(:secret)))
-                      :secret)))
-                (if (functionp secret)
-                    (funcall secret)
-                  secret))))
+              (auth-source-pick-first-password
+               :host server
+               :user nick
+               ;; secrets.el wouldn’t accept a number
+               :port (if (numberp port) (number-to-string port) port)
+               :require '(:secret))))
     ;; client certificate (only useful if connecting over TLS)
     (setq erc-session-client-certificate client-certificate)
     ;; debug output buffer
@@ -3187,16 +3180,12 @@ For a list of user commands (/join /part, ...):
 (put 'erc-cmd-HELP 'process-not-needed t)
 
 (defun erc-server-join-channel (server channel &optional secret)
-  (let* ((secret (or secret
-                    (plist-get (nth 0 (auth-source-search
-                                       :max 1
-                                       :host server
-                                       :port "irc"
-                                       :user channel))
-                               :secret)))
-        (password (if (functionp secret)
-                      (funcall secret)
-                    secret)))
+  (let ((password
+         (or secret
+             (auth-source-pick-first-password
+             :host server
+             :port "irc"
+             :user channel))))
     (erc-log (format "cmd: JOIN: %s" channel))
     (erc-server-send (concat "JOIN " channel
                             (if password
index d2f5b9a97e2e48d20282402b5038519c37fd75dd..9a48f710e5594eba1818a2f9b0329a0c61498c66 100644 (file)
@@ -454,7 +454,7 @@ the `mail-source-keyword-map' variable."
                                                   search))))
                     :user)))
              user-auth)
-            ((and
+            ((and               ; cf. 'auth-source-pick-first-password'
               (eq keyword :password)
               (setq pass-auth
                     (plist-get
index ab4243e867f339148620a0946b7d82e6563e1641..090cb9b245b6a0c3d883c3b6eeb9a71625b4494f 100644 (file)
@@ -40,6 +40,7 @@
 
 (autoload 'auth-source-forget+ "auth-source")
 (autoload 'auth-source-search "auth-source")
+(autoload 'auth-info-password "auth-source")
 
 (nnoo-declare nnimap)
 
@@ -407,10 +408,7 @@ during splitting, which may be slow."
                                            :create t))))
     (if found
         (list (plist-get found :user)
-             (let ((secret (plist-get found :secret)))
-               (if (functionp secret)
-                   (funcall secret)
-                 secret))
+             (auth-info-password found)
              (plist-get found :save-function))
       nil)))
 
index 990bb4426f48c4cc1564a6595517d1008de3ad76..624c64d4d754cb016c665a3fd9ef081ba0b60592 100644 (file)
@@ -36,6 +36,7 @@
 (eval-when-compile (require 'cl-lib))
 
 (autoload 'auth-source-search "auth-source")
+(autoload 'auth-info-password "auth-source")
 
 (defgroup nntp nil
   "NNTP access for Gnus."
@@ -1175,10 +1176,7 @@ If SEND-IF-FORCE, only send authinfo to the server if the
                          "563" "nntps" "snews"))))
          (auth-user (plist-get auth-info :user))
          (auth-force (plist-get auth-info :force))
-         (auth-passwd (plist-get auth-info :secret))
-         (auth-passwd (if (functionp auth-passwd)
-                          (funcall auth-passwd)
-                        auth-passwd))
+         (auth-passwd (auth-info-password auth-info))
         (force (or (netrc-get alist "force")
                     nntp-authinfo-force
                     auth-force))
index 55921eca685726d4cabda7bfd68f938fbc359f12..3795377cd2b1cd4bb1f9b464d3c187357cadde2c 100644 (file)
@@ -4489,10 +4489,7 @@ password."
                                    :max 1 :user user :host host
                                    :require '(:secret)))))
                 (if found
-                    (let ((secret (plist-get found :secret)))
-                      (if (functionp secret)
-                          (funcall secret)
-                        secret))
+                    (auth-info-password found)
                   (read-passwd (if imap
                                    "IMAP password: "
                                  "POP password: "))))))
index 8ac0cd7e7c0d436f5df76c3703c77143891ee488..88e55e968c4305dd4c34bc220ce9d216a2297940 100644 (file)
@@ -554,11 +554,9 @@ for `smtpmail-try-auth-method'.")
                      :create ask-for-password)))
          (mech (or (plist-get auth-info :smtp-auth) (car mechs)))
          (user (plist-get auth-info :user))
-         (password (plist-get auth-info :secret))
+         (password (auth-info-password auth-info))
         (save-function (and ask-for-password
                             (plist-get auth-info :save-function))))
-    (when (functionp password)
-      (setq password (funcall password)))
     (when (and user
               (not password))
       ;; The user has stored the user name, but not the password, so
@@ -573,9 +571,7 @@ for `smtpmail-try-auth-method'.")
              :user smtpmail-smtp-user
              :require '(:user :secret)
              :create t))
-           password (plist-get auth-info :secret)))
-    (when (functionp password)
-      (setq password (funcall password)))
+           password (auth-info-password auth-info)))
     (let ((result (catch 'done
                     (if (and mech user password)
                        (smtpmail-try-auth-method process mech user password)
index 9a1153b3c6a0a6fc21e7a141abaaec24fc0113de..9d1600ed72f167e1a7ff02ac4b6695d5a837d9dc 100644 (file)
@@ -560,8 +560,8 @@ If ARG is non-nil, instead prompt for connection parameters."
                      (auth (auth-source-search :host server
                                                :user user-name
                                                :port port))
-                     (fn (plist-get (car auth) :secret)))
-            (setq password (funcall fn)))
+                     (pwd (auth-info-password (car auth))))
+            (setq password pwd))
          (when server
            (let (connected)
              (dolist (p (rcirc-process-list))
index 468bc90a9d7d845b114179f50d3e4c8d49c5ac0c..50342b9105ab12cb70b8f7d00b1e5fe422285313 100644 (file)
@@ -79,6 +79,7 @@
 (require 'sasl)
 (autoload 'sasl-find-mechanism "sasl")
 (autoload 'auth-source-search "auth-source")
+(autoload 'auth-info-password "auth-source")
 
 ;; User customizable variables:
 
@@ -230,10 +231,7 @@ Return the buffer associated with the connection."
                                           :max 1
                                           :create t))
            (user-name (or (plist-get (nth 0 auth-info) :user) ""))
-           (user-password (or (plist-get (nth 0 auth-info) :secret) ""))
-           (user-password (if (functionp user-password)
-                              (funcall user-password)
-                            user-password))
+           (user-password (or (auth-info-password (nth 0 auth-info)) ""))
            (client (sasl-make-client (sasl-find-mechanism (list mech))
                                      user-name "sieve" sieve-manage-server))
            (sasl-read-passphrase
index adde443fdd67793955030289d8a8d2cedc67ecfc..fdbb78123dfbb05ac96c677188fb48e822214b54 100644 (file)
@@ -5756,10 +5756,7 @@ Invokes `password-read' if available, `read-passwd' else."
                              :create t))
                            tramp-password-save-function
                            (plist-get auth-info :save-function)
-                           auth-passwd (plist-get auth-info :secret)))
-                (while (functionp auth-passwd)
-                  (setq auth-passwd (funcall auth-passwd)))
-                auth-passwd)
+                           auth-passwd (auth-info-password auth-info))))
 
               ;; Try the password cache.
               (progn