Update copyright notices for 2013.
[emacs.git] / lisp / gnus / auth-source.el
blob6e6c74509f09c93dce1a6d9b4d4b1c4c1cda8f8c
1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
3 ;; Copyright (C) 2008-2013 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 <http://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 'password-cache)
43 (require 'mm-util)
44 (require 'gnus-util)
46 (eval-when-compile (require 'cl))
47 (require 'eieio)
49 (autoload 'secrets-create-item "secrets")
50 (autoload 'secrets-delete-item "secrets")
51 (autoload 'secrets-get-alias "secrets")
52 (autoload 'secrets-get-attributes "secrets")
53 (autoload 'secrets-get-secret "secrets")
54 (autoload 'secrets-list-collections "secrets")
55 (autoload 'secrets-search-items "secrets")
57 (autoload 'rfc2104-hash "rfc2104")
59 (autoload 'plstore-open "plstore")
60 (autoload 'plstore-find "plstore")
61 (autoload 'plstore-put "plstore")
62 (autoload 'plstore-delete "plstore")
63 (autoload 'plstore-save "plstore")
64 (autoload 'plstore-get-file "plstore")
66 (autoload 'epg-make-context "epg")
67 (autoload 'epg-context-set-passphrase-callback "epg")
68 (autoload 'epg-decrypt-string "epg")
69 (autoload 'epg-context-set-armor "epg")
70 (autoload 'epg-encrypt-string "epg")
72 (autoload 'help-mode "help-mode" nil t)
74 (defvar secrets-enabled)
76 (defgroup auth-source nil
77 "Authentication sources."
78 :version "23.1" ;; No Gnus
79 :group 'gnus)
81 ;;;###autoload
82 (defcustom auth-source-cache-expiry 7200
83 "How many seconds passwords are cached, or nil to disable
84 expiring. Overrides `password-cache-expiry' through a
85 let-binding."
86 :version "24.1"
87 :group 'auth-source
88 :type '(choice (const :tag "Never" nil)
89 (const :tag "All Day" 86400)
90 (const :tag "2 Hours" 7200)
91 (const :tag "30 Minutes" 1800)
92 (integer :tag "Seconds")))
94 ;; The slots below correspond with the `auth-source-search' spec,
95 ;; so a backend with :host set, for instance, would match only
96 ;; searches for that host. Normally they are nil.
97 (defclass auth-source-backend ()
98 ((type :initarg :type
99 :initform 'netrc
100 :type symbol
101 :custom symbol
102 :documentation "The backend type.")
103 (source :initarg :source
104 :type string
105 :custom string
106 :documentation "The backend source.")
107 (host :initarg :host
108 :initform t
109 :type t
110 :custom string
111 :documentation "The backend host.")
112 (user :initarg :user
113 :initform t
114 :type t
115 :custom string
116 :documentation "The backend user.")
117 (port :initarg :port
118 :initform t
119 :type t
120 :custom string
121 :documentation "The backend protocol.")
122 (data :initarg :data
123 :initform nil
124 :documentation "Internal backend data.")
125 (create-function :initarg :create-function
126 :initform ignore
127 :type function
128 :custom function
129 :documentation "The create function.")
130 (search-function :initarg :search-function
131 :initform ignore
132 :type function
133 :custom function
134 :documentation "The search function.")))
136 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
137 (pop3 "pop3" "pop" "pop3s" "110" "995")
138 (ssh "ssh" "22")
139 (sftp "sftp" "115")
140 (smtp "smtp" "25"))
141 "List of authentication protocols and their names"
143 :group 'auth-source
144 :version "23.2" ;; No Gnus
145 :type '(repeat :tag "Authentication Protocols"
146 (cons :tag "Protocol Entry"
147 (symbol :tag "Protocol")
148 (repeat :tag "Names"
149 (string :tag "Name")))))
151 ;; Generate all the protocols in a format Customize can use.
152 ;; TODO: generate on the fly from auth-source-protocols
153 (defconst auth-source-protocols-customize
154 (mapcar (lambda (a)
155 (let ((p (car-safe a)))
156 (list 'const
157 :tag (upcase (symbol-name p))
158 p)))
159 auth-source-protocols))
161 (defvar auth-source-creation-defaults nil
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 (symbol-value '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 (symbol-value
198 'epa-file-auto-mode-alist-entry))
199 "\\.gpg\\'"))
200 (regexp :tag "Regular expression"))
201 (choice :tag "What to do"
202 (const :tag "Save GPG-encrypted password tokens" gpg)
203 (const :tag "Don't encrypt tokens" never))))))
205 (defvar auth-source-magic "auth-source-magic ")
207 (defcustom auth-source-do-cache t
208 "Whether auth-source should cache information with `password-cache'."
209 :group 'auth-source
210 :version "23.2" ;; No Gnus
211 :type `boolean)
213 (defcustom auth-source-debug nil
214 "Whether auth-source should log debug messages.
216 If the value is nil, debug messages are not logged.
218 If the value is t, debug messages are logged with `message'. In
219 that case, your authentication data will be in the clear (except
220 for passwords).
222 If the value is a function, debug messages are logged by calling
223 that function using the same arguments as `message'."
224 :group 'auth-source
225 :version "23.2" ;; No Gnus
226 :type `(choice
227 :tag "auth-source debugging mode"
228 (const :tag "Log using `message' to the *Messages* buffer" t)
229 (const :tag "Log all trivia with `message' to the *Messages* buffer"
230 trivia)
231 (function :tag "Function that takes arguments like `message'")
232 (const :tag "Don't log anything" nil)))
234 (defcustom auth-sources '("~/.authinfo" "~/.authinfo.gpg" "~/.netrc")
235 "List of authentication sources.
237 The default will get login and password information from
238 \"~/.authinfo.gpg\", which you should set up with the EPA/EPG
239 packages to be encrypted. If that file doesn't exist, it will
240 try the unencrypted version \"~/.authinfo\" and the famous
241 \"~/.netrc\" file.
243 See the auth.info manual for details.
245 Each entry is the authentication type with optional properties.
247 It's best to customize this with `M-x customize-variable' because the choices
248 can get pretty complex."
249 :group 'auth-source
250 :version "24.1" ;; No Gnus
251 :type `(repeat :tag "Authentication Sources"
252 (choice
253 (string :tag "Just a file")
254 (const :tag "Default Secrets API Collection" 'default)
255 (const :tag "Login Secrets API Collection" "secrets:Login")
256 (const :tag "Temp Secrets API Collection" "secrets:session")
258 (const :tag "Default internet Mac OS Keychain"
259 macos-keychain-internet)
261 (const :tag "Default generic Mac OS Keychain"
262 macos-keychain-generic)
264 (list :tag "Source definition"
265 (const :format "" :value :source)
266 (choice :tag "Authentication backend choice"
267 (string :tag "Authentication Source (file)")
268 (list
269 :tag "Secret Service API/KWallet/GNOME Keyring"
270 (const :format "" :value :secrets)
271 (choice :tag "Collection to use"
272 (string :tag "Collection name")
273 (const :tag "Default" 'default)
274 (const :tag "Login" "Login")
275 (const
276 :tag "Temporary" "session")))
277 (list
278 :tag "Mac OS internet Keychain"
279 (const :format ""
280 :value :macos-keychain-internet)
281 (choice :tag "Collection to use"
282 (string :tag "internet Keychain path")
283 (const :tag "default" 'default)))
284 (list
285 :tag "Mac OS generic Keychain"
286 (const :format ""
287 :value :macos-keychain-generic)
288 (choice :tag "Collection to use"
289 (string :tag "generic Keychain path")
290 (const :tag "default" 'default))))
291 (repeat :tag "Extra Parameters" :inline t
292 (choice :tag "Extra parameter"
293 (list
294 :tag "Host"
295 (const :format "" :value :host)
296 (choice :tag "Host (machine) choice"
297 (const :tag "Any" t)
298 (regexp
299 :tag "Regular expression")))
300 (list
301 :tag "Protocol"
302 (const :format "" :value :port)
303 (choice
304 :tag "Protocol"
305 (const :tag "Any" t)
306 ,@auth-source-protocols-customize))
307 (list :tag "User" :inline t
308 (const :format "" :value :user)
309 (choice
310 :tag "Personality/Username"
311 (const :tag "Any" t)
312 (string
313 :tag "Name")))))))))
315 (defcustom auth-source-gpg-encrypt-to t
316 "List of recipient keys that `authinfo.gpg' encrypted to.
317 If the value is not a list, symmetric encryption will be used."
318 :group 'auth-source
319 :version "24.1" ;; No Gnus
320 :type '(choice (const :tag "Symmetric encryption" t)
321 (repeat :tag "Recipient public keys"
322 (string :tag "Recipient public key"))))
324 ;; temp for debugging
325 ;; (unintern 'auth-source-protocols)
326 ;; (unintern 'auth-sources)
327 ;; (customize-variable 'auth-sources)
328 ;; (setq auth-sources nil)
329 ;; (format "%S" auth-sources)
330 ;; (customize-variable 'auth-source-protocols)
331 ;; (setq auth-source-protocols nil)
332 ;; (format "%S" auth-source-protocols)
333 ;; (auth-source-pick nil :host "a" :port 'imap)
334 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
335 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
336 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
337 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
338 ;; (auth-source-protocol-defaults 'imap)
340 ;; (let ((auth-source-debug 'debug)) (auth-source-do-debug "hello"))
341 ;; (let ((auth-source-debug t)) (auth-source-do-debug "hello"))
342 ;; (let ((auth-source-debug nil)) (auth-source-do-debug "hello"))
343 (defun auth-source-do-debug (&rest msg)
344 (when auth-source-debug
345 (apply 'auth-source-do-warn msg)))
347 (defun auth-source-do-trivia (&rest msg)
348 (when (or (eq auth-source-debug 'trivia)
349 (functionp auth-source-debug))
350 (apply 'auth-source-do-warn msg)))
352 (defun auth-source-do-warn (&rest msg)
353 (apply
354 ;; set logger to either the function in auth-source-debug or 'message
355 ;; note that it will be 'message if auth-source-debug is nil
356 (if (functionp auth-source-debug)
357 auth-source-debug
358 'message)
359 msg))
362 ;; (auth-source-read-char-choice "enter choice? " '(?a ?b ?q))
363 (defun auth-source-read-char-choice (prompt choices)
364 "Read one of CHOICES by `read-char-choice', or `read-char'.
365 `dropdown-list' support is disabled because it doesn't work reliably.
366 Only one of CHOICES will be returned. The PROMPT is augmented
367 with \"[a/b/c] \" if CHOICES is '\(?a ?b ?c\)."
368 (when choices
369 (let* ((prompt-choices
370 (apply 'concat (loop for c in choices
371 collect (format "%c/" c))))
372 (prompt-choices (concat "[" (substring prompt-choices 0 -1) "] "))
373 (full-prompt (concat prompt prompt-choices))
376 (while (not (memq k choices))
377 (setq k (cond
378 ((fboundp 'read-char-choice)
379 (read-char-choice full-prompt choices))
380 (t (message "%s" full-prompt)
381 (setq k (read-char))))))
382 k)))
384 ;; (auth-source-pick nil :host "any" :port 'imap :user "joe")
385 ;; (auth-source-pick t :host "any" :port 'imap :user "joe")
386 ;; (setq auth-sources '((:source (:secrets default) :host t :port t :user "joe")
387 ;; (:source (:secrets "session") :host t :port t :user "joe")
388 ;; (:source (:secrets "Login") :host t :port t)
389 ;; (:source "~/.authinfo.gpg" :host t :port t)))
391 ;; (setq auth-sources '((:source (:secrets default) :host t :port t :user "joe")
392 ;; (:source (:secrets "session") :host t :port t :user "joe")
393 ;; (:source (:secrets "Login") :host t :port t)
394 ;; ))
396 ;; (setq auth-sources '((:source "~/.authinfo.gpg" :host t :port t)))
398 ;; (auth-source-backend-parse "myfile.gpg")
399 ;; (auth-source-backend-parse 'default)
400 ;; (auth-source-backend-parse "secrets:Login")
401 ;; (auth-source-backend-parse 'macos-keychain-internet)
402 ;; (auth-source-backend-parse 'macos-keychain-generic)
403 ;; (auth-source-backend-parse "macos-keychain-internet:/path/here.keychain")
404 ;; (auth-source-backend-parse "macos-keychain-generic:/path/here.keychain")
406 (defun auth-source-backend-parse (entry)
407 "Creates an auth-source-backend from an ENTRY in `auth-sources'."
408 (auth-source-backend-parse-parameters
409 entry
410 (cond
411 ;; take 'default and recurse to get it as a Secrets API default collection
412 ;; matching any user, host, and protocol
413 ((eq entry 'default)
414 (auth-source-backend-parse '(:source (:secrets default))))
415 ;; take secrets:XYZ and recurse to get it as Secrets API collection "XYZ"
416 ;; matching any user, host, and protocol
417 ((and (stringp entry) (string-match "^secrets:\\(.+\\)" entry))
418 (auth-source-backend-parse `(:source (:secrets ,(match-string 1 entry)))))
420 ;; take 'macos-keychain-internet and recurse to get it as a Mac OS
421 ;; Keychain collection matching any user, host, and protocol
422 ((eq entry 'macos-keychain-internet)
423 (auth-source-backend-parse '(:source (:macos-keychain-internet default))))
424 ;; take 'macos-keychain-generic and recurse to get it as a Mac OS
425 ;; Keychain collection matching any user, host, and protocol
426 ((eq entry 'macos-keychain-generic)
427 (auth-source-backend-parse '(:source (:macos-keychain-generic default))))
428 ;; take macos-keychain-internet:XYZ and recurse to get it as MacOS
429 ;; Keychain "XYZ" matching any user, host, and protocol
430 ((and (stringp entry) (string-match "^macos-keychain-internet:\\(.+\\)"
431 entry))
432 (auth-source-backend-parse `(:source (:macos-keychain-internet
433 ,(match-string 1 entry)))))
434 ;; take macos-keychain-generic:XYZ and recurse to get it as MacOS
435 ;; Keychain "XYZ" matching any user, host, and protocol
436 ((and (stringp entry) (string-match "^macos-keychain-generic:\\(.+\\)"
437 entry))
438 (auth-source-backend-parse `(:source (:macos-keychain-generic
439 ,(match-string 1 entry)))))
441 ;; take just a file name and recurse to get it as a netrc file
442 ;; matching any user, host, and protocol
443 ((stringp entry)
444 (auth-source-backend-parse `(:source ,entry)))
446 ;; a file name with parameters
447 ((stringp (plist-get entry :source))
448 (if (equal (file-name-extension (plist-get entry :source)) "plist")
449 (auth-source-backend
450 (plist-get entry :source)
451 :source (plist-get entry :source)
452 :type 'plstore
453 :search-function 'auth-source-plstore-search
454 :create-function 'auth-source-plstore-create
455 :data (plstore-open (plist-get entry :source)))
456 (auth-source-backend
457 (plist-get entry :source)
458 :source (plist-get entry :source)
459 :type 'netrc
460 :search-function 'auth-source-netrc-search
461 :create-function 'auth-source-netrc-create)))
463 ;; the MacOS Keychain
464 ((and
465 (not (null (plist-get entry :source))) ; the source must not be nil
466 (listp (plist-get entry :source)) ; and it must be a list
468 (plist-get (plist-get entry :source) :macos-keychain-generic)
469 (plist-get (plist-get entry :source) :macos-keychain-internet)))
471 (let* ((source-spec (plist-get entry :source))
472 (keychain-generic (plist-get source-spec :macos-keychain-generic))
473 (keychain-type (if keychain-generic
474 'macos-keychain-generic
475 'macos-keychain-internet))
476 (source (plist-get source-spec (if keychain-generic
477 :macos-keychain-generic
478 :macos-keychain-internet))))
480 (when (symbolp source)
481 (setq source (symbol-name source)))
483 (auth-source-backend
484 (format "Mac OS Keychain (%s)" source)
485 :source source
486 :type keychain-type
487 :search-function 'auth-source-macos-keychain-search
488 :create-function 'auth-source-macos-keychain-create)))
490 ;; the Secrets API. We require the package, in order to have a
491 ;; defined value for `secrets-enabled'.
492 ((and
493 (not (null (plist-get entry :source))) ; the source must not be nil
494 (listp (plist-get entry :source)) ; and it must be a list
495 (require 'secrets nil t) ; and we must load the Secrets API
496 secrets-enabled) ; and that API must be enabled
498 ;; the source is either the :secrets key in ENTRY or
499 ;; if that's missing or nil, it's "session"
500 (let ((source (or (plist-get (plist-get entry :source) :secrets)
501 "session")))
503 ;; if the source is a symbol, we look for the alias named so,
504 ;; and if that alias is missing, we use "Login"
505 (when (symbolp source)
506 (setq source (or (secrets-get-alias (symbol-name source))
507 "Login")))
509 (if (featurep 'secrets)
510 (auth-source-backend
511 (format "Secrets API (%s)" source)
512 :source source
513 :type 'secrets
514 :search-function 'auth-source-secrets-search
515 :create-function 'auth-source-secrets-create)
516 (auth-source-do-warn
517 "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry)
518 (auth-source-backend
519 (format "Ignored Secrets API (%s)" source)
520 :source ""
521 :type 'ignore))))
523 ;; none of them
525 (auth-source-do-warn
526 "auth-source-backend-parse: invalid backend spec: %S" entry)
527 (auth-source-backend
528 "Empty"
529 :source ""
530 :type 'ignore)))))
532 (defun auth-source-backend-parse-parameters (entry backend)
533 "Fills in the extra auth-source-backend parameters of ENTRY.
534 Using the plist ENTRY, get the :host, :port, and :user search
535 parameters."
536 (let ((entry (if (stringp entry)
538 entry))
539 val)
540 (when (setq val (plist-get entry :host))
541 (oset backend host val))
542 (when (setq val (plist-get entry :user))
543 (oset backend user val))
544 (when (setq val (plist-get entry :port))
545 (oset backend port val)))
546 backend)
548 ;; (mapcar 'auth-source-backend-parse auth-sources)
550 (defun* auth-source-search (&rest spec
551 &key type max host user port secret
552 require create delete
553 &allow-other-keys)
554 "Search or modify authentication backends according to SPEC.
556 This function parses `auth-sources' for matches of the SPEC
557 plist. It can optionally create or update an authentication
558 token if requested. A token is just a standard Emacs property
559 list with a :secret property that can be a function; all the
560 other properties will always hold scalar values.
562 Typically the :secret property, if present, contains a password.
564 Common search keys are :max, :host, :port, and :user. In
565 addition, :create specifies how tokens will be or created.
566 Finally, :type can specify which backend types you want to check.
568 A string value is always matched literally. A symbol is matched
569 as its string value, literally. All the SPEC values can be
570 single values (symbol or string) or lists thereof (in which case
571 any of the search terms matches).
573 :create t means to create a token if possible.
575 A new token will be created if no matching tokens were found.
576 The new token will have only the keys the backend requires. For
577 the netrc backend, for instance, that's the user, host, and
578 port keys.
580 Here's an example:
582 \(let ((auth-source-creation-defaults '((user . \"defaultUser\")
583 (A . \"default A\"))))
584 (auth-source-search :host \"mine\" :type 'netrc :max 1
585 :P \"pppp\" :Q \"qqqq\"
586 :create t))
588 which says:
590 \"Search for any entry matching host 'mine' in backends of type
591 'netrc', maximum one result.
593 Create a new entry if you found none. The netrc backend will
594 automatically require host, user, and port. The host will be
595 'mine'. We prompt for the user with default 'defaultUser' and
596 for the port without a default. We will not prompt for A, Q,
597 or P. The resulting token will only have keys user, host, and
598 port.\"
600 :create '(A B C) also means to create a token if possible.
602 The behavior is like :create t but if the list contains any
603 parameter, that parameter will be required in the resulting
604 token. The value for that parameter will be obtained from the
605 search parameters or from user input. If any queries are needed,
606 the alist `auth-source-creation-defaults' will be checked for the
607 default value. If the user, host, or port are missing, the alist
608 `auth-source-creation-prompts' will be used to look up the
609 prompts IN THAT ORDER (so the 'user prompt will be queried first,
610 then 'host, then 'port, and finally 'secret). Each prompt string
611 can use %u, %h, and %p to show the user, host, and port.
613 Here's an example:
615 \(let ((auth-source-creation-defaults '((user . \"defaultUser\")
616 (A . \"default A\")))
617 (auth-source-creation-prompts
618 '((password . \"Enter IMAP password for %h:%p: \"))))
619 (auth-source-search :host '(\"nonesuch\" \"twosuch\") :type 'netrc :max 1
620 :P \"pppp\" :Q \"qqqq\"
621 :create '(A B Q)))
623 which says:
625 \"Search for any entry matching host 'nonesuch'
626 or 'twosuch' in backends of type 'netrc', maximum one result.
628 Create a new entry if you found none. The netrc backend will
629 automatically require host, user, and port. The host will be
630 'nonesuch' and Q will be 'qqqq'. We prompt for the password
631 with the shown prompt. We will not prompt for Q. The resulting
632 token will have keys user, host, port, A, B, and Q. It will not
633 have P with any value, even though P is used in the search to
634 find only entries that have P set to 'pppp'.\"
636 When multiple values are specified in the search parameter, the
637 user is prompted for which one. So :host (X Y Z) would ask the
638 user to choose between X, Y, and Z.
640 This creation can fail if the search was not specific enough to
641 create a new token (it's up to the backend to decide that). You
642 should `catch' the backend-specific error as usual. Some
643 backends (netrc, at least) will prompt the user rather than throw
644 an error.
646 :require (A B C) means that only results that contain those
647 tokens will be returned. Thus for instance requiring :secret
648 will ensure that any results will actually have a :secret
649 property.
651 :delete t means to delete any found entries. nil by default.
652 Use `auth-source-delete' in ELisp code instead of calling
653 `auth-source-search' directly with this parameter.
655 :type (X Y Z) will check only those backend types. 'netrc and
656 'secrets are the only ones supported right now.
658 :max N means to try to return at most N items (defaults to 1).
659 When 0 the function will return just t or nil to indicate if any
660 matches were found. More than N items may be returned, depending
661 on the search and the backend.
663 :host (X Y Z) means to match only hosts X, Y, or Z according to
664 the match rules above. Defaults to t.
666 :user (X Y Z) means to match only users X, Y, or Z according to
667 the match rules above. Defaults to t.
669 :port (P Q R) means to match only protocols P, Q, or R.
670 Defaults to t.
672 :K (V1 V2 V3) for any other key K will match values V1, V2, or
673 V3 (note the match rules above).
675 The return value is a list with at most :max tokens. Each token
676 is a plist with keys :backend :host :port :user, plus any other
677 keys provided by the backend (notably :secret). But note the
678 exception for :max 0, which see above.
680 The token can hold a :save-function key. If you call that, the
681 user will be prompted to save the data to the backend. You can't
682 request that this should happen right after creation, because
683 `auth-source-search' has no way of knowing if the token is
684 actually useful. So the caller must arrange to call this function.
686 The token's :secret key can hold a function. In that case you
687 must call it to obtain the actual value."
688 (let* ((backends (mapcar 'auth-source-backend-parse auth-sources))
689 (max (or max 1))
690 (ignored-keys '(:require :create :delete :max))
691 (keys (loop for i below (length spec) by 2
692 unless (memq (nth i spec) ignored-keys)
693 collect (nth i spec)))
694 (cached (auth-source-remembered-p spec))
695 ;; note that we may have cached results but found is still nil
696 ;; (there were no results from the search)
697 (found (auth-source-recall spec))
698 filtered-backends accessor-key backend)
700 (if (and cached auth-source-do-cache)
701 (auth-source-do-debug
702 "auth-source-search: found %d CACHED results matching %S"
703 (length found) spec)
705 (assert
706 (or (eq t create) (listp create)) t
707 "Invalid auth-source :create parameter (must be t or a list): %s %s")
709 (assert
710 (listp require) t
711 "Invalid auth-source :require parameter (must be a list): %s")
713 (setq filtered-backends (copy-sequence backends))
714 (dolist (backend backends)
715 (dolist (key keys)
716 ;; ignore invalid slots
717 (condition-case signal
718 (unless (eval `(auth-source-search-collection
719 (plist-get spec key)
720 (oref backend ,key)))
721 (setq filtered-backends (delq backend filtered-backends))
722 (return))
723 (invalid-slot-name))))
725 (auth-source-do-trivia
726 "auth-source-search: found %d backends matching %S"
727 (length filtered-backends) spec)
729 ;; (debug spec "filtered" filtered-backends)
730 ;; First go through all the backends without :create, so we can
731 ;; query them all.
732 (setq found (auth-source-search-backends filtered-backends
733 spec
734 ;; to exit early
736 ;; create is always nil here
737 nil delete
738 require))
740 (auth-source-do-debug
741 "auth-source-search: found %d results (max %d) matching %S"
742 (length found) max spec)
744 ;; If we didn't find anything, then we allow the backend(s) to
745 ;; create the entries.
746 (when (and create
747 (not found))
748 (setq found (auth-source-search-backends filtered-backends
749 spec
750 ;; to exit early
752 create delete
753 require))
754 (auth-source-do-debug
755 "auth-source-search: CREATED %d results (max %d) matching %S"
756 (length found) max spec))
758 ;; note we remember the lack of result too, if it's applicable
759 (when auth-source-do-cache
760 (auth-source-remember spec found)))
762 found))
764 (defun auth-source-search-backends (backends spec max create delete require)
765 (let (matches)
766 (dolist (backend backends)
767 (when (> max (length matches)) ; when we need more matches...
768 (let* ((bmatches (apply
769 (slot-value backend 'search-function)
770 :backend backend
771 :type (slot-value backend :type)
772 ;; note we're overriding whatever the spec
773 ;; has for :require, :create, and :delete
774 :require require
775 :create create
776 :delete delete
777 spec)))
778 (when bmatches
779 (auth-source-do-trivia
780 "auth-source-search-backend: got %d (max %d) in %s:%s matching %S"
781 (length bmatches) max
782 (slot-value backend :type)
783 (slot-value backend :source)
784 spec)
785 (setq matches (append matches bmatches))))))
786 matches))
788 ;; (auth-source-search :max 1)
789 ;; (funcall (plist-get (nth 0 (auth-source-search :max 1)) :secret))
790 ;; (auth-source-search :host "nonesuch" :type 'netrc :K 1)
791 ;; (auth-source-search :host "nonesuch" :type 'secrets)
793 (defun* auth-source-delete (&rest spec
794 &key delete
795 &allow-other-keys)
796 "Delete entries from the authentication backends according to SPEC.
797 Calls `auth-source-search' with the :delete property in SPEC set to t.
798 The backend may not actually delete the entries.
800 Returns the deleted entries."
801 (auth-source-search (plist-put spec :delete t)))
803 (defun auth-source-search-collection (collection value)
804 "Returns t is VALUE is t or COLLECTION is t or contains VALUE."
805 (when (and (atom collection) (not (eq t collection)))
806 (setq collection (list collection)))
808 ;; (debug :collection collection :value value)
809 (or (eq collection t)
810 (eq value t)
811 (equal collection value)
812 (member value collection)))
814 (defvar auth-source-netrc-cache nil)
816 (defun auth-source-forget-all-cached ()
817 "Forget all cached auth-source data."
818 (interactive)
819 (loop for sym being the symbols of password-data
820 ;; when the symbol name starts with auth-source-magic
821 when (string-match (concat "^" auth-source-magic)
822 (symbol-name sym))
823 ;; remove that key
824 do (password-cache-remove (symbol-name sym)))
825 (setq auth-source-netrc-cache nil))
827 (defun auth-source-format-cache-entry (spec)
828 "Format SPEC entry to put it in the password cache."
829 (concat auth-source-magic (format "%S" spec)))
831 (defun auth-source-remember (spec found)
832 "Remember FOUND search results for SPEC."
833 (let ((password-cache-expiry auth-source-cache-expiry))
834 (password-cache-add
835 (auth-source-format-cache-entry spec) found)))
837 (defun auth-source-recall (spec)
838 "Recall FOUND search results for SPEC."
839 (password-read-from-cache (auth-source-format-cache-entry spec)))
841 (defun auth-source-remembered-p (spec)
842 "Check if SPEC is remembered."
843 (password-in-cache-p
844 (auth-source-format-cache-entry spec)))
846 (defun auth-source-forget (spec)
847 "Forget any cached data matching SPEC exactly.
849 This is the same SPEC you passed to `auth-source-search'.
850 Returns t or nil for forgotten or not found."
851 (password-cache-remove (auth-source-format-cache-entry spec)))
853 ;; (loop for sym being the symbols of password-data when (string-match (concat "^" auth-source-magic) (symbol-name sym)) collect (symbol-name sym))
855 ;; (auth-source-remember '(:host "wedd") '(4 5 6))
856 ;; (auth-source-remembered-p '(:host "wedd"))
857 ;; (auth-source-remember '(:host "xedd") '(1 2 3))
858 ;; (auth-source-remembered-p '(:host "xedd"))
859 ;; (auth-source-remembered-p '(:host "zedd"))
860 ;; (auth-source-recall '(:host "xedd"))
861 ;; (auth-source-recall '(:host t))
862 ;; (auth-source-forget+ :host t)
864 (defun* auth-source-forget+ (&rest spec &allow-other-keys)
865 "Forget any cached data matching SPEC. Returns forgotten count.
867 This is not a full `auth-source-search' spec but works similarly.
868 For instance, \(:host \"myhost\" \"yourhost\") would find all the
869 cached data that was found with a search for those two hosts,
870 while \(:host t) would find all host entries."
871 (let ((count 0)
872 sname)
873 (loop for sym being the symbols of password-data
874 ;; when the symbol name matches with auth-source-magic
875 when (and (setq sname (symbol-name sym))
876 (string-match (concat "^" auth-source-magic "\\(.+\\)")
877 sname)
878 ;; and the spec matches what was stored in the cache
879 (auth-source-specmatchp spec (read (match-string 1 sname))))
880 ;; remove that key
881 do (progn
882 (password-cache-remove sname)
883 (incf count)))
884 count))
886 (defun auth-source-specmatchp (spec stored)
887 (let ((keys (loop for i below (length spec) by 2
888 collect (nth i spec))))
889 (not (eq
890 (dolist (key keys)
891 (unless (auth-source-search-collection (plist-get stored key)
892 (plist-get spec key))
893 (return 'no)))
894 'no))))
896 ;; (auth-source-pick-first-password :host "z.lifelogs.com")
897 ;; (auth-source-pick-first-password :port "imap")
898 (defun auth-source-pick-first-password (&rest spec)
899 "Pick the first secret found from applying SPEC to `auth-source-search'."
900 (let* ((result (nth 0 (apply 'auth-source-search (plist-put spec :max 1))))
901 (secret (plist-get result :secret)))
903 (if (functionp secret)
904 (funcall secret)
905 secret)))
907 ;; (auth-source-format-prompt "test %u %h %p" '((?u "user") (?h "host")))
908 (defun auth-source-format-prompt (prompt alist)
909 "Format PROMPT using %x (for any character x) specifiers in ALIST."
910 (dolist (cell alist)
911 (let ((c (nth 0 cell))
912 (v (nth 1 cell)))
913 (when (and c v)
914 (setq prompt (replace-regexp-in-string (format "%%%c" c)
915 (format "%s" v)
916 prompt)))))
917 prompt)
919 (defun auth-source-ensure-strings (values)
920 (unless (listp values)
921 (setq values (list values)))
922 (mapcar (lambda (value)
923 (if (numberp value)
924 (format "%s" value)
925 value))
926 values))
928 ;;; Backend specific parsing: netrc/authinfo backend
930 (defun auth-source--aput-1 (alist key val)
931 (let ((seen ())
932 (rest alist))
933 (while (and (consp rest) (not (equal key (caar rest))))
934 (push (pop rest) seen))
935 (cons (cons key val)
936 (if (null rest) alist
937 (nconc (nreverse seen)
938 (if (equal key (caar rest)) (cdr rest) rest))))))
939 (defmacro auth-source--aput (var key val)
940 `(setq ,var (auth-source--aput-1 ,var ,key ,val)))
942 (defun auth-source--aget (alist key)
943 (cdr (assoc key alist)))
945 ;; (auth-source-netrc-parse "~/.authinfo.gpg")
946 (defun* auth-source-netrc-parse (&rest
947 spec
948 &key file max host user port delete require
949 &allow-other-keys)
950 "Parse FILE and return a list of all entries in the file.
951 Note that the MAX parameter is used so we can exit the parse early."
952 (if (listp file)
953 ;; We got already parsed contents; just return it.
954 file
955 (when (file-exists-p file)
956 (setq port (auth-source-ensure-strings port))
957 (with-temp-buffer
958 (let* ((tokens '("machine" "host" "default" "login" "user"
959 "password" "account" "macdef" "force"
960 "port" "protocol"))
961 (max (or max 5000)) ; sanity check: default to stop at 5K
962 (modified 0)
963 (cached (cdr-safe (assoc file auth-source-netrc-cache)))
964 (cached-mtime (plist-get cached :mtime))
965 (cached-secrets (plist-get cached :secret))
966 alist elem result pair)
968 (if (and (functionp cached-secrets)
969 (equal cached-mtime
970 (nth 5 (file-attributes file))))
971 (progn
972 (auth-source-do-trivia
973 "auth-source-netrc-parse: using CACHED file data for %s"
974 file)
975 (insert (funcall cached-secrets)))
976 (insert-file-contents file)
977 ;; cache all netrc files (used to be just .gpg files)
978 ;; Store the contents of the file heavily encrypted in memory.
979 ;; (note for the irony-impaired: they are just obfuscated)
980 (auth-source--aput
981 auth-source-netrc-cache file
982 (list :mtime (nth 5 (file-attributes file))
983 :secret (lexical-let ((v (mapcar '1+ (buffer-string))))
984 (lambda () (apply 'string (mapcar '1- v)))))))
985 (goto-char (point-min))
986 ;; Go through the file, line by line.
987 (while (and (not (eobp))
988 (> max 0))
990 (narrow-to-region (point) (point-at-eol))
991 ;; For each line, get the tokens and values.
992 (while (not (eobp))
993 (skip-chars-forward "\t ")
994 ;; Skip lines that begin with a "#".
995 (if (eq (char-after) ?#)
996 (goto-char (point-max))
997 (unless (eobp)
998 (setq elem
999 (if (= (following-char) ?\")
1000 (read (current-buffer))
1001 (buffer-substring
1002 (point) (progn (skip-chars-forward "^\t ")
1003 (point)))))
1004 (cond
1005 ((equal elem "macdef")
1006 ;; We skip past the macro definition.
1007 (widen)
1008 (while (and (zerop (forward-line 1))
1009 (looking-at "$")))
1010 (narrow-to-region (point) (point)))
1011 ((member elem tokens)
1012 ;; Tokens that don't have a following value are ignored,
1013 ;; except "default".
1014 (when (and pair (or (cdr pair)
1015 (equal (car pair) "default")))
1016 (push pair alist))
1017 (setq pair (list elem)))
1019 ;; Values that haven't got a preceding token are ignored.
1020 (when pair
1021 (setcdr pair elem)
1022 (push pair alist)
1023 (setq pair nil)))))))
1025 (when (and alist
1026 (> max 0)
1027 (auth-source-search-collection
1028 host
1030 (auth-source--aget alist "machine")
1031 (auth-source--aget alist "host")
1033 (auth-source-search-collection
1034 user
1036 (auth-source--aget alist "login")
1037 (auth-source--aget alist "account")
1038 (auth-source--aget alist "user")
1040 (auth-source-search-collection
1041 port
1043 (auth-source--aget alist "port")
1044 (auth-source--aget alist "protocol")
1047 ;; the required list of keys is nil, or
1048 (null require)
1049 ;; every element of require is in the normalized list
1050 (let ((normalized (nth 0 (auth-source-netrc-normalize
1051 (list alist) file))))
1052 (loop for req in require
1053 always (plist-get normalized req)))))
1054 (decf max)
1055 (push (nreverse alist) result)
1056 ;; to delete a line, we just comment it out
1057 (when delete
1058 (goto-char (point-min))
1059 (insert "#")
1060 (incf modified)))
1061 (setq alist nil
1062 pair nil)
1063 (widen)
1064 (forward-line 1))
1066 (when (< 0 modified)
1067 (when auth-source-gpg-encrypt-to
1068 ;; (see bug#7487) making `epa-file-encrypt-to' local to
1069 ;; this buffer lets epa-file skip the key selection query
1070 ;; (see the `local-variable-p' check in
1071 ;; `epa-file-write-region').
1072 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
1073 (make-local-variable 'epa-file-encrypt-to))
1074 (if (listp auth-source-gpg-encrypt-to)
1075 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
1077 ;; ask AFTER we've successfully opened the file
1078 (when (y-or-n-p (format "Save file %s? (%d deletions)"
1079 file modified))
1080 (write-region (point-min) (point-max) file nil 'silent)
1081 (auth-source-do-debug
1082 "auth-source-netrc-parse: modified %d lines in %s"
1083 modified file)))
1085 (nreverse result))))))
1087 (defvar auth-source-passphrase-alist nil)
1089 (defun auth-source-token-passphrase-callback-function (context key-id file)
1090 (let* ((file (file-truename file))
1091 (entry (assoc file auth-source-passphrase-alist))
1092 passphrase)
1093 ;; return the saved passphrase, calling a function if needed
1094 (or (copy-sequence (if (functionp (cdr entry))
1095 (funcall (cdr entry))
1096 (cdr entry)))
1097 (progn
1098 (unless entry
1099 (setq entry (list file))
1100 (push entry auth-source-passphrase-alist))
1101 (setq passphrase
1102 (read-passwd
1103 (format "Passphrase for %s tokens: " file)
1105 (setcdr entry (lexical-let ((p (copy-sequence passphrase)))
1106 (lambda () p)))
1107 passphrase))))
1109 ;; (auth-source-epa-extract-gpg-token "gpg:LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tClZlcnNpb246IEdudVBHIHYxLjQuMTEgKEdOVS9MaW51eCkKCmpBMEVBd01DT25qMjB1ak9rZnRneVI3K21iNm9aZWhuLzRad3cySkdlbnVaKzRpeEswWDY5di9icDI1U1dsQT0KPS9yc2wKLS0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo=" "~/.netrc")
1110 (defun auth-source-epa-extract-gpg-token (secret file)
1111 "Pass either the decoded SECRET or the gpg:BASE64DATA version.
1112 FILE is the file from which we obtained this token."
1113 (when (string-match "^gpg:\\(.+\\)" secret)
1114 (setq secret (base64-decode-string (match-string 1 secret))))
1115 (let ((context (epg-make-context 'OpenPGP))
1116 plain)
1117 (epg-context-set-passphrase-callback
1118 context
1119 (cons #'auth-source-token-passphrase-callback-function
1120 file))
1121 (epg-decrypt-string context secret)))
1123 ;; (insert (auth-source-epa-make-gpg-token "mysecret" "~/.netrc"))
1124 (defun auth-source-epa-make-gpg-token (secret file)
1125 (let ((context (epg-make-context 'OpenPGP))
1126 (pp-escape-newlines nil)
1127 cipher)
1128 (epg-context-set-armor context t)
1129 (epg-context-set-passphrase-callback
1130 context
1131 (cons #'auth-source-token-passphrase-callback-function
1132 file))
1133 (setq cipher (epg-encrypt-string context secret nil))
1134 (with-temp-buffer
1135 (insert cipher)
1136 (base64-encode-region (point-min) (point-max) t)
1137 (concat "gpg:" (buffer-substring-no-properties
1138 (point-min)
1139 (point-max))))))
1141 (defun auth-source-netrc-normalize (alist filename)
1142 (mapcar (lambda (entry)
1143 (let (ret item)
1144 (while (setq item (pop entry))
1145 (let ((k (car item))
1146 (v (cdr item)))
1148 ;; apply key aliases
1149 (setq k (cond ((member k '("machine")) "host")
1150 ((member k '("login" "account")) "user")
1151 ((member k '("protocol")) "port")
1152 ((member k '("password")) "secret")
1153 (t k)))
1155 ;; send back the secret in a function (lexical binding)
1156 (when (equal k "secret")
1157 (setq v (lexical-let ((lexv v)
1158 (token-decoder nil))
1159 (when (string-match "^gpg:" lexv)
1160 ;; it's a GPG token: create a token decoder
1161 ;; which unsets itself once
1162 (setq token-decoder
1163 (lambda (val)
1164 (prog1
1165 (auth-source-epa-extract-gpg-token
1167 filename)
1168 (setq token-decoder nil)))))
1169 (lambda ()
1170 (when token-decoder
1171 (setq lexv (funcall token-decoder lexv)))
1172 lexv))))
1173 (setq ret (plist-put ret
1174 (intern (concat ":" k))
1175 v))))
1176 ret))
1177 alist))
1179 ;; (setq secret (plist-get (nth 0 (auth-source-search :host t :type 'netrc :K 1 :max 1)) :secret))
1180 ;; (funcall secret)
1182 (defun* auth-source-netrc-search (&rest
1183 spec
1184 &key backend require create delete
1185 type max host user port
1186 &allow-other-keys)
1187 "Given a property list SPEC, return search matches from the :backend.
1188 See `auth-source-search' for details on SPEC."
1189 ;; just in case, check that the type is correct (null or same as the backend)
1190 (assert (or (null type) (eq type (oref backend type)))
1191 t "Invalid netrc search: %s %s")
1193 (let ((results (auth-source-netrc-normalize
1194 (auth-source-netrc-parse
1195 :max max
1196 :require require
1197 :delete delete
1198 :file (oref backend source)
1199 :host (or host t)
1200 :user (or user t)
1201 :port (or port t))
1202 (oref backend source))))
1204 ;; if we need to create an entry AND none were found to match
1205 (when (and create
1206 (not results))
1208 ;; create based on the spec and record the value
1209 (setq results (or
1210 ;; if the user did not want to create the entry
1211 ;; in the file, it will be returned
1212 (apply (slot-value backend 'create-function) spec)
1213 ;; if not, we do the search again without :create
1214 ;; to get the updated data.
1216 ;; the result will be returned, even if the search fails
1217 (apply 'auth-source-netrc-search
1218 (plist-put spec :create nil)))))
1219 results))
1221 (defun auth-source-netrc-element-or-first (v)
1222 (if (listp v)
1223 (nth 0 v)
1226 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
1227 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))
1229 (defun* auth-source-netrc-create (&rest spec
1230 &key backend
1231 secret host user port create
1232 &allow-other-keys)
1233 (let* ((base-required '(host user port secret))
1234 ;; we know (because of an assertion in auth-source-search) that the
1235 ;; :create parameter is either t or a list (which includes nil)
1236 (create-extra (if (eq t create) nil create))
1237 (current-data (car (auth-source-search :max 1
1238 :host host
1239 :port port)))
1240 (required (append base-required create-extra))
1241 (file (oref backend source))
1242 (add "")
1243 ;; `valist' is an alist
1244 valist
1245 ;; `artificial' will be returned if no creation is needed
1246 artificial)
1248 ;; only for base required elements (defined as function parameters):
1249 ;; fill in the valist with whatever data we may have from the search
1250 ;; we complete the first value if it's a list and use the value otherwise
1251 (dolist (br base-required)
1252 (when (symbol-value br)
1253 (let ((br-choice (cond
1254 ;; all-accepting choice (predicate is t)
1255 ((eq t (symbol-value br)) nil)
1256 ;; just the value otherwise
1257 (t (symbol-value br)))))
1258 (when br-choice
1259 (auth-source--aput valist br br-choice)))))
1261 ;; for extra required elements, see if the spec includes a value for them
1262 (dolist (er create-extra)
1263 (let ((name (concat ":" (symbol-name er)))
1264 (keys (loop for i below (length spec) by 2
1265 collect (nth i spec))))
1266 (dolist (k keys)
1267 (when (equal (symbol-name k) name)
1268 (auth-source--aput valist er (plist-get spec k))))))
1270 ;; for each required element
1271 (dolist (r required)
1272 (let* ((data (auth-source--aget valist r))
1273 ;; take the first element if the data is a list
1274 (data (or (auth-source-netrc-element-or-first data)
1275 (plist-get current-data
1276 (intern (format ":%s" r) obarray))))
1277 ;; this is the default to be offered
1278 (given-default (auth-source--aget
1279 auth-source-creation-defaults r))
1280 ;; the default supplementals are simple:
1281 ;; for the user, try `given-default' and then (user-login-name);
1282 ;; otherwise take `given-default'
1283 (default (cond
1284 ((and (not given-default) (eq r 'user))
1285 (user-login-name))
1286 (t given-default)))
1287 (printable-defaults (list
1288 (cons 'user
1290 (auth-source-netrc-element-or-first
1291 (auth-source--aget valist 'user))
1292 (plist-get artificial :user)
1293 "[any user]"))
1294 (cons 'host
1296 (auth-source-netrc-element-or-first
1297 (auth-source--aget valist 'host))
1298 (plist-get artificial :host)
1299 "[any host]"))
1300 (cons 'port
1302 (auth-source-netrc-element-or-first
1303 (auth-source--aget valist 'port))
1304 (plist-get artificial :port)
1305 "[any port]"))))
1306 (prompt (or (auth-source--aget auth-source-creation-prompts r)
1307 (case r
1308 (secret "%p password for %u@%h: ")
1309 (user "%p user name for %h: ")
1310 (host "%p host name for user %u: ")
1311 (port "%p port for %u@%h: "))
1312 (format "Enter %s (%%u@%%h:%%p): " r)))
1313 (prompt (auth-source-format-prompt
1314 prompt
1315 `((?u ,(auth-source--aget printable-defaults 'user))
1316 (?h ,(auth-source--aget printable-defaults 'host))
1317 (?p ,(auth-source--aget printable-defaults 'port))))))
1319 ;; Store the data, prompting for the password if needed.
1320 (setq data (or data
1321 (if (eq r 'secret)
1322 ;; Special case prompt for passwords.
1323 ;; TODO: make the default (setq auth-source-netrc-use-gpg-tokens `((,(if (boundp 'epa-file-auto-mode-alist-entry) (car (symbol-value 'epa-file-auto-mode-alist-entry)) "\\.gpg\\'") nil) (t gpg)))
1324 ;; TODO: or maybe leave as (setq auth-source-netrc-use-gpg-tokens 'never)
1325 (let* ((ep (format "Use GPG password tokens in %s?" file))
1326 (gpg-encrypt
1327 (cond
1328 ((eq auth-source-netrc-use-gpg-tokens 'never)
1329 'never)
1330 ((listp auth-source-netrc-use-gpg-tokens)
1331 (let ((check (copy-sequence
1332 auth-source-netrc-use-gpg-tokens))
1333 item ret)
1334 (while check
1335 (setq item (pop check))
1336 (when (or (eq (car item) t)
1337 (string-match (car item) file))
1338 (setq ret (cdr item))
1339 (setq check nil)))))
1340 (t 'never)))
1341 (plain (or (eval default) (read-passwd prompt))))
1342 ;; ask if we don't know what to do (in which case
1343 ;; auth-source-netrc-use-gpg-tokens must be a list)
1344 (unless gpg-encrypt
1345 (setq gpg-encrypt (if (y-or-n-p ep) 'gpg 'never))
1346 ;; TODO: save the defcustom now? or ask?
1347 (setq auth-source-netrc-use-gpg-tokens
1348 (cons `(,file ,gpg-encrypt)
1349 auth-source-netrc-use-gpg-tokens)))
1350 (if (eq gpg-encrypt 'gpg)
1351 (auth-source-epa-make-gpg-token plain file)
1352 plain))
1353 (if (stringp default)
1354 (read-string (if (string-match ": *\\'" prompt)
1355 (concat (substring prompt 0 (match-beginning 0))
1356 " (default " default "): ")
1357 (concat prompt "(default " default ") "))
1358 nil nil default)
1359 (eval default)))))
1361 (when data
1362 (setq artificial (plist-put artificial
1363 (intern (concat ":" (symbol-name r)))
1364 (if (eq r 'secret)
1365 (lexical-let ((data data))
1366 (lambda () data))
1367 data))))
1369 ;; When r is not an empty string...
1370 (when (and (stringp data)
1371 (< 0 (length data)))
1372 ;; this function is not strictly necessary but I think it
1373 ;; makes the code clearer -tzz
1374 (let ((printer (lambda ()
1375 ;; append the key (the symbol name of r)
1376 ;; and the value in r
1377 (format "%s%s %s"
1378 ;; prepend a space
1379 (if (zerop (length add)) "" " ")
1380 ;; remap auth-source tokens to netrc
1381 (case r
1382 (user "login")
1383 (host "machine")
1384 (secret "password")
1385 (port "port") ; redundant but clearer
1386 (t (symbol-name r)))
1387 (if (string-match "[\"# ]" data)
1388 (format "%S" data)
1389 data)))))
1390 (setq add (concat add (funcall printer)))))))
1392 (plist-put
1393 artificial
1394 :save-function
1395 (lexical-let ((file file)
1396 (add add))
1397 (lambda () (auth-source-netrc-saver file add))))
1399 (list artificial)))
1401 ;;(funcall (plist-get (nth 0 (auth-source-search :host '("nonesuch2") :user "tzz" :port "imap" :create t :max 1)) :save-function))
1402 (defun auth-source-netrc-saver (file add)
1403 "Save a line ADD in FILE, prompting along the way.
1404 Respects `auth-source-save-behavior'. Uses
1405 `auth-source-netrc-cache' to avoid prompting more than once."
1406 (let* ((key (format "%s %s" file (rfc2104-hash 'md5 64 16 file add)))
1407 (cached (assoc key auth-source-netrc-cache)))
1409 (if cached
1410 (auth-source-do-trivia
1411 "auth-source-netrc-saver: found previous run for key %s, returning"
1412 key)
1413 (with-temp-buffer
1414 (when (file-exists-p file)
1415 (insert-file-contents file))
1416 (when auth-source-gpg-encrypt-to
1417 ;; (see bug#7487) making `epa-file-encrypt-to' local to
1418 ;; this buffer lets epa-file skip the key selection query
1419 ;; (see the `local-variable-p' check in
1420 ;; `epa-file-write-region').
1421 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
1422 (make-local-variable 'epa-file-encrypt-to))
1423 (if (listp auth-source-gpg-encrypt-to)
1424 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
1425 ;; we want the new data to be found first, so insert at beginning
1426 (goto-char (point-min))
1428 ;; Ask AFTER we've successfully opened the file.
1429 (let ((prompt (format "Save auth info to file %s? " file))
1430 (done (not (eq auth-source-save-behavior 'ask)))
1431 (bufname "*auth-source Help*")
1433 (while (not done)
1434 (setq k (auth-source-read-char-choice prompt '(?y ?n ?N ?e ??)))
1435 (case k
1436 (?y (setq done t))
1437 (?? (save-excursion
1438 (with-output-to-temp-buffer bufname
1439 (princ
1440 (concat "(y)es, save\n"
1441 "(n)o but use the info\n"
1442 "(N)o and don't ask to save again\n"
1443 "(e)dit the line\n"
1444 "(?) for help as you can see.\n"))
1445 ;; Why? Doesn't with-output-to-temp-buffer already do
1446 ;; the exact same thing anyway? --Stef
1447 (set-buffer standard-output)
1448 (help-mode))))
1449 (?n (setq add ""
1450 done t))
1452 (setq add ""
1453 done t)
1454 (customize-save-variable 'auth-source-save-behavior nil))
1455 (?e (setq add (read-string "Line to add: " add)))
1456 (t nil)))
1458 (when (get-buffer-window bufname)
1459 (delete-window (get-buffer-window bufname)))
1461 ;; Make sure the info is not saved.
1462 (when (null auth-source-save-behavior)
1463 (setq add ""))
1465 (when (< 0 (length add))
1466 (progn
1467 (unless (bolp)
1468 (insert "\n"))
1469 (insert add "\n")
1470 (write-region (point-min) (point-max) file nil 'silent)
1471 ;; Make the .authinfo file non-world-readable.
1472 (set-file-modes file #o600)
1473 (auth-source-do-debug
1474 "auth-source-netrc-create: wrote 1 new line to %s"
1475 file)
1476 (message "Saved new authentication information to %s" file)
1477 nil))))
1478 (auth-source--aput auth-source-netrc-cache key "ran"))))
1480 ;;; Backend specific parsing: Secrets API backend
1482 ;; (let ((auth-sources '(default))) (auth-source-search :max 1 :create t))
1483 ;; (let ((auth-sources '(default))) (auth-source-search :max 1 :delete t))
1484 ;; (let ((auth-sources '(default))) (auth-source-search :max 1))
1485 ;; (let ((auth-sources '(default))) (auth-source-search))
1486 ;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1))
1487 ;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1 :signon_realm "https://git.gnus.org/Git"))
1489 (defun* auth-source-secrets-search (&rest
1490 spec
1491 &key backend create delete label
1492 type max host user port
1493 &allow-other-keys)
1494 "Search the Secrets API; spec is like `auth-source'.
1496 The :label key specifies the item's label. It is the only key
1497 that can specify a substring. Any :label value besides a string
1498 will allow any label.
1500 All other search keys must match exactly. If you need substring
1501 matching, do a wider search and narrow it down yourself.
1503 You'll get back all the properties of the token as a plist.
1505 Here's an example that looks for the first item in the 'Login'
1506 Secrets collection:
1508 \(let ((auth-sources '(\"secrets:Login\")))
1509 (auth-source-search :max 1)
1511 Here's another that looks for the first item in the 'Login'
1512 Secrets collection whose label contains 'gnus':
1514 \(let ((auth-sources '(\"secrets:Login\")))
1515 (auth-source-search :max 1 :label \"gnus\")
1517 And this one looks for the first item in the 'Login' Secrets
1518 collection that's a Google Chrome entry for the git.gnus.org site
1519 authentication tokens:
1521 \(let ((auth-sources '(\"secrets:Login\")))
1522 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1525 ;; TODO
1526 (assert (not create) nil
1527 "The Secrets API auth-source backend doesn't support creation yet")
1528 ;; TODO
1529 ;; (secrets-delete-item coll elt)
1530 (assert (not delete) nil
1531 "The Secrets API auth-source backend doesn't support deletion yet")
1533 (let* ((coll (oref backend source))
1534 (max (or max 5000)) ; sanity check: default to stop at 5K
1535 (ignored-keys '(:create :delete :max :backend :label :require :type))
1536 (search-keys (loop for i below (length spec) by 2
1537 unless (memq (nth i spec) ignored-keys)
1538 collect (nth i spec)))
1539 ;; build a search spec without the ignored keys
1540 ;; if a search key is nil or t (match anything), we skip it
1541 (search-spec (apply 'append (mapcar
1542 (lambda (k)
1543 (if (or (null (plist-get spec k))
1544 (eq t (plist-get spec k)))
1546 (list k (plist-get spec k))))
1547 search-keys)))
1548 ;; needed keys (always including host, login, port, and secret)
1549 (returned-keys (mm-delete-duplicates (append
1550 '(:host :login :port :secret)
1551 search-keys)))
1552 (items (loop for item in (apply 'secrets-search-items coll search-spec)
1553 unless (and (stringp label)
1554 (not (string-match label item)))
1555 collect item))
1556 ;; TODO: respect max in `secrets-search-items', not after the fact
1557 (items (butlast items (- (length items) max)))
1558 ;; convert the item name to a full plist
1559 (items (mapcar (lambda (item)
1560 (append
1561 ;; make an entry for the secret (password) element
1562 (list
1563 :secret
1564 (lexical-let ((v (secrets-get-secret coll item)))
1565 (lambda () v)))
1566 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1567 (apply 'append
1568 (mapcar (lambda (entry)
1569 (list (car entry) (cdr entry)))
1570 (secrets-get-attributes coll item)))))
1571 items))
1572 ;; ensure each item has each key in `returned-keys'
1573 (items (mapcar (lambda (plist)
1574 (append
1575 (apply 'append
1576 (mapcar (lambda (req)
1577 (if (plist-get plist req)
1579 (list req nil)))
1580 returned-keys))
1581 plist))
1582 items)))
1583 items))
1585 (defun* auth-source-secrets-create (&rest
1586 spec
1587 &key backend type max host user port
1588 &allow-other-keys)
1589 ;; TODO
1590 ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1591 (debug spec))
1593 ;;; Backend specific parsing: Mac OS Keychain (using /usr/bin/security) backend
1595 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search :max 1 :create t))
1596 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search :max 1 :delete t))
1597 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search :max 1))
1598 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search))
1600 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search :max 1 :create t))
1601 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search :max 1 :delete t))
1602 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search :max 1))
1603 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search))
1605 ;; (let ((auth-sources '("macos-keychain-internet:/Users/tzz/Library/Keychains/login.keychain"))) (auth-source-search :max 1))
1606 ;; (let ((auth-sources '("macos-keychain-generic:Login"))) (auth-source-search :max 1 :host "git.gnus.org"))
1608 (defun* auth-source-macos-keychain-search (&rest
1609 spec
1610 &key backend create delete label
1611 type max host user port
1612 &allow-other-keys)
1613 "Search the MacOS Keychain; spec is like `auth-source'.
1615 All search keys must match exactly. If you need substring
1616 matching, do a wider search and narrow it down yourself.
1618 You'll get back all the properties of the token as a plist.
1620 The :type key is either 'macos-keychain-internet or
1621 'macos-keychain-generic.
1623 For the internet keychain type, the :label key searches the
1624 item's labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1625 Similarly, :host maps to \"-s HOST\", :user maps to \"-a USER\",
1626 and :port maps to \"-P PORT\" or \"-r PROT\"
1627 (note PROT has to be a 4-character string).
1629 For the generic keychain type, the :label key searches the item's
1630 labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1631 Similarly, :host maps to \"-c HOST\" (the \"creator\" keychain
1632 field), :user maps to \"-a USER\", and :port maps to \"-s PORT\".
1634 Here's an example that looks for the first item in the default
1635 generic MacOS Keychain:
1637 \(let ((auth-sources '(macos-keychain-generic)))
1638 (auth-source-search :max 1)
1640 Here's another that looks for the first item in the internet
1641 MacOS Keychain collection whose label is 'gnus':
1643 \(let ((auth-sources '(macos-keychain-internet)))
1644 (auth-source-search :max 1 :label \"gnus\")
1646 And this one looks for the first item in the internet keychain
1647 entries for git.gnus.org:
1649 \(let ((auth-sources '(macos-keychain-internet\")))
1650 (auth-source-search :max 1 :host \"git.gnus.org\"))
1652 ;; TODO
1653 (assert (not create) nil
1654 "The MacOS Keychain auth-source backend doesn't support creation yet")
1655 ;; TODO
1656 ;; (macos-keychain-delete-item coll elt)
1657 (assert (not delete) nil
1658 "The MacOS Keychain auth-source backend doesn't support deletion yet")
1660 (let* ((coll (oref backend source))
1661 (max (or max 5000)) ; sanity check: default to stop at 5K
1662 (ignored-keys '(:create :delete :max :backend :label))
1663 (search-keys (loop for i below (length spec) by 2
1664 unless (memq (nth i spec) ignored-keys)
1665 collect (nth i spec)))
1666 ;; build a search spec without the ignored keys
1667 ;; if a search key is nil or t (match anything), we skip it
1668 (search-spec (apply 'append (mapcar
1669 (lambda (k)
1670 (if (or (null (plist-get spec k))
1671 (eq t (plist-get spec k)))
1673 (list k (plist-get spec k))))
1674 search-keys)))
1675 ;; needed keys (always including host, login, port, and secret)
1676 (returned-keys (mm-delete-duplicates (append
1677 '(:host :login :port :secret)
1678 search-keys)))
1679 (items (apply 'auth-source-macos-keychain-search-items
1680 coll
1681 type
1683 search-spec))
1685 ;; ensure each item has each key in `returned-keys'
1686 (items (mapcar (lambda (plist)
1687 (append
1688 (apply 'append
1689 (mapcar (lambda (req)
1690 (if (plist-get plist req)
1692 (list req nil)))
1693 returned-keys))
1694 plist))
1695 items)))
1696 items))
1698 (defun* auth-source-macos-keychain-search-items (coll type max
1699 &rest spec
1700 &key label type
1701 host user port
1702 &allow-other-keys)
1704 (let* ((keychain-generic (eq type 'macos-keychain-generic))
1705 (args `(,(if keychain-generic
1706 "find-generic-password"
1707 "find-internet-password")
1708 "-g"))
1709 (ret (list :type type)))
1710 (when label
1711 (setq args (append args (list "-l" label))))
1712 (when host
1713 (setq args (append args (list (if keychain-generic "-c" "-s") host))))
1714 (when user
1715 (setq args (append args (list "-a" user))))
1717 (when port
1718 (if keychain-generic
1719 (setq args (append args (list "-s" port)))
1720 (setq args (append args (list
1721 (if (string-match "[0-9]+" port) "-P" "-r")
1722 port)))))
1724 (unless (equal coll "default")
1725 (setq args (append args (list coll))))
1727 (with-temp-buffer
1728 (apply 'call-process "/usr/bin/security" nil t nil args)
1729 (goto-char (point-min))
1730 (while (not (eobp))
1731 (cond
1732 ((looking-at "^password: \"\\(.+\\)\"$")
1733 (auth-source-macos-keychain-result-append
1735 keychain-generic
1736 "secret"
1737 (lexical-let ((v (match-string 1)))
1738 (lambda () v))))
1739 ;; TODO: check if this is really the label
1740 ;; match 0x00000007 <blob>="AppleID"
1741 ((looking-at "^[ ]+0x00000007 <blob>=\"\\(.+\\)\"")
1742 (auth-source-macos-keychain-result-append
1744 keychain-generic
1745 "label"
1746 (match-string 1)))
1747 ;; match "crtr"<uint32>="aapl"
1748 ;; match "svce"<blob>="AppleID"
1749 ((looking-at "^[ ]+\"\\([a-z]+\\)\"[^=]+=\"\\(.+\\)\"")
1750 (auth-source-macos-keychain-result-append
1752 keychain-generic
1753 (match-string 1)
1754 (match-string 2))))
1755 (forward-line)))
1756 ;; return `ret' iff it has the :secret key
1757 (and (plist-get ret :secret) (list ret))))
1759 (defun auth-source-macos-keychain-result-append (result generic k v)
1760 (push v result)
1761 (setq k (cond
1762 ((equal k "acct") "user")
1763 ;; for generic keychains, creator is host, service is port
1764 ((and generic (equal k "crtr")) "host")
1765 ((and generic (equal k "svce")) "port")
1766 ;; for internet keychains, protocol is port, server is host
1767 ((and (not generic) (equal k "ptcl")) "port")
1768 ((and (not generic) (equal k "srvr")) "host")
1769 (t k)))
1771 (push (intern (format ":%s" k)) result))
1773 (defun* auth-source-macos-keychain-create (&rest
1774 spec
1775 &key backend type max host user port
1776 &allow-other-keys)
1777 ;; TODO
1778 (debug spec))
1780 ;;; Backend specific parsing: PLSTORE backend
1782 (defun* auth-source-plstore-search (&rest
1783 spec
1784 &key backend create delete label
1785 type max host user port
1786 &allow-other-keys)
1787 "Search the PLSTORE; spec is like `auth-source'."
1788 (let* ((store (oref backend data))
1789 (max (or max 5000)) ; sanity check: default to stop at 5K
1790 (ignored-keys '(:create :delete :max :backend :label :require :type))
1791 (search-keys (loop for i below (length spec) by 2
1792 unless (memq (nth i spec) ignored-keys)
1793 collect (nth i spec)))
1794 ;; build a search spec without the ignored keys
1795 ;; if a search key is nil or t (match anything), we skip it
1796 (search-spec (apply 'append (mapcar
1797 (lambda (k)
1798 (let ((v (plist-get spec k)))
1799 (if (or (null v)
1800 (eq t v))
1802 (if (stringp v)
1803 (setq v (list v)))
1804 (list k v))))
1805 search-keys)))
1806 ;; needed keys (always including host, login, port, and secret)
1807 (returned-keys (mm-delete-duplicates (append
1808 '(:host :login :port :secret)
1809 search-keys)))
1810 (items (plstore-find store search-spec))
1811 (item-names (mapcar #'car items))
1812 (items (butlast items (- (length items) max)))
1813 ;; convert the item to a full plist
1814 (items (mapcar (lambda (item)
1815 (let* ((plist (copy-tree (cdr item)))
1816 (secret (plist-member plist :secret)))
1817 (if secret
1818 (setcar
1819 (cdr secret)
1820 (lexical-let ((v (car (cdr secret))))
1821 (lambda () v))))
1822 plist))
1823 items))
1824 ;; ensure each item has each key in `returned-keys'
1825 (items (mapcar (lambda (plist)
1826 (append
1827 (apply 'append
1828 (mapcar (lambda (req)
1829 (if (plist-get plist req)
1831 (list req nil)))
1832 returned-keys))
1833 plist))
1834 items)))
1835 (cond
1836 ;; if we need to create an entry AND none were found to match
1837 ((and create
1838 (not items))
1840 ;; create based on the spec and record the value
1841 (setq items (or
1842 ;; if the user did not want to create the entry
1843 ;; in the file, it will be returned
1844 (apply (slot-value backend 'create-function) spec)
1845 ;; if not, we do the search again without :create
1846 ;; to get the updated data.
1848 ;; the result will be returned, even if the search fails
1849 (apply 'auth-source-plstore-search
1850 (plist-put spec :create nil)))))
1851 ((and delete
1852 item-names)
1853 (dolist (item-name item-names)
1854 (plstore-delete store item-name))
1855 (plstore-save store)))
1856 items))
1858 (defun* auth-source-plstore-create (&rest spec
1859 &key backend
1860 secret host user port create
1861 &allow-other-keys)
1862 (let* ((base-required '(host user port secret))
1863 (base-secret '(secret))
1864 ;; we know (because of an assertion in auth-source-search) that the
1865 ;; :create parameter is either t or a list (which includes nil)
1866 (create-extra (if (eq t create) nil create))
1867 (current-data (car (auth-source-search :max 1
1868 :host host
1869 :port port)))
1870 (required (append base-required create-extra))
1871 (file (oref backend source))
1872 (add "")
1873 ;; `valist' is an alist
1874 valist
1875 ;; `artificial' will be returned if no creation is needed
1876 artificial
1877 secret-artificial)
1879 ;; only for base required elements (defined as function parameters):
1880 ;; fill in the valist with whatever data we may have from the search
1881 ;; we complete the first value if it's a list and use the value otherwise
1882 (dolist (br base-required)
1883 (when (symbol-value br)
1884 (let ((br-choice (cond
1885 ;; all-accepting choice (predicate is t)
1886 ((eq t (symbol-value br)) nil)
1887 ;; just the value otherwise
1888 (t (symbol-value br)))))
1889 (when br-choice
1890 (auth-source--aput valist br br-choice)))))
1892 ;; for extra required elements, see if the spec includes a value for them
1893 (dolist (er create-extra)
1894 (let ((name (concat ":" (symbol-name er)))
1895 (keys (loop for i below (length spec) by 2
1896 collect (nth i spec))))
1897 (dolist (k keys)
1898 (when (equal (symbol-name k) name)
1899 (auth-source--aput valist er (plist-get spec k))))))
1901 ;; for each required element
1902 (dolist (r required)
1903 (let* ((data (auth-source--aget valist r))
1904 ;; take the first element if the data is a list
1905 (data (or (auth-source-netrc-element-or-first data)
1906 (plist-get current-data
1907 (intern (format ":%s" r) obarray))))
1908 ;; this is the default to be offered
1909 (given-default (auth-source--aget
1910 auth-source-creation-defaults r))
1911 ;; the default supplementals are simple:
1912 ;; for the user, try `given-default' and then (user-login-name);
1913 ;; otherwise take `given-default'
1914 (default (cond
1915 ((and (not given-default) (eq r 'user))
1916 (user-login-name))
1917 (t given-default)))
1918 (printable-defaults (list
1919 (cons 'user
1921 (auth-source-netrc-element-or-first
1922 (auth-source--aget valist 'user))
1923 (plist-get artificial :user)
1924 "[any user]"))
1925 (cons 'host
1927 (auth-source-netrc-element-or-first
1928 (auth-source--aget valist 'host))
1929 (plist-get artificial :host)
1930 "[any host]"))
1931 (cons 'port
1933 (auth-source-netrc-element-or-first
1934 (auth-source--aget valist 'port))
1935 (plist-get artificial :port)
1936 "[any port]"))))
1937 (prompt (or (auth-source--aget auth-source-creation-prompts r)
1938 (case r
1939 (secret "%p password for %u@%h: ")
1940 (user "%p user name for %h: ")
1941 (host "%p host name for user %u: ")
1942 (port "%p port for %u@%h: "))
1943 (format "Enter %s (%%u@%%h:%%p): " r)))
1944 (prompt (auth-source-format-prompt
1945 prompt
1946 `((?u ,(auth-source--aget printable-defaults 'user))
1947 (?h ,(auth-source--aget printable-defaults 'host))
1948 (?p ,(auth-source--aget printable-defaults 'port))))))
1950 ;; Store the data, prompting for the password if needed.
1951 (setq data (or data
1952 (if (eq r 'secret)
1953 (or (eval default) (read-passwd prompt))
1954 (if (stringp default)
1955 (read-string
1956 (if (string-match ": *\\'" prompt)
1957 (concat (substring prompt 0 (match-beginning 0))
1958 " (default " default "): ")
1959 (concat prompt "(default " default ") "))
1960 nil nil default)
1961 (eval default)))))
1963 (when data
1964 (if (member r base-secret)
1965 (setq secret-artificial
1966 (plist-put secret-artificial
1967 (intern (concat ":" (symbol-name r)))
1968 data))
1969 (setq artificial (plist-put artificial
1970 (intern (concat ":" (symbol-name r)))
1971 data))))))
1972 (plstore-put (oref backend data)
1973 (sha1 (format "%s@%s:%s"
1974 (plist-get artificial :user)
1975 (plist-get artificial :host)
1976 (plist-get artificial :port)))
1977 artificial secret-artificial)
1978 (if (y-or-n-p (format "Save auth info to file %s? "
1979 (plstore-get-file (oref backend data))))
1980 (plstore-save (oref backend data)))))
1982 ;;; older API
1984 ;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
1986 ;; deprecate the old interface
1987 (make-obsolete 'auth-source-user-or-password
1988 'auth-source-search "Emacs 24.1")
1989 (make-obsolete 'auth-source-forget-user-or-password
1990 'auth-source-forget "Emacs 24.1")
1992 (defun auth-source-user-or-password
1993 (mode host port &optional username create-missing delete-existing)
1994 "Find MODE (string or list of strings) matching HOST and PORT.
1996 DEPRECATED in favor of `auth-source-search'!
1998 USERNAME is optional and will be used as \"login\" in a search
1999 across the Secret Service API (see secrets.el) if the resulting
2000 items don't have a username. This means that if you search for
2001 username \"joe\" and it matches an item but the item doesn't have
2002 a :user attribute, the username \"joe\" will be returned.
2004 A non nil DELETE-EXISTING means deleting any matching password
2005 entry in the respective sources. This is useful only when
2006 CREATE-MISSING is non nil as well; the intended use case is to
2007 remove wrong password entries.
2009 If no matching entry is found, and CREATE-MISSING is non nil,
2010 the password will be retrieved interactively, and it will be
2011 stored in the password database which matches best (see
2012 `auth-sources').
2014 MODE can be \"login\" or \"password\"."
2015 (auth-source-do-debug
2016 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
2017 mode host port username)
2019 (let* ((listy (listp mode))
2020 (mode (if listy mode (list mode)))
2021 (cname (if username
2022 (format "%s %s:%s %s" mode host port username)
2023 (format "%s %s:%s" mode host port)))
2024 (search (list :host host :port port))
2025 (search (if username (append search (list :user username)) search))
2026 (search (if create-missing
2027 (append search (list :create t))
2028 search))
2029 (search (if delete-existing
2030 (append search (list :delete t))
2031 search))
2032 ;; (found (if (not delete-existing)
2033 ;; (gethash cname auth-source-cache)
2034 ;; (remhash cname auth-source-cache)
2035 ;; nil)))
2036 (found nil))
2037 (if found
2038 (progn
2039 (auth-source-do-debug
2040 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
2041 mode
2042 ;; don't show the password
2043 (if (and (member "password" mode) t)
2044 "SECRET"
2045 found)
2046 host port username)
2047 found) ; return the found data
2048 ;; else, if not found, search with a max of 1
2049 (let ((choice (nth 0 (apply 'auth-source-search
2050 (append '(:max 1) search)))))
2051 (when choice
2052 (dolist (m mode)
2053 (cond
2054 ((equal "password" m)
2055 (push (if (plist-get choice :secret)
2056 (funcall (plist-get choice :secret))
2057 nil) found))
2058 ((equal "login" m)
2059 (push (plist-get choice :user) found)))))
2060 (setq found (nreverse found))
2061 (setq found (if listy found (car-safe found)))))
2063 found))
2065 (defun auth-source-user-and-password (host &optional user)
2066 (let* ((auth-info (car
2067 (if user
2068 (auth-source-search
2069 :host host
2070 :user "yourusername"
2071 :max 1
2072 :require '(:user :secret)
2073 :create nil)
2074 (auth-source-search
2075 :host host
2076 :max 1
2077 :require '(:user :secret)
2078 :create nil))))
2079 (user (plist-get auth-info :user))
2080 (password (plist-get auth-info :secret)))
2081 (when (functionp password)
2082 (setq password (funcall password)))
2083 (list user password auth-info)))
2085 (provide 'auth-source)
2087 ;;; auth-source.el ends here