aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamien Cassou2017-02-04 08:51:32 +0100
committerTed Zlatanov2017-04-27 17:37:58 -0400
commit0e066efe3b84ee9ecfc0a075e0ce289311f6d160 (patch)
tree9bfcb8bb21f7d37b3124a7f171d255ead8dc56ff
parent9e43525077697e75244d1275ec0ba88a7c2ed7f9 (diff)
downloademacs-0e066efe3b84ee9ecfc0a075e0ce289311f6d160.tar.gz
emacs-0e066efe3b84ee9ecfc0a075e0ce289311f6d160.zip
Integrate auth-source with password-store
* lisp/auth-source-pass.el: auth-source backend for password-store. * test/lisp/auth-source-pass-tests.el: Tests for auth-source-pass behavior.
-rw-r--r--lisp/auth-source-pass.el255
-rw-r--r--test/lisp/auth-source-pass-tests.el234
2 files changed, 489 insertions, 0 deletions
diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
new file mode 100644
index 00000000000..a9d61cf58c3
--- /dev/null
+++ b/lisp/auth-source-pass.el
@@ -0,0 +1,255 @@
1;;; auth-source-pass.el --- Integrate auth-source with password-store -*- lexical-binding: t -*-
2
3;; Copyright (C) 2015 Damien Cassou & Nicolas Petton
4
5;; Author: Damien Cassou <damien@cassou.me>,
6;; Nicolas Petton <nicolas@petton.fr>
7;; Version: 2.0.0
8;; Package-Requires: ((emacs "24.4")
9;; Created: 07 Jun 2015
10;; Keywords: pass password-store auth-source username password login
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software: you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27;;; Commentary:
28
29;; Integrates password-store (http://passwordstore.org/) within
30;; auth-source.
31
32;;; Code:
33
34(require 'seq)
35(require 'subr-x)
36(eval-when-compile
37 (require 'cl-lib))
38(require 'auth-source)
39(require 'url-parse)
40
41(cl-defun auth-source-pass-search (&rest spec
42 &key backend type host user port
43 &allow-other-keys)
44 "Given a property list SPEC, return search matches from the :backend.
45See `auth-source-search' for details on SPEC."
46 (cl-assert (or (null type) (eq type (oref backend type)))
47 t "Invalid password-store search: %s %s")
48 (when (listp host)
49 ;; Take the first non-nil item of the list of hosts
50 (setq host (seq-find #'identity host)))
51 (list (auth-source-pass--build-result host port user)))
52
53(defun auth-source-pass--build-result (host port user)
54 "Build auth-source-pass entry matching HOST, PORT and USER."
55 (let ((entry (auth-source-pass--find-match host user)))
56 (when entry
57 (let ((retval (list
58 :host host
59 :port (or (auth-source-pass-get "port" entry) port)
60 :user (or (auth-source-pass-get "user" entry) user)
61 :secret (lambda () (auth-source-pass-get 'secret entry)))))
62 (auth-source-pass--do-debug "return %s as final result (plus hidden password)"
63 (seq-subseq retval 0 -2)) ;; remove password
64 retval))))
65
66;;;###autoload
67(defun auth-source-pass-enable ()
68 "Enable auth-source-password-store."
69 ;; To add password-store to the list of sources, evaluate the following:
70 (add-to-list 'auth-sources 'password-store)
71 ;; clear the cache (required after each change to #'auth-source-pass-search)
72 (auth-source-forget-all-cached))
73
74(defvar auth-source-pass-backend
75 (auth-source-backend
76 (format "Password store")
77 :source "." ;; not used
78 :type 'password-store
79 :search-function #'auth-source-pass-search)
80 "Auth-source backend for password-store.")
81
82(defun auth-source-pass-backend-parse (entry)
83 "Create a password-store auth-source backend from ENTRY."
84 (when (eq entry 'password-store)
85 (auth-source-backend-parse-parameters entry auth-source-pass-backend)))
86
87(add-hook 'auth-source-backend-parser-functions #'auth-source-pass-backend-parse)
88
89
90(defun auth-source-pass-get (key entry)
91 "Return the value associated to KEY in the password-store entry ENTRY.
92
93ENTRY is the name of a password-store entry.
94The key used to retrieve the password is the symbol `secret'.
95
96The convention used as the format for a password-store file is
97the following (see http://www.passwordstore.org/#organization):
98
99secret
100key1: value1
101key2: value2"
102 (let ((data (auth-source-pass-parse-entry entry)))
103 (or (cdr (assoc key data))
104 (and (string= key "user")
105 (cdr (assoc "username" data))))))
106
107(defun auth-source-pass--read-entry (entry)
108 "Return a string with the file content of ENTRY."
109 (with-temp-buffer
110 (insert-file-contents (expand-file-name
111 (format "%s.gpg" entry)
112 "~/.password-store"))
113 (buffer-substring-no-properties (point-min) (point-max))))
114
115(defun auth-source-pass-parse-entry (entry)
116 "Return an alist of the data associated with ENTRY.
117
118ENTRY is the name of a password-store entry."
119 (let ((file-contents (ignore-errors (auth-source-pass--read-entry entry))))
120 (and file-contents
121 (cons `(secret . ,(auth-source-pass--parse-secret file-contents))
122 (auth-source-pass--parse-data file-contents)))))
123
124(defun auth-source-pass--parse-secret (contents)
125 "Parse the password-store data in the string CONTENTS and return its secret.
126The secret is the first line of CONTENTS."
127 (car (split-string contents "\\\n" t)))
128
129(defun auth-source-pass--parse-data (contents)
130 "Parse the password-store data in the string CONTENTS and return an alist.
131CONTENTS is the contents of a password-store formatted file."
132 (let ((lines (split-string contents "\\\n" t "\\\s")))
133 (seq-remove #'null
134 (mapcar (lambda (line)
135 (let ((pair (mapcar #'string-trim
136 (split-string line ":"))))
137 (when (> (length pair) 1)
138 (cons (car pair)
139 (mapconcat #'identity (cdr pair) ":")))))
140 (cdr lines)))))
141
142(defun auth-source-pass--user-match-p (entry user)
143 "Return true iff ENTRY match USER."
144 (or (null user)
145 (string= user (auth-source-pass-get "user" entry))))
146
147(defun auth-source-pass--hostname (host)
148 "Extract hostname from HOST."
149 (let ((url (url-generic-parse-url host)))
150 (or (url-host url) host)))
151
152(defun auth-source-pass--hostname-with-user (host)
153 "Extract hostname and user from HOST."
154 (let* ((url (url-generic-parse-url host))
155 (user (url-user url))
156 (hostname (url-host url)))
157 (cond
158 ((and user hostname) (format "%s@%s" user hostname))
159 (hostname hostname)
160 (t host))))
161
162(defun auth-source-pass--remove-directory-name (name)
163 "Remove directories from NAME.
164E.g., if NAME is \"foo/bar\", return \"bar\"."
165 (replace-regexp-in-string ".*/" "" name))
166
167(defun auth-source-pass--do-debug (&rest msg)
168 "Call `auth-source-do-debug` with MSG and a prefix."
169 (apply #'auth-source-do-debug
170 (cons (concat "auth-source-password-store: " (car msg))
171 (cdr msg))))
172
173(defun auth-source-pass--select-one-entry (entries user)
174 "Select one entry from ENTRIES by searching for a field matching USER."
175 (let ((number (length entries))
176 (entry-with-user
177 (and user
178 (seq-find (lambda (entry)
179 (string-equal (auth-source-pass-get "user" entry) user))
180 entries))))
181 (auth-source-pass--do-debug "found %s matches: %s" number
182 (mapconcat #'identity entries ", "))
183 (if entry-with-user
184 (progn
185 (auth-source-pass--do-debug "return %s as it contains matching user field"
186 entry-with-user)
187 entry-with-user)
188 (auth-source-pass--do-debug "return %s as it is the first one" (car entries))
189 (car entries))))
190
191(defun auth-source-pass--entry-valid-p (entry)
192 "Return t iff ENTRY can be opened.
193Also displays a warning if not. This function is slow, don't call it too
194often."
195 (if (auth-source-pass-parse-entry entry)
196 t
197 (auth-source-pass--do-debug "entry '%s' is not valid" entry)
198 nil))
199
200;; TODO: add tests for that when `assess-with-filesystem' is included
201;; in Emacs
202(defun auth-source-pass-entries ()
203 "Return a list of all password store entries."
204 (let ((store-dir (expand-file-name "~/.password-store/")))
205 (mapcar
206 (lambda (file) (file-name-sans-extension (file-relative-name file store-dir)))
207 (directory-files-recursively store-dir "\.gpg$"))))
208
209(defun auth-source-pass--find-all-by-entry-name (name)
210 "Search the store for all entries matching NAME.
211Only return valid entries as of `auth-source-pass--entry-valid-p'."
212 (seq-filter (lambda (entry)
213 (and
214 (string-equal
215 name
216 (auth-source-pass--remove-directory-name entry))
217 (auth-source-pass--entry-valid-p entry)))
218 (auth-source-pass-entries)))
219
220(defun auth-source-pass--find-one-by-entry-name (name user)
221 "Search the store for an entry matching NAME.
222If USER is non nil, give precedence to entries containing a user field
223matching USER."
224 (auth-source-pass--do-debug "searching for '%s' in entry names (user: %s)"
225 name
226 user)
227 (let ((matching-entries (auth-source-pass--find-all-by-entry-name name)))
228 (pcase (length matching-entries)
229 (0 (auth-source-pass--do-debug "no match found")
230 nil)
231 (1 (auth-source-pass--do-debug "found 1 match: %s" (car matching-entries))
232 (car matching-entries))
233 (_ (auth-source-pass--select-one-entry matching-entries user)))))
234
235(defun auth-source-pass--find-match (host user)
236 "Return a password-store entry name matching HOST and USER.
237If many matches are found, return the first one. If no match is
238found, return nil."
239 (or
240 (if (url-user (url-generic-parse-url host))
241 ;; if HOST contains a user (e.g., "user@host.com"), <HOST>
242 (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostname-with-user host) user)
243 ;; otherwise, if USER is provided, search for <USER>@<HOST>
244 (when (stringp user)
245 (auth-source-pass--find-one-by-entry-name (concat user "@" (auth-source-pass--hostname host)) user)))
246 ;; if that didn't work, search for HOST without it's user component if any
247 (auth-source-pass--find-one-by-entry-name (auth-source-pass--hostname host) user)
248 ;; if that didn't work, remove subdomain: foo.bar.com -> bar.com
249 (let ((components (split-string host "\\.")))
250 (when (= (length components) 3)
251 ;; start from scratch
252 (auth-source-pass--find-match (mapconcat 'identity (cdr components) ".") user)))))
253
254(provide 'auth-source-pass)
255;;; auth-source-pass.el ends here
diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el
new file mode 100644
index 00000000000..c3586d8058c
--- /dev/null
+++ b/test/lisp/auth-source-pass-tests.el
@@ -0,0 +1,234 @@
1;;; auth-source-pass-tests.el --- Tests for auth-source-pass.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2013 Damien Cassou
4
5;; Author: Damien Cassou <damien.cassou@gmail.com>
6
7;; This file is not part of GNU Emacs.
8
9;; This program is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; This program is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;; Tests for auth-source-pass.el
25
26;;; Code:
27
28(require 'ert)
29
30(require 'auth-source-pass)
31
32(eval-when-compile (require 'cl-macs))
33
34(ert-deftest auth-source-pass-parse-simple ()
35 (let ((content "pass\nkey1:val1\nkey2:val2\n"))
36 (should (equal (auth-source-pass--parse-data content)
37 '(("key1" . "val1")
38 ("key2" . "val2"))))))
39
40(ert-deftest auth-source-pass-parse-with-dash-line ()
41 (let ((content "pass\n--\nkey1:val1\nkey2:val2\n"))
42 (should (equal (auth-source-pass--parse-data content)
43 '(("key1" . "val1")
44 ("key2" . "val2"))))))
45
46(ert-deftest auth-source-pass-parse-with-trailing-spaces ()
47 (let ((content "pass\n--\nkey1 :val1 \nkey2: val2\n\n"))
48 (should (equal (auth-source-pass--parse-data content)
49 '(("key1" . "val1")
50 ("key2" . "val2"))))))
51
52(defvar auth-source-pass--debug-log nil
53 "Contains a list of all messages passed to `auth-source-do-debug`.")
54
55(defun auth-source-pass--should-have-message-containing (regexp)
56 "Assert that at least one `auth-source-do-debug` matched REGEXP."
57 (should (seq-find (lambda (message)
58 (string-match regexp message))
59 auth-source-pass--debug-log)))
60
61(defun auth-source-pass--debug (&rest msg)
62 "Format MSG and add that to `auth-source-pass--debug-log`.
63This function is intended to be set to `auth-source-debug`."
64 (add-to-list 'auth-source-pass--debug-log (apply #'format msg) t))
65
66(defmacro auth-source-pass--deftest (name arglist store &rest body)
67 "Define a new ert-test NAME with ARGLIST using STORE as password-store.
68BODY is a sequence of instructions that will be evaluated.
69
70This macro overrides `auth-source-pass-parse-entry' and `auth-source-pass-entries' to
71test code without touching the file system."
72 (declare (indent 3))
73 `(ert-deftest ,name ,arglist
74 (cl-letf (((symbol-function 'auth-source-pass-parse-entry) (lambda (entry) (cdr (cl-find entry ,store :key #'car :test #'string=))) )
75 ((symbol-function 'auth-source-pass-entries) (lambda () (mapcar #'car ,store)))
76 ((symbol-function 'auth-source-pass--entry-valid-p) (lambda (_entry) t)))
77 (let ((auth-source-debug #'auth-source-pass--debug)
78 (auth-source-pass--debug-log nil))
79 ,@body))))
80
81(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name ()
82 '(("foo"))
83 (should (equal (auth-source-pass--find-match "foo" nil)
84 "foo")))
85
86(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-part ()
87 '(("foo"))
88 (should (equal (auth-source-pass--find-match "https://foo" nil)
89 "foo")))
90
91(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-ignoring-user ()
92 '(("foo"))
93 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil)
94 "foo")))
95
96(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-with-user ()
97 '(("SomeUser@foo"))
98 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil)
99 "SomeUser@foo")))
100
101(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-prefer-full ()
102 '(("SomeUser@foo") ("foo"))
103 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil)
104 "SomeUser@foo")))
105
106;; same as previous one except the store is in another order
107(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-prefer-full-reversed ()
108 '(("foo") ("SomeUser@foo"))
109 (should (equal (auth-source-pass--find-match "https://SomeUser@foo" nil)
110 "SomeUser@foo")))
111
112(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain ()
113 '(("bar.com"))
114 (should (equal (auth-source-pass--find-match "foo.bar.com" nil)
115 "bar.com")))
116
117(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-with-user ()
118 '(("someone@bar.com"))
119 (should (equal (auth-source-pass--find-match "foo.bar.com" "someone")
120 "someone@bar.com")))
121
122(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-with-bad-user ()
123 '(("someoneelse@bar.com"))
124 (should (equal (auth-source-pass--find-match "foo.bar.com" "someone")
125 nil)))
126
127(auth-source-pass--deftest auth-source-pass-find-match-matching-at-entry-name-without-subdomain-prefer-full ()
128 '(("bar.com") ("foo.bar.com"))
129 (should (equal (auth-source-pass--find-match "foo.bar.com" nil)
130 "foo.bar.com")))
131
132(auth-source-pass--deftest auth-source-pass-dont-match-at-folder-name ()
133 '(("foo.bar.com/foo"))
134 (should (equal (auth-source-pass--find-match "foo.bar.com" nil)
135 nil)))
136
137(auth-source-pass--deftest auth-source-pass-search-with-user-first ()
138 '(("foo") ("user@foo"))
139 (should (equal (auth-source-pass--find-match "foo" "user")
140 "user@foo"))
141 (auth-source-pass--should-have-message-containing "Found 1 match"))
142
143(auth-source-pass--deftest auth-source-pass-give-priority-to-desired-user ()
144 '(("foo") ("subdir/foo" ("user" . "someone")))
145 (should (equal (auth-source-pass--find-match "foo" "someone")
146 "subdir/foo"))
147 (auth-source-pass--should-have-message-containing "Found 2 matches")
148 (auth-source-pass--should-have-message-containing "matching user field"))
149
150(auth-source-pass--deftest auth-source-pass-give-priority-to-desired-user-reversed ()
151 '(("foo" ("user" . "someone")) ("subdir/foo"))
152 (should (equal (auth-source-pass--find-match "foo" "someone")
153 "foo"))
154 (auth-source-pass--should-have-message-containing "Found 2 matches")
155 (auth-source-pass--should-have-message-containing "matching user field"))
156
157(auth-source-pass--deftest auth-source-pass-return-first-when-several-matches ()
158 '(("foo") ("subdir/foo"))
159 (should (equal (auth-source-pass--find-match "foo" nil)
160 "foo"))
161 (auth-source-pass--should-have-message-containing "Found 2 matches")
162 (auth-source-pass--should-have-message-containing "the first one"))
163
164(auth-source-pass--deftest auth-source-pass-make-divansantana-happy ()
165 '(("host.com"))
166 (should (equal (auth-source-pass--find-match "smtp.host.com" "myusername@host.co.za")
167 "host.com")))
168
169(ert-deftest auth-source-pass-hostname ()
170 (should (equal (auth-source-pass--hostname "https://foo.bar") "foo.bar"))
171 (should (equal (auth-source-pass--hostname "http://foo.bar") "foo.bar"))
172 (should (equal (auth-source-pass--hostname "https://SomeUser@foo.bar") "foo.bar")))
173
174(ert-deftest auth-source-pass-hostname-with-user ()
175 (should (equal (auth-source-pass--hostname-with-user "https://foo.bar") "foo.bar"))
176 (should (equal (auth-source-pass--hostname-with-user "http://foo.bar") "foo.bar"))
177 (should (equal (auth-source-pass--hostname-with-user "https://SomeUser@foo.bar") "SomeUser@foo.bar")))
178
179(defmacro auth-source-pass--deftest-build-result (name arglist store &rest body)
180 "Define a new ert-test NAME with ARGLIST using STORE as password-store.
181BODY is a sequence of instructions that will be evaluated.
182
183This macro overrides `auth-source-pass-parse-entry',
184`auth-source-pass-entries', and `auth-source-pass--find-match' to
185ease testing."
186 (declare (indent 3))
187 `(auth-source-pass--deftest ,name ,arglist ,store
188 (cl-letf (((symbol-function 'auth-source-pass-find-match)
189 (lambda (_host _user)
190 "foo")))
191 ,@body)))
192
193(auth-source-pass--deftest-build-result auth-source-pass-build-result-return-parameters ()
194 '(("foo"))
195 (let ((result (auth-source-pass--build-result "foo" 512 "user")))
196 (should (equal (plist-get result :port) 512))
197 (should (equal (plist-get result :user) "user"))))
198
199(auth-source-pass--deftest-build-result auth-source-pass-build-result-return-entry-values ()
200 '(("foo" ("port" . 512) ("user" . "anuser")))
201 (let ((result (auth-source-pass--build-result "foo" nil nil)))
202 (should (equal (plist-get result :port) 512))
203 (should (equal (plist-get result :user) "anuser"))))
204
205(auth-source-pass--deftest-build-result auth-source-pass-build-result-entry-takes-precedence ()
206 '(("foo" ("port" . 512) ("user" . "anuser")))
207 (let ((result (auth-source-pass--build-result "foo" 1024 "anotheruser")))
208 (should (equal (plist-get result :port) 512))
209 (should (equal (plist-get result :user) "anuser"))))
210
211(ert-deftest auth-source-pass-only-return-entries-that-can-be-open ()
212 (cl-letf (((symbol-function 'auth-source-pass-entries)
213 (lambda () '("foo.site.com" "bar.site.com")))
214 ((symbol-function 'auth-source-pass--entry-valid-p)
215 ;; only foo.site.com is valid
216 (lambda (entry) (string-equal entry "foo.site.com"))))
217 (should (equal (auth-source-pass--find-all-by-entry-name "foo.site.com")
218 '("foo.site.com")))
219 (should (equal (auth-source-pass--find-all-by-entry-name "bar.site.com")
220 '()))))
221
222(ert-deftest auth-source-pass-entry-is-not-valid-when-unreadable ()
223 (cl-letf (((symbol-function 'auth-source-pass--read-entry)
224 (lambda (entry)
225 ;; only foo is a valid entry
226 (if (string-equal entry "foo")
227 "password"
228 nil))))
229 (should (auth-source-pass--entry-valid-p "foo"))
230 (should-not (auth-source-pass--entry-valid-p "bar"))))
231
232(provide 'auth-source-pass-tests)
233
234;;; auth-source-pass-tests.el ends here