1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
3 ;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is the auth-source.el package. It lets users tell Gnus how to
26 ;; authenticate in a single place. Simplicity is the goal. Instead
27 ;; of providing 5000 options, we'll stick to simple, easy to
28 ;; understand options.
30 ;; See the auth.info Info documentation for details.
34 ;; - never decode the backend file unless it's necessary
35 ;; - a more generic way to match backends and search backend contents
36 ;; - absorb netrc.el and simplify it
37 ;; - protect passwords better
38 ;; - allow creating and changing netrc lines (not files) e.g. change a password
42 (require 'password-cache)
46 (eval-when-compile (require 'cl))
49 (autoload 'secrets-create-item "secrets")
50 (autoload 'secrets-delete-item "secrets")
51 (autoload 'secrets-get-alias "secrets")
52 (autoload 'secrets-get-attributes "secrets")
53 (autoload 'secrets-get-secret "secrets")
54 (autoload 'secrets-list-collections "secrets")
55 (autoload 'secrets-search-items "secrets")
57 (defvar secrets-enabled)
59 (defgroup auth-source nil
60 "Authentication sources."
61 :version "23.1" ;; No Gnus
65 (defcustom auth-source-cache-expiry 7200
66 "How many seconds passwords are cached, or nil to disable
67 expiring. Overrides `password-cache-expiry' through a
70 :type '(choice (const :tag "Never" nil)
71 (const :tag "All Day" 86400)
72 (const :tag "2 Hours" 7200)
73 (const :tag "30 Minutes" 1800)
74 (integer :tag "Seconds")))
76 (defclass auth-source-backend ()
81 :documentation "The backend type.")
82 (source :initarg :source
85 :documentation "The backend source.")
90 :documentation "The backend host.")
95 :documentation "The backend user.")
96 (protocol :initarg :protocol
100 :documentation "The backend protocol.")
101 (create-function :initarg :create-function
105 :documentation "The create function.")
106 (search-function :initarg :search-function
110 :documentation "The search function.")))
112 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
113 (pop3 "pop3" "pop" "pop3s" "110" "995")
117 "List of authentication protocols and their names"
120 :version "23.2" ;; No Gnus
121 :type '(repeat :tag "Authentication Protocols"
122 (cons :tag "Protocol Entry"
123 (symbol :tag "Protocol")
125 (string :tag "Name")))))
127 ;;; generate all the protocols in a format Customize can use
128 ;;; TODO: generate on the fly from auth-source-protocols
129 (defconst auth-source-protocols-customize
131 (let ((p (car-safe a)))
133 :tag (upcase (symbol-name p))
135 auth-source-protocols))
137 (defvar auth-source-creation-defaults nil
138 "Defaults for creating token values. Usually let-bound.")
140 (make-obsolete 'auth-source-hide-passwords nil "Emacs 24.1")
142 (defvar auth-source-magic "auth-source-magic ")
144 (defcustom auth-source-do-cache t
145 "Whether auth-source should cache information with `password-cache'."
147 :version "23.2" ;; No Gnus
150 (defcustom auth-source-debug t
151 "Whether auth-source should log debug messages.
153 If the value is nil, debug messages are not logged.
155 If the value is t, debug messages are logged with `message'. In
156 that case, your authentication data will be in the clear (except
159 If the value is a function, debug messages are logged by calling
160 that function using the same arguments as `message'."
162 :version "23.2" ;; No Gnus
164 :tag "auth-source debugging mode"
165 (const :tag "Log using `message' to the *Messages* buffer" t)
166 (function :tag "Function that takes arguments like `message'")
167 (const :tag "Don't log anything" nil)))
169 (defcustom auth-sources '("~/.authinfo.gpg" "~/.authinfo")
170 "List of authentication sources.
172 The default will get login and password information from
173 \"~/.authinfo.gpg\", which you should set up with the EPA/EPG
174 packages to be encrypted. If that file doesn't exist, it will
175 try the unencrypted version \"~/.authinfo\".
177 See the auth.info manual for details.
179 Each entry is the authentication type with optional properties.
181 It's best to customize this with `M-x customize-variable' because the choices
182 can get pretty complex."
184 :version "24.1" ;; No Gnus
185 :type `(repeat :tag "Authentication Sources"
187 (string :tag "Just a file")
188 (const :tag "Default Secrets API Collection" 'default)
189 (const :tag "Login Secrets API Collection" "secrets:Login")
190 (const :tag "Temp Secrets API Collection" "secrets:session")
191 (list :tag "Source definition"
192 (const :format "" :value :source)
193 (choice :tag "Authentication backend choice"
194 (string :tag "Authentication Source (file)")
196 :tag "Secret Service API/KWallet/GNOME Keyring"
197 (const :format "" :value :secrets)
198 (choice :tag "Collection to use"
199 (string :tag "Collection name")
200 (const :tag "Default" 'default)
201 (const :tag "Login" "Login")
203 :tag "Temporary" "session"))))
204 (repeat :tag "Extra Parameters" :inline t
205 (choice :tag "Extra parameter"
208 (const :format "" :value :host)
209 (choice :tag "Host (machine) choice"
212 :tag "Regular expression")))
215 (const :format "" :value :protocol)
219 ,@auth-source-protocols-customize))
220 (list :tag "User" :inline t
221 (const :format "" :value :user)
222 (choice :tag "Personality/Username"
224 (string :tag "Name")))))))))
226 (defcustom auth-source-gpg-encrypt-to t
227 "List of recipient keys that `authinfo.gpg' encrypted to.
228 If the value is not a list, symmetric encryption will be used."
230 :version "24.1" ;; No Gnus
231 :type '(choice (const :tag "Symmetric encryption" t)
232 (repeat :tag "Recipient public keys"
233 (string :tag "Recipient public key"))))
235 ;; temp for debugging
236 ;; (unintern 'auth-source-protocols)
237 ;; (unintern 'auth-sources)
238 ;; (customize-variable 'auth-sources)
239 ;; (setq auth-sources nil)
240 ;; (format "%S" auth-sources)
241 ;; (customize-variable 'auth-source-protocols)
242 ;; (setq auth-source-protocols nil)
243 ;; (format "%S" auth-source-protocols)
244 ;; (auth-source-pick nil :host "a" :port 'imap)
245 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
246 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
247 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
248 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
249 ;; (auth-source-protocol-defaults 'imap)
251 ;; (let ((auth-source-debug 'debug)) (auth-source-do-debug "hello"))
252 ;; (let ((auth-source-debug t)) (auth-source-do-debug "hello"))
253 ;; (let ((auth-source-debug nil)) (auth-source-do-debug "hello"))
254 (defun auth-source-do-debug (&rest msg)
255 (when auth-source-debug
256 (apply 'auth-source-do-warn msg)))
258 (defun auth-source-do-warn (&rest msg)
260 ;; set logger to either the function in auth-source-debug or 'message
261 ;; note that it will be 'message if auth-source-debug is nil
262 (if (functionp auth-source-debug)
268 ;; (auth-source-pick nil :host "any" :protocol 'imap :user "joe")
269 ;; (auth-source-pick t :host "any" :protocol 'imap :user "joe")
270 ;; (setq auth-sources '((:source (:secrets default) :host t :protocol t :user "joe")
271 ;; (:source (:secrets "session") :host t :protocol t :user "joe")
272 ;; (:source (:secrets "Login") :host t :protocol t)
273 ;; (:source "~/.authinfo.gpg" :host t :protocol t)))
275 ;; (setq auth-sources '((:source (:secrets default) :host t :protocol t :user "joe")
276 ;; (:source (:secrets "session") :host t :protocol t :user "joe")
277 ;; (:source (:secrets "Login") :host t :protocol t)
280 ;; (setq auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t)))
282 ;; (auth-source-backend-parse "myfile.gpg")
283 ;; (auth-source-backend-parse 'default)
284 ;; (auth-source-backend-parse "secrets:Login")
286 (defun auth-source-backend-parse (entry)
287 "Creates an auth-source-backend from an ENTRY in `auth-sources'."
288 (auth-source-backend-parse-parameters
291 ;; take 'default and recurse to get it as a Secrets API default collection
292 ;; matching any user, host, and protocol
294 (auth-source-backend-parse '(:source (:secrets default))))
295 ;; take secrets:XYZ and recurse to get it as Secrets API collection "XYZ"
296 ;; matching any user, host, and protocol
297 ((and (stringp entry) (string-match "^secrets:\\(.+\\)" entry))
298 (auth-source-backend-parse `(:source (:secrets ,(match-string 1 entry)))))
299 ;; take just a file name and recurse to get it as a netrc file
300 ;; matching any user, host, and protocol
302 (auth-source-backend-parse `(:source ,entry)))
304 ;; a file name with parameters
305 ((stringp (plist-get entry :source))
307 (plist-get entry :source)
308 :source (plist-get entry :source)
310 :search-function 'auth-source-netrc-search
311 :create-function 'auth-source-netrc-create))
313 ;; the Secrets API. We require the package, in order to have a
314 ;; defined value for `secrets-enabled'.
316 (not (null (plist-get entry :source))) ; the source must not be nil
317 (listp (plist-get entry :source)) ; and it must be a list
318 (require 'secrets nil t) ; and we must load the Secrets API
319 secrets-enabled) ; and that API must be enabled
321 ;; the source is either the :secrets key in ENTRY or
322 ;; if that's missing or nil, it's "session"
323 (let ((source (or (plist-get (plist-get entry :source) :secrets)
326 ;; if the source is a symbol, we look for the alias named so,
327 ;; and if that alias is missing, we use "Login"
328 (when (symbolp source)
329 (setq source (or (secrets-get-alias (symbol-name source))
332 (if (featurep 'secrets)
334 (format "Secrets API (%s)" source)
337 :search-function 'auth-source-secrets-search
338 :create-function 'auth-source-secrets-create)
340 "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry)
342 (format "Ignored Secrets API (%s)" source)
349 "auth-source-backend-parse: invalid backend spec: %S" entry)
355 (defun auth-source-backend-parse-parameters (entry backend)
356 "Fills in the extra auth-source-backend parameters of ENTRY.
357 Using the plist ENTRY, get the :host, :protocol, and :user search
358 parameters. Accepts :port as an alias to :protocol."
359 (let ((entry (if (stringp entry)
363 (when (setq val (plist-get entry :host))
364 (oset backend host val))
365 (when (setq val (plist-get entry :user))
366 (oset backend user val))
367 ;; accept :port as an alias for :protocol
368 (when (setq val (or (plist-get entry :protocol) (plist-get entry :port)))
369 (oset backend protocol val)))
372 ;; (mapcar 'auth-source-backend-parse auth-sources)
374 (defun* auth-source-search (&rest spec
375 &key type max host user protocol secret
378 "Search or modify authentication backends according to SPEC.
380 This function parses `auth-sources' for matches of the SPEC
381 plist. It can optionally create or update an authentication
382 token if requested. A token is just a standard Emacs property
383 list with a :secret property that can be a function; all the
384 other properties will always hold scalar values.
386 Typically the :secret property, if present, contains a password.
388 Common search keys are :max, :host, :protocol, and :user. In
389 addition, :create specifies how tokens will be or created.
390 Finally, :type can specify which backend types you want to check.
392 A string value is always matched literally. A symbol is matched
393 as its string value, literally. All the SPEC values can be
394 single values (symbol or string) or lists thereof (in which case
395 any of the search terms matches).
397 :create t means to create a token if possible.
399 A new token will be created if no matching tokens were found.
400 The new token will have only the keys the backend requires. For
401 the netrc backend, for instance, that's the user, host, and
406 \(let ((auth-source-creation-defaults '((user . \"defaultUser\")
407 (A . \"default A\"))))
408 (auth-source-search :host \"mine\" :type 'netrc :max 1
409 :P \"pppp\" :Q \"qqqq\"
414 \"Search for any entry matching host 'mine' in backends of type
415 'netrc', maximum one result.
417 Create a new entry if you found none. The netrc backend will
418 automatically require host, user, and protocol. The host will be
419 'mine'. We prompt for the user with default 'defaultUser' and
420 for the protocol without a default. We will not prompt for A, Q,
421 or P. The resulting token will only have keys user, host, and
424 :create '(A B C) also means to create a token if possible.
426 The behavior is like :create t but if the list contains any
427 parameter, that parameter will be required in the resulting
428 token. The value for that parameter will be obtained from the
429 search parameters or from user input. If any queries are needed,
430 the alist `auth-source-creation-defaults' will be checked for the
435 \(let ((auth-source-creation-defaults '((user . \"defaultUser\")
436 (A . \"default A\"))))
437 (auth-source-search :host '(\"nonesuch\" \"twosuch\") :type 'netrc :max 1
438 :P \"pppp\" :Q \"qqqq\"
443 \"Search for any entry matching host 'nonesuch'
444 or 'twosuch' in backends of type 'netrc', maximum one result.
446 Create a new entry if you found none. The netrc backend will
447 automatically require host, user, and protocol. The host will be
448 'nonesuch' and Q will be 'qqqq'. We prompt for A with default
449 'default A', for B and protocol with default nil, and for the
450 user with default 'defaultUser'. We will not prompt for Q. The
451 resulting token will have keys user, host, protocol, A, B, and Q.
452 It will not have P with any value, even though P is used in the
453 search to find only entries that have P set to 'pppp'.\"
455 When multiple values are specified in the search parameter, the
456 first one is used for creation. So :host (X Y Z) would create a
457 token for host X, for instance.
459 This creation can fail if the search was not specific enough to
460 create a new token (it's up to the backend to decide that). You
461 should `catch' the backend-specific error as usual. Some
462 backends (netrc, at least) will prompt the user rather than throw
465 :delete t means to delete any found entries. nil by default.
466 Use `auth-source-delete' in ELisp code instead of calling
467 `auth-source-search' directly with this parameter.
469 :type (X Y Z) will check only those backend types. 'netrc and
470 'secrets are the only ones supported right now.
472 :max N means to try to return at most N items (defaults to 1).
473 When 0 the function will return just t or nil to indicate if any
474 matches were found. More than N items may be returned, depending
475 on the search and the backend.
477 :host (X Y Z) means to match only hosts X, Y, or Z according to
478 the match rules above. Defaults to t.
480 :user (X Y Z) means to match only users X, Y, or Z according to
481 the match rules above. Defaults to t.
483 :protocol (P Q R) means to match only protocols P, Q, or R.
486 :K (V1 V2 V3) for any other key K will match values V1, V2, or
487 V3 (note the match rules above).
489 The return value is a list with at most :max tokens. Each token
490 is a plist with keys :backend :host :protocol :user, plus any other
491 keys provided by the backend (notably :secret). But note the
492 exception for :max 0, which see above.
494 The token's :secret key can hold a function. In that case you
495 must call it to obtain the actual value."
496 (let* ((backends (mapcar 'auth-source-backend-parse auth-sources))
498 (ignored-keys '(:create :delete :max))
499 (keys (loop for i below (length spec) by 2
500 unless (memq (nth i spec) ignored-keys)
501 collect (nth i spec)))
502 (found (auth-source-recall spec))
503 filtered-backends accessor-key found-here goal)
505 (if (and found auth-source-do-cache)
506 (auth-source-do-debug
507 "auth-source-search: found %d CACHED results matching %S"
511 (or (eq t create) (listp create)) t
512 "Invalid auth-source :create parameter (must be nil, t, or a list): %s %s")
514 (setq filtered-backends (copy-sequence backends))
515 (dolist (backend backends)
517 ;; ignore invalid slots
518 (condition-case signal
519 (unless (eval `(auth-source-search-collection
521 (oref backend ,key)))
522 (setq filtered-backends (delq backend filtered-backends))
524 (invalid-slot-name))))
526 (auth-source-do-debug
527 "auth-source-search: found %d backends matching %S"
528 (length filtered-backends) spec)
530 ;; (debug spec "filtered" filtered-backends)
532 (dolist (backend filtered-backends)
533 (setq found-here (apply
534 (slot-value backend 'search-function)
540 ;; if max is 0, as soon as we find something, return it
541 (when (and (zerop max) (> 0 (length found-here)))
544 ;; decrement the goal by the number of new results
545 (decf goal (length found-here))
546 ;; and append the new results to the full list
547 (setq found (append found found-here))
549 (auth-source-do-debug
550 "auth-source-search: found %d results (max %d/%d) in %S matching %S"
551 (length found-here) max goal backend spec)
553 ;; return full list if the goal is 0 or negative
554 (when (zerop (max 0 goal))
557 ;; change the :max parameter in the spec to the goal
558 (setq spec (plist-put spec :max goal)))
560 (when (and found auth-source-do-cache)
561 (auth-source-remember spec found)))
565 ;;; (auth-source-search :max 1)
566 ;;; (funcall (plist-get (nth 0 (auth-source-search :max 1)) :secret))
567 ;;; (auth-source-search :host "nonesuch" :type 'netrc :K 1)
568 ;;; (auth-source-search :host "nonesuch" :type 'secrets)
570 (defun* auth-source-delete (&rest spec
573 "Delete entries from the authentication backends according to SPEC.
574 Calls `auth-source-search' with the :delete property in SPEC set to t.
575 The backend may not actually delete the entries.
577 Returns the deleted entries."
578 (auth-source-search (plist-put spec :delete t)))
580 (defun auth-source-search-collection (collection value)
581 "Returns t is VALUE is t or COLLECTION is t or contains VALUE."
582 (when (and (atom collection) (not (eq t collection)))
583 (setq collection (list collection)))
585 ;; (debug :collection collection :value value)
586 (or (eq collection t)
588 (equal collection value)
589 (member value collection)))
591 (defun auth-source-forget-all-cached ()
592 "Forget all cached auth-source data."
594 (loop for sym being the symbols of password-data
595 ;; when the symbol name starts with auth-source-magic
596 when (string-match (concat "^" auth-source-magic)
599 do (password-cache-remove (symbol-name sym))))
601 (defun auth-source-remember (spec found)
602 "Remember FOUND search results for SPEC."
603 (let ((password-cache-expiry auth-source-cache-expiry))
605 (concat auth-source-magic (format "%S" spec)) found)))
607 (defun auth-source-recall (spec)
608 "Recall FOUND search results for SPEC."
609 (password-read-from-cache
610 (concat auth-source-magic (format "%S" spec))))
612 (defun auth-source-forget (spec)
613 "Forget any cached data matching SPEC exactly.
615 This is the same SPEC you passed to `auth-source-search'.
616 Returns t or nil for forgotten or not found."
617 (password-cache-remove (concat auth-source-magic (format "%S" spec))))
619 ;;; (loop for sym being the symbols of password-data when (string-match (concat "^" auth-source-magic) (symbol-name sym)) collect (symbol-name sym))
621 ;;; (auth-source-remember '(:host "wedd") '(4 5 6))
622 ;;; (auth-source-remember '(:host "xedd") '(1 2 3))
623 ;;; (auth-source-recall '(:host "xedd"))
624 ;;; (auth-source-recall '(:host t))
625 ;;; (auth-source-forget+ :host t)
627 (defun* auth-source-forget+ (&rest spec &allow-other-keys)
628 "Forget any cached data matching SPEC. Returns forgotten count.
630 This is not a full `auth-source-search' spec but works similarly.
631 For instance, \(:host \"myhost\" \"yourhost\") would find all the
632 cached data that was found with a search for those two hosts,
633 while \(:host t) would find all host entries."
636 (loop for sym being the symbols of password-data
637 ;; when the symbol name matches with auth-source-magic
638 when (and (setq sname (symbol-name sym))
639 (string-match (concat "^" auth-source-magic "\\(.+\\)")
641 ;; and the spec matches what was stored in the cache
642 (auth-source-specmatchp spec (read (match-string 1 sname))))
645 (password-cache-remove sname)
649 (defun auth-source-specmatchp (spec stored)
650 (let ((keys (loop for i below (length spec) by 2
651 collect (nth i spec))))
654 (unless (auth-source-search-collection (plist-get stored key)
655 (plist-get spec key))
659 ;;; Backend specific parsing: netrc/authinfo backend
661 ;;; (auth-source-netrc-parse "~/.authinfo.gpg")
662 (defun* auth-source-netrc-parse (&rest
664 &key file max host user protocol delete
666 "Parse FILE and return a list of all entries in the file.
667 Note that the MAX parameter is used so we can exit the parse early."
669 ;; We got already parsed contents; just return it.
671 (when (file-exists-p file)
673 (let ((tokens '("machine" "host" "default" "login" "user"
674 "password" "account" "macdef" "force"
676 (max (or max 5000)) ; sanity check: default to stop at 5K
678 alist elem result pair)
679 (insert-file-contents file)
680 (goto-char (point-min))
681 ;; Go through the file, line by line.
682 (while (and (not (eobp))
685 (narrow-to-region (point) (point-at-eol))
686 ;; For each line, get the tokens and values.
688 (skip-chars-forward "\t ")
689 ;; Skip lines that begin with a "#".
690 (if (eq (char-after) ?#)
691 (goto-char (point-max))
694 (if (= (following-char) ?\")
695 (read (current-buffer))
697 (point) (progn (skip-chars-forward "^\t ")
700 ((equal elem "macdef")
701 ;; We skip past the macro definition.
703 (while (and (zerop (forward-line 1))
705 (narrow-to-region (point) (point)))
706 ((member elem tokens)
707 ;; Tokens that don't have a following value are ignored,
709 (when (and pair (or (cdr pair)
710 (equal (car pair) "default")))
712 (setq pair (list elem)))
714 ;; Values that haven't got a preceding token are ignored.
718 (setq pair nil)))))))
722 (auth-source-search-collection
725 (aget alist "machine")
726 (aget alist "host")))
727 (auth-source-search-collection
731 (aget alist "account")
732 (aget alist "user")))
733 (auth-source-search-collection
737 (aget alist "protocol"))))
739 (push (nreverse alist) result)
740 ;; to delete a line, we just comment it out
742 (goto-char (point-min))
751 (when auth-source-gpg-encrypt-to
752 ;; (see bug#7487) making `epa-file-encrypt-to' local to
753 ;; this buffer lets epa-file skip the key selection query
754 ;; (see the `local-variable-p' check in
755 ;; `epa-file-write-region').
756 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
757 (make-local-variable 'epa-file-encrypt-to))
758 (if (listp auth-source-gpg-encrypt-to)
759 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
761 ;; ask AFTER we've successfully opened the file
762 (when (y-or-n-p (format "Save file %s? (%d modifications)"
764 (write-region (point-min) (point-max) file nil 'silent)
765 (auth-source-do-debug
766 "auth-source-netrc-parse: modified %d lines in %s"
769 (nreverse result))))))
771 (defun auth-source-netrc-normalize (alist)
772 (mapcar (lambda (entry)
774 (while (setq item (pop entry))
779 (setq k (cond ((member k '("machine")) "host")
780 ((member k '("login" "account")) "user")
781 ((member k '("protocol")) "port")
782 ((member k '("password")) "secret")
785 ;; send back the secret in a function (lexical binding)
786 (when (equal k "secret")
787 (setq v (lexical-let ((v v))
790 (setq ret (plist-put ret
791 (intern (concat ":" k))
797 ;;; (setq secret (plist-get (nth 0 (auth-source-search :host t :type 'netrc :K 1 :max 1)) :secret))
800 (defun* auth-source-netrc-search (&rest
802 &key backend create delete
803 type max host user protocol
805 "Given a property list SPEC, return search matches from the :backend.
806 See `auth-source-search' for details on SPEC."
807 ;; just in case, check that the type is correct (null or same as the backend)
808 (assert (or (null type) (eq type (oref backend type)))
809 t "Invalid netrc search: %s %s")
811 (let ((results (auth-source-netrc-normalize
812 (auth-source-netrc-parse
815 :file (oref backend source)
818 :protocol (or protocol t)))))
820 ;; if we need to create an entry AND none were found to match
822 (= 0 (length results)))
824 ;; create based on the spec and record the value
826 ;; if the user did not want to create the entry
827 ;; in the file, it will be returned
828 (apply (slot-value backend 'create-function) spec)
829 ;; if not, we do the search again without :create
830 ;; to get the updated data.
832 ;; the result will be returned, even if the search fails
833 (apply 'auth-source-netrc-search
834 (plist-put spec :create nil)))))
837 ;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
838 ;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))
840 (defun* auth-source-netrc-create (&rest spec
842 secret host user protocol create
844 (let* ((base-required '(host user protocol secret))
845 ;; we know (because of an assertion in auth-source-search) that the
846 ;; :create parameter is either t or a list (which includes nil)
847 (create-extra (if (eq t create) nil create))
848 (required (append base-required create-extra))
849 (file (oref backend source))
851 ;; `valist' is an alist
853 ;; `artificial' will be returned if no creation is needed
856 ;; only for base required elements (defined as function parameters):
857 ;; fill in the valist with whatever data we may have from the search
858 ;; we take the first value if it's a list, the whole value otherwise
859 (dolist (br base-required)
860 (when (symbol-value br)
861 (aput 'valist br (if (listp (symbol-value br))
862 (nth 0 (symbol-value br))
863 (symbol-value br)))))
865 ;; for extra required elements, see if the spec includes a value for them
866 (dolist (er create-extra)
867 (let ((name (concat ":" (symbol-name er)))
868 (keys (loop for i below (length spec) by 2
869 collect (nth i spec))))
871 (when (equal (symbol-name k) name)
872 (aput 'valist er (plist-get spec k))))))
874 ;; for each required element
876 (let* ((data (aget valist r))
877 (given-default (aget auth-source-creation-defaults r))
878 ;; the defaults are simple
880 ((and (not given-default) (eq r 'user))
882 ;; note we need this empty string
883 ((and (not given-default) (eq r 'protocol))
886 ;; the prompt's default string depends on the data so far
887 (default-string (if (and default (< 0 (length default)))
888 (format " (default %s)" default)
890 ;; the prompt should also show what's entered so far
891 (user-value (aget valist 'user))
892 (host-value (aget valist 'host))
893 (protocol-value (aget valist 'protocol))
894 (info-so-far (concat (if user-value
895 (format "%s@" user-value)
898 (format "%s" host-value)
901 ;; this distinguishes protocol between
902 (if (zerop (length protocol-value))
903 "" ; 'entered as "no default"' vs.
904 (format ":%s" protocol-value)) ; given
905 ;; and this is when the protocol is unknown
908 ;; now prompt if the search SPEC did not include a required key;
909 ;; take the result and put it in `data' AND store it in `valist'
913 ((and (null data) (eq r 'secret))
914 ;; special case prompt for passwords
915 (read-passwd (format "Password for %s: " info-so-far)))
918 (format "Enter %s for %s%s: "
919 r info-so-far default-string)
924 (setq artificial (plist-put artificial
925 (intern (concat ":" (symbol-name r)))
927 (lexical-let ((data data))
931 ;; when r is not an empty string...
932 (when (and (stringp data)
934 ;; append the key (the symbol name of r) and the value in r
935 (setq add (concat add
938 (if (zerop (length add)) "" " ")
939 ;; remap auth-source tokens to netrc
946 ;; the value will be printed in %S format
950 (when (file-exists-p file)
951 (insert-file-contents file))
952 (when auth-source-gpg-encrypt-to
953 ;; (see bug#7487) making `epa-file-encrypt-to' local to
954 ;; this buffer lets epa-file skip the key selection query
955 ;; (see the `local-variable-p' check in
956 ;; `epa-file-write-region').
957 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
958 (make-local-variable 'epa-file-encrypt-to))
959 (if (listp auth-source-gpg-encrypt-to)
960 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
961 (goto-char (point-max))
963 ;; ask AFTER we've successfully opened the file
964 (if (y-or-n-p (format "Add to file %s: line [%s]" file add))
969 (write-region (point-min) (point-max) file nil 'silent)
970 (auth-source-do-debug
971 "auth-source-netrc-create: wrote 1 new line to %s"
974 (list artificial)))))
976 ;;; Backend specific parsing: Secrets API backend
978 ;;; (let ((auth-sources '(default))) (auth-source-search :max 1 :create t))
979 ;;; (let ((auth-sources '(default))) (auth-source-search :max 1 :delete t))
980 ;;; (let ((auth-sources '(default))) (auth-source-search :max 1))
981 ;;; (let ((auth-sources '(default))) (auth-source-search))
982 ;;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1))
983 ;;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1 :signon_realm "https://git.gnus.org/Git"))
985 (defun* auth-source-secrets-search (&rest
987 &key backend create delete label
988 type max host user protocol
990 "Search the Secrets API; spec is like `auth-source'.
992 The :label key specifies the item's label. It is the only key
993 that can specify a substring. Any :label value besides a string
994 will allow any label.
996 All other search keys must match exactly. If you need substring
997 matching, do a wider search and narrow it down yourself.
999 You'll get back all the properties of the token as a plist.
1001 Here's an example that looks for the first item in the 'Login'
1004 \(let ((auth-sources '(\"secrets:Login\")))
1005 (auth-source-search :max 1)
1007 Here's another that looks for the first item in the 'Login'
1008 Secrets collection whose label contains 'gnus':
1010 \(let ((auth-sources '(\"secrets:Login\")))
1011 (auth-source-search :max 1 :label \"gnus\")
1013 And this one looks for the first item in the 'Login' Secrets
1014 collection that's a Google Chrome entry for the git.gnus.org site
1015 authentication tokens:
1017 \(let ((auth-sources '(\"secrets:Login\")))
1018 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1022 (assert (not create) nil
1023 "The Secrets API auth-source backend doesn't support creation yet")
1025 ;; (secrets-delete-item coll elt)
1026 (assert (not delete) nil
1027 "The Secrets API auth-source backend doesn't support deletion yet")
1029 (let* ((coll (oref backend source))
1030 (max (or max 5000)) ; sanity check: default to stop at 5K
1031 (ignored-keys '(:create :delete :max :backend :label))
1032 (search-keys (loop for i below (length spec) by 2
1033 unless (memq (nth i spec) ignored-keys)
1034 collect (nth i spec)))
1035 ;; build a search spec without the ignored keys
1036 ;; if a search key is nil or t (match anything), we skip it
1037 (search-spec (apply 'append (mapcar
1039 (if (or (null (plist-get spec k))
1040 (eq t (plist-get spec k)))
1042 (list k (plist-get spec k))))
1044 ;; needed keys (always including host, login, protocol, and secret)
1045 (returned-keys (delete-dups (append
1046 '(:host :login :protocol :secret)
1048 (items (loop for item in (apply 'secrets-search-items coll search-spec)
1049 unless (and (stringp label)
1050 (not (string-match label item)))
1052 ;; TODO: respect max in `secrets-search-items', not after the fact
1053 (items (butlast items (- (length items) max)))
1054 ;; convert the item name to a full plist
1055 (items (mapcar (lambda (item)
1057 ;; make an entry for the secret (password) element
1060 (lexical-let ((v (secrets-get-secret coll item)))
1062 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1064 (mapcar (lambda (entry)
1065 (list (car entry) (cdr entry)))
1066 (secrets-get-attributes coll item)))))
1068 ;; ensure each item has each key in `returned-keys'
1069 (items (mapcar (lambda (plist)
1072 (mapcar (lambda (req)
1073 (if (plist-get plist req)
1081 (defun* auth-source-secrets-create (&rest
1083 &key backend type max host user protocol
1086 ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1091 ;;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
1093 ;; deprecate the old interface
1094 (make-obsolete 'auth-source-user-or-password
1095 'auth-source-search "Emacs 24.1")
1096 (make-obsolete 'auth-source-forget-user-or-password
1097 'auth-source-forget "Emacs 24.1")
1099 (defun auth-source-user-or-password
1100 (mode host protocol &optional username create-missing delete-existing)
1101 "Find MODE (string or list of strings) matching HOST and PROTOCOL.
1103 DEPRECATED in favor of `auth-source-search'!
1105 USERNAME is optional and will be used as \"login\" in a search
1106 across the Secret Service API (see secrets.el) if the resulting
1107 items don't have a username. This means that if you search for
1108 username \"joe\" and it matches an item but the item doesn't have
1109 a :user attribute, the username \"joe\" will be returned.
1111 A non nil DELETE-EXISTING means deleting any matching password
1112 entry in the respective sources. This is useful only when
1113 CREATE-MISSING is non nil as well; the intended use case is to
1114 remove wrong password entries.
1116 If no matching entry is found, and CREATE-MISSING is non nil,
1117 the password will be retrieved interactively, and it will be
1118 stored in the password database which matches best (see
1121 MODE can be \"login\" or \"password\"."
1122 (auth-source-do-debug
1123 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
1124 mode host protocol username)
1126 (let* ((listy (listp mode))
1127 (mode (if listy mode (list mode)))
1129 (format "%s %s:%s %s" mode host protocol username)
1130 (format "%s %s:%s" mode host protocol)))
1131 (search (list :host host :protocol protocol))
1132 (search (if username (append search (list :user username)) search))
1133 (search (if create-missing
1134 (append search (list :create t))
1136 (search (if delete-existing
1137 (append search (list :delete t))
1139 ;; (found (if (not delete-existing)
1140 ;; (gethash cname auth-source-cache)
1141 ;; (remhash cname auth-source-cache)
1146 (auth-source-do-debug
1147 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
1149 ;; don't show the password
1150 (if (and (member "password" mode) t)
1153 host protocol username)
1154 found) ; return the found data
1155 ;; else, if not found, search with a max of 1
1156 (let ((choice (nth 0 (apply 'auth-source-search
1157 (append '(:max 1) search)))))
1161 ((equal "password" m)
1162 (push (if (plist-get choice :secret)
1163 (funcall (plist-get choice :secret))
1166 (push (plist-get choice :user) found)))))
1167 (setq found (nreverse found))
1168 (setq found (if listy found (car-safe found)))))
1172 (provide 'auth-source)
1174 ;;; auth-source.el ends here