diff options
| author | foudfou | 2017-02-16 09:34:17 +0100 |
|---|---|---|
| committer | Ted Zlatanov | 2017-04-26 15:10:17 -0400 |
| commit | c2d4ed8f2ee18d5e3fb56b31c2e1b784b1ea70e0 (patch) | |
| tree | 35d6953c446dc23c07a03b6aa3aa4c6448a4ea8b | |
| parent | 6f6e7c2add8e5eb4a1dd00a812f3cf35eb2eae37 (diff) | |
| download | emacs-c2d4ed8f2ee18d5e3fb56b31c2e1b784b1ea70e0.tar.gz emacs-c2d4ed8f2ee18d5e3fb56b31c2e1b784b1ea70e0.zip | |
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.
| -rw-r--r-- | lisp/auth-source-pass.el | 21 | ||||
| -rw-r--r-- | 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." | |||
| 206 | (lambda (file) (file-name-sans-extension (file-relative-name file store-dir))) | 206 | (lambda (file) (file-name-sans-extension (file-relative-name file store-dir))) |
| 207 | (directory-files-recursively store-dir "\.gpg$")))) | 207 | (directory-files-recursively store-dir "\.gpg$")))) |
| 208 | 208 | ||
| 209 | (defun auth-source-pass--find-all-by-entry-name (name) | 209 | (defun auth-source-pass--find-all-by-entry-name (entryname user) |
| 210 | "Search the store for all entries matching NAME. | 210 | "Search the store for all entries either matching ENTRYNAME/USER or ENTRYNAME. |
| 211 | Only return valid entries as of `auth-source-pass--entry-valid-p'." | 211 | Only return valid entries as of `auth-source-pass--entry-valid-p'." |
| 212 | (seq-filter (lambda (entry) | 212 | (seq-filter (lambda (entry) |
| 213 | (and | 213 | (and |
| 214 | (string-equal | 214 | (or |
| 215 | name | 215 | (let ((components-host-user |
| 216 | (auth-source-pass--remove-directory-name entry)) | 216 | (member entryname (split-string entry "/")))) |
| 217 | (and (= (length components-host-user) 2) | ||
| 218 | (string-equal user (cadr components-host-user)))) | ||
| 219 | (string-equal entryname (auth-source-pass--remove-directory-name entry))) | ||
| 217 | (auth-source-pass--entry-valid-p entry))) | 220 | (auth-source-pass--entry-valid-p entry))) |
| 218 | (auth-source-pass-entries))) | 221 | (auth-source-pass-entries))) |
| 219 | 222 | ||
| 220 | (defun auth-source-pass--find-one-by-entry-name (name user) | 223 | (defun auth-source-pass--find-one-by-entry-name (entryname user) |
| 221 | "Search the store for an entry matching NAME. | 224 | "Search the store for an entry matching ENTRYNAME. |
| 222 | If USER is non nil, give precedence to entries containing a user field | 225 | If USER is non nil, give precedence to entries containing a user field |
| 223 | matching USER." | 226 | matching USER." |
| 224 | (auth-source-pass--do-debug "searching for '%s' in entry names (user: %s)" | 227 | (auth-source-pass--do-debug "searching for '%s' in entry names (user: %s)" |
| 225 | name | 228 | entryname |
| 226 | user) | 229 | user) |
| 227 | (let ((matching-entries (auth-source-pass--find-all-by-entry-name name))) | 230 | (let ((matching-entries (auth-source-pass--find-all-by-entry-name entryname user))) |
| 228 | (pcase (length matching-entries) | 231 | (pcase (length matching-entries) |
| 229 | (0 (auth-source-pass--do-debug "no match found") | 232 | (0 (auth-source-pass--do-debug "no match found") |
| 230 | nil) | 233 | 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." | |||
| 210 | 210 | ||
| 211 | (ert-deftest auth-source-pass-only-return-entries-that-can-be-open () | 211 | (ert-deftest auth-source-pass-only-return-entries-that-can-be-open () |
| 212 | (cl-letf (((symbol-function 'auth-source-pass-entries) | 212 | (cl-letf (((symbol-function 'auth-source-pass-entries) |
| 213 | (lambda () '("foo.site.com" "bar.site.com"))) | 213 | (lambda () '("foo.site.com" "bar.site.com" |
| 214 | "mail/baz.site.com/scott"))) | ||
| 214 | ((symbol-function 'auth-source-pass--entry-valid-p) | 215 | ((symbol-function 'auth-source-pass--entry-valid-p) |
| 215 | ;; only foo.site.com is valid | 216 | ;; only foo.site.com and "mail/baz.site.com/scott" are valid |
| 216 | (lambda (entry) (string-equal entry "foo.site.com")))) | 217 | (lambda (entry) (member entry '("foo.site.com" |
| 217 | (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com") | 218 | "mail/baz.site.com/scott"))))) |
| 219 | (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com" "someuser") | ||
| 218 | '("foo.site.com"))) | 220 | '("foo.site.com"))) |
| 219 | (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com") | 221 | (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com" "someuser") |
| 220 | '())))) | 222 | '())) |
| 223 | (should (equal (auth-pass--find-all-by-entry-name "baz.site.com" "scott") | ||
| 224 | '("mail/baz.site.com/scott"))))) | ||
| 221 | 225 | ||
| 222 | (ert-deftest auth-source-pass-entry-is-not-valid-when-unreadable () | 226 | (ert-deftest auth-source-pass-entry-is-not-valid-when-unreadable () |
| 223 | (cl-letf (((symbol-function 'auth-source-pass--read-entry) | 227 | (cl-letf (((symbol-function 'auth-source-pass--read-entry) |