Unverified Commit b206b95c authored by foudfou's avatar foudfou Committed by Ted Zlatanov
Browse files

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.
parent 0e066efe
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment