1 ;;; auth-source.el --- authentication sources for Gnus and Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 2008-2018 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 <https://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
43 (require 'password-cache
)
45 (eval-when-compile (require 'cl-lib
))
48 (autoload 'secrets-create-item
"secrets")
49 (autoload 'secrets-delete-item
"secrets")
50 (autoload 'secrets-get-alias
"secrets")
51 (autoload 'secrets-get-attributes
"secrets")
52 (autoload 'secrets-get-secret
"secrets")
53 (autoload 'secrets-list-collections
"secrets")
54 (autoload 'secrets-search-items
"secrets")
56 (autoload 'rfc2104-hash
"rfc2104")
58 (autoload 'plstore-open
"plstore")
59 (autoload 'plstore-find
"plstore")
60 (autoload 'plstore-put
"plstore")
61 (autoload 'plstore-delete
"plstore")
62 (autoload 'plstore-save
"plstore")
63 (autoload 'plstore-get-file
"plstore")
65 (eval-when-compile (require 'epg
)) ;; setf-method for `epg-context-armor'
66 (autoload 'epg-make-context
"epg")
67 (autoload 'epg-context-set-passphrase-callback
"epg")
68 (autoload 'epg-decrypt-string
"epg")
69 (autoload 'epg-encrypt-string
"epg")
71 (autoload 'help-mode
"help-mode" nil t
)
73 (defvar secrets-enabled
)
75 (defgroup auth-source nil
76 "Authentication sources."
77 :version
"23.1" ;; No Gnus
81 (defcustom auth-source-cache-expiry
7200
82 "How many seconds passwords are cached, or nil to disable
83 expiring. Overrides `password-cache-expiry' through a
87 :type
'(choice (const :tag
"Never" nil
)
88 (const :tag
"All Day" 86400)
89 (const :tag
"2 Hours" 7200)
90 (const :tag
"30 Minutes" 1800)
91 (integer :tag
"Seconds")))
93 ;; The slots below correspond with the `auth-source-search' spec,
94 ;; so a backend with :host set, for instance, would match only
95 ;; searches for that host. Normally they are nil.
96 (defclass auth-source-backend
()
101 :documentation
"The backend type.")
102 (source :initarg
:source
105 :documentation
"The backend source.")
110 :documentation
"The backend host.")
115 :documentation
"The backend user.")
120 :documentation
"The backend protocol.")
123 :documentation
"Internal backend data.")
124 (create-function :initarg
:create-function
128 :documentation
"The create function.")
129 (search-function :initarg
:search-function
133 :documentation
"The search function.")))
135 (defcustom auth-source-protocols
'((imap "imap" "imaps" "143" "993")
136 (pop3 "pop3" "pop" "pop3s" "110" "995")
140 "List of authentication protocols and their names"
143 :version
"23.2" ;; No Gnus
144 :type
'(repeat :tag
"Authentication Protocols"
145 (cons :tag
"Protocol Entry"
146 (symbol :tag
"Protocol")
148 (string :tag
"Name")))))
150 ;; Generate all the protocols in a format Customize can use.
151 ;; TODO: generate on the fly from auth-source-protocols
152 (defconst auth-source-protocols-customize
154 (let ((p (car-safe a
)))
156 :tag
(upcase (symbol-name p
))
158 auth-source-protocols
))
160 (defvar auth-source-creation-defaults nil
161 ;; FIXME: AFAICT this is not set (or let-bound) anywhere!
162 "Defaults for creating token values. Usually let-bound.")
164 (defvar auth-source-creation-prompts nil
165 "Default prompts for token values. Usually let-bound.")
167 (make-obsolete 'auth-source-hide-passwords nil
"Emacs 24.1")
169 (defcustom auth-source-save-behavior
'ask
170 "If set, auth-source will respect it for save behavior."
172 :version
"23.2" ;; No Gnus
174 :tag
"auth-source new token save behavior"
175 (const :tag
"Always save" t
)
176 (const :tag
"Never save" nil
)
177 (const :tag
"Ask" ask
)))
179 ;; TODO: make the default (setq auth-source-netrc-use-gpg-tokens `((,(if (boundp 'epa-file-auto-mode-alist-entry) (car epa-file-auto-mode-alist-entry) "\\.gpg\\'") never) (t gpg)))
180 ;; TODO: or maybe leave as (setq auth-source-netrc-use-gpg-tokens 'never)
182 (defcustom auth-source-netrc-use-gpg-tokens
'never
183 "Set this to tell auth-source when to create GPG password
184 tokens in netrc files. It's either an alist or `never'.
185 Note that if EPA/EPG is not available, this should NOT be used."
187 :version
"23.2" ;; No Gnus
189 (const :tag
"Always use GPG password tokens" (t gpg
))
190 (const :tag
"Never use GPG password tokens" never
)
191 (repeat :tag
"Use a lookup list"
193 (choice :tag
"Matcher"
194 (const :tag
"Match anything" t
)
195 (const :tag
"The EPA encrypted file extensions"
196 ,(if (boundp 'epa-file-auto-mode-alist-entry
)
197 (car epa-file-auto-mode-alist-entry
)
199 (regexp :tag
"Regular expression"))
200 (choice :tag
"What to do"
201 (const :tag
"Save GPG-encrypted password tokens" gpg
)
202 (const :tag
"Don't encrypt tokens" never
))))))
204 (defcustom auth-source-do-cache t
205 "Whether auth-source should cache information with `password-cache'."
207 :version
"23.2" ;; No Gnus
210 (defcustom auth-source-debug nil
211 "Whether auth-source should log debug messages.
213 If the value is nil, debug messages are not logged.
215 If the value is t, debug messages are logged with `message'. In
216 that case, your authentication data will be in the clear (except
219 If the value is a function, debug messages are logged by calling
220 that function using the same arguments as `message'."
222 :version
"23.2" ;; No Gnus
224 :tag
"auth-source debugging mode"
225 (const :tag
"Log using `message' to the *Messages* buffer" t
)
226 (const :tag
"Log all trivia with `message' to the *Messages* buffer"
228 (function :tag
"Function that takes arguments like `message'")
229 (const :tag
"Don't log anything" nil
)))
231 (defcustom auth-sources
'("~/.authinfo" "~/.authinfo.gpg" "~/.netrc")
232 "List of authentication sources.
233 Each entry is the authentication type with optional properties.
234 Entries are tried in the order in which they appear.
235 See Info node `(auth)Help for users' for details.
237 If an entry names a file with the \".gpg\" extension and you have
238 EPA/EPG set up, the file will be encrypted and decrypted
239 automatically. See Info node `(epa)Encrypting/decrypting gpg files'
242 It's best to customize this with `\\[customize-variable]' because the choices
243 can get pretty complex."
245 :version
"24.1" ;; No Gnus
246 :type
`(repeat :tag
"Authentication Sources"
248 (string :tag
"Just a file")
249 (const :tag
"Default Secrets API Collection" default
)
250 (const :tag
"Login Secrets API Collection" "secrets:Login")
251 (const :tag
"Temp Secrets API Collection" "secrets:session")
253 (const :tag
"Default internet Mac OS Keychain"
254 macos-keychain-internet
)
256 (const :tag
"Default generic Mac OS Keychain"
257 macos-keychain-generic
)
259 (list :tag
"Source definition"
260 (const :format
"" :value
:source
)
261 (choice :tag
"Authentication backend choice"
262 (string :tag
"Authentication Source (file)")
264 :tag
"Secret Service API/KWallet/GNOME Keyring"
265 (const :format
"" :value
:secrets
)
266 (choice :tag
"Collection to use"
267 (string :tag
"Collection name")
268 (const :tag
"Default" default
)
269 (const :tag
"Login" "Login")
271 :tag
"Temporary" "session")))
273 :tag
"Mac OS internet Keychain"
275 :value
:macos-keychain-internet
)
276 (choice :tag
"Collection to use"
277 (string :tag
"internet Keychain path")
278 (const :tag
"default" default
)))
280 :tag
"Mac OS generic Keychain"
282 :value
:macos-keychain-generic
)
283 (choice :tag
"Collection to use"
284 (string :tag
"generic Keychain path")
285 (const :tag
"default" default
))))
286 (repeat :tag
"Extra Parameters" :inline t
287 (choice :tag
"Extra parameter"
290 (const :format
"" :value
:host
)
291 (choice :tag
"Host (machine) choice"
294 :tag
"Regular expression")))
297 (const :format
"" :value
:port
)
301 ,@auth-source-protocols-customize
))
302 (list :tag
"User" :inline t
303 (const :format
"" :value
:user
)
305 :tag
"Personality/Username"
310 (defcustom auth-source-gpg-encrypt-to t
311 "List of recipient keys that `authinfo.gpg' encrypted to.
312 If the value is not a list, symmetric encryption will be used."
314 :version
"24.1" ;; No Gnus
315 :type
'(choice (const :tag
"Symmetric encryption" t
)
316 (repeat :tag
"Recipient public keys"
317 (string :tag
"Recipient public key"))))
319 (defun auth-source-do-debug (&rest msg
)
320 (when auth-source-debug
321 (apply #'auth-source-do-warn msg
)))
323 (defun auth-source-do-trivia (&rest msg
)
324 (when (or (eq auth-source-debug
'trivia
)
325 (functionp auth-source-debug
))
326 (apply #'auth-source-do-warn msg
)))
328 (defun auth-source-do-warn (&rest msg
)
330 ;; set logger to either the function in auth-source-debug or 'message
331 ;; note that it will be 'message if auth-source-debug is nil
332 (if (functionp auth-source-debug
)
337 (defun auth-source-read-char-choice (prompt choices
)
338 "Read one of CHOICES by `read-char-choice', or `read-char'.
339 `dropdown-list' support is disabled because it doesn't work reliably.
340 Only one of CHOICES will be returned. The PROMPT is augmented
341 with \"[a/b/c] \" if CHOICES is \(?a ?b ?c)."
343 (let* ((prompt-choices
345 (cl-loop for c in choices collect
(format "%c/" c
))))
346 (prompt-choices (concat "[" (substring prompt-choices
0 -
1) "] "))
347 (full-prompt (concat prompt prompt-choices
))
350 (while (not (memq k choices
))
351 (setq k
(read-char-choice full-prompt choices
)))
354 (defvar auth-source-backend-parser-functions nil
355 "List of auth-source parser functions.
356 Each function takes an entry from `auth-sources' as parameter and
357 returns a backend or nil if the entry is not supported. Add a
358 parser function to this list with `add-hook'. Searching for a
359 backend starts with the first element on the list and stops as
360 soon as a function returns non-nil.")
362 (defun auth-source-backend-parse (entry)
363 "Create an auth-source-backend from an ENTRY in `auth-sources'."
366 (cl-dolist (f auth-source-backend-parser-functions
)
367 (when (setq backend
(funcall f entry
))
371 ;; none of the parsers worked
373 "auth-source-backend-parse: invalid backend spec: %S" entry
)
374 (setq backend
(make-instance 'auth-source-backend
377 (auth-source-backend-parse-parameters entry backend
)))
379 (defun auth-source-backends-parser-file (entry)
380 ;; take just a file name use it as a netrc/plist file
381 ;; matching any user, host, and protocol
382 (when (stringp entry
)
383 (setq entry
(list :source entry
)))
384 (let* ((source (plist-get entry
:source
))
386 (if (and (stringp source
)
387 (equal (file-name-extension source
) "gpg"))
388 (file-name-sans-extension source
)
390 (extension (or (and (stringp source-without-gpg
)
391 (file-name-extension source-without-gpg
))
393 (when (stringp source
)
395 ((equal extension
"plist")
400 :search-function
#'auth-source-plstore-search
401 :create-function
#'auth-source-plstore-create
402 :data
(plstore-open source
)))
403 ((member-ignore-case extension
'("json"))
408 :search-function
#'auth-source-json-search
))
414 :search-function
#'auth-source-netrc-search
415 :create-function
#'auth-source-netrc-create
))))))
417 ;; Note this function should be last in the parser functions, so we add it first
418 (add-hook 'auth-source-backend-parser-functions
'auth-source-backends-parser-file
)
420 (defun auth-source-backends-parser-macos-keychain (entry)
421 ;; take macos-keychain-{internet,generic}:XYZ and use it as macOS
422 ;; Keychain "XYZ" matching any user, host, and protocol
423 (when (and (stringp entry
) (string-match "^macos-keychain-internet:\\(.+\\)"
425 (setq entry
`(:source
(:macos-keychain-internet
426 ,(match-string 1 entry
)))))
427 (when (and (stringp entry
) (string-match "^macos-keychain-generic:\\(.+\\)"
429 (setq entry
`(:source
(:macos-keychain-generic
430 ,(match-string 1 entry
)))))
431 ;; take 'macos-keychain-internet or generic and use it as a Mac OS
432 ;; Keychain collection matching any user, host, and protocol
433 (when (eq entry
'macos-keychain-internet
)
434 (setq entry
'(:source
(:macos-keychain-internet default
))))
435 (when (eq entry
'macos-keychain-generic
)
436 (setq entry
'(:source
(:macos-keychain-generic default
))))
438 ;; the macOS Keychain
440 (not (null (plist-get entry
:source
))) ; the source must not be nil
441 (listp (plist-get entry
:source
)) ; and it must be a list
443 (plist-get (plist-get entry
:source
) :macos-keychain-generic
)
444 (plist-get (plist-get entry
:source
) :macos-keychain-internet
)))
446 (let* ((source-spec (plist-get entry
:source
))
447 (keychain-generic (plist-get source-spec
:macos-keychain-generic
))
448 (keychain-type (if keychain-generic
449 'macos-keychain-generic
450 'macos-keychain-internet
))
451 (source (plist-get source-spec
(if keychain-generic
452 :macos-keychain-generic
453 :macos-keychain-internet
))))
455 (when (symbolp source
)
456 (setq source
(symbol-name source
)))
459 (format "Mac OS Keychain (%s)" source
)
462 :search-function
#'auth-source-macos-keychain-search
463 :create-function
#'auth-source-macos-keychain-create
)))))
465 (add-hook 'auth-source-backend-parser-functions
'auth-source-backends-parser-macos-keychain
)
467 (defun auth-source-backends-parser-secrets (entry)
468 ;; take secrets:XYZ and use it as Secrets API collection "XYZ"
469 ;; matching any user, host, and protocol
470 (when (and (stringp entry
) (string-match "^secrets:\\(.+\\)" entry
))
471 (setq entry
`(:source
(:secrets
,(match-string 1 entry
)))))
472 ;; take 'default and use it as a Secrets API default collection
473 ;; matching any user, host, and protocol
474 (when (eq entry
'default
)
475 (setq entry
'(:source
(:secrets default
))))
477 ;; the Secrets API. We require the package, in order to have a
478 ;; defined value for `secrets-enabled'.
480 (not (null (plist-get entry
:source
))) ; the source must not be nil
481 (listp (plist-get entry
:source
)) ; and it must be a list
482 (not (null (plist-get
483 (plist-get entry
:source
)
484 :secrets
))) ; the source must have :secrets
485 (require 'secrets nil t
) ; and we must load the Secrets API
486 secrets-enabled
) ; and that API must be enabled
488 ;; the source is either the :secrets key in ENTRY or
489 ;; if that's missing or nil, it's "session"
490 (let ((source (plist-get (plist-get entry
:source
) :secrets
)))
492 ;; if the source is a symbol, we look for the alias named so,
493 ;; and if that alias is missing, we use "Login"
494 (when (symbolp source
)
495 (setq source
(or (secrets-get-alias (symbol-name source
))
498 (if (featurep 'secrets
)
500 (format "Secrets API (%s)" source
)
503 :search-function
#'auth-source-secrets-search
504 :create-function
#'auth-source-secrets-create
)
506 "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry
)
508 (format "Ignored Secrets API (%s)" source
)
512 (add-hook 'auth-source-backend-parser-functions
'auth-source-backends-parser-secrets
)
514 (defun auth-source-backend-parse-parameters (entry backend
)
515 "Fills in the extra auth-source-backend parameters of ENTRY.
516 Using the plist ENTRY, get the :host, :port, and :user search
518 (let ((entry (if (stringp entry
)
522 (when (setq val
(plist-get entry
:host
))
523 (oset backend host val
))
524 (when (setq val
(plist-get entry
:user
))
525 (oset backend user val
))
526 (when (setq val
(plist-get entry
:port
))
527 (oset backend port val
)))
530 ;; (mapcar 'auth-source-backend-parse auth-sources)
532 (cl-defun auth-source-search (&rest spec
533 &key max require create delete
535 "Search or modify authentication backends according to SPEC.
537 This function parses `auth-sources' for matches of the SPEC
538 plist. It can optionally create or update an authentication
539 token if requested. A token is just a standard Emacs property
540 list with a :secret property that can be a function; all the
541 other properties will always hold scalar values.
543 Typically the :secret property, if present, contains a password.
545 Common search keys are :max, :host, :port, and :user. In
546 addition, :create specifies if and how tokens will be created.
547 Finally, :type can specify which backend types you want to check.
549 A string value is always matched literally. A symbol is matched
550 as its string value, literally. All the SPEC values can be
551 single values (symbol or string) or lists thereof (in which case
552 any of the search terms matches).
554 :create t means to create a token if possible.
556 A new token will be created if no matching tokens were found.
557 The new token will have only the keys the backend requires. For
558 the netrc backend, for instance, that's the user, host, and
563 \(let ((auth-source-creation-defaults \\='((user . \"defaultUser\")
564 (A . \"default A\"))))
565 (auth-source-search :host \"mine\" :type \\='netrc :max 1
566 :P \"pppp\" :Q \"qqqq\"
571 \"Search for any entry matching host `mine' in backends of type
572 `netrc', maximum one result.
574 Create a new entry if you found none. The netrc backend will
575 automatically require host, user, and port. The host will be
576 `mine'. We prompt for the user with default `defaultUser' and
577 for the port without a default. We will not prompt for A, Q,
578 or P. The resulting token will only have keys user, host, and
581 :create \\='(A B C) also means to create a token if possible.
583 The behavior is like :create t but if the list contains any
584 parameter, that parameter will be required in the resulting
585 token. The value for that parameter will be obtained from the
586 search parameters or from user input. If any queries are needed,
587 the alist `auth-source-creation-defaults' will be checked for the
588 default value. If the user, host, or port are missing, the alist
589 `auth-source-creation-prompts' will be used to look up the
590 prompts IN THAT ORDER (so the `user' prompt will be queried first,
591 then `host', then `port', and finally `secret'). Each prompt string
592 can use %u, %h, and %p to show the user, host, and port.
596 \(let ((auth-source-creation-defaults \\='((user . \"defaultUser\")
597 (A . \"default A\")))
598 (auth-source-creation-prompts
599 \\='((password . \"Enter IMAP password for %h:%p: \"))))
600 (auth-source-search :host \\='(\"nonesuch\" \"twosuch\") :type \\='netrc :max 1
601 :P \"pppp\" :Q \"qqqq\"
602 :create \\='(A B Q)))
606 \"Search for any entry matching host `nonesuch'
607 or `twosuch' in backends of type `netrc', maximum one result.
609 Create a new entry if you found none. The netrc backend will
610 automatically require host, user, and port. The host will be
611 `nonesuch' and Q will be `qqqq'. We prompt for the password
612 with the shown prompt. We will not prompt for Q. The resulting
613 token will have keys user, host, port, A, B, and Q. It will not
614 have P with any value, even though P is used in the search to
615 find only entries that have P set to `pppp'.\"
617 When multiple values are specified in the search parameter, the
618 user is prompted for which one. So :host (X Y Z) would ask the
619 user to choose between X, Y, and Z.
621 This creation can fail if the search was not specific enough to
622 create a new token (it's up to the backend to decide that). You
623 should `catch' the backend-specific error as usual. Some
624 backends (netrc, at least) will prompt the user rather than throw
627 :require (A B C) means that only results that contain those
628 tokens will be returned. Thus for instance requiring :secret
629 will ensure that any results will actually have a :secret
632 :delete t means to delete any found entries. nil by default.
633 Use `auth-source-delete' in ELisp code instead of calling
634 `auth-source-search' directly with this parameter.
636 :type (X Y Z) will check only those backend types. `netrc' and
637 `secrets' are the only ones supported right now.
639 :max N means to try to return at most N items (defaults to 1).
640 More than N items may be returned, depending on the search and
643 When :max is 0 the function will return just t or nil to indicate
644 if any matches were found.
646 :host (X Y Z) means to match only hosts X, Y, or Z according to
647 the match rules above. Defaults to t.
649 :user (X Y Z) means to match only users X, Y, or Z according to
650 the match rules above. Defaults to t.
652 :port (P Q R) means to match only protocols P, Q, or R.
655 :K (V1 V2 V3) for any other key K will match values V1, V2, or
656 V3 (note the match rules above).
658 The return value is a list with at most :max tokens. Each token
659 is a plist with keys :backend :host :port :user, plus any other
660 keys provided by the backend (notably :secret). But note the
661 exception for :max 0, which see above.
663 The token can hold a :save-function key. If you call that, the
664 user will be prompted to save the data to the backend. You can't
665 request that this should happen right after creation, because
666 `auth-source-search' has no way of knowing if the token is
667 actually useful. So the caller must arrange to call this function.
669 The token's :secret key can hold a function. In that case you
670 must call it to obtain the actual value."
671 (let* ((backends (mapcar #'auth-source-backend-parse auth-sources
))
673 (ignored-keys '(:require
:create
:delete
:max
))
674 (keys (cl-loop for i below
(length spec
) by
2
675 unless
(memq (nth i spec
) ignored-keys
)
676 collect
(nth i spec
)))
677 (cached (auth-source-remembered-p spec
))
678 ;; note that we may have cached results but found is still nil
679 ;; (there were no results from the search)
680 (found (auth-source-recall spec
))
683 (if (and cached auth-source-do-cache
)
684 (auth-source-do-debug
685 "auth-source-search: found %d CACHED results matching %S"
689 (or (eq t create
) (listp create
)) t
690 "Invalid auth-source :create parameter (must be t or a list): %s %s")
694 "Invalid auth-source :require parameter (must be a list): %s")
696 (setq filtered-backends
(copy-sequence backends
))
697 (dolist (backend backends
)
698 (cl-dolist (key keys
)
699 ;; ignore invalid slots
701 (unless (auth-source-search-collection
703 (slot-value backend key
))
704 (setq filtered-backends
(delq backend filtered-backends
))
706 (invalid-slot-name nil
))))
708 (auth-source-do-trivia
709 "auth-source-search: found %d backends matching %S"
710 (length filtered-backends
) spec
)
712 ;; (debug spec "filtered" filtered-backends)
713 ;; First go through all the backends without :create, so we can
715 (setq found
(auth-source-search-backends filtered-backends
719 ;; create is always nil here
723 (auth-source-do-debug
724 "auth-source-search: found %d results (max %d) matching %S"
725 (length found
) max spec
)
727 ;; If we didn't find anything, then we allow the backend(s) to
728 ;; create the entries.
731 (setq found
(auth-source-search-backends filtered-backends
737 (auth-source-do-debug
738 "auth-source-search: CREATED %d results (max %d) matching %S"
739 (length found
) max spec
))
741 ;; note we remember the lack of result too, if it's applicable
742 (when auth-source-do-cache
743 (auth-source-remember spec found
)))
749 (defun auth-source-search-backends (backends spec max create delete require
)
750 (let ((max (if (zerop max
) 1 max
)) ; stop with 1 match if we're asked for zero
752 (dolist (backend backends
)
753 (when (> max
(length matches
)) ; if we need more matches...
754 (let* ((bmatches (apply
755 (slot-value backend
'search-function
)
757 :type
(slot-value backend
'type
)
758 ;; note we're overriding whatever the spec
759 ;; has for :max, :require, :create, and :delete
766 (auth-source-do-trivia
767 "auth-source-search-backend: got %d (max %d) in %s:%s matching %S"
768 (length bmatches
) max
769 (slot-value backend
'type
)
770 (slot-value backend
'source
)
772 (setq matches
(append matches bmatches
))))))
775 (defun auth-source-delete (&rest spec
)
776 "Delete entries from the authentication backends according to SPEC.
777 Calls `auth-source-search' with the :delete property in SPEC set to t.
778 The backend may not actually delete the entries.
780 Returns the deleted entries."
781 (auth-source-search (plist-put spec
:delete t
)))
783 (defun auth-source-search-collection (collection value
)
784 "Returns t is VALUE is t or COLLECTION is t or COLLECTION contains VALUE."
785 (when (and (atom collection
) (not (eq t collection
)))
786 (setq collection
(list collection
)))
788 ;; (debug :collection collection :value value)
789 (or (eq collection t
)
791 (equal collection value
)
792 (member value collection
)))
794 (defvar auth-source-netrc-cache nil
)
796 (defun auth-source-forget-all-cached ()
797 "Forget all cached auth-source data."
799 (maphash (lambda (key _password
)
800 (when (eq 'auth-source
(car-safe key
))
802 (password-cache-remove key
)))
804 (setq auth-source-netrc-cache nil
))
806 (defun auth-source-format-cache-entry (spec)
807 "Format SPEC entry to put it in the password cache."
808 `(auth-source .
,spec
))
810 (defun auth-source-remember (spec found
)
811 "Remember FOUND search results for SPEC."
812 (let ((password-cache-expiry auth-source-cache-expiry
))
814 (auth-source-format-cache-entry spec
) found
)))
816 (defun auth-source-recall (spec)
817 "Recall FOUND search results for SPEC."
818 (password-read-from-cache (auth-source-format-cache-entry spec
)))
820 (defun auth-source-remembered-p (spec)
821 "Check if SPEC is remembered."
823 (auth-source-format-cache-entry spec
)))
825 (defun auth-source-forget (spec)
826 "Forget any cached data matching SPEC exactly.
828 This is the same SPEC you passed to `auth-source-search'.
829 Returns t or nil for forgotten or not found."
830 (password-cache-remove (auth-source-format-cache-entry spec
)))
832 (defun auth-source-forget+ (&rest spec
)
833 "Forget any cached data matching SPEC. Returns forgotten count.
835 This is not a full `auth-source-search' spec but works similarly.
836 For instance, \(:host \"myhost\" \"yourhost\") would find all the
837 cached data that was found with a search for those two hosts,
838 while \(:host t) would find all host entries."
841 (lambda (key _password
)
842 (when (and (eq 'auth-source
(car-safe key
))
843 ;; and the spec matches what was stored in the cache
844 (auth-source-specmatchp spec
(cdr key
)))
846 (password-cache-remove key
)
851 (defun auth-source-specmatchp (spec stored
)
852 (let ((keys (cl-loop for i below
(length spec
) by
2
853 collect
(nth i spec
))))
855 (cl-dolist (key keys
)
856 (unless (auth-source-search-collection (plist-get stored key
)
857 (plist-get spec key
))
861 (defun auth-source-pick-first-password (&rest spec
)
862 "Pick the first secret found from applying SPEC to `auth-source-search'."
863 (let* ((result (nth 0 (apply #'auth-source-search
(plist-put spec
:max
1))))
864 (secret (plist-get result
:secret
)))
866 (if (functionp secret
)
870 (defun auth-source-format-prompt (prompt alist
)
871 "Format PROMPT using %x (for any character x) specifiers in ALIST."
873 (let ((c (nth 0 cell
))
876 (setq prompt
(replace-regexp-in-string (format "%%%c" c
)
881 (defun auth-source-ensure-strings (values)
884 (unless (listp values
)
885 (setq values
(list values
)))
886 (mapcar (lambda (value)
892 ;;; Backend specific parsing: netrc/authinfo backend
894 (defun auth-source--aput-1 (alist key val
)
897 (while (and (consp rest
) (not (equal key
(caar rest
))))
898 (push (pop rest
) seen
))
900 (if (null rest
) alist
901 (nconc (nreverse seen
)
902 (if (equal key
(caar rest
)) (cdr rest
) rest
))))))
903 (defmacro auth-source--aput
(var key val
)
904 `(setq ,var
(auth-source--aput-1 ,var
,key
,val
)))
906 (defun auth-source--aget (alist key
)
907 (cdr (assoc key alist
)))
909 ;; (auth-source-netrc-parse :file "~/.authinfo.gpg")
910 (cl-defun auth-source-netrc-parse (&key file max host user port require
912 "Parse FILE and return a list of all entries in the file.
913 Note that the MAX parameter is used so we can exit the parse early."
915 ;; We got already parsed contents; just return it.
917 (when (file-exists-p file
)
918 (setq port
(auth-source-ensure-strings port
))
920 (let* ((max (or max
5000)) ; sanity check: default to stop at 5K
922 (cached (cdr-safe (assoc file auth-source-netrc-cache
)))
923 (cached-mtime (plist-get cached
:mtime
))
924 (cached-secrets (plist-get cached
:secret
))
925 (check (lambda(alist)
927 (auth-source-search-collection
930 (auth-source--aget alist
"machine")
931 (auth-source--aget alist
"host")
933 (auth-source-search-collection
936 (auth-source--aget alist
"login")
937 (auth-source--aget alist
"account")
938 (auth-source--aget alist
"user")
940 (auth-source-search-collection
943 (auth-source--aget alist
"port")
944 (auth-source--aget alist
"protocol")
947 ;; the required list of keys is nil, or
949 ;; every element of require is in n (normalized)
950 (let ((n (nth 0 (auth-source-netrc-normalize
951 (list alist
) file
))))
952 (cl-loop for req in require
953 always
(plist-get n req
)))))))
956 (if (and (functionp cached-secrets
)
958 (nth 5 (file-attributes file
))))
960 (auth-source-do-trivia
961 "auth-source-netrc-parse: using CACHED file data for %s"
963 (insert (funcall cached-secrets
)))
964 (insert-file-contents file
)
965 ;; cache all netrc files (used to be just .gpg files)
966 ;; Store the contents of the file heavily encrypted in memory.
967 ;; (note for the irony-impaired: they are just obfuscated)
969 auth-source-netrc-cache file
970 (list :mtime
(nth 5 (file-attributes file
))
971 :secret
(let ((v (mapcar #'1+ (buffer-string))))
972 (lambda () (apply #'string
(mapcar #'1- v
)))))))
973 (goto-char (point-min))
974 (let ((entries (auth-source-netrc-parse-entries check max
))
976 (while (setq alist
(pop entries
))
977 (push (nreverse alist
) result
)))
980 (when auth-source-gpg-encrypt-to
981 ;; (see bug#7487) making `epa-file-encrypt-to' local to
982 ;; this buffer lets epa-file skip the key selection query
983 ;; (see the `local-variable-p' check in
984 ;; `epa-file-write-region').
985 (unless (local-variable-p 'epa-file-encrypt-to
(current-buffer))
986 (make-local-variable 'epa-file-encrypt-to
))
987 (if (listp auth-source-gpg-encrypt-to
)
988 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to
)))
990 ;; ask AFTER we've successfully opened the file
991 (when (y-or-n-p (format "Save file %s? (%d deletions)"
993 (write-region (point-min) (point-max) file nil
'silent
)
994 (auth-source-do-debug
995 "auth-source-netrc-parse: modified %d lines in %s"
998 (nreverse result
))))))
1000 (defun auth-source-netrc-parse-next-interesting ()
1001 "Advance to the next interesting position in the current buffer."
1002 ;; If we're looking at a comment or are at the end of the line, move forward
1003 (while (or (looking-at "#")
1007 (skip-chars-forward "\t "))
1009 (defun auth-source-netrc-parse-one ()
1010 "Read one thing from the current buffer."
1011 (auth-source-netrc-parse-next-interesting)
1013 (when (or (looking-at "'\\([^']*\\)'")
1014 (looking-at "\"\\([^\"]*\\)\"")
1015 (looking-at "\\([^ \t\n]+\\)"))
1016 (forward-char (length (match-string 0)))
1017 (auth-source-netrc-parse-next-interesting)
1018 (match-string-no-properties 1)))
1020 ;; with thanks to org-mode
1021 (defsubst auth-source-current-line
(&optional pos
)
1023 (and pos
(goto-char pos
))
1024 ;; works also in narrowed buffer, because we start at 1, not point-min
1025 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
1027 (defun auth-source-netrc-parse-entries(check max
)
1028 "Parse up to MAX netrc entries, passed by CHECK, from the current buffer."
1029 (let ((adder (lambda(check alist all
)
1032 (> max
(length all
))
1033 (funcall check alist
))
1036 item item2 all alist default
)
1037 (while (setq item
(auth-source-netrc-parse-one))
1038 (setq default
(equal item
"default"))
1039 ;; We're starting a new machine. Save the old one.
1042 (equal item
"machine")))
1043 ;; (auth-source-do-trivia
1044 ;; "auth-source-netrc-parse-entries: got entry %S" alist)
1045 (setq all
(funcall adder check alist all
)
1047 ;; In default entries, we don't have a next token.
1048 ;; We store them as ("machine" . t)
1050 (push (cons "machine" t
) alist
)
1051 ;; Not a default entry. Grab the next item.
1052 (when (setq item2
(auth-source-netrc-parse-one))
1053 ;; Did we get a "machine" value?
1054 (if (equal item2
"machine")
1056 "%s: Unexpected `machine' token at line %d"
1057 "auth-source-netrc-parse-entries"
1058 (auth-source-current-line))
1059 (push (cons item item2
) alist
)))))
1061 ;; Clean up: if there's an entry left over, use it.
1063 (setq all
(funcall adder check alist all
))
1064 ;; (auth-source-do-trivia
1065 ;; "auth-source-netrc-parse-entries: got2 entry %S" alist)
1069 (defvar auth-source-passphrase-alist nil
)
1071 (defun auth-source-token-passphrase-callback-function (_context _key-id file
)
1072 (let* ((file (file-truename file
))
1073 (entry (assoc file auth-source-passphrase-alist
))
1075 ;; return the saved passphrase, calling a function if needed
1076 (or (copy-sequence (if (functionp (cdr entry
))
1077 (funcall (cdr entry
))
1081 (setq entry
(list file
))
1082 (push entry auth-source-passphrase-alist
))
1085 (format "Passphrase for %s tokens: " file
)
1087 (setcdr entry
(let ((p (copy-sequence passphrase
)))
1091 (defun auth-source-epa-extract-gpg-token (secret file
)
1092 "Pass either the decoded SECRET or the gpg:BASE64DATA version.
1093 FILE is the file from which we obtained this token."
1094 (when (string-match "^gpg:\\(.+\\)" secret
)
1095 (setq secret
(base64-decode-string (match-string 1 secret
))))
1096 (let ((context (epg-make-context 'OpenPGP
)))
1097 (epg-context-set-passphrase-callback
1099 (cons #'auth-source-token-passphrase-callback-function
1101 (epg-decrypt-string context secret
)))
1103 (defvar pp-escape-newlines
)
1105 (defun auth-source-epa-make-gpg-token (secret file
)
1106 (let ((context (epg-make-context 'OpenPGP
))
1107 (pp-escape-newlines nil
)
1109 (setf (epg-context-armor context
) t
)
1110 (epg-context-set-passphrase-callback
1112 (cons #'auth-source-token-passphrase-callback-function
1114 (setq cipher
(epg-encrypt-string context secret nil
))
1117 (base64-encode-region (point-min) (point-max) t
)
1118 (concat "gpg:" (buffer-substring-no-properties
1122 (defun auth-source--symbol-keyword (symbol)
1123 (intern (format ":%s" symbol
)))
1125 (defun auth-source-netrc-normalize (alist filename
)
1126 (mapcar (lambda (entry)
1128 (while (setq item
(pop entry
))
1129 (let ((k (car item
))
1132 ;; apply key aliases
1133 (setq k
(cond ((member k
'("machine")) "host")
1134 ((member k
'("login" "account")) "user")
1135 ((member k
'("protocol")) "port")
1136 ((member k
'("password")) "secret")
1139 ;; send back the secret in a function (lexical binding)
1140 (when (equal k
"secret")
1141 (setq v
(let ((lexv v
)
1142 (token-decoder nil
))
1143 (when (string-match "^gpg:" lexv
)
1144 ;; it's a GPG token: create a token decoder
1145 ;; which unsets itself once
1149 (auth-source-epa-extract-gpg-token
1152 (setq token-decoder nil
)))))
1155 (setq lexv
(funcall token-decoder lexv
)))
1157 (setq ret
(plist-put ret
1158 (auth-source--symbol-keyword k
)
1163 (cl-defun auth-source-netrc-search (&rest spec
1164 &key backend require create
1165 type max host user port
1167 "Given a property list SPEC, return search matches from the :backend.
1168 See `auth-source-search' for details on SPEC."
1169 ;; just in case, check that the type is correct (null or same as the backend)
1170 (cl-assert (or (null type
) (eq type
(oref backend type
)))
1171 t
"Invalid netrc search: %s %s")
1173 (let ((results (auth-source-netrc-normalize
1174 (auth-source-netrc-parse
1177 :file
(oref backend source
)
1181 (oref backend source
))))
1183 ;; if we need to create an entry AND none were found to match
1187 ;; create based on the spec and record the value
1189 ;; if the user did not want to create the entry
1190 ;; in the file, it will be returned
1191 (apply (slot-value backend
'create-function
) spec
)
1192 ;; if not, we do the search again without :create
1193 ;; to get the updated data.
1195 ;; the result will be returned, even if the search fails
1196 (apply #'auth-source-netrc-search
1197 (plist-put spec
:create nil
)))))
1200 (defun auth-source-netrc-element-or-first (v)
1205 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
1206 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))
1208 (cl-defun auth-source-netrc-create (&rest spec
1209 &key backend host port create
1211 (let* ((base-required '(host user port secret
))
1212 ;; we know (because of an assertion in auth-source-search) that the
1213 ;; :create parameter is either t or a list (which includes nil)
1214 (create-extra (if (eq t create
) nil create
))
1215 (current-data (car (auth-source-search :max
1
1218 (required (append base-required create-extra
))
1219 (file (oref backend source
))
1221 ;; `valist' is an alist
1223 ;; `artificial' will be returned if no creation is needed
1226 ;; only for base required elements (defined as function parameters):
1227 ;; fill in the valist with whatever data we may have from the search
1228 ;; we complete the first value if it's a list and use the value otherwise
1229 (dolist (br base-required
)
1230 (let ((val (plist-get spec
(auth-source--symbol-keyword br
))))
1232 (let ((br-choice (cond
1233 ;; all-accepting choice (predicate is t)
1235 ;; just the value otherwise
1238 (auth-source--aput valist br br-choice
))))))
1240 ;; for extra required elements, see if the spec includes a value for them
1241 (dolist (er create-extra
)
1242 (let ((k (auth-source--symbol-keyword er
))
1243 (keys (cl-loop for i below
(length spec
) by
2
1244 collect
(nth i spec
))))
1246 (auth-source--aput valist er
(plist-get spec k
)))))
1248 ;; for each required element
1249 (dolist (r required
)
1250 (let* ((data (auth-source--aget valist r
))
1251 ;; take the first element if the data is a list
1252 (data (or (auth-source-netrc-element-or-first data
)
1253 (plist-get current-data
1254 (auth-source--symbol-keyword r
))))
1255 ;; this is the default to be offered
1256 (given-default (auth-source--aget
1257 auth-source-creation-defaults r
))
1258 ;; the default supplementals are simple:
1259 ;; for the user, try `given-default' and then (user-login-name);
1260 ;; otherwise take `given-default'
1262 ((and (not given-default
) (eq r
'user
))
1265 (printable-defaults (list
1268 (auth-source-netrc-element-or-first
1269 (auth-source--aget valist
'user
))
1270 (plist-get artificial
:user
)
1274 (auth-source-netrc-element-or-first
1275 (auth-source--aget valist
'host
))
1276 (plist-get artificial
:host
)
1280 (auth-source-netrc-element-or-first
1281 (auth-source--aget valist
'port
))
1282 (plist-get artificial
:port
)
1284 (prompt (or (auth-source--aget auth-source-creation-prompts r
)
1286 (secret "%p password for %u@%h: ")
1287 (user "%p user name for %h: ")
1288 (host "%p host name for user %u: ")
1289 (port "%p port for %u@%h: "))
1290 (format "Enter %s (%%u@%%h:%%p): " r
)))
1291 (prompt (auth-source-format-prompt
1293 `((?u
,(auth-source--aget printable-defaults
'user
))
1294 (?h
,(auth-source--aget printable-defaults
'host
))
1295 (?p
,(auth-source--aget printable-defaults
'port
))))))
1297 ;; Store the data, prompting for the password if needed.
1300 ;; Special case prompt for passwords.
1301 ;; TODO: make the default (setq auth-source-netrc-use-gpg-tokens `((,(if (boundp 'epa-file-auto-mode-alist-entry) (car epa-file-auto-mode-alist-entry) "\\.gpg\\'") nil) (t gpg)))
1302 ;; TODO: or maybe leave as (setq auth-source-netrc-use-gpg-tokens 'never)
1303 (let* ((ep (format "Use GPG password tokens in %s?" file
))
1306 ((eq auth-source-netrc-use-gpg-tokens
'never
)
1308 ((listp auth-source-netrc-use-gpg-tokens
)
1309 (let ((check (copy-sequence
1310 auth-source-netrc-use-gpg-tokens
))
1313 (setq item
(pop check
))
1314 (when (or (eq (car item
) t
)
1315 (string-match (car item
) file
))
1316 (setq ret
(cdr item
))
1320 (plain (or (eval default
) (read-passwd prompt
))))
1321 ;; ask if we don't know what to do (in which case
1322 ;; auth-source-netrc-use-gpg-tokens must be a list)
1324 (setq gpg-encrypt
(if (y-or-n-p ep
) 'gpg
'never
))
1325 ;; TODO: save the defcustom now? or ask?
1326 (setq auth-source-netrc-use-gpg-tokens
1327 (cons `(,file
,gpg-encrypt
)
1328 auth-source-netrc-use-gpg-tokens
)))
1329 (if (eq gpg-encrypt
'gpg
)
1330 (auth-source-epa-make-gpg-token plain file
)
1332 (if (stringp default
)
1333 (read-string (if (string-match ": *\\'" prompt
)
1334 (concat (substring prompt
0 (match-beginning 0))
1335 " (default " default
"): ")
1336 (concat prompt
"(default " default
") "))
1341 (setq artificial
(plist-put artificial
1342 (auth-source--symbol-keyword r
)
1348 ;; When r is not an empty string...
1349 (when (and (stringp data
)
1350 (< 0 (length data
)))
1351 ;; this function is not strictly necessary but I think it
1352 ;; makes the code clearer -tzz
1353 (let ((printer (lambda ()
1354 ;; append the key (the symbol name of r)
1355 ;; and the value in r
1358 (if (zerop (length add
)) "" " ")
1359 ;; remap auth-source tokens to netrc
1364 (port "port") ; redundant but clearer
1365 (t (symbol-name r
)))
1366 (if (string-match "[\"# ]" data
)
1369 (setq add
(concat add
(funcall printer
)))))))
1376 (lambda () (auth-source-netrc-saver file add
))))
1380 (defun auth-source-netrc-saver (file add
)
1381 "Save a line ADD in FILE, prompting along the way.
1382 Respects `auth-source-save-behavior'. Uses
1383 `auth-source-netrc-cache' to avoid prompting more than once."
1384 (let* ((key (format "%s %s" file
(rfc2104-hash 'md5
64 16 file add
)))
1385 (cached (assoc key auth-source-netrc-cache
)))
1388 (auth-source-do-trivia
1389 "auth-source-netrc-saver: found previous run for key %s, returning"
1392 (when (file-exists-p file
)
1393 (insert-file-contents file
))
1394 (when auth-source-gpg-encrypt-to
1395 ;; (see bug#7487) making `epa-file-encrypt-to' local to
1396 ;; this buffer lets epa-file skip the key selection query
1397 ;; (see the `local-variable-p' check in
1398 ;; `epa-file-write-region').
1399 (unless (local-variable-p 'epa-file-encrypt-to
(current-buffer))
1400 (make-local-variable 'epa-file-encrypt-to
))
1401 (if (listp auth-source-gpg-encrypt-to
)
1402 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to
)))
1403 ;; we want the new data to be found first, so insert at beginning
1404 (goto-char (point-min))
1406 ;; Ask AFTER we've successfully opened the file.
1407 (let ((prompt (format "Save auth info to file %s? " file
))
1408 (done (not (eq auth-source-save-behavior
'ask
)))
1409 (bufname "*auth-source Help*")
1412 (setq k
(auth-source-read-char-choice prompt
'(?y ?n ?N ?e ??
)))
1416 (with-output-to-temp-buffer bufname
1418 (concat "(y)es, save\n"
1419 "(n)o but use the info\n"
1420 "(N)o and don't ask to save again\n"
1422 "(?) for help as you can see.\n"))
1423 ;; Why? Doesn't with-output-to-temp-buffer already do
1424 ;; the exact same thing anyway? --Stef
1425 (set-buffer standard-output
)
1432 (customize-save-variable 'auth-source-save-behavior nil
))
1433 (?e
(setq add
(read-string "Line to add: " add
)))
1436 (when (get-buffer-window bufname
)
1437 (delete-window (get-buffer-window bufname
)))
1439 ;; Make sure the info is not saved.
1440 (when (null auth-source-save-behavior
)
1443 (when (< 0 (length add
))
1448 (write-region (point-min) (point-max) file nil
'silent
)
1449 ;; Make the .authinfo file non-world-readable.
1450 (set-file-modes file
#o600
)
1451 (auth-source-do-debug
1452 "auth-source-netrc-create: wrote 1 new line to %s"
1454 (message "Saved new authentication information to %s" file
)
1456 (auth-source--aput auth-source-netrc-cache key
"ran"))))
1458 ;;; Backend specific parsing: Secrets API backend
1460 (defun auth-source-secrets-listify-pattern (pattern)
1461 "Convert a pattern with lists to a list of string patterns.
1463 auth-source patterns can have values of the form :foo (\"bar\"
1464 \"qux\"), which means to match any secret with :foo equal to
1465 \"bar\" or :foo equal to \"qux\". The secrets backend supports
1466 only string values for patterns, so this routine returns a list
1467 of patterns that is equivalent to the single original pattern
1468 when interpreted such that if a secret matches any pattern in the
1469 list, it matches the original pattern."
1472 (let* ((key (pop pattern
))
1473 (value (pop pattern
))
1474 (tails (auth-source-secrets-listify-pattern pattern
))
1475 (heads (if (stringp value
)
1476 (list (list key value
))
1477 (mapcar (lambda (v) (list key v
)) value
))))
1478 (cl-loop for h in heads
1479 nconc
(cl-loop for tl in tails collect
(append h tl
))))))
1481 (cl-defun auth-source-secrets-search (&rest spec
1482 &key backend create delete label max
1484 "Search the Secrets API; spec is like `auth-source'.
1486 The :label key specifies the item's label. It is the only key
1487 that can specify a substring. Any :label value besides a string
1488 will allow any label.
1490 All other search keys must match exactly. If you need substring
1491 matching, do a wider search and narrow it down yourself.
1493 You'll get back all the properties of the token as a plist.
1495 Here's an example that looks for the first item in the `Login'
1498 (let ((auth-sources \\='(\"secrets:Login\")))
1499 (auth-source-search :max 1)
1501 Here's another that looks for the first item in the `Login'
1502 Secrets collection whose label contains `gnus':
1504 (let ((auth-sources \\='(\"secrets:Login\")))
1505 (auth-source-search :max 1 :label \"gnus\")
1507 And this one looks for the first item in the `Login' Secrets
1508 collection that's a Google Chrome entry for the git.gnus.org site
1509 authentication tokens:
1511 (let ((auth-sources \\='(\"secrets:Login\")))
1512 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1516 (cl-assert (not create
) nil
1517 "The Secrets API auth-source backend doesn't support creation yet")
1519 ;; (secrets-delete-item coll elt)
1520 (cl-assert (not delete
) nil
1521 "The Secrets API auth-source backend doesn't support deletion yet")
1523 (let* ((coll (oref backend source
))
1524 (max (or max
5000)) ; sanity check: default to stop at 5K
1525 (ignored-keys '(:create
:delete
:max
:backend
:label
:require
:type
))
1526 (search-keys (cl-loop for i below
(length spec
) by
2
1527 unless
(memq (nth i spec
) ignored-keys
)
1528 collect
(nth i spec
)))
1529 ;; build a search spec without the ignored keys
1530 ;; if a search key is nil or t (match anything), we skip it
1531 (search-specs (auth-source-secrets-listify-pattern
1532 (apply #'append
(mapcar
1534 (if (or (null (plist-get spec k
))
1535 (eq t
(plist-get spec k
)))
1537 (list k
(plist-get spec k
))))
1539 ;; needed keys (always including host, login, port, and secret)
1540 (returned-keys (delete-dups (append
1541 '(:host
:login
:port
:secret
)
1545 for search-spec in search-specs
1547 (cl-loop for item in
(apply #'secrets-search-items coll search-spec
)
1548 unless
(and (stringp label
)
1549 (not (string-match label item
)))
1551 ;; TODO: respect max in `secrets-search-items', not after the fact
1552 (items (butlast items
(- (length items
) max
)))
1553 ;; convert the item name to a full plist
1554 (items (mapcar (lambda (item)
1556 ;; make an entry for the secret (password) element
1559 (let ((v (secrets-get-secret coll item
)))
1561 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1563 (mapcar (lambda (entry)
1564 (list (car entry
) (cdr entry
)))
1565 (secrets-get-attributes coll item
)))))
1567 ;; ensure each item has each key in `returned-keys'
1568 (items (mapcar (lambda (plist)
1571 (mapcar (lambda (req)
1572 (if (plist-get plist req
)
1580 (defun auth-source-secrets-create (&rest spec
)
1582 ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1585 ;;; Backend specific parsing: Mac OS Keychain (using /usr/bin/security) backend
1587 (cl-defun auth-source-macos-keychain-search (&rest spec
1588 &key backend create delete type max
1590 "Search the macOS Keychain; spec is like `auth-source'.
1592 All search keys must match exactly. If you need substring
1593 matching, do a wider search and narrow it down yourself.
1595 You'll get back all the properties of the token as a plist.
1597 The :type key is either `macos-keychain-internet' or
1598 `macos-keychain-generic'.
1600 For the internet keychain type, the :label key searches the
1601 item's labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1602 Similarly, :host maps to \"-s HOST\", :user maps to \"-a USER\",
1603 and :port maps to \"-P PORT\" or \"-r PROT\"
1604 \(note PROT has to be a 4-character string).
1606 For the generic keychain type, the :label key searches the item's
1607 labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1608 Similarly, :host maps to \"-c HOST\" (the \"creator\" keychain
1609 field), :user maps to \"-a USER\", and :port maps to \"-s PORT\".
1611 Here's an example that looks for the first item in the default
1612 generic macOS Keychain:
1614 (let ((auth-sources \\='(macos-keychain-generic)))
1615 (auth-source-search :max 1)
1617 Here's another that looks for the first item in the internet
1618 macOS Keychain collection whose label is `gnus':
1620 (let ((auth-sources \\='(macos-keychain-internet)))
1621 (auth-source-search :max 1 :label \"gnus\")
1623 And this one looks for the first item in the internet keychain
1624 entries for git.gnus.org:
1626 (let ((auth-sources \\='(macos-keychain-internet\")))
1627 (auth-source-search :max 1 :host \"git.gnus.org\"))
1630 (cl-assert (not create
) nil
1631 "The macOS Keychain auth-source backend doesn't support creation yet")
1633 ;; (macos-keychain-delete-item coll elt)
1634 (cl-assert (not delete
) nil
1635 "The macOS Keychain auth-source backend doesn't support deletion yet")
1637 (let* ((coll (oref backend source
))
1638 (max (or max
5000)) ; sanity check: default to stop at 5K
1639 ;; Filter out ignored keys from the spec
1640 (ignored-keys '(:create
:delete
:max
:backend
:label
:host
:port
))
1641 ;; Build a search spec without the ignored keys
1642 ;; FIXME make this loop a function? it's used in at least 3 places
1643 (search-keys (cl-loop for i below
(length spec
) by
2
1644 unless
(memq (nth i spec
) ignored-keys
)
1645 collect
(nth i spec
)))
1646 ;; If a search key value is nil or t (match anything), we skip it
1647 (search-spec (apply #'append
(mapcar
1649 (if (or (null (plist-get spec k
))
1650 (eq t
(plist-get spec k
)))
1652 (list k
(plist-get spec k
))))
1654 ;; needed keys (always including host, login, port, and secret)
1655 (returned-keys (delete-dups (append
1656 '(:host
:login
:port
:secret
)
1658 ;; Extract host and port from spec
1659 (hosts (plist-get spec
:host
))
1660 (hosts (if (and hosts
(listp hosts
)) hosts
`(,hosts
)))
1661 (ports (plist-get spec
:port
))
1662 (ports (if (and ports
(listp ports
)) ports
`(,ports
)))
1663 ;; Loop through all combinations of host/port and pass each of these to
1664 ;; auth-source-macos-keychain-search-items
1665 (items (catch 'match
1666 (dolist (host hosts
)
1667 (dolist (port ports
)
1668 (let* ((port (if port
(format "%S" port
)))
1669 (items (apply #'auth-source-macos-keychain-search-items
1676 (throw 'match items
)))))))
1678 ;; ensure each item has each key in `returned-keys'
1679 (items (mapcar (lambda (plist)
1682 (mapcar (lambda (req)
1683 (if (plist-get plist req
)
1692 (defun auth-source--decode-octal-string (string)
1693 "Convert octal string to utf-8 string. E.g: 'a\134b' to 'a\b'"
1694 (let ((list (string-to-list string
))
1695 (size (length string
)))
1696 (decode-coding-string
1697 (apply #'unibyte-string
1698 (cl-loop for i
= 0 then
(+ i
(if (eq (nth i list
) ?
\\) 4 1))
1699 for var
= (nth i list
)
1702 collect
(string-to-number
1703 (concat (cl-subseq list
(+ i
1) (+ i
4))) 8)
1708 (cl-defun auth-source-macos-keychain-search-items (coll _type _max host port
1709 &key label type user
1711 (let* ((keychain-generic (eq type
'macos-keychain-generic
))
1712 (args `(,(if keychain-generic
1713 "find-generic-password"
1714 "find-internet-password")
1716 (ret (list :type type
)))
1718 (setq args
(append args
(list "-l" label
))))
1720 (setq args
(append args
(list (if keychain-generic
"-c" "-s") host
))))
1722 (setq args
(append args
(list "-a" user
))))
1725 (if keychain-generic
1726 (setq args
(append args
(list "-s" port
)))
1727 (setq args
(append args
(list
1728 (if (string-match "[0-9]+" port
) "-P" "-r")
1731 (unless (equal coll
"default")
1732 (setq args
(append args
(list coll
))))
1735 (apply #'call-process
"/usr/bin/security" nil t nil args
)
1736 (goto-char (point-min))
1739 ((looking-at "^password: \\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1740 (setq ret
(auth-source-macos-keychain-result-append
1744 (let ((v (auth-source--decode-octal-string
1747 ;; TODO: check if this is really the label
1748 ;; match 0x00000007 <blob>="AppleID"
1750 "^[ ]+0x00000007 <blob>=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1751 (setq ret
(auth-source-macos-keychain-result-append
1755 (auth-source--decode-octal-string (match-string 1)))))
1756 ;; match "crtr"<uint32>="aapl"
1757 ;; match "svce"<blob>="AppleID"
1759 "^[ ]+\"\\([a-z]+\\)\"[^=]+=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1760 (setq ret
(auth-source-macos-keychain-result-append
1763 (auth-source--decode-octal-string (match-string 1))
1764 (auth-source--decode-octal-string (match-string 2))))))
1766 ;; return `ret' iff it has the :secret key
1767 (and (plist-get ret
:secret
) (list ret
))))
1769 (defun auth-source-macos-keychain-result-append (result generic k v
)
1771 (push (auth-source--symbol-keyword
1773 ((equal k
"acct") "user")
1774 ;; for generic keychains, creator is host, service is port
1775 ((and generic
(equal k
"crtr")) "host")
1776 ((and generic
(equal k
"svce")) "port")
1777 ;; for internet keychains, protocol is port, server is host
1778 ((and (not generic
) (equal k
"ptcl")) "port")
1779 ((and (not generic
) (equal k
"srvr")) "host")
1783 (defun auth-source-macos-keychain-create (&rest spec
)
1787 ;;; Backend specific parsing: PLSTORE backend
1789 (cl-defun auth-source-plstore-search (&rest spec
1790 &key backend create delete max
1792 "Search the PLSTORE; spec is like `auth-source'."
1793 (let* ((store (oref backend data
))
1794 (max (or max
5000)) ; sanity check: default to stop at 5K
1795 (ignored-keys '(:create
:delete
:max
:backend
:label
:require
:type
))
1796 (search-keys (cl-loop for i below
(length spec
) by
2
1797 unless
(memq (nth i spec
) ignored-keys
)
1798 collect
(nth i spec
)))
1799 ;; build a search spec without the ignored keys
1800 ;; if a search key is nil or t (match anything), we skip it
1801 (search-spec (apply #'append
(mapcar
1803 (let ((v (plist-get spec k
)))
1811 ;; needed keys (always including host, login, port, and secret)
1812 (returned-keys (delete-dups (append
1813 '(:host
:login
:port
:secret
)
1815 (items (plstore-find store search-spec
))
1816 (item-names (mapcar #'car items
))
1817 (items (butlast items
(- (length items
) max
)))
1818 ;; convert the item to a full plist
1819 (items (mapcar (lambda (item)
1820 (let* ((plist (copy-tree (cdr item
)))
1821 (secret (plist-member plist
:secret
)))
1825 (let ((v (car (cdr secret
))))
1829 ;; ensure each item has each key in `returned-keys'
1830 (items (mapcar (lambda (plist)
1833 (mapcar (lambda (req)
1834 (if (plist-get plist req
)
1841 ;; if we need to create an entry AND none were found to match
1845 ;; create based on the spec and record the value
1847 ;; if the user did not want to create the entry
1848 ;; in the file, it will be returned
1849 (apply (slot-value backend
'create-function
) spec
)
1850 ;; if not, we do the search again without :create
1851 ;; to get the updated data.
1853 ;; the result will be returned, even if the search fails
1854 (apply #'auth-source-plstore-search
1855 (plist-put spec
:create nil
)))))
1858 (dolist (item-name item-names
)
1859 (plstore-delete store item-name
))
1860 (plstore-save store
)))
1863 (cl-defun auth-source-plstore-create (&rest spec
1864 &key backend host port create
1866 (let* ((base-required '(host user port secret
))
1867 (base-secret '(secret))
1868 ;; we know (because of an assertion in auth-source-search) that the
1869 ;; :create parameter is either t or a list (which includes nil)
1870 (create-extra (if (eq t create
) nil create
))
1871 (current-data (car (auth-source-search :max
1
1874 (required (append base-required create-extra
))
1875 ;; `valist' is an alist
1877 ;; `artificial' will be returned if no creation is needed
1881 ;; only for base required elements (defined as function parameters):
1882 ;; fill in the valist with whatever data we may have from the search
1883 ;; we complete the first value if it's a list and use the value otherwise
1884 (dolist (br base-required
)
1885 (let ((val (plist-get spec
(auth-source--symbol-keyword br
))))
1887 (let ((br-choice (cond
1888 ;; all-accepting choice (predicate is t)
1890 ;; just the value otherwise
1893 (auth-source--aput valist br br-choice
))))))
1895 ;; for extra required elements, see if the spec includes a value for them
1896 (dolist (er create-extra
)
1897 (let ((k (auth-source--symbol-keyword er
))
1898 (keys (cl-loop for i below
(length spec
) by
2
1899 collect
(nth i spec
))))
1901 (auth-source--aput valist er
(plist-get spec k
)))))
1903 ;; for each required element
1904 (dolist (r required
)
1905 (let* ((data (auth-source--aget valist r
))
1906 ;; take the first element if the data is a list
1907 (data (or (auth-source-netrc-element-or-first data
)
1908 (plist-get current-data
1909 (auth-source--symbol-keyword r
))))
1910 ;; this is the default to be offered
1911 (given-default (auth-source--aget
1912 auth-source-creation-defaults r
))
1913 ;; the default supplementals are simple:
1914 ;; for the user, try `given-default' and then (user-login-name);
1915 ;; otherwise take `given-default'
1917 ((and (not given-default
) (eq r
'user
))
1920 (printable-defaults (list
1923 (auth-source-netrc-element-or-first
1924 (auth-source--aget valist
'user
))
1925 (plist-get artificial
:user
)
1929 (auth-source-netrc-element-or-first
1930 (auth-source--aget valist
'host
))
1931 (plist-get artificial
:host
)
1935 (auth-source-netrc-element-or-first
1936 (auth-source--aget valist
'port
))
1937 (plist-get artificial
:port
)
1939 (prompt (or (auth-source--aget auth-source-creation-prompts r
)
1941 (secret "%p password for %u@%h: ")
1942 (user "%p user name for %h: ")
1943 (host "%p host name for user %u: ")
1944 (port "%p port for %u@%h: "))
1945 (format "Enter %s (%%u@%%h:%%p): " r
)))
1946 (prompt (auth-source-format-prompt
1948 `((?u
,(auth-source--aget printable-defaults
'user
))
1949 (?h
,(auth-source--aget printable-defaults
'host
))
1950 (?p
,(auth-source--aget printable-defaults
'port
))))))
1952 ;; Store the data, prompting for the password if needed.
1955 (or (eval default
) (read-passwd prompt
))
1956 (if (stringp default
)
1958 (if (string-match ": *\\'" prompt
)
1959 (concat (substring prompt
0 (match-beginning 0))
1960 " (default " default
"): ")
1961 (concat prompt
"(default " default
") "))
1966 (if (member r base-secret
)
1967 (setq secret-artificial
1968 (plist-put secret-artificial
1969 (auth-source--symbol-keyword r
)
1971 (setq artificial
(plist-put artificial
1972 (auth-source--symbol-keyword r
)
1974 (plstore-put (oref backend data
)
1975 (sha1 (format "%s@%s:%s"
1976 (plist-get artificial
:user
)
1977 (plist-get artificial
:host
)
1978 (plist-get artificial
:port
)))
1979 artificial secret-artificial
)
1980 (if (y-or-n-p (format "Save auth info to file %s? "
1981 (plstore-get-file (oref backend data
))))
1982 (plstore-save (oref backend data
)))))
1984 ;;; Backend specific parsing: JSON backend
1985 ;;; (auth-source-search :max 1 :machine "imap.gmail.com")
1986 ;;; (auth-source-search :max 1 :host '("my-gmail" "imap.gmail.com") :port '(993 "imaps" "imap" "993" "143") :user nil :require '(:user :secret))
1988 (defun auth-source-json-check (host user port require item
)
1990 (auth-source-search-collection
1993 (plist-get item
:machine
)
1994 (plist-get item
:host
)
1996 (auth-source-search-collection
1999 (plist-get item
:login
)
2000 (plist-get item
:account
)
2001 (plist-get item
:user
)
2003 (auth-source-search-collection
2006 (plist-get item
:port
)
2007 (plist-get item
:protocol
)
2010 ;; the required list of keys is nil, or
2012 ;; every element of require is in
2013 (cl-loop for req in require
2014 always
(plist-get item req
)))))
2016 (cl-defun auth-source-json-search (&rest spec
2017 &key backend require
2018 type max host user port
2020 "Given a property list SPEC, return search matches from the :backend.
2021 See `auth-source-search' for details on SPEC."
2022 ;; just in case, check that the type is correct (null or same as the backend)
2023 (cl-assert (or (null type
) (eq type
(oref backend type
)))
2024 t
"Invalid JSON search: %s %s")
2026 ;; Hide the secrets early to avoid accidental exposure.
2028 (mapcar (lambda (entry)
2031 (let* ((item (pop entry
))
2032 (k (auth-source--symbol-keyword (car item
)))
2034 (setq k
(cond ((memq k
'(:machine
)) :host
)
2035 ((memq k
'(:login
:account
)) :user
)
2036 ((memq k
'(:protocol
)) :port
)
2037 ((memq k
'(:password
)) :secret
)
2039 ;; send back the secret in a function (lexical binding)
2040 (when (eq k
:secret
)
2041 (setq v
(let ((lexv v
))
2043 (setq ret
(plist-put ret k v
))))
2045 (json-read-file (oref backend source
))))
2046 (max (or max
5000)) ; sanity check: default to stop at 5K
2048 (dolist (item jdata
)
2050 (> max
(length all
))
2051 (auth-source-json-check host user port require item
))
2057 ;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
2059 ;; deprecate the old interface
2060 (make-obsolete 'auth-source-user-or-password
2061 'auth-source-search
"Emacs 24.1")
2062 (make-obsolete 'auth-source-forget-user-or-password
2063 'auth-source-forget
"Emacs 24.1")
2065 (defun auth-source-user-or-password
2066 (mode host port
&optional username create-missing delete-existing
)
2067 "Find MODE (string or list of strings) matching HOST and PORT.
2069 DEPRECATED in favor of `auth-source-search'!
2071 USERNAME is optional and will be used as \"login\" in a search
2072 across the Secret Service API (see secrets.el) if the resulting
2073 items don't have a username. This means that if you search for
2074 username \"joe\" and it matches an item but the item doesn't have
2075 a :user attribute, the username \"joe\" will be returned.
2077 A non nil DELETE-EXISTING means deleting any matching password
2078 entry in the respective sources. This is useful only when
2079 CREATE-MISSING is non nil as well; the intended use case is to
2080 remove wrong password entries.
2082 If no matching entry is found, and CREATE-MISSING is non nil,
2083 the password will be retrieved interactively, and it will be
2084 stored in the password database which matches best (see
2087 MODE can be \"login\" or \"password\"."
2088 (auth-source-do-debug
2089 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
2090 mode host port username
)
2092 (let* ((listy (listp mode
))
2093 (mode (if listy mode
(list mode
)))
2094 ;; (cname (if username
2095 ;; (format "%s %s:%s %s" mode host port username)
2096 ;; (format "%s %s:%s" mode host port)))
2097 (search (list :host host
:port port
))
2098 (search (if username
(append search
(list :user username
)) search
))
2099 (search (if create-missing
2100 (append search
(list :create t
))
2102 (search (if delete-existing
2103 (append search
(list :delete t
))
2105 ;; (found (if (not delete-existing)
2106 ;; (gethash cname auth-source-cache)
2107 ;; (remhash cname auth-source-cache)
2112 (auth-source-do-debug
2113 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
2115 ;; don't show the password
2116 (if (and (member "password" mode
) t
)
2120 found
) ; return the found data
2121 ;; else, if not found, search with a max of 1
2122 (let ((choice (nth 0 (apply #'auth-source-search
2123 (append '(:max
1) search
)))))
2127 ((equal "password" m
)
2128 (push (if (plist-get choice
:secret
)
2129 (funcall (plist-get choice
:secret
))
2132 (push (plist-get choice
:user
) found
)))))
2133 (setq found
(nreverse found
))
2134 (setq found
(if listy found
(car-safe found
)))))
2138 (defun auth-source-user-and-password (host &optional user
)
2139 (let* ((auth-info (car
2145 :require
'(:user
:secret
)
2150 :require
'(:user
:secret
)
2152 (user (plist-get auth-info
:user
))
2153 (password (plist-get auth-info
:secret
)))
2154 (when (functionp password
)
2155 (setq password
(funcall password
)))
2156 (list user password auth-info
)))
2158 (provide 'auth-source
)
2160 ;;; auth-source.el ends here