Add `auth-info-password' and use it instead of ad hoc code

* 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).
This commit is contained in:
Sam Steingold 2022-01-06 14:42:10 -05:00
parent 19c6cad182
commit ad5cf84fa7
11 changed files with 42 additions and 73 deletions

View file

@ -853,15 +853,17 @@ while \(:host t) would find all host entries."
(cl-return 'no))) (cl-return 'no)))
'no)))) 'no))))
(defun auth-source-pick-first-password (&rest spec) (defun auth-info-password (auth-info)
"Pick the first secret found from applying SPEC to `auth-source-search'." "Return the :secret password from the AUTH-INFO."
(let* ((result (nth 0 (apply #'auth-source-search (plist-put spec :max 1)))) (let ((secret (plist-get auth-info :secret)))
(secret (plist-get result :secret)))
(if (functionp secret) (if (functionp secret)
(funcall secret) (funcall secret)
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) (defun auth-source-format-prompt (prompt alist)
"Format PROMPT using %x (for any character x) specifiers in ALIST. "Format PROMPT using %x (for any character x) specifiers in ALIST.
Remove trailing \": \"." Remove trailing \": \"."
@ -1800,10 +1802,9 @@ authentication tokens:
(plist-put (plist-put
artificial artificial
:save-function :save-function
(let* ((collection collection) (let ((collection collection)
(item (plist-get artificial :label)) (item (plist-get artificial :label))
(secret (plist-get artificial :secret)) (secret (auth-info-password artificial)))
(secret (if (functionp secret) (funcall secret) secret)))
(lambda () (lambda ()
(auth-source-secrets-saver collection item secret args))))) (auth-source-secrets-saver collection item secret args)))))
@ -2410,9 +2411,7 @@ MODE can be \"login\" or \"password\"."
:require '(:user :secret) :require '(:user :secret)
:create nil)))) :create nil))))
(user (plist-get auth-info :user)) (user (plist-get auth-info :user))
(password (plist-get auth-info :secret))) (password (auth-info-password auth-info)))
(when (functionp password)
(setq password (funcall password)))
(list user password auth-info))) (list user password auth-info)))
;;; Tiny mode for editing .netrc/.authinfo modes (that basically just ;;; Tiny mode for editing .netrc/.authinfo modes (that basically just

View file

@ -444,15 +444,12 @@ it returns nil."
(cl-second (assoc network (cl-second (assoc network
erc-nickserv-passwords))))) erc-nickserv-passwords)))))
(when erc-use-auth-source-for-nickserv-password (when erc-use-auth-source-for-nickserv-password
(let ((secret (cl-first (auth-source-search (auth-source-pick-first-password
:max 1 :require '(:secret) :require '(:secret)
:host server :host server
;; Ensure a string for :port ;; Ensure a string for :port
:port (format "%s" port) :port (format "%s" port)
:user nick)))) :user nick))
(when secret
(let ((passwd (plist-get secret :secret)))
(if (functionp passwd) (funcall passwd) passwd)))))
(when erc-prompt-for-nickserv-password (when erc-prompt-for-nickserv-password
(read-passwd (read-passwd
(format "NickServ password for %s on %s (RET to cancel): " (format "NickServ password for %s on %s (RET to cancel): "

View file

@ -2062,19 +2062,12 @@ Returns the buffer for the given server or channel."
;; password stuff ;; password stuff
(setq erc-session-password (setq erc-session-password
(or passwd (or passwd
(let ((secret (auth-source-pick-first-password
(plist-get :host server
(nth 0 :user nick
(auth-source-search :host server ;; secrets.el wouldnt accept a number
:max 1 :port (if (numberp port) (number-to-string port) port)
:user nick :require '(:secret))))
;; secrets.el wouldnt accept a number
:port (if (numberp port) (number-to-string port) port)
:require '(:secret)))
:secret)))
(if (functionp secret)
(funcall secret)
secret))))
;; client certificate (only useful if connecting over TLS) ;; client certificate (only useful if connecting over TLS)
(setq erc-session-client-certificate client-certificate) (setq erc-session-client-certificate client-certificate)
;; debug output buffer ;; debug output buffer
@ -3187,16 +3180,12 @@ For a list of user commands (/join /part, ...):
(put 'erc-cmd-HELP 'process-not-needed t) (put 'erc-cmd-HELP 'process-not-needed t)
(defun erc-server-join-channel (server channel &optional secret) (defun erc-server-join-channel (server channel &optional secret)
(let* ((secret (or secret (let ((password
(plist-get (nth 0 (auth-source-search (or secret
:max 1 (auth-source-pick-first-password
:host server :host server
:port "irc" :port "irc"
:user channel)) :user channel))))
:secret)))
(password (if (functionp secret)
(funcall secret)
secret)))
(erc-log (format "cmd: JOIN: %s" channel)) (erc-log (format "cmd: JOIN: %s" channel))
(erc-server-send (concat "JOIN " channel (erc-server-send (concat "JOIN " channel
(if password (if password

View file

@ -454,7 +454,7 @@ the `mail-source-keyword-map' variable."
search)))) search))))
:user))) :user)))
user-auth) user-auth)
((and ((and ; cf. 'auth-source-pick-first-password'
(eq keyword :password) (eq keyword :password)
(setq pass-auth (setq pass-auth
(plist-get (plist-get

View file

@ -40,6 +40,7 @@
(autoload 'auth-source-forget+ "auth-source") (autoload 'auth-source-forget+ "auth-source")
(autoload 'auth-source-search "auth-source") (autoload 'auth-source-search "auth-source")
(autoload 'auth-info-password "auth-source")
(nnoo-declare nnimap) (nnoo-declare nnimap)
@ -407,10 +408,7 @@ during splitting, which may be slow."
:create t)))) :create t))))
(if found (if found
(list (plist-get found :user) (list (plist-get found :user)
(let ((secret (plist-get found :secret))) (auth-info-password found)
(if (functionp secret)
(funcall secret)
secret))
(plist-get found :save-function)) (plist-get found :save-function))
nil))) nil)))

View file

@ -36,6 +36,7 @@
(eval-when-compile (require 'cl-lib)) (eval-when-compile (require 'cl-lib))
(autoload 'auth-source-search "auth-source") (autoload 'auth-source-search "auth-source")
(autoload 'auth-info-password "auth-source")
(defgroup nntp nil (defgroup nntp nil
"NNTP access for Gnus." "NNTP access for Gnus."
@ -1175,10 +1176,7 @@ If SEND-IF-FORCE, only send authinfo to the server if the
"563" "nntps" "snews")))) "563" "nntps" "snews"))))
(auth-user (plist-get auth-info :user)) (auth-user (plist-get auth-info :user))
(auth-force (plist-get auth-info :force)) (auth-force (plist-get auth-info :force))
(auth-passwd (plist-get auth-info :secret)) (auth-passwd (auth-info-password auth-info))
(auth-passwd (if (functionp auth-passwd)
(funcall auth-passwd)
auth-passwd))
(force (or (netrc-get alist "force") (force (or (netrc-get alist "force")
nntp-authinfo-force nntp-authinfo-force
auth-force)) auth-force))

View file

@ -4489,10 +4489,7 @@ password."
:max 1 :user user :host host :max 1 :user user :host host
:require '(:secret))))) :require '(:secret)))))
(if found (if found
(let ((secret (plist-get found :secret))) (auth-info-password found)
(if (functionp secret)
(funcall secret)
secret))
(read-passwd (if imap (read-passwd (if imap
"IMAP password: " "IMAP password: "
"POP password: ")))))) "POP password: "))))))

View file

@ -554,11 +554,9 @@ for `smtpmail-try-auth-method'.")
:create ask-for-password))) :create ask-for-password)))
(mech (or (plist-get auth-info :smtp-auth) (car mechs))) (mech (or (plist-get auth-info :smtp-auth) (car mechs)))
(user (plist-get auth-info :user)) (user (plist-get auth-info :user))
(password (plist-get auth-info :secret)) (password (auth-info-password auth-info))
(save-function (and ask-for-password (save-function (and ask-for-password
(plist-get auth-info :save-function)))) (plist-get auth-info :save-function))))
(when (functionp password)
(setq password (funcall password)))
(when (and user (when (and user
(not password)) (not password))
;; The user has stored the user name, but not the password, so ;; 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 :user smtpmail-smtp-user
:require '(:user :secret) :require '(:user :secret)
:create t)) :create t))
password (plist-get auth-info :secret))) password (auth-info-password auth-info)))
(when (functionp password)
(setq password (funcall password)))
(let ((result (catch 'done (let ((result (catch 'done
(if (and mech user password) (if (and mech user password)
(smtpmail-try-auth-method process mech user password) (smtpmail-try-auth-method process mech user password)

View file

@ -560,8 +560,8 @@ If ARG is non-nil, instead prompt for connection parameters."
(auth (auth-source-search :host server (auth (auth-source-search :host server
:user user-name :user user-name
:port port)) :port port))
(fn (plist-get (car auth) :secret))) (pwd (auth-info-password (car auth))))
(setq password (funcall fn))) (setq password pwd))
(when server (when server
(let (connected) (let (connected)
(dolist (p (rcirc-process-list)) (dolist (p (rcirc-process-list))

View file

@ -79,6 +79,7 @@
(require 'sasl) (require 'sasl)
(autoload 'sasl-find-mechanism "sasl") (autoload 'sasl-find-mechanism "sasl")
(autoload 'auth-source-search "auth-source") (autoload 'auth-source-search "auth-source")
(autoload 'auth-info-password "auth-source")
;; User customizable variables: ;; User customizable variables:
@ -230,10 +231,7 @@ Return the buffer associated with the connection."
:max 1 :max 1
:create t)) :create t))
(user-name (or (plist-get (nth 0 auth-info) :user) "")) (user-name (or (plist-get (nth 0 auth-info) :user) ""))
(user-password (or (plist-get (nth 0 auth-info) :secret) "")) (user-password (or (auth-info-password (nth 0 auth-info)) ""))
(user-password (if (functionp user-password)
(funcall user-password)
user-password))
(client (sasl-make-client (sasl-find-mechanism (list mech)) (client (sasl-make-client (sasl-find-mechanism (list mech))
user-name "sieve" sieve-manage-server)) user-name "sieve" sieve-manage-server))
(sasl-read-passphrase (sasl-read-passphrase

View file

@ -5756,10 +5756,7 @@ Invokes `password-read' if available, `read-passwd' else."
:create t)) :create t))
tramp-password-save-function tramp-password-save-function
(plist-get auth-info :save-function) (plist-get auth-info :save-function)
auth-passwd (plist-get auth-info :secret))) auth-passwd (auth-info-password auth-info))))
(while (functionp auth-passwd)
(setq auth-passwd (funcall auth-passwd)))
auth-passwd)
;; Try the password cache. ;; Try the password cache.
(progn (progn