Print test timings unconditionally
[emacs.git] / lisp / auth-source.el
blob66e1897b8777e7c4b2fd262ae47659f6b172ef77
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>
6 ;; Keywords: news
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/>.
23 ;;; Commentary:
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.
32 ;; TODO:
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
40 ;;; Code:
42 (require 'json)
43 (require 'password-cache)
45 (eval-when-compile (require 'cl-lib))
46 (require 'eieio)
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
78 :group 'gnus)
80 ;;;###autoload
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
84 let-binding."
85 :version "24.1"
86 :group 'auth-source
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 ()
97 ((type :initarg :type
98 :initform 'netrc
99 :type symbol
100 :custom symbol
101 :documentation "The backend type.")
102 (source :initarg :source
103 :type string
104 :custom string
105 :documentation "The backend source.")
106 (host :initarg :host
107 :initform t
108 :type t
109 :custom string
110 :documentation "The backend host.")
111 (user :initarg :user
112 :initform t
113 :type t
114 :custom string
115 :documentation "The backend user.")
116 (port :initarg :port
117 :initform t
118 :type t
119 :custom string
120 :documentation "The backend protocol.")
121 (data :initarg :data
122 :initform nil
123 :documentation "Internal backend data.")
124 (create-function :initarg :create-function
125 :initform ignore
126 :type function
127 :custom function
128 :documentation "The create function.")
129 (search-function :initarg :search-function
130 :initform ignore
131 :type function
132 :custom function
133 :documentation "The search function.")))
135 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
136 (pop3 "pop3" "pop" "pop3s" "110" "995")
137 (ssh "ssh" "22")
138 (sftp "sftp" "115")
139 (smtp "smtp" "25"))
140 "List of authentication protocols and their names"
142 :group 'auth-source
143 :version "23.2" ;; No Gnus
144 :type '(repeat :tag "Authentication Protocols"
145 (cons :tag "Protocol Entry"
146 (symbol :tag "Protocol")
147 (repeat :tag "Names"
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
153 (mapcar (lambda (a)
154 (let ((p (car-safe a)))
155 (list 'const
156 :tag (upcase (symbol-name p))
157 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."
171 :group 'auth-source
172 :version "23.2" ;; No Gnus
173 :type `(choice
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."
186 :group 'auth-source
187 :version "23.2" ;; No Gnus
188 :type `(choice
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"
192 (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)
198 "\\.gpg\\'"))
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'."
206 :group 'auth-source
207 :version "23.2" ;; No Gnus
208 :type `boolean)
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
217 for passwords).
219 If the value is a function, debug messages are logged by calling
220 that function using the same arguments as `message'."
221 :group 'auth-source
222 :version "23.2" ;; No Gnus
223 :type `(choice
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"
227 trivia)
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'
240 for details.
242 It's best to customize this with `\\[customize-variable]' because the choices
243 can get pretty complex."
244 :group 'auth-source
245 :version "24.1" ;; No Gnus
246 :type `(repeat :tag "Authentication Sources"
247 (choice
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)")
263 (list
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")
270 (const
271 :tag "Temporary" "session")))
272 (list
273 :tag "Mac OS internet Keychain"
274 (const :format ""
275 :value :macos-keychain-internet)
276 (choice :tag "Collection to use"
277 (string :tag "internet Keychain path")
278 (const :tag "default" default)))
279 (list
280 :tag "Mac OS generic Keychain"
281 (const :format ""
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"
288 (list
289 :tag "Host"
290 (const :format "" :value :host)
291 (choice :tag "Host (machine) choice"
292 (const :tag "Any" t)
293 (regexp
294 :tag "Regular expression")))
295 (list
296 :tag "Protocol"
297 (const :format "" :value :port)
298 (choice
299 :tag "Protocol"
300 (const :tag "Any" t)
301 ,@auth-source-protocols-customize))
302 (list :tag "User" :inline t
303 (const :format "" :value :user)
304 (choice
305 :tag "Personality/Username"
306 (const :tag "Any" t)
307 (string
308 :tag "Name")))))))))
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."
313 :group 'auth-source
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)
329 (apply
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)
333 auth-source-debug
334 'message)
335 msg))
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)."
342 (when choices
343 (let* ((prompt-choices
344 (apply #'concat
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)))
352 k)))
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'."
365 (let (backend)
366 (cl-dolist (f auth-source-backend-parser-functions)
367 (when (setq backend (funcall f entry))
368 (cl-return)))
370 (unless backend
371 ;; none of the parsers worked
372 (auth-source-do-warn
373 "auth-source-backend-parse: invalid backend spec: %S" entry)
374 (setq backend (make-instance 'auth-source-backend
375 :source ""
376 :type 'ignore)))
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))
385 (source-without-gpg
386 (if (and (stringp source)
387 (equal (file-name-extension source) "gpg"))
388 (file-name-sans-extension source)
389 (or source "")))
390 (extension (or (and (stringp source-without-gpg)
391 (file-name-extension source-without-gpg))
392 "")))
393 (when (stringp source)
394 (cond
395 ((equal extension "plist")
396 (auth-source-backend
397 source
398 :source source
399 :type 'plstore
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"))
404 (auth-source-backend
405 source
406 :source source
407 :type 'json
408 :search-function #'auth-source-json-search))
410 (auth-source-backend
411 source
412 :source source
413 :type 'netrc
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:\\(.+\\)"
424 entry))
425 (setq entry `(:source (:macos-keychain-internet
426 ,(match-string 1 entry)))))
427 (when (and (stringp entry) (string-match "^macos-keychain-generic:\\(.+\\)"
428 entry))
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))))
437 (cond
438 ;; the macOS Keychain
439 ((and
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)))
458 (auth-source-backend
459 (format "Mac OS Keychain (%s)" source)
460 :source source
461 :type keychain-type
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))))
476 (cond
477 ;; the Secrets API. We require the package, in order to have a
478 ;; defined value for `secrets-enabled'.
479 ((and
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))
496 "Login")))
498 (if (featurep 'secrets)
499 (auth-source-backend
500 (format "Secrets API (%s)" source)
501 :source source
502 :type 'secrets
503 :search-function #'auth-source-secrets-search
504 :create-function #'auth-source-secrets-create)
505 (auth-source-do-warn
506 "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry)
507 (auth-source-backend
508 (format "Ignored Secrets API (%s)" source)
509 :source ""
510 :type 'ignore))))))
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
517 parameters."
518 (let ((entry (if (stringp entry)
520 entry))
521 val)
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)))
528 backend)
530 ;; (mapcar 'auth-source-backend-parse auth-sources)
532 (cl-defun auth-source-search (&rest spec
533 &key max require create delete
534 &allow-other-keys)
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
559 port keys.
561 Here's an example:
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\"
567 :create t))
569 which says:
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
579 port.\"
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.
594 Here's an example:
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)))
604 which says:
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
625 an error.
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
630 property.
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
641 the backend.
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.
653 Defaults to t.
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))
672 (max (or max 1))
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))
681 filtered-backends)
683 (if (and cached auth-source-do-cache)
684 (auth-source-do-debug
685 "auth-source-search: found %d CACHED results matching %S"
686 (length found) spec)
688 (cl-assert
689 (or (eq t create) (listp create)) t
690 "Invalid auth-source :create parameter (must be t or a list): %s %s")
692 (cl-assert
693 (listp require) t
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
700 (condition-case nil
701 (unless (auth-source-search-collection
702 (plist-get spec key)
703 (slot-value backend key))
704 (setq filtered-backends (delq backend filtered-backends))
705 (cl-return))
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
714 ;; query them all.
715 (setq found (auth-source-search-backends filtered-backends
716 spec
717 ;; to exit early
719 ;; create is always nil here
720 nil delete
721 require))
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.
729 (when (and create
730 (not found))
731 (setq found (auth-source-search-backends filtered-backends
732 spec
733 ;; to exit early
735 create delete
736 require))
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)))
745 (if (zerop max)
746 (not (null found))
747 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
751 matches)
752 (dolist (backend backends)
753 (when (> max (length matches)) ; if we need more matches...
754 (let* ((bmatches (apply
755 (slot-value backend 'search-function)
756 :backend backend
757 :type (slot-value backend 'type)
758 ;; note we're overriding whatever the spec
759 ;; has for :max, :require, :create, and :delete
760 :max max
761 :require require
762 :create create
763 :delete delete
764 spec)))
765 (when bmatches
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)
771 spec)
772 (setq matches (append matches bmatches))))))
773 matches))
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)
790 (eq value 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."
798 (interactive)
799 (maphash (lambda (key _password)
800 (when (eq 'auth-source (car-safe key))
801 ;; remove that key
802 (password-cache-remove key)))
803 password-data)
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))
813 (password-cache-add
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."
822 (password-in-cache-p
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."
839 (let ((count 0))
840 (maphash
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)))
845 ;; remove that key
846 (password-cache-remove key)
847 (cl-incf count)))
848 password-data)
849 count))
851 (defun auth-source-specmatchp (spec stored)
852 (let ((keys (cl-loop for i below (length spec) by 2
853 collect (nth i spec))))
854 (not (eq
855 (cl-dolist (key keys)
856 (unless (auth-source-search-collection (plist-get stored key)
857 (plist-get spec key))
858 (cl-return 'no)))
859 'no))))
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)
867 (funcall secret)
868 secret)))
870 (defun auth-source-format-prompt (prompt alist)
871 "Format PROMPT using %x (for any character x) specifiers in ALIST."
872 (dolist (cell alist)
873 (let ((c (nth 0 cell))
874 (v (nth 1 cell)))
875 (when (and c v)
876 (setq prompt (replace-regexp-in-string (format "%%%c" c)
877 (format "%s" v)
878 prompt nil t)))))
879 prompt)
881 (defun auth-source-ensure-strings (values)
882 (if (eq values t)
883 values
884 (unless (listp values)
885 (setq values (list values)))
886 (mapcar (lambda (value)
887 (if (numberp value)
888 (format "%s" value)
889 value))
890 values)))
892 ;;; Backend specific parsing: netrc/authinfo backend
894 (defun auth-source--aput-1 (alist key val)
895 (let ((seen ())
896 (rest alist))
897 (while (and (consp rest) (not (equal key (caar rest))))
898 (push (pop rest) seen))
899 (cons (cons key val)
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
911 &allow-other-keys)
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."
914 (if (listp file)
915 ;; We got already parsed contents; just return it.
916 file
917 (when (file-exists-p file)
918 (setq port (auth-source-ensure-strings port))
919 (with-temp-buffer
920 (let* ((max (or max 5000)) ; sanity check: default to stop at 5K
921 (modified 0)
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)
926 (and alist
927 (auth-source-search-collection
928 host
930 (auth-source--aget alist "machine")
931 (auth-source--aget alist "host")
933 (auth-source-search-collection
934 user
936 (auth-source--aget alist "login")
937 (auth-source--aget alist "account")
938 (auth-source--aget alist "user")
940 (auth-source-search-collection
941 port
943 (auth-source--aget alist "port")
944 (auth-source--aget alist "protocol")
947 ;; the required list of keys is nil, or
948 (null require)
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)))))))
954 result)
956 (if (and (functionp cached-secrets)
957 (equal cached-mtime
958 (nth 5 (file-attributes file))))
959 (progn
960 (auth-source-do-trivia
961 "auth-source-netrc-parse: using CACHED file data for %s"
962 file)
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)
968 (auth-source--aput
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))
975 alist)
976 (while (setq alist (pop entries))
977 (push (nreverse alist) result)))
979 (when (< 0 modified)
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)"
992 file modified))
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"
996 modified file)))
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 "#")
1004 (and (eolp)
1005 (not (eobp))))
1006 (forward-line 1))
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)
1022 (save-excursion
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)
1030 (when (and
1031 alist
1032 (> max (length all))
1033 (funcall check alist))
1034 (push alist all))
1035 all))
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.
1040 (when (and alist
1041 (or default
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)
1046 alist nil))
1047 ;; In default entries, we don't have a next token.
1048 ;; We store them as ("machine" . t)
1049 (if default
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")
1055 (error
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.
1062 (when alist
1063 (setq all (funcall adder check alist all))
1064 ;; (auth-source-do-trivia
1065 ;; "auth-source-netrc-parse-entries: got2 entry %S" alist)
1067 (nreverse all)))
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))
1074 passphrase)
1075 ;; return the saved passphrase, calling a function if needed
1076 (or (copy-sequence (if (functionp (cdr entry))
1077 (funcall (cdr entry))
1078 (cdr entry)))
1079 (progn
1080 (unless entry
1081 (setq entry (list file))
1082 (push entry auth-source-passphrase-alist))
1083 (setq passphrase
1084 (read-passwd
1085 (format "Passphrase for %s tokens: " file)
1087 (setcdr entry (let ((p (copy-sequence passphrase)))
1088 (lambda () p)))
1089 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
1098 context
1099 (cons #'auth-source-token-passphrase-callback-function
1100 file))
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)
1108 cipher)
1109 (setf (epg-context-armor context) t)
1110 (epg-context-set-passphrase-callback
1111 context
1112 (cons #'auth-source-token-passphrase-callback-function
1113 file))
1114 (setq cipher (epg-encrypt-string context secret nil))
1115 (with-temp-buffer
1116 (insert cipher)
1117 (base64-encode-region (point-min) (point-max) t)
1118 (concat "gpg:" (buffer-substring-no-properties
1119 (point-min)
1120 (point-max))))))
1122 (defun auth-source--symbol-keyword (symbol)
1123 (intern (format ":%s" symbol)))
1125 (defun auth-source-netrc-normalize (alist filename)
1126 (mapcar (lambda (entry)
1127 (let (ret item)
1128 (while (setq item (pop entry))
1129 (let ((k (car item))
1130 (v (cdr 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")
1137 (t k)))
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
1146 (setq token-decoder
1147 (lambda (val)
1148 (prog1
1149 (auth-source-epa-extract-gpg-token
1151 filename)
1152 (setq token-decoder nil)))))
1153 (lambda ()
1154 (when token-decoder
1155 (setq lexv (funcall token-decoder lexv)))
1156 lexv))))
1157 (setq ret (plist-put ret
1158 (auth-source--symbol-keyword k)
1159 v))))
1160 ret))
1161 alist))
1163 (cl-defun auth-source-netrc-search (&rest spec
1164 &key backend require create
1165 type max host user port
1166 &allow-other-keys)
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
1175 :max max
1176 :require require
1177 :file (oref backend source)
1178 :host (or host t)
1179 :user (or user t)
1180 :port (or port t))
1181 (oref backend source))))
1183 ;; if we need to create an entry AND none were found to match
1184 (when (and create
1185 (not results))
1187 ;; create based on the spec and record the value
1188 (setq results (or
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)))))
1198 results))
1200 (defun auth-source-netrc-element-or-first (v)
1201 (if (listp v)
1202 (nth 0 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
1210 &allow-other-keys)
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
1216 :host host
1217 :port port)))
1218 (required (append base-required create-extra))
1219 (file (oref backend source))
1220 (add "")
1221 ;; `valist' is an alist
1222 valist
1223 ;; `artificial' will be returned if no creation is needed
1224 artificial)
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))))
1231 (when val
1232 (let ((br-choice (cond
1233 ;; all-accepting choice (predicate is t)
1234 ((eq t val) nil)
1235 ;; just the value otherwise
1236 (t val))))
1237 (when br-choice
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))))
1245 (when (memq k keys)
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'
1261 (default (cond
1262 ((and (not given-default) (eq r 'user))
1263 (user-login-name))
1264 (t given-default)))
1265 (printable-defaults (list
1266 (cons 'user
1268 (auth-source-netrc-element-or-first
1269 (auth-source--aget valist 'user))
1270 (plist-get artificial :user)
1271 "[any user]"))
1272 (cons 'host
1274 (auth-source-netrc-element-or-first
1275 (auth-source--aget valist 'host))
1276 (plist-get artificial :host)
1277 "[any host]"))
1278 (cons 'port
1280 (auth-source-netrc-element-or-first
1281 (auth-source--aget valist 'port))
1282 (plist-get artificial :port)
1283 "[any port]"))))
1284 (prompt (or (auth-source--aget auth-source-creation-prompts r)
1285 (cl-case 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
1292 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.
1298 (setq data (or data
1299 (if (eq r 'secret)
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))
1304 (gpg-encrypt
1305 (cond
1306 ((eq auth-source-netrc-use-gpg-tokens 'never)
1307 'never)
1308 ((listp auth-source-netrc-use-gpg-tokens)
1309 (let ((check (copy-sequence
1310 auth-source-netrc-use-gpg-tokens))
1311 item ret)
1312 (while check
1313 (setq item (pop check))
1314 (when (or (eq (car item) t)
1315 (string-match (car item) file))
1316 (setq ret (cdr item))
1317 (setq check nil)))
1318 ret))
1319 (t 'never)))
1320 (plain (or (eval default) (read-passwd prompt))))
1321 ;; ask if we don't know what to do (in which case
1322 ;; auth-source-netrc-use-gpg-tokens must be a list)
1323 (unless gpg-encrypt
1324 (setq gpg-encrypt (if (y-or-n-p ep) 'gpg 'never))
1325 ;; TODO: save the defcustom now? or ask?
1326 (setq auth-source-netrc-use-gpg-tokens
1327 (cons `(,file ,gpg-encrypt)
1328 auth-source-netrc-use-gpg-tokens)))
1329 (if (eq gpg-encrypt 'gpg)
1330 (auth-source-epa-make-gpg-token plain file)
1331 plain))
1332 (if (stringp default)
1333 (read-string (if (string-match ": *\\'" prompt)
1334 (concat (substring prompt 0 (match-beginning 0))
1335 " (default " default "): ")
1336 (concat prompt "(default " default ") "))
1337 nil nil default)
1338 (eval default)))))
1340 (when data
1341 (setq artificial (plist-put artificial
1342 (auth-source--symbol-keyword r)
1343 (if (eq r 'secret)
1344 (let ((data data))
1345 (lambda () data))
1346 data))))
1348 ;; When r is not an empty string...
1349 (when (and (stringp data)
1350 (< 0 (length data)))
1351 ;; this function is not strictly necessary but I think it
1352 ;; makes the code clearer -tzz
1353 (let ((printer (lambda ()
1354 ;; append the key (the symbol name of r)
1355 ;; and the value in r
1356 (format "%s%s %s"
1357 ;; prepend a space
1358 (if (zerop (length add)) "" " ")
1359 ;; remap auth-source tokens to netrc
1360 (cl-case r
1361 (user "login")
1362 (host "machine")
1363 (secret "password")
1364 (port "port") ; redundant but clearer
1365 (t (symbol-name r)))
1366 (if (string-match "[\"# ]" data)
1367 (format "%S" data)
1368 data)))))
1369 (setq add (concat add (funcall printer)))))))
1371 (plist-put
1372 artificial
1373 :save-function
1374 (let ((file file)
1375 (add add))
1376 (lambda () (auth-source-netrc-saver file add))))
1378 (list artificial)))
1380 (defun auth-source-netrc-saver (file add)
1381 "Save a line ADD in FILE, prompting along the way.
1382 Respects `auth-source-save-behavior'. Uses
1383 `auth-source-netrc-cache' to avoid prompting more than once."
1384 (let* ((key (format "%s %s" file (rfc2104-hash 'md5 64 16 file add)))
1385 (cached (assoc key auth-source-netrc-cache)))
1387 (if cached
1388 (auth-source-do-trivia
1389 "auth-source-netrc-saver: found previous run for key %s, returning"
1390 key)
1391 (with-temp-buffer
1392 (when (file-exists-p file)
1393 (insert-file-contents file))
1394 (when auth-source-gpg-encrypt-to
1395 ;; (see bug#7487) making `epa-file-encrypt-to' local to
1396 ;; this buffer lets epa-file skip the key selection query
1397 ;; (see the `local-variable-p' check in
1398 ;; `epa-file-write-region').
1399 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
1400 (make-local-variable 'epa-file-encrypt-to))
1401 (if (listp auth-source-gpg-encrypt-to)
1402 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
1403 ;; we want the new data to be found first, so insert at beginning
1404 (goto-char (point-min))
1406 ;; Ask AFTER we've successfully opened the file.
1407 (let ((prompt (format "Save auth info to file %s? " file))
1408 (done (not (eq auth-source-save-behavior 'ask)))
1409 (bufname "*auth-source Help*")
1411 (while (not done)
1412 (setq k (auth-source-read-char-choice prompt '(?y ?n ?N ?e ??)))
1413 (cl-case k
1414 (?y (setq done t))
1415 (?? (save-excursion
1416 (with-output-to-temp-buffer bufname
1417 (princ
1418 (concat "(y)es, save\n"
1419 "(n)o but use the info\n"
1420 "(N)o and don't ask to save again\n"
1421 "(e)dit the line\n"
1422 "(?) for help as you can see.\n"))
1423 ;; Why? Doesn't with-output-to-temp-buffer already do
1424 ;; the exact same thing anyway? --Stef
1425 (set-buffer standard-output)
1426 (help-mode))))
1427 (?n (setq add ""
1428 done t))
1430 (setq add ""
1431 done t)
1432 (customize-save-variable 'auth-source-save-behavior nil))
1433 (?e (setq add (read-string "Line to add: " add)))
1434 (t nil)))
1436 (when (get-buffer-window bufname)
1437 (delete-window (get-buffer-window bufname)))
1439 ;; Make sure the info is not saved.
1440 (when (null auth-source-save-behavior)
1441 (setq add ""))
1443 (when (< 0 (length add))
1444 (progn
1445 (unless (bolp)
1446 (insert "\n"))
1447 (insert add "\n")
1448 (write-region (point-min) (point-max) file nil 'silent)
1449 ;; Make the .authinfo file non-world-readable.
1450 (set-file-modes file #o600)
1451 (auth-source-do-debug
1452 "auth-source-netrc-create: wrote 1 new line to %s"
1453 file)
1454 (message "Saved new authentication information to %s" file)
1455 nil))))
1456 (auth-source--aput auth-source-netrc-cache key "ran"))))
1458 ;;; Backend specific parsing: Secrets API backend
1460 (defun auth-source-secrets-listify-pattern (pattern)
1461 "Convert a pattern with lists to a list of string patterns.
1463 auth-source patterns can have values of the form :foo (\"bar\"
1464 \"qux\"), which means to match any secret with :foo equal to
1465 \"bar\" or :foo equal to \"qux\". The secrets backend supports
1466 only string values for patterns, so this routine returns a list
1467 of patterns that is equivalent to the single original pattern
1468 when interpreted such that if a secret matches any pattern in the
1469 list, it matches the original pattern."
1470 (if (null pattern)
1471 '(nil)
1472 (let* ((key (pop pattern))
1473 (value (pop pattern))
1474 (tails (auth-source-secrets-listify-pattern pattern))
1475 (heads (if (stringp value)
1476 (list (list key value))
1477 (mapcar (lambda (v) (list key v)) value))))
1478 (cl-loop for h in heads
1479 nconc (cl-loop for tl in tails collect (append h tl))))))
1481 (cl-defun auth-source-secrets-search (&rest spec
1482 &key backend create delete label max
1483 &allow-other-keys)
1484 "Search the Secrets API; spec is like `auth-source'.
1486 The :label key specifies the item's label. It is the only key
1487 that can specify a substring. Any :label value besides a string
1488 will allow any label.
1490 All other search keys must match exactly. If you need substring
1491 matching, do a wider search and narrow it down yourself.
1493 You'll get back all the properties of the token as a plist.
1495 Here's an example that looks for the first item in the `Login'
1496 Secrets collection:
1498 (let ((auth-sources \\='(\"secrets:Login\")))
1499 (auth-source-search :max 1)
1501 Here's another that looks for the first item in the `Login'
1502 Secrets collection whose label contains `gnus':
1504 (let ((auth-sources \\='(\"secrets:Login\")))
1505 (auth-source-search :max 1 :label \"gnus\")
1507 And this one looks for the first item in the `Login' Secrets
1508 collection that's a Google Chrome entry for the git.gnus.org site
1509 authentication tokens:
1511 (let ((auth-sources \\='(\"secrets:Login\")))
1512 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1515 ;; TODO
1516 (cl-assert (not create) nil
1517 "The Secrets API auth-source backend doesn't support creation yet")
1518 ;; TODO
1519 ;; (secrets-delete-item coll elt)
1520 (cl-assert (not delete) nil
1521 "The Secrets API auth-source backend doesn't support deletion yet")
1523 (let* ((coll (oref backend source))
1524 (max (or max 5000)) ; sanity check: default to stop at 5K
1525 (ignored-keys '(:create :delete :max :backend :label :require :type))
1526 (search-keys (cl-loop for i below (length spec) by 2
1527 unless (memq (nth i spec) ignored-keys)
1528 collect (nth i spec)))
1529 ;; build a search spec without the ignored keys
1530 ;; if a search key is nil or t (match anything), we skip it
1531 (search-specs (auth-source-secrets-listify-pattern
1532 (apply #'append (mapcar
1533 (lambda (k)
1534 (if (or (null (plist-get spec k))
1535 (eq t (plist-get spec k)))
1537 (list k (plist-get spec k))))
1538 search-keys))))
1539 ;; needed keys (always including host, login, port, and secret)
1540 (returned-keys (delete-dups (append
1541 '(:host :login :port :secret)
1542 search-keys)))
1543 (items
1544 (cl-loop
1545 for search-spec in search-specs
1546 nconc
1547 (cl-loop for item in (apply #'secrets-search-items coll search-spec)
1548 unless (and (stringp label)
1549 (not (string-match label item)))
1550 collect item)))
1551 ;; TODO: respect max in `secrets-search-items', not after the fact
1552 (items (butlast items (- (length items) max)))
1553 ;; convert the item name to a full plist
1554 (items (mapcar (lambda (item)
1555 (append
1556 ;; make an entry for the secret (password) element
1557 (list
1558 :secret
1559 (let ((v (secrets-get-secret coll item)))
1560 (lambda () v)))
1561 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1562 (apply #'append
1563 (mapcar (lambda (entry)
1564 (list (car entry) (cdr entry)))
1565 (secrets-get-attributes coll item)))))
1566 items))
1567 ;; ensure each item has each key in `returned-keys'
1568 (items (mapcar (lambda (plist)
1569 (append
1570 (apply #'append
1571 (mapcar (lambda (req)
1572 (if (plist-get plist req)
1574 (list req nil)))
1575 returned-keys))
1576 plist))
1577 items)))
1578 items))
1580 (defun auth-source-secrets-create (&rest spec)
1581 ;; TODO
1582 ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1583 (debug spec))
1585 ;;; Backend specific parsing: Mac OS Keychain (using /usr/bin/security) backend
1587 (cl-defun auth-source-macos-keychain-search (&rest spec
1588 &key backend create delete type max
1589 &allow-other-keys)
1590 "Search the macOS Keychain; spec is like `auth-source'.
1592 All search keys must match exactly. If you need substring
1593 matching, do a wider search and narrow it down yourself.
1595 You'll get back all the properties of the token as a plist.
1597 The :type key is either `macos-keychain-internet' or
1598 `macos-keychain-generic'.
1600 For the internet keychain type, the :label key searches the
1601 item's labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1602 Similarly, :host maps to \"-s HOST\", :user maps to \"-a USER\",
1603 and :port maps to \"-P PORT\" or \"-r PROT\"
1604 \(note PROT has to be a 4-character string).
1606 For the generic keychain type, the :label key searches the item's
1607 labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1608 Similarly, :host maps to \"-c HOST\" (the \"creator\" keychain
1609 field), :user maps to \"-a USER\", and :port maps to \"-s PORT\".
1611 Here's an example that looks for the first item in the default
1612 generic macOS Keychain:
1614 (let ((auth-sources \\='(macos-keychain-generic)))
1615 (auth-source-search :max 1)
1617 Here's another that looks for the first item in the internet
1618 macOS Keychain collection whose label is `gnus':
1620 (let ((auth-sources \\='(macos-keychain-internet)))
1621 (auth-source-search :max 1 :label \"gnus\")
1623 And this one looks for the first item in the internet keychain
1624 entries for git.gnus.org:
1626 (let ((auth-sources \\='(macos-keychain-internet\")))
1627 (auth-source-search :max 1 :host \"git.gnus.org\"))
1629 ;; TODO
1630 (cl-assert (not create) nil
1631 "The macOS Keychain auth-source backend doesn't support creation yet")
1632 ;; TODO
1633 ;; (macos-keychain-delete-item coll elt)
1634 (cl-assert (not delete) nil
1635 "The macOS Keychain auth-source backend doesn't support deletion yet")
1637 (let* ((coll (oref backend source))
1638 (max (or max 5000)) ; sanity check: default to stop at 5K
1639 ;; Filter out ignored keys from the spec
1640 (ignored-keys '(:create :delete :max :backend :label :host :port))
1641 ;; Build a search spec without the ignored keys
1642 ;; FIXME make this loop a function? it's used in at least 3 places
1643 (search-keys (cl-loop for i below (length spec) by 2
1644 unless (memq (nth i spec) ignored-keys)
1645 collect (nth i spec)))
1646 ;; If a search key value is nil or t (match anything), we skip it
1647 (search-spec (apply #'append (mapcar
1648 (lambda (k)
1649 (if (or (null (plist-get spec k))
1650 (eq t (plist-get spec k)))
1652 (list k (plist-get spec k))))
1653 search-keys)))
1654 ;; needed keys (always including host, login, port, and secret)
1655 (returned-keys (delete-dups (append
1656 '(:host :login :port :secret)
1657 search-keys)))
1658 ;; Extract host and port from spec
1659 (hosts (plist-get spec :host))
1660 (hosts (if (and hosts (listp hosts)) hosts `(,hosts)))
1661 (ports (plist-get spec :port))
1662 (ports (if (and ports (listp ports)) ports `(,ports)))
1663 ;; Loop through all combinations of host/port and pass each of these to
1664 ;; auth-source-macos-keychain-search-items
1665 (items (catch 'match
1666 (dolist (host hosts)
1667 (dolist (port ports)
1668 (let* ((port (if port (format "%S" port)))
1669 (items (apply #'auth-source-macos-keychain-search-items
1670 coll
1671 type
1673 host port
1674 search-spec)))
1675 (when items
1676 (throw 'match items)))))))
1678 ;; ensure each item has each key in `returned-keys'
1679 (items (mapcar (lambda (plist)
1680 (append
1681 (apply #'append
1682 (mapcar (lambda (req)
1683 (if (plist-get plist req)
1685 (list req nil)))
1686 returned-keys))
1687 plist))
1688 items)))
1689 items))
1692 (defun auth-source--decode-octal-string (string)
1693 "Convert octal string to utf-8 string. E.g: 'a\134b' to 'a\b'"
1694 (let ((list (string-to-list string))
1695 (size (length string)))
1696 (decode-coding-string
1697 (apply #'unibyte-string
1698 (cl-loop for i = 0 then (+ i (if (eq (nth i list) ?\\) 4 1))
1699 for var = (nth i list)
1700 while (< i size)
1701 if (eq var ?\\)
1702 collect (string-to-number
1703 (concat (cl-subseq list (+ i 1) (+ i 4))) 8)
1704 else
1705 collect var))
1706 'utf-8)))
1708 (cl-defun auth-source-macos-keychain-search-items (coll _type _max host port
1709 &key label type user
1710 &allow-other-keys)
1711 (let* ((keychain-generic (eq type 'macos-keychain-generic))
1712 (args `(,(if keychain-generic
1713 "find-generic-password"
1714 "find-internet-password")
1715 "-g"))
1716 (ret (list :type type)))
1717 (when label
1718 (setq args (append args (list "-l" label))))
1719 (when host
1720 (setq args (append args (list (if keychain-generic "-c" "-s") host))))
1721 (when user
1722 (setq args (append args (list "-a" user))))
1724 (when port
1725 (if keychain-generic
1726 (setq args (append args (list "-s" port)))
1727 (setq args (append args (list
1728 (if (string-match "[0-9]+" port) "-P" "-r")
1729 port)))))
1731 (unless (equal coll "default")
1732 (setq args (append args (list coll))))
1734 (with-temp-buffer
1735 (apply #'call-process "/usr/bin/security" nil t nil args)
1736 (goto-char (point-min))
1737 (while (not (eobp))
1738 (cond
1739 ((looking-at "^password: \\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1740 (setq ret (auth-source-macos-keychain-result-append
1742 keychain-generic
1743 "secret"
1744 (let ((v (auth-source--decode-octal-string
1745 (match-string 1))))
1746 (lambda () v)))))
1747 ;; TODO: check if this is really the label
1748 ;; match 0x00000007 <blob>="AppleID"
1749 ((looking-at
1750 "^[ ]+0x00000007 <blob>=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1751 (setq ret (auth-source-macos-keychain-result-append
1753 keychain-generic
1754 "label"
1755 (auth-source--decode-octal-string (match-string 1)))))
1756 ;; match "crtr"<uint32>="aapl"
1757 ;; match "svce"<blob>="AppleID"
1758 ((looking-at
1759 "^[ ]+\"\\([a-z]+\\)\"[^=]+=\\(?:0x[0-9A-F]+\\)? *\"\\(.+\\)\"")
1760 (setq ret (auth-source-macos-keychain-result-append
1762 keychain-generic
1763 (auth-source--decode-octal-string (match-string 1))
1764 (auth-source--decode-octal-string (match-string 2))))))
1765 (forward-line)))
1766 ;; return `ret' iff it has the :secret key
1767 (and (plist-get ret :secret) (list ret))))
1769 (defun auth-source-macos-keychain-result-append (result generic k v)
1770 (push v result)
1771 (push (auth-source--symbol-keyword
1772 (cond
1773 ((equal k "acct") "user")
1774 ;; for generic keychains, creator is host, service is port
1775 ((and generic (equal k "crtr")) "host")
1776 ((and generic (equal k "svce")) "port")
1777 ;; for internet keychains, protocol is port, server is host
1778 ((and (not generic) (equal k "ptcl")) "port")
1779 ((and (not generic) (equal k "srvr")) "host")
1780 (t k)))
1781 result))
1783 (defun auth-source-macos-keychain-create (&rest spec)
1784 ;; TODO
1785 (debug spec))
1787 ;;; Backend specific parsing: PLSTORE backend
1789 (cl-defun auth-source-plstore-search (&rest spec
1790 &key backend create delete max
1791 &allow-other-keys)
1792 "Search the PLSTORE; spec is like `auth-source'."
1793 (let* ((store (oref backend data))
1794 (max (or max 5000)) ; sanity check: default to stop at 5K
1795 (ignored-keys '(:create :delete :max :backend :label :require :type))
1796 (search-keys (cl-loop for i below (length spec) by 2
1797 unless (memq (nth i spec) ignored-keys)
1798 collect (nth i spec)))
1799 ;; build a search spec without the ignored keys
1800 ;; if a search key is nil or t (match anything), we skip it
1801 (search-spec (apply #'append (mapcar
1802 (lambda (k)
1803 (let ((v (plist-get spec k)))
1804 (if (or (null v)
1805 (eq t v))
1807 (if (stringp v)
1808 (setq v (list v)))
1809 (list k v))))
1810 search-keys)))
1811 ;; needed keys (always including host, login, port, and secret)
1812 (returned-keys (delete-dups (append
1813 '(:host :login :port :secret)
1814 search-keys)))
1815 (items (plstore-find store search-spec))
1816 (item-names (mapcar #'car items))
1817 (items (butlast items (- (length items) max)))
1818 ;; convert the item to a full plist
1819 (items (mapcar (lambda (item)
1820 (let* ((plist (copy-tree (cdr item)))
1821 (secret (plist-member plist :secret)))
1822 (if secret
1823 (setcar
1824 (cdr secret)
1825 (let ((v (car (cdr secret))))
1826 (lambda () v))))
1827 plist))
1828 items))
1829 ;; ensure each item has each key in `returned-keys'
1830 (items (mapcar (lambda (plist)
1831 (append
1832 (apply #'append
1833 (mapcar (lambda (req)
1834 (if (plist-get plist req)
1836 (list req nil)))
1837 returned-keys))
1838 plist))
1839 items)))
1840 (cond
1841 ;; if we need to create an entry AND none were found to match
1842 ((and create
1843 (not items))
1845 ;; create based on the spec and record the value
1846 (setq items (or
1847 ;; if the user did not want to create the entry
1848 ;; in the file, it will be returned
1849 (apply (slot-value backend 'create-function) spec)
1850 ;; if not, we do the search again without :create
1851 ;; to get the updated data.
1853 ;; the result will be returned, even if the search fails
1854 (apply #'auth-source-plstore-search
1855 (plist-put spec :create nil)))))
1856 ((and delete
1857 item-names)
1858 (dolist (item-name item-names)
1859 (plstore-delete store item-name))
1860 (plstore-save store)))
1861 items))
1863 (cl-defun auth-source-plstore-create (&rest spec
1864 &key backend host port create
1865 &allow-other-keys)
1866 (let* ((base-required '(host user port secret))
1867 (base-secret '(secret))
1868 ;; we know (because of an assertion in auth-source-search) that the
1869 ;; :create parameter is either t or a list (which includes nil)
1870 (create-extra (if (eq t create) nil create))
1871 (current-data (car (auth-source-search :max 1
1872 :host host
1873 :port port)))
1874 (required (append base-required create-extra))
1875 ;; `valist' is an alist
1876 valist
1877 ;; `artificial' will be returned if no creation is needed
1878 artificial
1879 secret-artificial)
1881 ;; only for base required elements (defined as function parameters):
1882 ;; fill in the valist with whatever data we may have from the search
1883 ;; we complete the first value if it's a list and use the value otherwise
1884 (dolist (br base-required)
1885 (let ((val (plist-get spec (auth-source--symbol-keyword br))))
1886 (when val
1887 (let ((br-choice (cond
1888 ;; all-accepting choice (predicate is t)
1889 ((eq t val) nil)
1890 ;; just the value otherwise
1891 (t val))))
1892 (when br-choice
1893 (auth-source--aput valist br br-choice))))))
1895 ;; for extra required elements, see if the spec includes a value for them
1896 (dolist (er create-extra)
1897 (let ((k (auth-source--symbol-keyword er))
1898 (keys (cl-loop for i below (length spec) by 2
1899 collect (nth i spec))))
1900 (when (memq k keys)
1901 (auth-source--aput valist er (plist-get spec k)))))
1903 ;; for each required element
1904 (dolist (r required)
1905 (let* ((data (auth-source--aget valist r))
1906 ;; take the first element if the data is a list
1907 (data (or (auth-source-netrc-element-or-first data)
1908 (plist-get current-data
1909 (auth-source--symbol-keyword r))))
1910 ;; this is the default to be offered
1911 (given-default (auth-source--aget
1912 auth-source-creation-defaults r))
1913 ;; the default supplementals are simple:
1914 ;; for the user, try `given-default' and then (user-login-name);
1915 ;; otherwise take `given-default'
1916 (default (cond
1917 ((and (not given-default) (eq r 'user))
1918 (user-login-name))
1919 (t given-default)))
1920 (printable-defaults (list
1921 (cons 'user
1923 (auth-source-netrc-element-or-first
1924 (auth-source--aget valist 'user))
1925 (plist-get artificial :user)
1926 "[any user]"))
1927 (cons 'host
1929 (auth-source-netrc-element-or-first
1930 (auth-source--aget valist 'host))
1931 (plist-get artificial :host)
1932 "[any host]"))
1933 (cons 'port
1935 (auth-source-netrc-element-or-first
1936 (auth-source--aget valist 'port))
1937 (plist-get artificial :port)
1938 "[any port]"))))
1939 (prompt (or (auth-source--aget auth-source-creation-prompts r)
1940 (cl-case r
1941 (secret "%p password for %u@%h: ")
1942 (user "%p user name for %h: ")
1943 (host "%p host name for user %u: ")
1944 (port "%p port for %u@%h: "))
1945 (format "Enter %s (%%u@%%h:%%p): " r)))
1946 (prompt (auth-source-format-prompt
1947 prompt
1948 `((?u ,(auth-source--aget printable-defaults 'user))
1949 (?h ,(auth-source--aget printable-defaults 'host))
1950 (?p ,(auth-source--aget printable-defaults 'port))))))
1952 ;; Store the data, prompting for the password if needed.
1953 (setq data (or data
1954 (if (eq r 'secret)
1955 (or (eval default) (read-passwd prompt))
1956 (if (stringp default)
1957 (read-string
1958 (if (string-match ": *\\'" prompt)
1959 (concat (substring prompt 0 (match-beginning 0))
1960 " (default " default "): ")
1961 (concat prompt "(default " default ") "))
1962 nil nil default)
1963 (eval default)))))
1965 (when data
1966 (if (member r base-secret)
1967 (setq secret-artificial
1968 (plist-put secret-artificial
1969 (auth-source--symbol-keyword r)
1970 data))
1971 (setq artificial (plist-put artificial
1972 (auth-source--symbol-keyword r)
1973 data))))))
1974 (plstore-put (oref backend data)
1975 (sha1 (format "%s@%s:%s"
1976 (plist-get artificial :user)
1977 (plist-get artificial :host)
1978 (plist-get artificial :port)))
1979 artificial secret-artificial)
1980 (if (y-or-n-p (format "Save auth info to file %s? "
1981 (plstore-get-file (oref backend data))))
1982 (plstore-save (oref backend data)))))
1984 ;;; Backend specific parsing: JSON backend
1985 ;;; (auth-source-search :max 1 :machine "imap.gmail.com")
1986 ;;; (auth-source-search :max 1 :host '("my-gmail" "imap.gmail.com") :port '(993 "imaps" "imap" "993" "143") :user nil :require '(:user :secret))
1988 (defun auth-source-json-check (host user port require item)
1989 (and item
1990 (auth-source-search-collection
1991 (or host t)
1993 (plist-get item :machine)
1994 (plist-get item :host)
1996 (auth-source-search-collection
1997 (or user t)
1999 (plist-get item :login)
2000 (plist-get item :account)
2001 (plist-get item :user)
2003 (auth-source-search-collection
2004 (or port t)
2006 (plist-get item :port)
2007 (plist-get item :protocol)
2010 ;; the required list of keys is nil, or
2011 (null require)
2012 ;; every element of require is in
2013 (cl-loop for req in require
2014 always (plist-get item req)))))
2016 (cl-defun auth-source-json-search (&rest spec
2017 &key backend require
2018 type max host user port
2019 &allow-other-keys)
2020 "Given a property list SPEC, return search matches from the :backend.
2021 See `auth-source-search' for details on SPEC."
2022 ;; just in case, check that the type is correct (null or same as the backend)
2023 (cl-assert (or (null type) (eq type (oref backend type)))
2024 t "Invalid JSON search: %s %s")
2026 ;; Hide the secrets early to avoid accidental exposure.
2027 (let* ((jdata
2028 (mapcar (lambda (entry)
2029 (let (ret)
2030 (while entry
2031 (let* ((item (pop entry))
2032 (k (auth-source--symbol-keyword (car item)))
2033 (v (cdr item)))
2034 (setq k (cond ((memq k '(:machine)) :host)
2035 ((memq k '(:login :account)) :user)
2036 ((memq k '(:protocol)) :port)
2037 ((memq k '(:password)) :secret)
2038 (t k)))
2039 ;; send back the secret in a function (lexical binding)
2040 (when (eq k :secret)
2041 (setq v (let ((lexv v))
2042 (lambda () lexv))))
2043 (setq ret (plist-put ret k v))))
2044 ret))
2045 (json-read-file (oref backend source))))
2046 (max (or max 5000)) ; sanity check: default to stop at 5K
2047 all)
2048 (dolist (item jdata)
2049 (when (and item
2050 (> max (length all))
2051 (auth-source-json-check host user port require item))
2052 (push item all)))
2053 (nreverse all)))
2055 ;;; older API
2057 ;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
2059 ;; deprecate the old interface
2060 (make-obsolete 'auth-source-user-or-password
2061 'auth-source-search "Emacs 24.1")
2062 (make-obsolete 'auth-source-forget-user-or-password
2063 'auth-source-forget "Emacs 24.1")
2065 (defun auth-source-user-or-password
2066 (mode host port &optional username create-missing delete-existing)
2067 "Find MODE (string or list of strings) matching HOST and PORT.
2069 DEPRECATED in favor of `auth-source-search'!
2071 USERNAME is optional and will be used as \"login\" in a search
2072 across the Secret Service API (see secrets.el) if the resulting
2073 items don't have a username. This means that if you search for
2074 username \"joe\" and it matches an item but the item doesn't have
2075 a :user attribute, the username \"joe\" will be returned.
2077 A non nil DELETE-EXISTING means deleting any matching password
2078 entry in the respective sources. This is useful only when
2079 CREATE-MISSING is non nil as well; the intended use case is to
2080 remove wrong password entries.
2082 If no matching entry is found, and CREATE-MISSING is non nil,
2083 the password will be retrieved interactively, and it will be
2084 stored in the password database which matches best (see
2085 `auth-sources').
2087 MODE can be \"login\" or \"password\"."
2088 (auth-source-do-debug
2089 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
2090 mode host port username)
2092 (let* ((listy (listp mode))
2093 (mode (if listy mode (list mode)))
2094 ;; (cname (if username
2095 ;; (format "%s %s:%s %s" mode host port username)
2096 ;; (format "%s %s:%s" mode host port)))
2097 (search (list :host host :port port))
2098 (search (if username (append search (list :user username)) search))
2099 (search (if create-missing
2100 (append search (list :create t))
2101 search))
2102 (search (if delete-existing
2103 (append search (list :delete t))
2104 search))
2105 ;; (found (if (not delete-existing)
2106 ;; (gethash cname auth-source-cache)
2107 ;; (remhash cname auth-source-cache)
2108 ;; nil)))
2109 (found nil))
2110 (if found
2111 (progn
2112 (auth-source-do-debug
2113 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
2114 mode
2115 ;; don't show the password
2116 (if (and (member "password" mode) t)
2117 "SECRET"
2118 found)
2119 host port username)
2120 found) ; return the found data
2121 ;; else, if not found, search with a max of 1
2122 (let ((choice (nth 0 (apply #'auth-source-search
2123 (append '(:max 1) search)))))
2124 (when choice
2125 (dolist (m mode)
2126 (cond
2127 ((equal "password" m)
2128 (push (if (plist-get choice :secret)
2129 (funcall (plist-get choice :secret))
2130 nil) found))
2131 ((equal "login" m)
2132 (push (plist-get choice :user) found)))))
2133 (setq found (nreverse found))
2134 (setq found (if listy found (car-safe found)))))
2136 found))
2138 (defun auth-source-user-and-password (host &optional user)
2139 (let* ((auth-info (car
2140 (if user
2141 (auth-source-search
2142 :host host
2143 :user user
2144 :max 1
2145 :require '(:user :secret)
2146 :create nil)
2147 (auth-source-search
2148 :host host
2149 :max 1
2150 :require '(:user :secret)
2151 :create nil))))
2152 (user (plist-get auth-info :user))
2153 (password (plist-get auth-info :secret)))
2154 (when (functionp password)
2155 (setq password (funcall password)))
2156 (list user password auth-info)))
2158 (provide 'auth-source)
2160 ;;; auth-source.el ends here