1 ;;; auth-source.el --- authentication sources for Gnus and Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 2008-2017 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
))
1318 ;; FIXME: `ret' unused.
1319 ;; Should we return it here?
1322 (plain (or (eval default
) (read-passwd prompt
))))
1323 ;; ask if we don't know what to do (in which case
1324 ;; auth-source-netrc-use-gpg-tokens must be a list)
1326 (setq gpg-encrypt
(if (y-or-n-p ep
) 'gpg
'never
))
1327 ;; TODO: save the defcustom now? or ask?
1328 (setq auth-source-netrc-use-gpg-tokens
1329 (cons `(,file
,gpg-encrypt
)
1330 auth-source-netrc-use-gpg-tokens
)))
1331 (if (eq gpg-encrypt
'gpg
)
1332 (auth-source-epa-make-gpg-token plain file
)
1334 (if (stringp default
)
1335 (read-string (if (string-match ": *\\'" prompt
)
1336 (concat (substring prompt
0 (match-beginning 0))
1337 " (default " default
"): ")
1338 (concat prompt
"(default " default
") "))
1343 (setq artificial
(plist-put artificial
1344 (auth-source--symbol-keyword r
)
1350 ;; When r is not an empty string...
1351 (when (and (stringp data
)
1352 (< 0 (length data
)))
1353 ;; this function is not strictly necessary but I think it
1354 ;; makes the code clearer -tzz
1355 (let ((printer (lambda ()
1356 ;; append the key (the symbol name of r)
1357 ;; and the value in r
1360 (if (zerop (length add
)) "" " ")
1361 ;; remap auth-source tokens to netrc
1366 (port "port") ; redundant but clearer
1367 (t (symbol-name r
)))
1368 (if (string-match "[\"# ]" data
)
1371 (setq add
(concat add
(funcall printer
)))))))
1378 (lambda () (auth-source-netrc-saver file add
))))
1382 (defun auth-source-netrc-saver (file add
)
1383 "Save a line ADD in FILE, prompting along the way.
1384 Respects `auth-source-save-behavior'. Uses
1385 `auth-source-netrc-cache' to avoid prompting more than once."
1386 (let* ((key (format "%s %s" file
(rfc2104-hash 'md5
64 16 file add
)))
1387 (cached (assoc key auth-source-netrc-cache
)))
1390 (auth-source-do-trivia
1391 "auth-source-netrc-saver: found previous run for key %s, returning"
1394 (when (file-exists-p file
)
1395 (insert-file-contents file
))
1396 (when auth-source-gpg-encrypt-to
1397 ;; (see bug#7487) making `epa-file-encrypt-to' local to
1398 ;; this buffer lets epa-file skip the key selection query
1399 ;; (see the `local-variable-p' check in
1400 ;; `epa-file-write-region').
1401 (unless (local-variable-p 'epa-file-encrypt-to
(current-buffer))
1402 (make-local-variable 'epa-file-encrypt-to
))
1403 (if (listp auth-source-gpg-encrypt-to
)
1404 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to
)))
1405 ;; we want the new data to be found first, so insert at beginning
1406 (goto-char (point-min))
1408 ;; Ask AFTER we've successfully opened the file.
1409 (let ((prompt (format "Save auth info to file %s? " file
))
1410 (done (not (eq auth-source-save-behavior
'ask
)))
1411 (bufname "*auth-source Help*")
1414 (setq k
(auth-source-read-char-choice prompt
'(?y ?n ?N ?e ??
)))
1418 (with-output-to-temp-buffer bufname
1420 (concat "(y)es, save\n"
1421 "(n)o but use the info\n"
1422 "(N)o and don't ask to save again\n"
1424 "(?) for help as you can see.\n"))
1425 ;; Why? Doesn't with-output-to-temp-buffer already do
1426 ;; the exact same thing anyway? --Stef
1427 (set-buffer standard-output
)
1434 (customize-save-variable 'auth-source-save-behavior nil
))
1435 (?e
(setq add
(read-string "Line to add: " add
)))
1438 (when (get-buffer-window bufname
)
1439 (delete-window (get-buffer-window bufname
)))
1441 ;; Make sure the info is not saved.
1442 (when (null auth-source-save-behavior
)
1445 (when (< 0 (length add
))
1450 (write-region (point-min) (point-max) file nil
'silent
)
1451 ;; Make the .authinfo file non-world-readable.
1452 (set-file-modes file
#o600
)
1453 (auth-source-do-debug
1454 "auth-source-netrc-create: wrote 1 new line to %s"
1456 (message "Saved new authentication information to %s" file
)
1458 (auth-source--aput auth-source-netrc-cache key
"ran"))))
1460 ;;; Backend specific parsing: Secrets API backend
1462 (defun auth-source-secrets-listify-pattern (pattern)
1463 "Convert a pattern with lists to a list of string patterns.
1465 auth-source patterns can have values of the form :foo (\"bar\"
1466 \"qux\"), which means to match any secret with :foo equal to
1467 \"bar\" or :foo equal to \"qux\". The secrets backend supports
1468 only string values for patterns, so this routine returns a list
1469 of patterns that is equivalent to the single original pattern
1470 when interpreted such that if a secret matches any pattern in the
1471 list, it matches the original pattern."
1474 (let* ((key (pop pattern
))
1475 (value (pop pattern
))
1476 (tails (auth-source-secrets-listify-pattern pattern
))
1477 (heads (if (stringp value
)
1478 (list (list key value
))
1479 (mapcar (lambda (v) (list key v
)) value
))))
1480 (cl-loop for h in heads
1481 nconc
(cl-loop for tl in tails collect
(append h tl
))))))
1483 (cl-defun auth-source-secrets-search (&rest spec
1484 &key backend create delete label max
1486 "Search the Secrets API; spec is like `auth-source'.
1488 The :label key specifies the item's label. It is the only key
1489 that can specify a substring. Any :label value besides a string
1490 will allow any label.
1492 All other search keys must match exactly. If you need substring
1493 matching, do a wider search and narrow it down yourself.
1495 You'll get back all the properties of the token as a plist.
1497 Here's an example that looks for the first item in the `Login'
1500 (let ((auth-sources \\='(\"secrets:Login\")))
1501 (auth-source-search :max 1)
1503 Here's another that looks for the first item in the `Login'
1504 Secrets collection whose label contains `gnus':
1506 (let ((auth-sources \\='(\"secrets:Login\")))
1507 (auth-source-search :max 1 :label \"gnus\")
1509 And this one looks for the first item in the `Login' Secrets
1510 collection that's a Google Chrome entry for the git.gnus.org site
1511 authentication tokens:
1513 (let ((auth-sources \\='(\"secrets:Login\")))
1514 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1518 (cl-assert (not create
) nil
1519 "The Secrets API auth-source backend doesn't support creation yet")
1521 ;; (secrets-delete-item coll elt)
1522 (cl-assert (not delete
) nil
1523 "The Secrets API auth-source backend doesn't support deletion yet")
1525 (let* ((coll (oref backend source
))
1526 (max (or max
5000)) ; sanity check: default to stop at 5K
1527 (ignored-keys '(:create
:delete
:max
:backend
:label
:require
:type
))
1528 (search-keys (cl-loop for i below
(length spec
) by
2
1529 unless
(memq (nth i spec
) ignored-keys
)
1530 collect
(nth i spec
)))
1531 ;; build a search spec without the ignored keys
1532 ;; if a search key is nil or t (match anything), we skip it
1533 (search-specs (auth-source-secrets-listify-pattern
1534 (apply #'append
(mapcar
1536 (if (or (null (plist-get spec k
))
1537 (eq t
(plist-get spec k
)))
1539 (list k
(plist-get spec k
))))
1541 ;; needed keys (always including host, login, port, and secret)
1542 (returned-keys (delete-dups (append
1543 '(:host
:login
:port
:secret
)
1547 for search-spec in search-specs
1549 (cl-loop for item in
(apply #'secrets-search-items coll search-spec
)
1550 unless
(and (stringp label
)
1551 (not (string-match label item
)))
1553 ;; TODO: respect max in `secrets-search-items', not after the fact
1554 (items (butlast items
(- (length items
) max
)))
1555 ;; convert the item name to a full plist
1556 (items (mapcar (lambda (item)
1558 ;; make an entry for the secret (password) element
1561 (let ((v (secrets-get-secret coll item
)))
1563 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1565 (mapcar (lambda (entry)
1566 (list (car entry
) (cdr entry
)))
1567 (secrets-get-attributes coll item
)))))
1569 ;; ensure each item has each key in `returned-keys'
1570 (items (mapcar (lambda (plist)
1573 (mapcar (lambda (req)
1574 (if (plist-get plist req
)
1582 (defun auth-source-secrets-create (&rest spec
)
1584 ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1587 ;;; Backend specific parsing: Mac OS Keychain (using /usr/bin/security) backend
1589 (cl-defun auth-source-macos-keychain-search (&rest spec
1590 &key backend create delete type max
1592 "Search the macOS Keychain; spec is like `auth-source'.
1594 All search keys must match exactly. If you need substring
1595 matching, do a wider search and narrow it down yourself.
1597 You'll get back all the properties of the token as a plist.
1599 The :type key is either `macos-keychain-internet' or
1600 `macos-keychain-generic'.
1602 For the internet keychain type, the :label key searches the
1603 item's labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1604 Similarly, :host maps to \"-s HOST\", :user maps to \"-a USER\",
1605 and :port maps to \"-P PORT\" or \"-r PROT\"
1606 \(note PROT has to be a 4-character string).
1608 For the generic keychain type, the :label key searches the item's
1609 labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1610 Similarly, :host maps to \"-c HOST\" (the \"creator\" keychain
1611 field), :user maps to \"-a USER\", and :port maps to \"-s PORT\".
1613 Here's an example that looks for the first item in the default
1614 generic macOS Keychain:
1616 (let ((auth-sources \\='(macos-keychain-generic)))
1617 (auth-source-search :max 1)
1619 Here's another that looks for the first item in the internet
1620 macOS Keychain collection whose label is `gnus':
1622 (let ((auth-sources \\='(macos-keychain-internet)))
1623 (auth-source-search :max 1 :label \"gnus\")
1625 And this one looks for the first item in the internet keychain
1626 entries for git.gnus.org:
1628 (let ((auth-sources \\='(macos-keychain-internet\")))
1629 (auth-source-search :max 1 :host \"git.gnus.org\"))
1632 (cl-assert (not create
) nil
1633 "The macOS Keychain auth-source backend doesn't support creation yet")
1635 ;; (macos-keychain-delete-item coll elt)
1636 (cl-assert (not delete
) nil
1637 "The macOS Keychain auth-source backend doesn't support deletion yet")
1639 (let* ((coll (oref backend source
))
1640 (max (or max
5000)) ; sanity check: default to stop at 5K
1641 ;; Filter out ignored keys from the spec
1642 (ignored-keys '(:create
:delete
:max
:backend
:label
:host
:port
))
1643 ;; Build a search spec without the ignored keys
1644 ;; FIXME make this loop a function? it's used in at least 3 places
1645 (search-keys (cl-loop for i below
(length spec
) by
2
1646 unless
(memq (nth i spec
) ignored-keys
)
1647 collect
(nth i spec
)))
1648 ;; If a search key value is nil or t (match anything), we skip it
1649 (search-spec (apply #'append
(mapcar
1651 (if (or (null (plist-get spec k
))
1652 (eq t
(plist-get spec k
)))
1654 (list k
(plist-get spec k
))))
1656 ;; needed keys (always including host, login, port, and secret)
1657 (returned-keys (delete-dups (append
1658 '(:host
:login
:port
:secret
)
1660 ;; Extract host and port from spec
1661 (hosts (plist-get spec
:host
))
1662 (hosts (if (and hosts
(listp hosts
)) hosts
`(,hosts
)))
1663 (ports (plist-get spec
:port
))
1664 (ports (if (and ports
(listp ports
)) ports
`(,ports
)))
1665 ;; Loop through all combinations of host/port and pass each of these to
1666 ;; auth-source-macos-keychain-search-items
1667 (items (catch 'match
1668 (dolist (host hosts
)
1669 (dolist (port ports
)
1670 (let* ((port (if port
(format "%S" port
)))
1671 (items (apply #'auth-source-macos-keychain-search-items
1678 (throw 'match items
)))))))
1680 ;; ensure each item has each key in `returned-keys'
1681 (items (mapcar (lambda (plist)
1684 (mapcar (lambda (req)
1685 (if (plist-get plist req
)
1694 (defun auth-source--decode-octal-string (string)
1695 "Convert octal string to utf-8 string. E.g: 'a\134b' to 'a\b'"
1696 (let ((list (string-to-list string
))
1697 (size (length string
)))
1698 (decode-coding-string
1699 (apply #'unibyte-string
1700 (cl-loop for i
= 0 then
(+ i
(if (eq (nth i list
) ?
\\) 4 1))
1701 for var
= (nth i list
)
1704 collect
(string-to-number
1705 (concat (cl-subseq list
(+ i
1) (+ i
4))) 8)
1710 (cl-defun auth-source-macos-keychain-search-items (coll _type _max host port
1711 &key label type user
1713 (let* ((keychain-generic (eq type
'macos-keychain-generic
))
1714 (args `(,(if keychain-generic
1715 "find-generic-password"
1716 "find-internet-password")
1718 (ret (list :type type
)))
1720 (setq args
(append args
(list "-l" label
))))
1722 (setq args
(append args
(list (if keychain-generic
"-c" "-s") host
))))
1724 (setq args
(append args
(list "-a" user
))))
1727 (if keychain-generic
1728 (setq args
(append args
(list "-s" port
)))
1729 (setq args
(append args
(list
1730 (if (string-match "[0-9]+" port
) "-P" "-r")
1733 (unless (equal coll
"default")
1734 (setq args
(append args
(list coll
))))
1737 (apply #'call-process
"/usr/bin/security" nil t nil args
)
1738 (goto-char (point-min))
1741 ((looking-at "^password: \\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1742 (setq ret
(auth-source-macos-keychain-result-append
1746 (let ((v (auth-source--decode-octal-string
1749 ;; TODO: check if this is really the label
1750 ;; match 0x00000007 <blob>="AppleID"
1752 "^[ ]+0x00000007 <blob>=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1753 (setq ret
(auth-source-macos-keychain-result-append
1757 (auth-source--decode-octal-string (match-string 1)))))
1758 ;; match "crtr"<uint32>="aapl"
1759 ;; match "svce"<blob>="AppleID"
1761 "^[ ]+\"\\([a-z]+\\)\"[^=]+=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1762 (setq ret
(auth-source-macos-keychain-result-append
1765 (auth-source--decode-octal-string (match-string 1))
1766 (auth-source--decode-octal-string (match-string 2))))))
1768 ;; return `ret' iff it has the :secret key
1769 (and (plist-get ret
:secret
) (list ret
))))
1771 (defun auth-source-macos-keychain-result-append (result generic k v
)
1773 (push (auth-source--symbol-keyword
1775 ((equal k
"acct") "user")
1776 ;; for generic keychains, creator is host, service is port
1777 ((and generic
(equal k
"crtr")) "host")
1778 ((and generic
(equal k
"svce")) "port")
1779 ;; for internet keychains, protocol is port, server is host
1780 ((and (not generic
) (equal k
"ptcl")) "port")
1781 ((and (not generic
) (equal k
"srvr")) "host")
1785 (defun auth-source-macos-keychain-create (&rest spec
)
1789 ;;; Backend specific parsing: PLSTORE backend
1791 (cl-defun auth-source-plstore-search (&rest spec
1792 &key backend create delete max
1794 "Search the PLSTORE; spec is like `auth-source'."
1795 (let* ((store (oref backend data
))
1796 (max (or max
5000)) ; sanity check: default to stop at 5K
1797 (ignored-keys '(:create
:delete
:max
:backend
:label
:require
:type
))
1798 (search-keys (cl-loop for i below
(length spec
) by
2
1799 unless
(memq (nth i spec
) ignored-keys
)
1800 collect
(nth i spec
)))
1801 ;; build a search spec without the ignored keys
1802 ;; if a search key is nil or t (match anything), we skip it
1803 (search-spec (apply #'append
(mapcar
1805 (let ((v (plist-get spec k
)))
1813 ;; needed keys (always including host, login, port, and secret)
1814 (returned-keys (delete-dups (append
1815 '(:host
:login
:port
:secret
)
1817 (items (plstore-find store search-spec
))
1818 (item-names (mapcar #'car items
))
1819 (items (butlast items
(- (length items
) max
)))
1820 ;; convert the item to a full plist
1821 (items (mapcar (lambda (item)
1822 (let* ((plist (copy-tree (cdr item
)))
1823 (secret (plist-member plist
:secret
)))
1827 (let ((v (car (cdr secret
))))
1831 ;; ensure each item has each key in `returned-keys'
1832 (items (mapcar (lambda (plist)
1835 (mapcar (lambda (req)
1836 (if (plist-get plist req
)
1843 ;; if we need to create an entry AND none were found to match
1847 ;; create based on the spec and record the value
1849 ;; if the user did not want to create the entry
1850 ;; in the file, it will be returned
1851 (apply (slot-value backend
'create-function
) spec
)
1852 ;; if not, we do the search again without :create
1853 ;; to get the updated data.
1855 ;; the result will be returned, even if the search fails
1856 (apply #'auth-source-plstore-search
1857 (plist-put spec
:create nil
)))))
1860 (dolist (item-name item-names
)
1861 (plstore-delete store item-name
))
1862 (plstore-save store
)))
1865 (cl-defun auth-source-plstore-create (&rest spec
1866 &key backend host port create
1868 (let* ((base-required '(host user port secret
))
1869 (base-secret '(secret))
1870 ;; we know (because of an assertion in auth-source-search) that the
1871 ;; :create parameter is either t or a list (which includes nil)
1872 (create-extra (if (eq t create
) nil create
))
1873 (current-data (car (auth-source-search :max
1
1876 (required (append base-required create-extra
))
1877 ;; `valist' is an alist
1879 ;; `artificial' will be returned if no creation is needed
1883 ;; only for base required elements (defined as function parameters):
1884 ;; fill in the valist with whatever data we may have from the search
1885 ;; we complete the first value if it's a list and use the value otherwise
1886 (dolist (br base-required
)
1887 (let ((val (plist-get spec
(auth-source--symbol-keyword br
))))
1889 (let ((br-choice (cond
1890 ;; all-accepting choice (predicate is t)
1892 ;; just the value otherwise
1895 (auth-source--aput valist br br-choice
))))))
1897 ;; for extra required elements, see if the spec includes a value for them
1898 (dolist (er create-extra
)
1899 (let ((k (auth-source--symbol-keyword er
))
1900 (keys (cl-loop for i below
(length spec
) by
2
1901 collect
(nth i spec
))))
1903 (auth-source--aput valist er
(plist-get spec k
)))))
1905 ;; for each required element
1906 (dolist (r required
)
1907 (let* ((data (auth-source--aget valist r
))
1908 ;; take the first element if the data is a list
1909 (data (or (auth-source-netrc-element-or-first data
)
1910 (plist-get current-data
1911 (auth-source--symbol-keyword r
))))
1912 ;; this is the default to be offered
1913 (given-default (auth-source--aget
1914 auth-source-creation-defaults r
))
1915 ;; the default supplementals are simple:
1916 ;; for the user, try `given-default' and then (user-login-name);
1917 ;; otherwise take `given-default'
1919 ((and (not given-default
) (eq r
'user
))
1922 (printable-defaults (list
1925 (auth-source-netrc-element-or-first
1926 (auth-source--aget valist
'user
))
1927 (plist-get artificial
:user
)
1931 (auth-source-netrc-element-or-first
1932 (auth-source--aget valist
'host
))
1933 (plist-get artificial
:host
)
1937 (auth-source-netrc-element-or-first
1938 (auth-source--aget valist
'port
))
1939 (plist-get artificial
:port
)
1941 (prompt (or (auth-source--aget auth-source-creation-prompts r
)
1943 (secret "%p password for %u@%h: ")
1944 (user "%p user name for %h: ")
1945 (host "%p host name for user %u: ")
1946 (port "%p port for %u@%h: "))
1947 (format "Enter %s (%%u@%%h:%%p): " r
)))
1948 (prompt (auth-source-format-prompt
1950 `((?u
,(auth-source--aget printable-defaults
'user
))
1951 (?h
,(auth-source--aget printable-defaults
'host
))
1952 (?p
,(auth-source--aget printable-defaults
'port
))))))
1954 ;; Store the data, prompting for the password if needed.
1957 (or (eval default
) (read-passwd prompt
))
1958 (if (stringp default
)
1960 (if (string-match ": *\\'" prompt
)
1961 (concat (substring prompt
0 (match-beginning 0))
1962 " (default " default
"): ")
1963 (concat prompt
"(default " default
") "))
1968 (if (member r base-secret
)
1969 (setq secret-artificial
1970 (plist-put secret-artificial
1971 (auth-source--symbol-keyword r
)
1973 (setq artificial
(plist-put artificial
1974 (auth-source--symbol-keyword r
)
1976 (plstore-put (oref backend data
)
1977 (sha1 (format "%s@%s:%s"
1978 (plist-get artificial
:user
)
1979 (plist-get artificial
:host
)
1980 (plist-get artificial
:port
)))
1981 artificial secret-artificial
)
1982 (if (y-or-n-p (format "Save auth info to file %s? "
1983 (plstore-get-file (oref backend data
))))
1984 (plstore-save (oref backend data
)))))
1986 ;;; Backend specific parsing: JSON backend
1987 ;;; (auth-source-search :max 1 :machine "imap.gmail.com")
1988 ;;; (auth-source-search :max 1 :host '("my-gmail" "imap.gmail.com") :port '(993 "imaps" "imap" "993" "143") :user nil :require '(:user :secret))
1990 (defun auth-source-json-check (host user port require item
)
1992 (auth-source-search-collection
1995 (plist-get item
:machine
)
1996 (plist-get item
:host
)
1998 (auth-source-search-collection
2001 (plist-get item
:login
)
2002 (plist-get item
:account
)
2003 (plist-get item
:user
)
2005 (auth-source-search-collection
2008 (plist-get item
:port
)
2009 (plist-get item
:protocol
)
2012 ;; the required list of keys is nil, or
2014 ;; every element of require is in
2015 (cl-loop for req in require
2016 always
(plist-get item req
)))))
2018 (cl-defun auth-source-json-search (&rest spec
2019 &key backend require
2020 type max host user port
2022 "Given a property list SPEC, return search matches from the :backend.
2023 See `auth-source-search' for details on SPEC."
2024 ;; just in case, check that the type is correct (null or same as the backend)
2025 (cl-assert (or (null type
) (eq type
(oref backend type
)))
2026 t
"Invalid JSON search: %s %s")
2028 ;; Hide the secrets early to avoid accidental exposure.
2030 (mapcar (lambda (entry)
2033 (let* ((item (pop entry
))
2034 (k (auth-source--symbol-keyword (car item
)))
2036 (setq k
(cond ((memq k
'(:machine
)) :host
)
2037 ((memq k
'(:login
:account
)) :user
)
2038 ((memq k
'(:protocol
)) :port
)
2039 ((memq k
'(:password
)) :secret
)
2041 ;; send back the secret in a function (lexical binding)
2042 (when (eq k
:secret
)
2043 (setq v
(let ((lexv v
))
2045 (setq ret
(plist-put ret k v
))))
2047 (json-read-file (oref backend source
))))
2048 (max (or max
5000)) ; sanity check: default to stop at 5K
2050 (dolist (item jdata
)
2052 (> max
(length all
))
2053 (auth-source-json-check host user port require item
))
2059 ;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
2061 ;; deprecate the old interface
2062 (make-obsolete 'auth-source-user-or-password
2063 'auth-source-search
"Emacs 24.1")
2064 (make-obsolete 'auth-source-forget-user-or-password
2065 'auth-source-forget
"Emacs 24.1")
2067 (defun auth-source-user-or-password
2068 (mode host port
&optional username create-missing delete-existing
)
2069 "Find MODE (string or list of strings) matching HOST and PORT.
2071 DEPRECATED in favor of `auth-source-search'!
2073 USERNAME is optional and will be used as \"login\" in a search
2074 across the Secret Service API (see secrets.el) if the resulting
2075 items don't have a username. This means that if you search for
2076 username \"joe\" and it matches an item but the item doesn't have
2077 a :user attribute, the username \"joe\" will be returned.
2079 A non nil DELETE-EXISTING means deleting any matching password
2080 entry in the respective sources. This is useful only when
2081 CREATE-MISSING is non nil as well; the intended use case is to
2082 remove wrong password entries.
2084 If no matching entry is found, and CREATE-MISSING is non nil,
2085 the password will be retrieved interactively, and it will be
2086 stored in the password database which matches best (see
2089 MODE can be \"login\" or \"password\"."
2090 (auth-source-do-debug
2091 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
2092 mode host port username
)
2094 (let* ((listy (listp mode
))
2095 (mode (if listy mode
(list mode
)))
2096 ;; (cname (if username
2097 ;; (format "%s %s:%s %s" mode host port username)
2098 ;; (format "%s %s:%s" mode host port)))
2099 (search (list :host host
:port port
))
2100 (search (if username
(append search
(list :user username
)) search
))
2101 (search (if create-missing
2102 (append search
(list :create t
))
2104 (search (if delete-existing
2105 (append search
(list :delete t
))
2107 ;; (found (if (not delete-existing)
2108 ;; (gethash cname auth-source-cache)
2109 ;; (remhash cname auth-source-cache)
2114 (auth-source-do-debug
2115 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
2117 ;; don't show the password
2118 (if (and (member "password" mode
) t
)
2122 found
) ; return the found data
2123 ;; else, if not found, search with a max of 1
2124 (let ((choice (nth 0 (apply #'auth-source-search
2125 (append '(:max
1) search
)))))
2129 ((equal "password" m
)
2130 (push (if (plist-get choice
:secret
)
2131 (funcall (plist-get choice
:secret
))
2134 (push (plist-get choice
:user
) found
)))))
2135 (setq found
(nreverse found
))
2136 (setq found
(if listy found
(car-safe found
)))))
2140 (defun auth-source-user-and-password (host &optional user
)
2141 (let* ((auth-info (car
2147 :require
'(:user
:secret
)
2152 :require
'(:user
:secret
)
2154 (user (plist-get auth-info
:user
))
2155 (password (plist-get auth-info
:secret
)))
2156 (when (functionp password
)
2157 (setq password
(funcall password
)))
2158 (list user password auth-info
)))
2160 (provide 'auth-source
)
2162 ;;; auth-source.el ends here