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
"26.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"
309 (sexp :tag
"A data structure (external provider)"))))
311 (defcustom auth-source-gpg-encrypt-to t
312 "List of recipient keys that `authinfo.gpg' encrypted to.
313 If the value is not a list, symmetric encryption will be used."
315 :version
"24.1" ;; No Gnus
316 :type
'(choice (const :tag
"Symmetric encryption" t
)
317 (repeat :tag
"Recipient public keys"
318 (string :tag
"Recipient public key"))))
320 (defun auth-source-do-debug (&rest msg
)
321 (when auth-source-debug
322 (apply #'auth-source-do-warn msg
)))
324 (defun auth-source-do-trivia (&rest msg
)
325 (when (or (eq auth-source-debug
'trivia
)
326 (functionp auth-source-debug
))
327 (apply #'auth-source-do-warn msg
)))
329 (defun auth-source-do-warn (&rest msg
)
331 ;; set logger to either the function in auth-source-debug or 'message
332 ;; note that it will be 'message if auth-source-debug is nil
333 (if (functionp auth-source-debug
)
338 (defun auth-source-read-char-choice (prompt choices
)
339 "Read one of CHOICES by `read-char-choice', or `read-char'.
340 `dropdown-list' support is disabled because it doesn't work reliably.
341 Only one of CHOICES will be returned. The PROMPT is augmented
342 with \"[a/b/c] \" if CHOICES is \(?a ?b ?c)."
344 (let* ((prompt-choices
346 (cl-loop for c in choices collect
(format "%c/" c
))))
347 (prompt-choices (concat "[" (substring prompt-choices
0 -
1) "] "))
348 (full-prompt (concat prompt prompt-choices
))
351 (while (not (memq k choices
))
352 (setq k
(read-char-choice full-prompt choices
)))
355 (defvar auth-source-backend-parser-functions nil
356 "List of auth-source parser functions.
357 Each function takes an entry from `auth-sources' as parameter and
358 returns a backend or nil if the entry is not supported. Add a
359 parser function to this list with `add-hook'. Searching for a
360 backend starts with the first element on the list and stops as
361 soon as a function returns non-nil.")
363 (defun auth-source-backend-parse (entry)
364 "Create an auth-source-backend from an ENTRY in `auth-sources'."
367 (cl-dolist (f auth-source-backend-parser-functions
)
368 (when (setq backend
(funcall f entry
))
372 ;; none of the parsers worked
374 "auth-source-backend-parse: invalid backend spec: %S" entry
)
375 (setq backend
(make-instance 'auth-source-backend
378 (auth-source-backend-parse-parameters entry backend
)))
380 (defun auth-source-backends-parser-file (entry)
381 ;; take just a file name use it as a netrc/plist file
382 ;; matching any user, host, and protocol
383 (when (stringp entry
)
384 (setq entry
(list :source entry
)))
385 (let* ((source (plist-get entry
:source
))
387 (if (and (stringp source
)
388 (equal (file-name-extension source
) "gpg"))
389 (file-name-sans-extension source
)
391 (extension (or (and (stringp source-without-gpg
)
392 (file-name-extension source-without-gpg
))
394 (when (stringp source
)
396 ((equal extension
"plist")
401 :search-function
#'auth-source-plstore-search
402 :create-function
#'auth-source-plstore-create
403 :data
(plstore-open source
)))
404 ((member-ignore-case extension
'("json"))
409 :search-function
#'auth-source-json-search
))
415 :search-function
#'auth-source-netrc-search
416 :create-function
#'auth-source-netrc-create
))))))
418 ;; Note this function should be last in the parser functions, so we add it first
419 (add-hook 'auth-source-backend-parser-functions
'auth-source-backends-parser-file
)
421 (defun auth-source-backends-parser-macos-keychain (entry)
422 ;; take macos-keychain-{internet,generic}:XYZ and use it as macOS
423 ;; Keychain "XYZ" matching any user, host, and protocol
424 (when (and (stringp entry
) (string-match "^macos-keychain-internet:\\(.+\\)"
426 (setq entry
`(:source
(:macos-keychain-internet
427 ,(match-string 1 entry
)))))
428 (when (and (stringp entry
) (string-match "^macos-keychain-generic:\\(.+\\)"
430 (setq entry
`(:source
(:macos-keychain-generic
431 ,(match-string 1 entry
)))))
432 ;; take 'macos-keychain-internet or generic and use it as a Mac OS
433 ;; Keychain collection matching any user, host, and protocol
434 (when (eq entry
'macos-keychain-internet
)
435 (setq entry
'(:source
(:macos-keychain-internet default
))))
436 (when (eq entry
'macos-keychain-generic
)
437 (setq entry
'(:source
(:macos-keychain-generic default
))))
439 ;; the macOS Keychain
441 (not (null (plist-get entry
:source
))) ; the source must not be nil
442 (listp (plist-get entry
:source
)) ; and it must be a list
444 (plist-get (plist-get entry
:source
) :macos-keychain-generic
)
445 (plist-get (plist-get entry
:source
) :macos-keychain-internet
)))
447 (let* ((source-spec (plist-get entry
:source
))
448 (keychain-generic (plist-get source-spec
:macos-keychain-generic
))
449 (keychain-type (if keychain-generic
450 'macos-keychain-generic
451 'macos-keychain-internet
))
452 (source (plist-get source-spec
(if keychain-generic
453 :macos-keychain-generic
454 :macos-keychain-internet
))))
456 (when (symbolp source
)
457 (setq source
(symbol-name source
)))
460 (format "Mac OS Keychain (%s)" source
)
463 :search-function
#'auth-source-macos-keychain-search
464 :create-function
#'auth-source-macos-keychain-create
)))))
466 (add-hook 'auth-source-backend-parser-functions
'auth-source-backends-parser-macos-keychain
)
468 (defun auth-source-backends-parser-secrets (entry)
469 ;; take secrets:XYZ and use it as Secrets API collection "XYZ"
470 ;; matching any user, host, and protocol
471 (when (and (stringp entry
) (string-match "^secrets:\\(.+\\)" entry
))
472 (setq entry
`(:source
(:secrets
,(match-string 1 entry
)))))
473 ;; take 'default and use it as a Secrets API default collection
474 ;; matching any user, host, and protocol
475 (when (eq entry
'default
)
476 (setq entry
'(:source
(:secrets default
))))
478 ;; the Secrets API. We require the package, in order to have a
479 ;; defined value for `secrets-enabled'.
481 (not (null (plist-get entry
:source
))) ; the source must not be nil
482 (listp (plist-get entry
:source
)) ; and it must be a list
483 (not (null (plist-get
484 (plist-get entry
:source
)
485 :secrets
))) ; the source must have :secrets
486 (require 'secrets nil t
) ; and we must load the Secrets API
487 secrets-enabled
) ; and that API must be enabled
489 ;; the source is either the :secrets key in ENTRY or
490 ;; if that's missing or nil, it's "session"
491 (let ((source (plist-get (plist-get entry
:source
) :secrets
)))
493 ;; if the source is a symbol, we look for the alias named so,
494 ;; and if that alias is missing, we use "Login"
495 (when (symbolp source
)
496 (setq source
(or (secrets-get-alias (symbol-name source
))
499 (if (featurep 'secrets
)
501 (format "Secrets API (%s)" source
)
504 :search-function
#'auth-source-secrets-search
505 :create-function
#'auth-source-secrets-create
)
507 "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry
)
509 (format "Ignored Secrets API (%s)" source
)
513 (add-hook 'auth-source-backend-parser-functions
'auth-source-backends-parser-secrets
)
515 (defun auth-source-backend-parse-parameters (entry backend
)
516 "Fills in the extra auth-source-backend parameters of ENTRY.
517 Using the plist ENTRY, get the :host, :port, and :user search
519 (let ((entry (if (stringp entry
)
523 (when (setq val
(plist-get entry
:host
))
524 (oset backend host val
))
525 (when (setq val
(plist-get entry
:user
))
526 (oset backend user val
))
527 (when (setq val
(plist-get entry
:port
))
528 (oset backend port val
)))
531 ;; (mapcar 'auth-source-backend-parse auth-sources)
533 (cl-defun auth-source-search (&rest spec
534 &key max require create delete
536 "Search or modify authentication backends according to SPEC.
538 This function parses `auth-sources' for matches of the SPEC
539 plist. It can optionally create or update an authentication
540 token if requested. A token is just a standard Emacs property
541 list with a :secret property that can be a function; all the
542 other properties will always hold scalar values.
544 Typically the :secret property, if present, contains a password.
546 Common search keys are :max, :host, :port, and :user. In
547 addition, :create specifies if and how tokens will be created.
548 Finally, :type can specify which backend types you want to check.
550 A string value is always matched literally. A symbol is matched
551 as its string value, literally. All the SPEC values can be
552 single values (symbol or string) or lists thereof (in which case
553 any of the search terms matches).
555 :create t means to create a token if possible.
557 A new token will be created if no matching tokens were found.
558 The new token will have only the keys the backend requires. For
559 the netrc backend, for instance, that's the user, host, and
564 \(let ((auth-source-creation-defaults \\='((user . \"defaultUser\")
565 (A . \"default A\"))))
566 (auth-source-search :host \"mine\" :type \\='netrc :max 1
567 :P \"pppp\" :Q \"qqqq\"
572 \"Search for any entry matching host `mine' in backends of type
573 `netrc', maximum one result.
575 Create a new entry if you found none. The netrc backend will
576 automatically require host, user, and port. The host will be
577 `mine'. We prompt for the user with default `defaultUser' and
578 for the port without a default. We will not prompt for A, Q,
579 or P. The resulting token will only have keys user, host, and
582 :create \\='(A B C) also means to create a token if possible.
584 The behavior is like :create t but if the list contains any
585 parameter, that parameter will be required in the resulting
586 token. The value for that parameter will be obtained from the
587 search parameters or from user input. If any queries are needed,
588 the alist `auth-source-creation-defaults' will be checked for the
589 default value. If the user, host, or port are missing, the alist
590 `auth-source-creation-prompts' will be used to look up the
591 prompts IN THAT ORDER (so the `user' prompt will be queried first,
592 then `host', then `port', and finally `secret'). Each prompt string
593 can use %u, %h, and %p to show the user, host, and port.
597 \(let ((auth-source-creation-defaults \\='((user . \"defaultUser\")
598 (A . \"default A\")))
599 (auth-source-creation-prompts
600 \\='((password . \"Enter IMAP password for %h:%p: \"))))
601 (auth-source-search :host \\='(\"nonesuch\" \"twosuch\") :type \\='netrc :max 1
602 :P \"pppp\" :Q \"qqqq\"
603 :create \\='(A B Q)))
607 \"Search for any entry matching host `nonesuch'
608 or `twosuch' in backends of type `netrc', maximum one result.
610 Create a new entry if you found none. The netrc backend will
611 automatically require host, user, and port. The host will be
612 `nonesuch' and Q will be `qqqq'. We prompt for the password
613 with the shown prompt. We will not prompt for Q. The resulting
614 token will have keys user, host, port, A, B, and Q. It will not
615 have P with any value, even though P is used in the search to
616 find only entries that have P set to `pppp'.\"
618 When multiple values are specified in the search parameter, the
619 user is prompted for which one. So :host (X Y Z) would ask the
620 user to choose between X, Y, and Z.
622 This creation can fail if the search was not specific enough to
623 create a new token (it's up to the backend to decide that). You
624 should `catch' the backend-specific error as usual. Some
625 backends (netrc, at least) will prompt the user rather than throw
628 :require (A B C) means that only results that contain those
629 tokens will be returned. Thus for instance requiring :secret
630 will ensure that any results will actually have a :secret
633 :delete t means to delete any found entries. nil by default.
634 Use `auth-source-delete' in ELisp code instead of calling
635 `auth-source-search' directly with this parameter.
637 :type (X Y Z) will check only those backend types. `netrc' and
638 `secrets' are the only ones supported right now.
640 :max N means to try to return at most N items (defaults to 1).
641 More than N items may be returned, depending on the search and
644 When :max is 0 the function will return just t or nil to indicate
645 if any matches were found.
647 :host (X Y Z) means to match only hosts X, Y, or Z according to
648 the match rules above. Defaults to t.
650 :user (X Y Z) means to match only users X, Y, or Z according to
651 the match rules above. Defaults to t.
653 :port (P Q R) means to match only protocols P, Q, or R.
656 :K (V1 V2 V3) for any other key K will match values V1, V2, or
657 V3 (note the match rules above).
659 The return value is a list with at most :max tokens. Each token
660 is a plist with keys :backend :host :port :user, plus any other
661 keys provided by the backend (notably :secret). But note the
662 exception for :max 0, which see above.
664 The token can hold a :save-function key. If you call that, the
665 user will be prompted to save the data to the backend. You can't
666 request that this should happen right after creation, because
667 `auth-source-search' has no way of knowing if the token is
668 actually useful. So the caller must arrange to call this function.
670 The token's :secret key can hold a function. In that case you
671 must call it to obtain the actual value."
672 (let* ((backends (mapcar #'auth-source-backend-parse auth-sources
))
674 (ignored-keys '(:require
:create
:delete
:max
))
675 (keys (cl-loop for i below
(length spec
) by
2
676 unless
(memq (nth i spec
) ignored-keys
)
677 collect
(nth i spec
)))
678 (cached (auth-source-remembered-p spec
))
679 ;; note that we may have cached results but found is still nil
680 ;; (there were no results from the search)
681 (found (auth-source-recall spec
))
684 (if (and cached auth-source-do-cache
)
685 (auth-source-do-debug
686 "auth-source-search: found %d CACHED results matching %S"
690 (or (eq t create
) (listp create
)) t
691 "Invalid auth-source :create parameter (must be t or a list): %s %s")
695 "Invalid auth-source :require parameter (must be a list): %s")
697 (setq filtered-backends
(copy-sequence backends
))
698 (dolist (backend backends
)
699 (cl-dolist (key keys
)
700 ;; ignore invalid slots
702 (unless (auth-source-search-collection
704 (slot-value backend key
))
705 (setq filtered-backends
(delq backend filtered-backends
))
707 (invalid-slot-name nil
))))
709 (auth-source-do-trivia
710 "auth-source-search: found %d backends matching %S"
711 (length filtered-backends
) spec
)
713 ;; (debug spec "filtered" filtered-backends)
714 ;; First go through all the backends without :create, so we can
716 (setq found
(auth-source-search-backends filtered-backends
720 ;; create is always nil here
724 (auth-source-do-debug
725 "auth-source-search: found %d results (max %d) matching %S"
726 (length found
) max spec
)
728 ;; If we didn't find anything, then we allow the backend(s) to
729 ;; create the entries.
732 (setq found
(auth-source-search-backends filtered-backends
738 (auth-source-do-debug
739 "auth-source-search: CREATED %d results (max %d) matching %S"
740 (length found
) max spec
))
742 ;; note we remember the lack of result too, if it's applicable
743 (when auth-source-do-cache
744 (auth-source-remember spec found
)))
750 (defun auth-source-search-backends (backends spec max create delete require
)
751 (let ((max (if (zerop max
) 1 max
)) ; stop with 1 match if we're asked for zero
753 (dolist (backend backends
)
754 (when (> max
(length matches
)) ; if we need more matches...
755 (let* ((bmatches (apply
756 (slot-value backend
'search-function
)
758 :type
(slot-value backend
'type
)
759 ;; note we're overriding whatever the spec
760 ;; has for :max, :require, :create, and :delete
767 (auth-source-do-trivia
768 "auth-source-search-backend: got %d (max %d) in %s:%s matching %S"
769 (length bmatches
) max
770 (slot-value backend
'type
)
771 (slot-value backend
'source
)
773 (setq matches
(append matches bmatches
))))))
776 (defun auth-source-delete (&rest spec
)
777 "Delete entries from the authentication backends according to SPEC.
778 Calls `auth-source-search' with the :delete property in SPEC set to t.
779 The backend may not actually delete the entries.
781 Returns the deleted entries."
782 (auth-source-search (plist-put spec
:delete t
)))
784 (defun auth-source-search-collection (collection value
)
785 "Returns t is VALUE is t or COLLECTION is t or COLLECTION contains VALUE."
786 (when (and (atom collection
) (not (eq t collection
)))
787 (setq collection
(list collection
)))
789 ;; (debug :collection collection :value value)
790 (or (eq collection t
)
792 (equal collection value
)
793 (member value collection
)))
795 (defvar auth-source-netrc-cache nil
)
797 (defun auth-source-forget-all-cached ()
798 "Forget all cached auth-source data."
800 (maphash (lambda (key _password
)
801 (when (eq 'auth-source
(car-safe key
))
803 (password-cache-remove key
)))
805 (setq auth-source-netrc-cache nil
))
807 (defun auth-source-format-cache-entry (spec)
808 "Format SPEC entry to put it in the password cache."
809 `(auth-source .
,spec
))
811 (defun auth-source-remember (spec found
)
812 "Remember FOUND search results for SPEC."
813 (let ((password-cache-expiry auth-source-cache-expiry
))
815 (auth-source-format-cache-entry spec
) found
)))
817 (defun auth-source-recall (spec)
818 "Recall FOUND search results for SPEC."
819 (password-read-from-cache (auth-source-format-cache-entry spec
)))
821 (defun auth-source-remembered-p (spec)
822 "Check if SPEC is remembered."
824 (auth-source-format-cache-entry spec
)))
826 (defun auth-source-forget (spec)
827 "Forget any cached data matching SPEC exactly.
829 This is the same SPEC you passed to `auth-source-search'.
830 Returns t or nil for forgotten or not found."
831 (password-cache-remove (auth-source-format-cache-entry spec
)))
833 (defun auth-source-forget+ (&rest spec
)
834 "Forget any cached data matching SPEC. Returns forgotten count.
836 This is not a full `auth-source-search' spec but works similarly.
837 For instance, \(:host \"myhost\" \"yourhost\") would find all the
838 cached data that was found with a search for those two hosts,
839 while \(:host t) would find all host entries."
842 (lambda (key _password
)
843 (when (and (eq 'auth-source
(car-safe key
))
844 ;; and the spec matches what was stored in the cache
845 (auth-source-specmatchp spec
(cdr key
)))
847 (password-cache-remove key
)
852 (defun auth-source-specmatchp (spec stored
)
853 (let ((keys (cl-loop for i below
(length spec
) by
2
854 collect
(nth i spec
))))
856 (cl-dolist (key keys
)
857 (unless (auth-source-search-collection (plist-get stored key
)
858 (plist-get spec key
))
862 (defun auth-source-pick-first-password (&rest spec
)
863 "Pick the first secret found from applying SPEC to `auth-source-search'."
864 (let* ((result (nth 0 (apply #'auth-source-search
(plist-put spec
:max
1))))
865 (secret (plist-get result
:secret
)))
867 (if (functionp secret
)
871 (defun auth-source-format-prompt (prompt alist
)
872 "Format PROMPT using %x (for any character x) specifiers in ALIST."
874 (let ((c (nth 0 cell
))
877 (setq prompt
(replace-regexp-in-string (format "%%%c" c
)
882 (defun auth-source-ensure-strings (values)
885 (unless (listp values
)
886 (setq values
(list values
)))
887 (mapcar (lambda (value)
893 ;;; Backend specific parsing: netrc/authinfo backend
895 (defun auth-source--aput-1 (alist key val
)
898 (while (and (consp rest
) (not (equal key
(caar rest
))))
899 (push (pop rest
) seen
))
901 (if (null rest
) alist
902 (nconc (nreverse seen
)
903 (if (equal key
(caar rest
)) (cdr rest
) rest
))))))
904 (defmacro auth-source--aput
(var key val
)
905 `(setq ,var
(auth-source--aput-1 ,var
,key
,val
)))
907 (defun auth-source--aget (alist key
)
908 (cdr (assoc key alist
)))
910 ;; (auth-source-netrc-parse :file "~/.authinfo.gpg")
911 (cl-defun auth-source-netrc-parse (&key file max host user port require
913 "Parse FILE and return a list of all entries in the file.
914 Note that the MAX parameter is used so we can exit the parse early."
916 ;; We got already parsed contents; just return it.
918 (when (file-exists-p file
)
919 (setq port
(auth-source-ensure-strings port
))
921 (let* ((max (or max
5000)) ; sanity check: default to stop at 5K
923 (cached (cdr-safe (assoc file auth-source-netrc-cache
)))
924 (cached-mtime (plist-get cached
:mtime
))
925 (cached-secrets (plist-get cached
:secret
))
926 (check (lambda(alist)
928 (auth-source-search-collection
931 (auth-source--aget alist
"machine")
932 (auth-source--aget alist
"host")
934 (auth-source-search-collection
937 (auth-source--aget alist
"login")
938 (auth-source--aget alist
"account")
939 (auth-source--aget alist
"user")
941 (auth-source-search-collection
944 (auth-source--aget alist
"port")
945 (auth-source--aget alist
"protocol")
948 ;; the required list of keys is nil, or
950 ;; every element of require is in n (normalized)
951 (let ((n (nth 0 (auth-source-netrc-normalize
952 (list alist
) file
))))
953 (cl-loop for req in require
954 always
(plist-get n req
)))))))
957 (if (and (functionp cached-secrets
)
959 (nth 5 (file-attributes file
))))
961 (auth-source-do-trivia
962 "auth-source-netrc-parse: using CACHED file data for %s"
964 (insert (funcall cached-secrets
)))
965 (insert-file-contents file
)
966 ;; cache all netrc files (used to be just .gpg files)
967 ;; Store the contents of the file heavily encrypted in memory.
968 ;; (note for the irony-impaired: they are just obfuscated)
970 auth-source-netrc-cache file
971 (list :mtime
(nth 5 (file-attributes file
))
972 :secret
(let ((v (mapcar #'1+ (buffer-string))))
973 (lambda () (apply #'string
(mapcar #'1- v
)))))))
974 (goto-char (point-min))
975 (let ((entries (auth-source-netrc-parse-entries check max
))
977 (while (setq alist
(pop entries
))
978 (push (nreverse alist
) result
)))
981 (when auth-source-gpg-encrypt-to
982 ;; (see bug#7487) making `epa-file-encrypt-to' local to
983 ;; this buffer lets epa-file skip the key selection query
984 ;; (see the `local-variable-p' check in
985 ;; `epa-file-write-region').
986 (unless (local-variable-p 'epa-file-encrypt-to
(current-buffer))
987 (make-local-variable 'epa-file-encrypt-to
))
988 (if (listp auth-source-gpg-encrypt-to
)
989 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to
)))
991 ;; ask AFTER we've successfully opened the file
992 (when (y-or-n-p (format "Save file %s? (%d deletions)"
994 (write-region (point-min) (point-max) file nil
'silent
)
995 (auth-source-do-debug
996 "auth-source-netrc-parse: modified %d lines in %s"
999 (nreverse result
))))))
1001 (defun auth-source-netrc-parse-next-interesting ()
1002 "Advance to the next interesting position in the current buffer."
1003 ;; If we're looking at a comment or are at the end of the line, move forward
1004 (while (or (looking-at "#")
1008 (skip-chars-forward "\t "))
1010 (defun auth-source-netrc-parse-one ()
1011 "Read one thing from the current buffer."
1012 (auth-source-netrc-parse-next-interesting)
1014 (when (or (looking-at "'\\([^']*\\)'")
1015 (looking-at "\"\\([^\"]*\\)\"")
1016 (looking-at "\\([^ \t\n]+\\)"))
1017 (forward-char (length (match-string 0)))
1018 (auth-source-netrc-parse-next-interesting)
1019 (match-string-no-properties 1)))
1021 ;; with thanks to org-mode
1022 (defsubst auth-source-current-line
(&optional pos
)
1024 (and pos
(goto-char pos
))
1025 ;; works also in narrowed buffer, because we start at 1, not point-min
1026 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
1028 (defun auth-source-netrc-parse-entries(check max
)
1029 "Parse up to MAX netrc entries, passed by CHECK, from the current buffer."
1030 (let ((adder (lambda(check alist all
)
1033 (> max
(length all
))
1034 (funcall check alist
))
1037 item item2 all alist default
)
1038 (while (setq item
(auth-source-netrc-parse-one))
1039 (setq default
(equal item
"default"))
1040 ;; We're starting a new machine. Save the old one.
1043 (equal item
"machine")))
1044 ;; (auth-source-do-trivia
1045 ;; "auth-source-netrc-parse-entries: got entry %S" alist)
1046 (setq all
(funcall adder check alist all
)
1048 ;; In default entries, we don't have a next token.
1049 ;; We store them as ("machine" . t)
1051 (push (cons "machine" t
) alist
)
1052 ;; Not a default entry. Grab the next item.
1053 (when (setq item2
(auth-source-netrc-parse-one))
1054 ;; Did we get a "machine" value?
1055 (if (equal item2
"machine")
1057 "%s: Unexpected `machine' token at line %d"
1058 "auth-source-netrc-parse-entries"
1059 (auth-source-current-line))
1060 (push (cons item item2
) alist
)))))
1062 ;; Clean up: if there's an entry left over, use it.
1064 (setq all
(funcall adder check alist all
))
1065 ;; (auth-source-do-trivia
1066 ;; "auth-source-netrc-parse-entries: got2 entry %S" alist)
1070 (defvar auth-source-passphrase-alist nil
)
1072 (defun auth-source-token-passphrase-callback-function (_context _key-id file
)
1073 (let* ((file (file-truename file
))
1074 (entry (assoc file auth-source-passphrase-alist
))
1076 ;; return the saved passphrase, calling a function if needed
1077 (or (copy-sequence (if (functionp (cdr entry
))
1078 (funcall (cdr entry
))
1082 (setq entry
(list file
))
1083 (push entry auth-source-passphrase-alist
))
1086 (format "Passphrase for %s tokens: " file
)
1088 (setcdr entry
(let ((p (copy-sequence passphrase
)))
1092 (defun auth-source-epa-extract-gpg-token (secret file
)
1093 "Pass either the decoded SECRET or the gpg:BASE64DATA version.
1094 FILE is the file from which we obtained this token."
1095 (when (string-match "^gpg:\\(.+\\)" secret
)
1096 (setq secret
(base64-decode-string (match-string 1 secret
))))
1097 (let ((context (epg-make-context 'OpenPGP
)))
1098 (epg-context-set-passphrase-callback
1100 (cons #'auth-source-token-passphrase-callback-function
1102 (epg-decrypt-string context secret
)))
1104 (defvar pp-escape-newlines
)
1106 (defun auth-source-epa-make-gpg-token (secret file
)
1107 (let ((context (epg-make-context 'OpenPGP
))
1108 (pp-escape-newlines nil
)
1110 (setf (epg-context-armor context
) t
)
1111 (epg-context-set-passphrase-callback
1113 (cons #'auth-source-token-passphrase-callback-function
1115 (setq cipher
(epg-encrypt-string context secret nil
))
1118 (base64-encode-region (point-min) (point-max) t
)
1119 (concat "gpg:" (buffer-substring-no-properties
1123 (defun auth-source--symbol-keyword (symbol)
1124 (intern (format ":%s" symbol
)))
1126 (defun auth-source-netrc-normalize (alist filename
)
1127 (mapcar (lambda (entry)
1129 (while (setq item
(pop entry
))
1130 (let ((k (car item
))
1133 ;; apply key aliases
1134 (setq k
(cond ((member k
'("machine")) "host")
1135 ((member k
'("login" "account")) "user")
1136 ((member k
'("protocol")) "port")
1137 ((member k
'("password")) "secret")
1140 ;; send back the secret in a function (lexical binding)
1141 (when (equal k
"secret")
1142 (setq v
(let ((lexv v
)
1143 (token-decoder nil
))
1144 (when (string-match "^gpg:" lexv
)
1145 ;; it's a GPG token: create a token decoder
1146 ;; which unsets itself once
1150 (auth-source-epa-extract-gpg-token
1153 (setq token-decoder nil
)))))
1156 (setq lexv
(funcall token-decoder lexv
)))
1158 (setq ret
(plist-put ret
1159 (auth-source--symbol-keyword k
)
1164 (cl-defun auth-source-netrc-search (&rest spec
1165 &key backend require create
1166 type max host user port
1168 "Given a property list SPEC, return search matches from the :backend.
1169 See `auth-source-search' for details on SPEC."
1170 ;; just in case, check that the type is correct (null or same as the backend)
1171 (cl-assert (or (null type
) (eq type
(oref backend type
)))
1172 t
"Invalid netrc search: %s %s")
1174 (let ((results (auth-source-netrc-normalize
1175 (auth-source-netrc-parse
1178 :file
(oref backend source
)
1182 (oref backend source
))))
1184 ;; if we need to create an entry AND none were found to match
1188 ;; create based on the spec and record the value
1190 ;; if the user did not want to create the entry
1191 ;; in the file, it will be returned
1192 (apply (slot-value backend
'create-function
) spec
)
1193 ;; if not, we do the search again without :create
1194 ;; to get the updated data.
1196 ;; the result will be returned, even if the search fails
1197 (apply #'auth-source-netrc-search
1198 (plist-put spec
:create nil
)))))
1201 (defun auth-source-netrc-element-or-first (v)
1206 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
1207 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))
1209 (cl-defun auth-source-netrc-create (&rest spec
1210 &key backend host port create
1212 (let* ((base-required '(host user port secret
))
1213 ;; we know (because of an assertion in auth-source-search) that the
1214 ;; :create parameter is either t or a list (which includes nil)
1215 (create-extra (if (eq t create
) nil create
))
1216 (current-data (car (auth-source-search :max
1
1219 (required (append base-required create-extra
))
1220 (file (oref backend source
))
1222 ;; `valist' is an alist
1224 ;; `artificial' will be returned if no creation is needed
1227 ;; only for base required elements (defined as function parameters):
1228 ;; fill in the valist with whatever data we may have from the search
1229 ;; we complete the first value if it's a list and use the value otherwise
1230 (dolist (br base-required
)
1231 (let ((val (plist-get spec
(auth-source--symbol-keyword br
))))
1233 (let ((br-choice (cond
1234 ;; all-accepting choice (predicate is t)
1236 ;; just the value otherwise
1239 (auth-source--aput valist br br-choice
))))))
1241 ;; for extra required elements, see if the spec includes a value for them
1242 (dolist (er create-extra
)
1243 (let ((k (auth-source--symbol-keyword er
))
1244 (keys (cl-loop for i below
(length spec
) by
2
1245 collect
(nth i spec
))))
1247 (auth-source--aput valist er
(plist-get spec k
)))))
1249 ;; for each required element
1250 (dolist (r required
)
1251 (let* ((data (auth-source--aget valist r
))
1252 ;; take the first element if the data is a list
1253 (data (or (auth-source-netrc-element-or-first data
)
1254 (plist-get current-data
1255 (auth-source--symbol-keyword r
))))
1256 ;; this is the default to be offered
1257 (given-default (auth-source--aget
1258 auth-source-creation-defaults r
))
1259 ;; the default supplementals are simple:
1260 ;; for the user, try `given-default' and then (user-login-name);
1261 ;; otherwise take `given-default'
1263 ((and (not given-default
) (eq r
'user
))
1266 (printable-defaults (list
1269 (auth-source-netrc-element-or-first
1270 (auth-source--aget valist
'user
))
1271 (plist-get artificial
:user
)
1275 (auth-source-netrc-element-or-first
1276 (auth-source--aget valist
'host
))
1277 (plist-get artificial
:host
)
1281 (auth-source-netrc-element-or-first
1282 (auth-source--aget valist
'port
))
1283 (plist-get artificial
:port
)
1285 (prompt (or (auth-source--aget auth-source-creation-prompts r
)
1287 (secret "%p password for %u@%h: ")
1288 (user "%p user name for %h: ")
1289 (host "%p host name for user %u: ")
1290 (port "%p port for %u@%h: "))
1291 (format "Enter %s (%%u@%%h:%%p): " r
)))
1292 (prompt (auth-source-format-prompt
1294 `((?u
,(auth-source--aget printable-defaults
'user
))
1295 (?h
,(auth-source--aget printable-defaults
'host
))
1296 (?p
,(auth-source--aget printable-defaults
'port
))))))
1298 ;; Store the data, prompting for the password if needed.
1301 ;; Special case prompt for passwords.
1302 ;; 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)))
1303 ;; TODO: or maybe leave as (setq auth-source-netrc-use-gpg-tokens 'never)
1304 (let* ((ep (format "Use GPG password tokens in %s?" file
))
1307 ((eq auth-source-netrc-use-gpg-tokens
'never
)
1309 ((listp auth-source-netrc-use-gpg-tokens
)
1310 (let ((check (copy-sequence
1311 auth-source-netrc-use-gpg-tokens
))
1314 (setq item
(pop check
))
1315 (when (or (eq (car item
) t
)
1316 (string-match (car item
) file
))
1317 (setq ret
(cdr item
))
1321 (plain (or (eval default
) (read-passwd prompt
))))
1322 ;; ask if we don't know what to do (in which case
1323 ;; auth-source-netrc-use-gpg-tokens must be a list)
1325 (setq gpg-encrypt
(if (y-or-n-p ep
) 'gpg
'never
))
1326 ;; TODO: save the defcustom now? or ask?
1327 (setq auth-source-netrc-use-gpg-tokens
1328 (cons `(,file
,gpg-encrypt
)
1329 auth-source-netrc-use-gpg-tokens
)))
1330 (if (eq gpg-encrypt
'gpg
)
1331 (auth-source-epa-make-gpg-token plain file
)
1333 (if (stringp default
)
1334 (read-string (if (string-match ": *\\'" prompt
)
1335 (concat (substring prompt
0 (match-beginning 0))
1336 " (default " default
"): ")
1337 (concat prompt
"(default " default
") "))
1342 (setq artificial
(plist-put artificial
1343 (auth-source--symbol-keyword r
)
1349 ;; When r is not an empty string...
1350 (when (and (stringp data
)
1351 (< 0 (length data
)))
1352 ;; this function is not strictly necessary but I think it
1353 ;; makes the code clearer -tzz
1354 (let ((printer (lambda ()
1355 ;; append the key (the symbol name of r)
1356 ;; and the value in r
1359 (if (zerop (length add
)) "" " ")
1360 ;; remap auth-source tokens to netrc
1365 (port "port") ; redundant but clearer
1366 (t (symbol-name r
)))
1367 (if (string-match "[\"# ]" data
)
1370 (setq add
(concat add
(funcall printer
)))))))
1377 (lambda () (auth-source-netrc-saver file add
))))
1381 (defun auth-source-netrc-saver (file add
)
1382 "Save a line ADD in FILE, prompting along the way.
1383 Respects `auth-source-save-behavior'. Uses
1384 `auth-source-netrc-cache' to avoid prompting more than once."
1385 (let* ((key (format "%s %s" file
(rfc2104-hash 'md5
64 16 file add
)))
1386 (cached (assoc key auth-source-netrc-cache
)))
1389 (auth-source-do-trivia
1390 "auth-source-netrc-saver: found previous run for key %s, returning"
1393 (when (file-exists-p file
)
1394 (insert-file-contents file
))
1395 (when auth-source-gpg-encrypt-to
1396 ;; (see bug#7487) making `epa-file-encrypt-to' local to
1397 ;; this buffer lets epa-file skip the key selection query
1398 ;; (see the `local-variable-p' check in
1399 ;; `epa-file-write-region').
1400 (unless (local-variable-p 'epa-file-encrypt-to
(current-buffer))
1401 (make-local-variable 'epa-file-encrypt-to
))
1402 (if (listp auth-source-gpg-encrypt-to
)
1403 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to
)))
1404 ;; we want the new data to be found first, so insert at beginning
1405 (goto-char (point-min))
1407 ;; Ask AFTER we've successfully opened the file.
1408 (let ((prompt (format "Save auth info to file %s? " file
))
1409 (done (not (eq auth-source-save-behavior
'ask
)))
1410 (bufname "*auth-source Help*")
1413 (setq k
(auth-source-read-char-choice prompt
'(?y ?n ?N ?e ??
)))
1417 (with-output-to-temp-buffer bufname
1419 (concat "(y)es, save\n"
1420 "(n)o but use the info\n"
1421 "(N)o and don't ask to save again\n"
1423 "(?) for help as you can see.\n"))
1424 ;; Why? Doesn't with-output-to-temp-buffer already do
1425 ;; the exact same thing anyway? --Stef
1426 (set-buffer standard-output
)
1433 (customize-save-variable 'auth-source-save-behavior nil
))
1434 (?e
(setq add
(read-string "Line to add: " add
)))
1437 (when (get-buffer-window bufname
)
1438 (delete-window (get-buffer-window bufname
)))
1440 ;; Make sure the info is not saved.
1441 (when (null auth-source-save-behavior
)
1444 (when (< 0 (length add
))
1449 (write-region (point-min) (point-max) file nil
'silent
)
1450 ;; Make the .authinfo file non-world-readable.
1451 (set-file-modes file
#o600
)
1452 (auth-source-do-debug
1453 "auth-source-netrc-create: wrote 1 new line to %s"
1455 (message "Saved new authentication information to %s" file
)
1457 (auth-source--aput auth-source-netrc-cache key
"ran"))))
1459 ;;; Backend specific parsing: Secrets API backend
1461 (defun auth-source-secrets-listify-pattern (pattern)
1462 "Convert a pattern with lists to a list of string patterns.
1464 auth-source patterns can have values of the form :foo (\"bar\"
1465 \"qux\"), which means to match any secret with :foo equal to
1466 \"bar\" or :foo equal to \"qux\". The secrets backend supports
1467 only string values for patterns, so this routine returns a list
1468 of patterns that is equivalent to the single original pattern
1469 when interpreted such that if a secret matches any pattern in the
1470 list, it matches the original pattern."
1473 (let* ((key (pop pattern
))
1474 (value (pop pattern
))
1475 (tails (auth-source-secrets-listify-pattern pattern
))
1476 (heads (if (stringp value
)
1477 (list (list key value
))
1478 (mapcar (lambda (v) (list key v
)) value
))))
1479 (cl-loop for h in heads
1480 nconc
(cl-loop for tl in tails collect
(append h tl
))))))
1482 (cl-defun auth-source-secrets-search (&rest spec
1483 &key backend create delete label max
1485 "Search the Secrets API; spec is like `auth-source'.
1487 The :label key specifies the item's label. It is the only key
1488 that can specify a substring. Any :label value besides a string
1489 will allow any label.
1491 All other search keys must match exactly. If you need substring
1492 matching, do a wider search and narrow it down yourself.
1494 You'll get back all the properties of the token as a plist.
1496 Here's an example that looks for the first item in the `Login'
1499 (let ((auth-sources \\='(\"secrets:Login\")))
1500 (auth-source-search :max 1))
1502 Here's another that looks for the first item in the `Login'
1503 Secrets collection whose label contains `gnus':
1505 (let ((auth-sources \\='(\"secrets:Login\")))
1506 (auth-source-search :max 1 :label \"gnus\"))
1508 And this one looks for the first item in the `Login' Secrets
1509 collection that's a Google Chrome entry for the git.gnus.org site
1510 authentication tokens:
1512 (let ((auth-sources \\='(\"secrets:Login\")))
1513 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1517 ;; (secrets-delete-item coll elt)
1518 (cl-assert (not delete
) nil
1519 "The Secrets API auth-source backend doesn't support deletion yet")
1521 (let* ((coll (oref backend source
))
1522 (max (or max
5000)) ; sanity check: default to stop at 5K
1523 (ignored-keys '(:create
:delete
:max
:backend
:label
:require
:type
))
1524 (search-keys (cl-loop for i below
(length spec
) by
2
1525 unless
(memq (nth i spec
) ignored-keys
)
1526 collect
(nth i spec
)))
1527 ;; build a search spec without the ignored keys
1528 ;; if a search key is nil or t (match anything), we skip it
1529 (search-specs (auth-source-secrets-listify-pattern
1530 (apply #'append
(mapcar
1532 (if (or (null (plist-get spec k
))
1533 (eq t
(plist-get spec k
)))
1535 (list k
(plist-get spec k
))))
1537 ;; needed keys (always including host, login, port, and secret)
1538 (returned-keys (delete-dups (append
1539 '(:host
:login
:port
:secret
)
1543 for search-spec in search-specs
1545 (cl-loop for item in
(apply #'secrets-search-items coll search-spec
)
1546 unless
(and (stringp label
)
1547 (not (string-match label item
)))
1549 ;; TODO: respect max in `secrets-search-items', not after the fact
1550 (items (butlast items
(- (length items
) max
)))
1551 ;; convert the item name to a full plist
1552 (items (mapcar (lambda (item)
1554 ;; make an entry for the secret (password) element
1557 (let ((v (secrets-get-secret coll item
)))
1559 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1561 (mapcar (lambda (entry)
1562 (list (car entry
) (cdr entry
)))
1563 (secrets-get-attributes coll item
)))))
1565 ;; ensure each item has each key in `returned-keys'
1566 (items (mapcar (lambda (plist)
1569 (mapcar (lambda (req)
1570 (if (plist-get plist req
)
1577 ;; if we need to create an entry AND none were found to match
1581 ;; create based on the spec and record the value
1583 ;; if the user did not want to create the entry
1584 ;; in the file, it will be returned
1585 (apply (slot-value backend
'create-function
) spec
)
1586 ;; if not, we do the search again without :create
1587 ;; to get the updated data.
1589 ;; the result will be returned, even if the search fails
1590 (apply #'auth-source-secrets-search
1591 (plist-put spec
:create nil
))))))
1594 (cl-defun auth-source-secrets-create (&rest spec
1595 &key backend host port create
1597 (let* ((base-required '(host user port secret label
))
1598 ;; we know (because of an assertion in auth-source-search) that the
1599 ;; :create parameter is either t or a list (which includes nil)
1600 (create-extra (if (eq t create
) nil create
))
1601 (current-data (car (auth-source-search :max
1
1604 (required (append base-required create-extra
))
1605 (collection (oref backend source
))
1606 ;; `args' are the arguments for `secrets-create-item'.
1608 ;; `valist' is an alist
1610 ;; `artificial' will be returned if no creation is needed
1613 ;; only for base required elements (defined as function parameters):
1614 ;; fill in the valist with whatever data we may have from the search
1615 ;; we complete the first value if it's a list and use the value otherwise
1616 (dolist (br base-required
)
1617 (let ((val (plist-get spec
(auth-source--symbol-keyword br
))))
1619 (let ((br-choice (cond
1620 ;; all-accepting choice (predicate is t)
1622 ;; just the value otherwise
1625 (auth-source--aput valist br br-choice
))))))
1627 ;; for extra required elements, see if the spec includes a value for them
1628 (dolist (er create-extra
)
1629 (let ((k (auth-source--symbol-keyword er
))
1630 (keys (cl-loop for i below
(length spec
) by
2
1631 collect
(nth i spec
))))
1633 (auth-source--aput valist er
(plist-get spec k
)))))
1635 ;; for each required element
1636 (dolist (r required
)
1637 (let* ((data (auth-source--aget valist r
))
1638 ;; take the first element if the data is a list
1639 (data (or (auth-source-netrc-element-or-first data
)
1640 (plist-get current-data
1641 (auth-source--symbol-keyword r
))))
1642 ;; this is the default to be offered
1643 (given-default (auth-source--aget
1644 auth-source-creation-defaults r
))
1645 ;; the default supplementals are simple:
1646 ;; for the user, try `given-default' and then (user-login-name);
1647 ;; for the label, try `given-default' and then user@host;
1648 ;; otherwise take `given-default'
1650 ((and (not given-default
) (eq r
'user
))
1652 ((and (not given-default
) (eq r
'label
))
1654 (or (auth-source-netrc-element-or-first
1655 (auth-source--aget valist
'user
))
1656 (plist-get artificial
:user
))
1657 (or (auth-source-netrc-element-or-first
1658 (auth-source--aget valist
'host
))
1659 (plist-get artificial
:host
))))
1661 (printable-defaults (list
1664 (auth-source-netrc-element-or-first
1665 (auth-source--aget valist
'user
))
1666 (plist-get artificial
:user
)
1670 (auth-source-netrc-element-or-first
1671 (auth-source--aget valist
'host
))
1672 (plist-get artificial
:host
)
1676 (auth-source-netrc-element-or-first
1677 (auth-source--aget valist
'port
))
1678 (plist-get artificial
:port
)
1682 (auth-source-netrc-element-or-first
1683 (auth-source--aget valist
'label
))
1684 (plist-get artificial
:label
)
1686 (prompt (or (auth-source--aget auth-source-creation-prompts r
)
1688 (secret "%p password for %u@%h: ")
1689 (user "%p user name for %h: ")
1690 (host "%p host name for user %u: ")
1691 (port "%p port for %u@%h: ")
1692 (label "Enter label for %u@%h: "))
1693 (format "Enter %s (%%u@%%h:%%p): " r
)))
1694 (prompt (auth-source-format-prompt
1696 `((?u
,(auth-source--aget printable-defaults
'user
))
1697 (?h
,(auth-source--aget printable-defaults
'host
))
1698 (?p
,(auth-source--aget printable-defaults
'port
))))))
1700 ;; Store the data, prompting for the password if needed.
1703 (or (eval default
) (read-passwd prompt
))
1704 (if (stringp default
)
1705 (read-string (if (string-match ": *\\'" prompt
)
1706 (concat (substring prompt
0 (match-beginning 0))
1707 " (default " default
"): ")
1708 (concat prompt
"(default " default
") "))
1713 (setq artificial
(plist-put artificial
1714 (auth-source--symbol-keyword r
)
1720 ;; When r is not an empty string...
1721 (when (and (stringp data
)
1723 (not (member r
'(secret label
))))
1724 ;; append the key (the symbol name of r)
1725 ;; and the value in r
1726 (setq args
(append args
(list (auth-source--symbol-keyword r
) data
))))))
1731 (let* ((collection collection
)
1732 (item (plist-get artificial
:label
))
1733 (secret (plist-get artificial
:secret
))
1734 (secret (if (functionp secret
) (funcall secret
) secret
)))
1735 (lambda () (apply 'secrets-create-item collection item secret args
))))
1739 ;;; Backend specific parsing: Mac OS Keychain (using /usr/bin/security) backend
1741 (cl-defun auth-source-macos-keychain-search (&rest spec
1742 &key backend create delete type max
1744 "Search the macOS Keychain; spec is like `auth-source'.
1746 All search keys must match exactly. If you need substring
1747 matching, do a wider search and narrow it down yourself.
1749 You'll get back all the properties of the token as a plist.
1751 The :type key is either `macos-keychain-internet' or
1752 `macos-keychain-generic'.
1754 For the internet keychain type, the :label key searches the
1755 item's labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1756 Similarly, :host maps to \"-s HOST\", :user maps to \"-a USER\",
1757 and :port maps to \"-P PORT\" or \"-r PROT\"
1758 \(note PROT has to be a 4-character string).
1760 For the generic keychain type, the :label key searches the item's
1761 labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1762 Similarly, :host maps to \"-c HOST\" (the \"creator\" keychain
1763 field), :user maps to \"-a USER\", and :port maps to \"-s PORT\".
1765 Here's an example that looks for the first item in the default
1766 generic macOS Keychain:
1768 (let ((auth-sources \\='(macos-keychain-generic)))
1769 (auth-source-search :max 1)
1771 Here's another that looks for the first item in the internet
1772 macOS Keychain collection whose label is `gnus':
1774 (let ((auth-sources \\='(macos-keychain-internet)))
1775 (auth-source-search :max 1 :label \"gnus\")
1777 And this one looks for the first item in the internet keychain
1778 entries for git.gnus.org:
1780 (let ((auth-sources \\='(macos-keychain-internet\")))
1781 (auth-source-search :max 1 :host \"git.gnus.org\"))
1784 (cl-assert (not create
) nil
1785 "The macOS Keychain auth-source backend doesn't support creation yet")
1787 ;; (macos-keychain-delete-item coll elt)
1788 (cl-assert (not delete
) nil
1789 "The macOS Keychain auth-source backend doesn't support deletion yet")
1791 (let* ((coll (oref backend source
))
1792 (max (or max
5000)) ; sanity check: default to stop at 5K
1793 ;; Filter out ignored keys from the spec
1794 (ignored-keys '(:create
:delete
:max
:backend
:label
:host
:port
))
1795 ;; Build a search spec without the ignored keys
1796 ;; FIXME make this loop a function? it's used in at least 3 places
1797 (search-keys (cl-loop for i below
(length spec
) by
2
1798 unless
(memq (nth i spec
) ignored-keys
)
1799 collect
(nth i spec
)))
1800 ;; If a search key value is nil or t (match anything), we skip it
1801 (search-spec (apply #'append
(mapcar
1803 (if (or (null (plist-get spec k
))
1804 (eq t
(plist-get spec k
)))
1806 (list k
(plist-get spec k
))))
1808 ;; needed keys (always including host, login, port, and secret)
1809 (returned-keys (delete-dups (append
1810 '(:host
:login
:port
:secret
)
1812 ;; Extract host and port from spec
1813 (hosts (plist-get spec
:host
))
1814 (hosts (if (and hosts
(listp hosts
)) hosts
`(,hosts
)))
1815 (ports (plist-get spec
:port
))
1816 (ports (if (and ports
(listp ports
)) ports
`(,ports
)))
1817 ;; Loop through all combinations of host/port and pass each of these to
1818 ;; auth-source-macos-keychain-search-items
1819 (items (catch 'match
1820 (dolist (host hosts
)
1821 (dolist (port ports
)
1822 (let* ((port (if port
(format "%S" port
)))
1823 (items (apply #'auth-source-macos-keychain-search-items
1830 (throw 'match items
)))))))
1832 ;; ensure each item has each key in `returned-keys'
1833 (items (mapcar (lambda (plist)
1836 (mapcar (lambda (req)
1837 (if (plist-get plist req
)
1846 (defun auth-source--decode-octal-string (string)
1847 "Convert octal string to utf-8 string. E.g: 'a\134b' to 'a\b'"
1848 (let ((list (string-to-list string
))
1849 (size (length string
)))
1850 (decode-coding-string
1851 (apply #'unibyte-string
1852 (cl-loop for i
= 0 then
(+ i
(if (eq (nth i list
) ?
\\) 4 1))
1853 for var
= (nth i list
)
1856 collect
(string-to-number
1857 (concat (cl-subseq list
(+ i
1) (+ i
4))) 8)
1862 (cl-defun auth-source-macos-keychain-search-items (coll _type _max host port
1863 &key label type user
1865 (let* ((keychain-generic (eq type
'macos-keychain-generic
))
1866 (args `(,(if keychain-generic
1867 "find-generic-password"
1868 "find-internet-password")
1870 (ret (list :type type
)))
1872 (setq args
(append args
(list "-l" label
))))
1874 (setq args
(append args
(list (if keychain-generic
"-c" "-s") host
))))
1876 (setq args
(append args
(list "-a" user
))))
1879 (if keychain-generic
1880 (setq args
(append args
(list "-s" port
)))
1881 (setq args
(append args
(list
1882 (if (string-match "[0-9]+" port
) "-P" "-r")
1885 (unless (equal coll
"default")
1886 (setq args
(append args
(list coll
))))
1889 (apply #'call-process
"/usr/bin/security" nil t nil args
)
1890 (goto-char (point-min))
1893 ((looking-at "^password: \\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1894 (setq ret
(auth-source-macos-keychain-result-append
1898 (let ((v (auth-source--decode-octal-string
1901 ;; TODO: check if this is really the label
1902 ;; match 0x00000007 <blob>="AppleID"
1904 "^[ ]+0x00000007 <blob>=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1905 (setq ret
(auth-source-macos-keychain-result-append
1909 (auth-source--decode-octal-string (match-string 1)))))
1910 ;; match "crtr"<uint32>="aapl"
1911 ;; match "svce"<blob>="AppleID"
1913 "^[ ]+\"\\([a-z]+\\)\"[^=]+=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1914 (setq ret
(auth-source-macos-keychain-result-append
1917 (auth-source--decode-octal-string (match-string 1))
1918 (auth-source--decode-octal-string (match-string 2))))))
1920 ;; return `ret' iff it has the :secret key
1921 (and (plist-get ret
:secret
) (list ret
))))
1923 (defun auth-source-macos-keychain-result-append (result generic k v
)
1925 (push (auth-source--symbol-keyword
1927 ((equal k
"acct") "user")
1928 ;; for generic keychains, creator is host, service is port
1929 ((and generic
(equal k
"crtr")) "host")
1930 ((and generic
(equal k
"svce")) "port")
1931 ;; for internet keychains, protocol is port, server is host
1932 ((and (not generic
) (equal k
"ptcl")) "port")
1933 ((and (not generic
) (equal k
"srvr")) "host")
1937 (defun auth-source-macos-keychain-create (&rest spec
)
1941 ;;; Backend specific parsing: PLSTORE backend
1943 (cl-defun auth-source-plstore-search (&rest spec
1944 &key backend create delete max
1946 "Search the PLSTORE; spec is like `auth-source'."
1947 (let* ((store (oref backend data
))
1948 (max (or max
5000)) ; sanity check: default to stop at 5K
1949 (ignored-keys '(:create
:delete
:max
:backend
:label
:require
:type
))
1950 (search-keys (cl-loop for i below
(length spec
) by
2
1951 unless
(memq (nth i spec
) ignored-keys
)
1952 collect
(nth i spec
)))
1953 ;; build a search spec without the ignored keys
1954 ;; if a search key is nil or t (match anything), we skip it
1955 (search-spec (apply #'append
(mapcar
1957 (let ((v (plist-get spec k
)))
1965 ;; needed keys (always including host, login, port, and secret)
1966 (returned-keys (delete-dups (append
1967 '(:host
:login
:port
:secret
)
1969 (items (plstore-find store search-spec
))
1970 (item-names (mapcar #'car items
))
1971 (items (butlast items
(- (length items
) max
)))
1972 ;; convert the item to a full plist
1973 (items (mapcar (lambda (item)
1974 (let* ((plist (copy-tree (cdr item
)))
1975 (secret (plist-member plist
:secret
)))
1979 (let ((v (car (cdr secret
))))
1983 ;; ensure each item has each key in `returned-keys'
1984 (items (mapcar (lambda (plist)
1987 (mapcar (lambda (req)
1988 (if (plist-get plist req
)
1995 ;; if we need to create an entry AND none were found to match
1999 ;; create based on the spec and record the value
2001 ;; if the user did not want to create the entry
2002 ;; in the file, it will be returned
2003 (apply (slot-value backend
'create-function
) spec
)
2004 ;; if not, we do the search again without :create
2005 ;; to get the updated data.
2007 ;; the result will be returned, even if the search fails
2008 (apply #'auth-source-plstore-search
2009 (plist-put spec
:create nil
)))))
2012 (dolist (item-name item-names
)
2013 (plstore-delete store item-name
))
2014 (plstore-save store
)))
2017 (cl-defun auth-source-plstore-create (&rest spec
2018 &key backend host port create
2020 (let* ((base-required '(host user port secret
))
2021 (base-secret '(secret))
2022 ;; we know (because of an assertion in auth-source-search) that the
2023 ;; :create parameter is either t or a list (which includes nil)
2024 (create-extra (if (eq t create
) nil create
))
2025 (current-data (car (auth-source-search :max
1
2028 (required (append base-required create-extra
))
2029 ;; `valist' is an alist
2031 ;; `artificial' will be returned if no creation is needed
2035 ;; only for base required elements (defined as function parameters):
2036 ;; fill in the valist with whatever data we may have from the search
2037 ;; we complete the first value if it's a list and use the value otherwise
2038 (dolist (br base-required
)
2039 (let ((val (plist-get spec
(auth-source--symbol-keyword br
))))
2041 (let ((br-choice (cond
2042 ;; all-accepting choice (predicate is t)
2044 ;; just the value otherwise
2047 (auth-source--aput valist br br-choice
))))))
2049 ;; for extra required elements, see if the spec includes a value for them
2050 (dolist (er create-extra
)
2051 (let ((k (auth-source--symbol-keyword er
))
2052 (keys (cl-loop for i below
(length spec
) by
2
2053 collect
(nth i spec
))))
2055 (auth-source--aput valist er
(plist-get spec k
)))))
2057 ;; for each required element
2058 (dolist (r required
)
2059 (let* ((data (auth-source--aget valist r
))
2060 ;; take the first element if the data is a list
2061 (data (or (auth-source-netrc-element-or-first data
)
2062 (plist-get current-data
2063 (auth-source--symbol-keyword r
))))
2064 ;; this is the default to be offered
2065 (given-default (auth-source--aget
2066 auth-source-creation-defaults r
))
2067 ;; the default supplementals are simple:
2068 ;; for the user, try `given-default' and then (user-login-name);
2069 ;; otherwise take `given-default'
2071 ((and (not given-default
) (eq r
'user
))
2074 (printable-defaults (list
2077 (auth-source-netrc-element-or-first
2078 (auth-source--aget valist
'user
))
2079 (plist-get artificial
:user
)
2083 (auth-source-netrc-element-or-first
2084 (auth-source--aget valist
'host
))
2085 (plist-get artificial
:host
)
2089 (auth-source-netrc-element-or-first
2090 (auth-source--aget valist
'port
))
2091 (plist-get artificial
:port
)
2093 (prompt (or (auth-source--aget auth-source-creation-prompts r
)
2095 (secret "%p password for %u@%h: ")
2096 (user "%p user name for %h: ")
2097 (host "%p host name for user %u: ")
2098 (port "%p port for %u@%h: "))
2099 (format "Enter %s (%%u@%%h:%%p): " r
)))
2100 (prompt (auth-source-format-prompt
2102 `((?u
,(auth-source--aget printable-defaults
'user
))
2103 (?h
,(auth-source--aget printable-defaults
'host
))
2104 (?p
,(auth-source--aget printable-defaults
'port
))))))
2106 ;; Store the data, prompting for the password if needed.
2109 (or (eval default
) (read-passwd prompt
))
2110 (if (stringp default
)
2112 (if (string-match ": *\\'" prompt
)
2113 (concat (substring prompt
0 (match-beginning 0))
2114 " (default " default
"): ")
2115 (concat prompt
"(default " default
") "))
2120 (if (member r base-secret
)
2121 (setq secret-artificial
2122 (plist-put secret-artificial
2123 (auth-source--symbol-keyword r
)
2125 (setq artificial
(plist-put artificial
2126 (auth-source--symbol-keyword r
)
2128 (plstore-put (oref backend data
)
2129 (sha1 (format "%s@%s:%s"
2130 (plist-get artificial
:user
)
2131 (plist-get artificial
:host
)
2132 (plist-get artificial
:port
)))
2133 artificial secret-artificial
)
2134 (if (y-or-n-p (format "Save auth info to file %s? "
2135 (plstore-get-file (oref backend data
))))
2136 (plstore-save (oref backend data
)))))
2138 ;;; Backend specific parsing: JSON backend
2139 ;;; (auth-source-search :max 1 :machine "imap.gmail.com")
2140 ;;; (auth-source-search :max 1 :host '("my-gmail" "imap.gmail.com") :port '(993 "imaps" "imap" "993" "143") :user nil :require '(:user :secret))
2142 (defun auth-source-json-check (host user port require item
)
2144 (auth-source-search-collection
2147 (plist-get item
:machine
)
2148 (plist-get item
:host
)
2150 (auth-source-search-collection
2153 (plist-get item
:login
)
2154 (plist-get item
:account
)
2155 (plist-get item
:user
)
2157 (auth-source-search-collection
2160 (plist-get item
:port
)
2161 (plist-get item
:protocol
)
2164 ;; the required list of keys is nil, or
2166 ;; every element of require is in
2167 (cl-loop for req in require
2168 always
(plist-get item req
)))))
2170 (cl-defun auth-source-json-search (&rest spec
2171 &key backend require
2172 type max host user port
2174 "Given a property list SPEC, return search matches from the :backend.
2175 See `auth-source-search' for details on SPEC."
2176 ;; just in case, check that the type is correct (null or same as the backend)
2177 (cl-assert (or (null type
) (eq type
(oref backend type
)))
2178 t
"Invalid JSON search: %s %s")
2180 ;; Hide the secrets early to avoid accidental exposure.
2182 (mapcar (lambda (entry)
2185 (let* ((item (pop entry
))
2186 (k (auth-source--symbol-keyword (car item
)))
2188 (setq k
(cond ((memq k
'(:machine
)) :host
)
2189 ((memq k
'(:login
:account
)) :user
)
2190 ((memq k
'(:protocol
)) :port
)
2191 ((memq k
'(:password
)) :secret
)
2193 ;; send back the secret in a function (lexical binding)
2194 (when (eq k
:secret
)
2195 (setq v
(let ((lexv v
))
2197 (setq ret
(plist-put ret k v
))))
2199 (json-read-file (oref backend source
))))
2200 (max (or max
5000)) ; sanity check: default to stop at 5K
2202 (dolist (item jdata
)
2204 (> max
(length all
))
2205 (auth-source-json-check host user port require item
))
2211 ;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
2213 ;; deprecate the old interface
2214 (make-obsolete 'auth-source-user-or-password
2215 'auth-source-search
"Emacs 24.1")
2216 (make-obsolete 'auth-source-forget-user-or-password
2217 'auth-source-forget
"Emacs 24.1")
2219 (defun auth-source-user-or-password
2220 (mode host port
&optional username create-missing delete-existing
)
2221 "Find MODE (string or list of strings) matching HOST and PORT.
2223 DEPRECATED in favor of `auth-source-search'!
2225 USERNAME is optional and will be used as \"login\" in a search
2226 across the Secret Service API (see secrets.el) if the resulting
2227 items don't have a username. This means that if you search for
2228 username \"joe\" and it matches an item but the item doesn't have
2229 a :user attribute, the username \"joe\" will be returned.
2231 A non nil DELETE-EXISTING means deleting any matching password
2232 entry in the respective sources. This is useful only when
2233 CREATE-MISSING is non nil as well; the intended use case is to
2234 remove wrong password entries.
2236 If no matching entry is found, and CREATE-MISSING is non nil,
2237 the password will be retrieved interactively, and it will be
2238 stored in the password database which matches best (see
2241 MODE can be \"login\" or \"password\"."
2242 (auth-source-do-debug
2243 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
2244 mode host port username
)
2246 (let* ((listy (listp mode
))
2247 (mode (if listy mode
(list mode
)))
2248 ;; (cname (if username
2249 ;; (format "%s %s:%s %s" mode host port username)
2250 ;; (format "%s %s:%s" mode host port)))
2251 (search (list :host host
:port port
))
2252 (search (if username
(append search
(list :user username
)) search
))
2253 (search (if create-missing
2254 (append search
(list :create t
))
2256 (search (if delete-existing
2257 (append search
(list :delete t
))
2259 ;; (found (if (not delete-existing)
2260 ;; (gethash cname auth-source-cache)
2261 ;; (remhash cname auth-source-cache)
2266 (auth-source-do-debug
2267 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
2269 ;; don't show the password
2270 (if (and (member "password" mode
) t
)
2274 found
) ; return the found data
2275 ;; else, if not found, search with a max of 1
2276 (let ((choice (nth 0 (apply #'auth-source-search
2277 (append '(:max
1) search
)))))
2281 ((equal "password" m
)
2282 (push (if (plist-get choice
:secret
)
2283 (funcall (plist-get choice
:secret
))
2286 (push (plist-get choice
:user
) found
)))))
2287 (setq found
(nreverse found
))
2288 (setq found
(if listy found
(car-safe found
)))))
2292 (defun auth-source-user-and-password (host &optional user
)
2293 (let* ((auth-info (car
2299 :require
'(:user
:secret
)
2304 :require
'(:user
:secret
)
2306 (user (plist-get auth-info
:user
))
2307 (password (plist-get auth-info
:secret
)))
2308 (when (functionp password
)
2309 (setq password
(funcall password
)))
2310 (list user password auth-info
)))
2312 (provide 'auth-source
)
2314 ;;; auth-source.el ends here