From c2d4ed8f2ee18d5e3fb56b31c2e1b784b1ea70e0 Mon Sep 17 00:00:00 2001 From: foudfou Date: Thu, 16 Feb 2017 09:34:17 +0100 Subject: [PATCH] auth-source-pass: Enable finding entries by "host/username" * lisp/auth-source-pass.el: Enable finding entries by "host/username". * test/lisp/auth-source-pass-tests.el: Adjust tests to check it. --- lisp/auth-source-pass.el | 21 ++++++++++++--------- test/lisp/auth-source-pass-tests.el | 16 ++++++++++------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el index a9d61cf58c3..e59cfa2d25f 100644 --- a/lisp/auth-source-pass.el +++ b/lisp/auth-source-pass.el @@ -206,25 +206,28 @@ often." (lambda (file) (file-name-sans-extension (file-relative-name file store-dir))) (directory-files-recursively store-dir "\.gpg$")))) -(defun auth-source-pass--find-all-by-entry-name (name) - "Search the store for all entries matching NAME. +(defun auth-source-pass--find-all-by-entry-name (entryname user) + "Search the store for all entries either matching ENTRYNAME/USER or ENTRYNAME. Only return valid entries as of `auth-source-pass--entry-valid-p'." (seq-filter (lambda (entry) (and - (string-equal - name - (auth-source-pass--remove-directory-name entry)) + (or + (let ((components-host-user + (member entryname (split-string entry "/")))) + (and (= (length components-host-user) 2) + (string-equal user (cadr components-host-user)))) + (string-equal entryname (auth-source-pass--remove-directory-name entry))) (auth-source-pass--entry-valid-p entry))) (auth-source-pass-entries))) -(defun auth-source-pass--find-one-by-entry-name (name user) - "Search the store for an entry matching NAME. +(defun auth-source-pass--find-one-by-entry-name (entryname user) + "Search the store for an entry matching ENTRYNAME. If USER is non nil, give precedence to entries containing a user field matching USER." (auth-source-pass--do-debug "searching for '%s' in entry names (user: %s)" - name + entryname user) - (let ((matching-entries (auth-source-pass--find-all-by-entry-name name))) + (let ((matching-entries (auth-source-pass--find-all-by-entry-name entryname user))) (pcase (length matching-entries) (0 (auth-source-pass--do-debug "no match found") nil) diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el index c3586d8058c..1a7c9a70365 100644 --- a/test/lisp/auth-source-pass-tests.el +++ b/test/lisp/auth-source-pass-tests.el @@ -210,14 +210,18 @@ ease testing." (ert-deftest auth-source-pass-only-return-entries-that-can-be-open () (cl-letf (((symbol-function 'auth-source-pass-entries) - (lambda () '("foo.site.com" "bar.site.com"))) + (lambda () '("foo.site.com" "bar.site.com" + "mail/baz.site.com/scott"))) ((symbol-function 'auth-source-pass--entry-valid-p) - ;; only foo.site.com is valid - (lambda (entry) (string-equal entry "foo.site.com")))) - (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com") + ;; only foo.site.com and "mail/baz.site.com/scott" are valid + (lambda (entry) (member entry '("foo.site.com" + "mail/baz.site.com/scott"))))) + (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com" "someuser") '("foo.site.com"))) - (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com") - '())))) + (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com" "someuser") + '())) + (should (equal (auth-pass--find-all-by-entry-name "baz.site.com" "scott") + '("mail/baz.site.com/scott"))))) (ert-deftest auth-source-pass-entry-is-not-valid-when-unreadable () (cl-letf (((symbol-function 'auth-source-pass--read-entry) -- 2.39.5