>From 9de7567ab61df0f5dda03e320c3c292c4a66ac55 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Fri, 4 Nov 2022 20:01:38 -0700 Subject: [PATCH 0/2] *** NOT A PATCH *** *** BLURB HERE *** F. Jason Park (2): [POC] Make auth-source-pass behave more like other backends [POC] Support auth-source-pass in ERC doc/misc/erc.texi | 3 +- lisp/auth-source-pass.el | 105 +++++++++++++++++++- lisp/erc/erc-compat.el | 101 +++++++++++++++++++ lisp/erc/erc.el | 7 +- test/lisp/auth-source-pass-tests.el | 144 ++++++++++++++++++++++++++++ test/lisp/erc/erc-services-tests.el | 3 - 6 files changed, 357 insertions(+), 6 deletions(-) Interdiff: diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el index 5638bdbd90..44c47c30b7 100644 --- a/lisp/auth-source-pass.el +++ b/lisp/auth-source-pass.el @@ -101,13 +101,29 @@ auth-source-pass--build-result (seq-subseq retval 0 -2)) ;; remove password retval)))) +(defvar auth-source-pass--match-regexp nil) + +(defun auth-source-pass--match-regexp (s) + (rx-to-string ; autoloaded + `(: (or bot "/") + (or (: (? (group-n 20 (+ (not (in ?\ ?/ ?@ ,s)))) "@") + (group-n 10 (+ (not (in ?\ ?/ ?@ ,s)))) + (? ,s (group-n 30 (+ (not (in ?\ ?/ ,s)))))) + (: (group-n 11 (+ (not (in ?\ ?/ ?@ ,s)))) + (? ,s (group-n 31 (+ (not (in ?\ ?/ ,s))))) + (? "/" (group-n 21 (+ (not (in ?\ ?/ ,s))))))) + eot) + 'no-group)) + (defun auth-source-pass--build-result-many (hosts ports users require max) "Return multiple `auth-source-pass--build-result' values." (unless (listp hosts) (setq hosts (list hosts))) (unless (listp users) (setq users (list users))) (unless (listp ports) (setq ports (list ports))) - (let ((rv (auth-source-pass--find-match-many hosts users ports - require (or max 1)))) + (let* ((auth-source-pass--match-regexp (auth-source-pass--match-regexp + auth-source-pass-port-separator)) + (rv (auth-source-pass--find-match-many hosts users ports + require (or max 1)))) (when auth-source-debug (auth-source-pass--do-debug "final result: %S" rv)) (if (eq auth-source-pass-standard-search 'test) @@ -237,16 +253,6 @@ auth-source-pass--find-match hosts (list hosts)))) -(defconst auth-source-pass--match-regexp - (rx (or bot "/") - (or (: (? (group-n 20 (+ (not (in " /@")))) "@") - (group-n 10 (+ (not (in " /:@")))) - (? ":" (group-n 30 (+ (not (in " /:")))))) - (: (group-n 11 (+ (not (in " /:@")))) - (? ":" (group-n 31 (+ (not (in " /:"))))) - (? "/" (group-n 21 (+ (not (in " /:"))))))) - eot)) - (defun auth-source-pass--retrieve-parsed (seen path port-number-p) (when-let ((m (string-match auth-source-pass--match-regexp path))) (puthash path diff --git a/lisp/erc/erc-compat.el b/lisp/erc/erc-compat.el index eb9cf45186..747a1152ff 100644 --- a/lisp/erc/erc-compat.el +++ b/lisp/erc/erc-compat.el @@ -182,6 +182,7 @@ erc-compat--with-memoization (declare-function auth-source-pass-entries "auth-source-pass" nil) (declare-function auth-source-pass-parse-entry "auth-source-pass" (entry)) +;; This basically hard codes `auth-source-pass-port-separator' to ":" (defun erc-compat--auth-source-pass--retrieve-parsed (seen e port-number-p) (when-let ((pat (rx (or bot "/") (or (: (? (group-n 20 (+ (not (in " /@")))) "@") diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el index 14d1361eae..242fc356b4 100644 --- a/test/lisp/auth-source-pass-tests.el +++ b/test/lisp/auth-source-pass-tests.el @@ -562,6 +562,34 @@ auth-source-pass-standard-search--wild-port-req-miss (should-not (auth-source-search :host "x.com" :port 22 :require '(:port) :max 2))))) +;; Specifying a :host without a :user finds a lone entry and does not +;; include extra fields (i.e., :port nil) in the result +;; https://lists.gnu.org/archive/html/emacs-devel/2022-11/msg00130.html + +(ert-deftest auth-source-pass-standard-search--netrc-akib () + (ert-with-temp-file netrc-file + :text "\ +machine x.com password a +machine disroot.org user akib password b +machine z.com password c +" + (let* ((auth-sources (list netrc-file)) + (auth-source-do-cache nil) + (results (auth-source-search :host "disroot.org" :max 2))) + (dolist (result results) + (setf result (plist-put result :secret (auth-info-password result)))) + (should (equal results + '((:host "disroot.org" :user "akib" :secret "b"))))))) + +(ert-deftest auth-source-pass-standard-search--akib () + (let ((auth-source-pass-standard-search 'test)) + (auth-source-pass--with-store '(("x.com" (secret . "a")) + ("akib@disroot.org" (secret . "b")) + ("z.com" (secret . "c"))) + (auth-source-pass-enable) + (should (equal (auth-source-search :host "disroot.org" :max 2) + '((:host "disroot.org" :user "akib" :secret "b"))))))) + ;; A retrieved store entry mustn't be nil regardless of whether its ;; path contains port or user components -- 2.38.1