From 35e2b8a26be06ac1e563801eeb2193c57fd1189d Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Wed, 23 Nov 2022 21:31:19 -0800 Subject: [PATCH] Add erc-sasl-auth-source-function to cached options * lisp/erc/erc-sasl.el (erc-sasl--read-password): Consult cached options instead of `erc-sasl-auth-source-function'. (erc-sasl--init): Add `erc-sasl-auth-source-function' to `erc-sasl--options'. * test/lisp/erc/erc-sasl-tests.el (erc-sasl--read-password--basic, erc-sasl--read-password--auth-source): Look for original value of `erc-sasl-auth-source-function' in `erc-sasl--options' under the `authfn' key. --- lisp/erc/erc-sasl.el | 10 ++++++---- test/lisp/erc/erc-sasl-tests.el | 31 ++++++++++++++++--------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lisp/erc/erc-sasl.el b/lisp/erc/erc-sasl.el index 9084d873ce4..5ee7169de5f 100644 --- a/lisp/erc/erc-sasl.el +++ b/lisp/erc/erc-sasl.el @@ -137,10 +137,11 @@ PROMPT is passed to `read-passwd' if necessary." ((found (pcase (alist-get 'password erc-sasl--options) (:password erc-session-password) ((and (pred stringp) v) (unless (string-empty-p v) v)) - ((and (guard erc-sasl-auth-source-function) - v (let host - (or v (erc-networks--id-given erc-networks--id)))) - (apply erc-sasl-auth-source-function + ((and (let fn (alist-get 'authfn erc-sasl--options)) + (guard fn) v + (let host + (or v (erc-networks--id-given erc-networks--id)))) + (apply fn :user (erc-sasl--get-user) (and host (list :host (symbol-name host)))))))) (copy-sequence (erc--unfun found)) @@ -293,6 +294,7 @@ PROMPT is passed to `read-passwd' if necessary." `((user . ,erc-sasl-user) (password . ,erc-sasl-password) (mechanism . ,erc-sasl-mechanism) + (authfn . ,erc-sasl-auth-source-function) (authzid . ,erc-sasl-authzid))))) (defun erc-sasl--mechanism-offered-p (offered) diff --git a/test/lisp/erc/erc-sasl-tests.el b/test/lisp/erc/erc-sasl-tests.el index 64593ca270c..3e6828ff644 100644 --- a/test/lisp/erc/erc-sasl-tests.el +++ b/test/lisp/erc/erc-sasl-tests.el @@ -42,17 +42,17 @@ (erc-sasl--options '((password . :password)))) (should (string= (erc-sasl--read-password nil) "foo")))) - (ert-info ("Fallback to prompt skip auth-source") - (should-not erc-sasl-auth-source-function) - (let ((erc-session-password "bar") - (erc-networks--id (erc-networks--id-create nil))) + (ert-info ("Prompt when no authfn and :password resolves to nil") + (let ((erc-session-password nil) + (erc-sasl--options + '((password . :password) (user . :user) (authfn)))) (should (string= (ert-simulate-keys "bar\r" (erc-sasl--read-password "?")) "bar")))) - (ert-info ("Prompt when auth-source fails and `erc-sasl-password' null") - (let ((erc-sasl--options '((password))) - (erc-sasl-auth-source-function #'ignore)) + (ert-info ("Prompt when auth-source fails and `erc-session-password' null") + (should-not erc-session-password) + (let ((erc-sasl--options '((password) (authfn . ignore)))) (should (string= (ert-simulate-keys "baz\r" (erc-sasl--read-password "pwd:")) "baz"))))) @@ -71,36 +71,37 @@ (erc-session-port 6697) (erc-networks--id (erc-networks--id-create nil)) calls - (erc-sasl-auth-source-function - (lambda (&rest r) - (push r calls) - (apply #'erc--auth-source-search r))) + (fn (lambda (&rest r) + (push r calls) + (apply #'erc-auth-source-search r))) erc-server-announced-name ; too early auth-source-do-cache) (ert-info ("Symbol as password specifies machine") - (let ((erc-sasl--options '((user . "bob") (password . FSF.chat))) + (let ((erc-sasl--options + `((user . "bob") (password . FSF.chat) (authfn . ,fn))) (erc-networks--id (make-erc-networks--id))) (should (string= (erc-sasl--read-password nil) "sesame")) (should (equal (pop calls) '(:user "bob" :host "FSF.chat"))))) (ert-info ("ID for :host and `erc-session-username' for :user") ; *1 (let ((erc-session-username "bob") - (erc-sasl--options '((user . :user) (password))) + (erc-sasl--options `((user . :user) (password) (authfn . ,fn))) (erc-networks--id (erc-networks--id-create 'GNU/chat))) (should (string= (erc-sasl--read-password nil) "spam")) (should (equal (pop calls) '(:user "bob" :host "GNU/chat"))))) (ert-info ("ID for :host and current nick for :user") ; *1 (let ((erc-server-current-nick "bob") - (erc-sasl--options '((user . :nick) (password))) + (erc-sasl--options `((user . :nick) (password) (authfn . ,fn))) (erc-networks--id (erc-networks--id-create 'GNU/chat))) (should (string= (erc-sasl--read-password nil) "spam")) (should (equal (pop calls) '(:user "bob" :host "GNU/chat"))))) (ert-info ("Symbol as password, entry lacks user field") (let ((erc-server-current-nick "fake") - (erc-sasl--options '((user . :nick) (password . MyHost))) + (erc-sasl--options + `((user . :nick) (password . MyHost) (authfn . ,fn))) (erc-networks--id (erc-networks--id-create 'GNU/chat))) (should (string= (erc-sasl--read-password nil) "123")) (should (equal (pop calls) '(:user "fake" :host "MyHost")))))))) -- 2.39.2