]> git.eshelyaron.com Git - emacs.git/commitdiff
auth-source-pass: Enable finding entries by "host/username"
authorfoudfou <foudil.newbie+git@gmail.com>
Thu, 16 Feb 2017 08:34:17 +0000 (09:34 +0100)
committerTed Zlatanov <tzz@lifelogs.com>
Thu, 27 Apr 2017 21:37:58 +0000 (17:37 -0400)
* 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
test/lisp/auth-source-pass-tests.el

index a9d61cf58c380faf118235e62385851708f38b09..e59cfa2d25f4281a4b76a8c67806f280fef8c8b5 100644 (file)
@@ -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)
index c3586d8058ce4e871965eba0ae081749e8506def..1a7c9a70365f2eb34e2481fb909d093b26d1b8c3 100644 (file)
@@ -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)