diff options
| author | Damien Cassou | 2017-02-04 08:51:32 +0100 |
|---|---|---|
| committer | Ted Zlatanov | 2017-04-27 17:37:58 -0400 |
| commit | 0e066efe3b84ee9ecfc0a075e0ce289311f6d160 (patch) | |
| tree | 9bfcb8bb21f7d37b3124a7f171d255ead8dc56ff /lisp | |
| parent | 9e43525077697e75244d1275ec0ba88a7c2ed7f9 (diff) | |
| download | emacs-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.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/auth-source-pass.el | 255 |
1 files changed, 255 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. | ||
| 45 | See `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 | |||
| 93 | ENTRY is the name of a password-store entry. | ||
| 94 | The key used to retrieve the password is the symbol `secret'. | ||
| 95 | |||
| 96 | The convention used as the format for a password-store file is | ||
| 97 | the following (see http://www.passwordstore.org/#organization): | ||
| 98 | |||
| 99 | secret | ||
| 100 | key1: value1 | ||
| 101 | key2: 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 | |||
| 118 | ENTRY 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. | ||
| 126 | The 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. | ||
| 131 | CONTENTS 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. | ||
| 164 | E.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. | ||
| 193 | Also displays a warning if not. This function is slow, don't call it too | ||
| 194 | often." | ||
| 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. | ||
| 211 | Only 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. | ||
| 222 | If USER is non nil, give precedence to entries containing a user field | ||
| 223 | matching 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. | ||
| 237 | If many matches are found, return the first one. If no match is | ||
| 238 | found, 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 | ||