From a0381a48cb4ff960ef2dd55dd511f5c18e535f6e Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Sat, 10 Aug 2024 07:15:36 -0700 Subject: [PATCH 0/1] *** NOT A PATCH *** *** BLURB HERE *** F. Jason Park (1): [POC] Match attrs with auth-source-pass-extra-query-keywords lisp/auth-source-pass.el | 143 ++++++++++++++++++++-------- test/lisp/auth-source-pass-tests.el | 96 ++++++++++++++++++- 2 files changed, 195 insertions(+), 44 deletions(-) Interdiff: diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el index 8982e07a6be..0df7817f501 100644 --- a/lisp/auth-source-pass.el +++ b/lisp/auth-source-pass.el @@ -264,10 +264,10 @@ auth-source-pass--cast-port n)) (t (format "%s" val)))) -(defun auth-source-pass--match-parts (parts key reference require) - (let ((value (plist-get parts key))) +(defun auth-source-pass--match-parts (cache key reference require) + (let ((value (plist-get cache key))) (if (memq key require) - (or (null reference) (equal value reference)) + (if reference (equal value reference) value) (or (null reference) (null value) (equal value reference))))) (defvar auth-source-pass-check-attrs-with-extra-query-keywords t @@ -299,13 +299,15 @@ auth-source-pass--find-matched-entry (setq suffixedp t) u)) ((match-string 20 entry)) - ((and user optp) (funcall getat "user")))) + ((and optp (or user (memq :user require))) + (funcall getat "user")))) (p (cond (cached (plist-get cached :port)) ((match-string 30 entry)) ((match-string 31 entry)) - ((and port optp) (funcall getat "port")))) + ((and optp (or port (memq :port require))) + (funcall getat "port")))) ;; - s) + s extras) (when p (setq p (auth-source-pass--cast-port p port))) (unless cached @@ -320,18 +322,29 @@ auth-source-pass--find-matched-entry (auth-source-pass--match-parts cached :user user require) (setq s (or (funcall getat 'secret) (not (memq :secret require))))) - (unless (or user u) - (when (setq u (funcall getat "user")) - (setq cached (plist-put cached :user u)))) - (unless (or port p) - (when (setq p (funcall getat "port")) - (setq p (auth-source-pass--cast-port p port) - cached (plist-put cached :port p)))) + (let (tmp) + (while-let ((v (pop attrs)) + (k (pop v))) + (pcase k + ((or "user" "username") + (unless (or user u) + (setq u v + cached (plist-put cached :user u)))) + ("port" + (unless (or port p) + (setq p (auth-source-pass--cast-port v port) + cached (plist-put cached :port p)))) + ((pred stringp) + (push (intern (concat ":" k)) extras) + (push v extras) + (push (cons k v) tmp)))) + (setq attrs (nreverse tmp))) (puthash entry (plist-put cached :attrs attrs) seen) `( :host ,host ,@(and u (list :user u)) ,@(and p (list :port p)) - ,@(and s (not (eq s t)) (list :secret s))))))) + ,@(and s (not (eq s t)) (list :secret s)) + ,@(nreverse extras)))))) (defun auth-source-pass--find-match-many (hosts users ports require max) "Return plists for valid combinations of HOSTS, USERS, PORTS." diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el index 2ce5d12a6bc..c6662cd8b42 100644 --- a/test/lisp/auth-source-pass-tests.el +++ b/test/lisp/auth-source-pass-tests.el @@ -668,6 +668,23 @@ auth-source-pass-extra-query-keywords--akib/attr/require (should (equal results '((:host "disroot.org" :user "akib" :secret "b"))))))) +(ert-deftest auth-source-pass-extra-query-keywords--akib/attr/extras () + (auth-source-pass--with-store '(("x.com" (secret . "a")) + ("disroot.org" (secret . "b") + ("user" . "akib") + ("port" . "42") + ("foo" . "1") + ("bar" . "2")) + ("z.com" (secret . "c"))) + (auth-source-pass-enable) + (let* ((auth-source-pass-extra-query-keywords t) + (results (auth-source-search :host "disroot.org" :max 2))) + (dolist (result results) + (setf (plist-get result :secret) (auth-info-password result))) + (should (equal results + '((:host "disroot.org" :user "akib" :port "42" + :secret "b" :foo "1" :bar "2"))))))) + ;; Searches for :host are case-sensitive, and a returned host isn't ;; normalized. -- 2.46.0