aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--etc/NEWS3
-rw-r--r--lisp/auth-source.el36
-rw-r--r--lisp/password-cache.el30
-rw-r--r--test/lisp/auth-source-tests.el2
4 files changed, 34 insertions, 37 deletions
diff --git a/etc/NEWS b/etc/NEWS
index ef4c125ab16..a785c6a86b2 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1084,6 +1084,9 @@ fontification, and commenting for embedded JavaScript and CSS.
1084 1084
1085* Incompatible Lisp Changes in Emacs 26.1 1085* Incompatible Lisp Changes in Emacs 26.1
1086 1086
1087*** password-data is now a hash-table
1088so that `password-read' can use any object for the `key' argument.
1089
1087+++ 1090+++
1088*** Command 'dired-mark-extension' now automatically prepends a '.' to the 1091*** Command 'dired-mark-extension' now automatically prepends a '.' to the
1089extension when not present. The new command 'dired-mark-suffix' behaves 1092extension when not present. The new command 'dired-mark-suffix' behaves
diff --git a/lisp/auth-source.el b/lisp/auth-source.el
index d1747bda3da..d4b44a59529 100644
--- a/lisp/auth-source.el
+++ b/lisp/auth-source.el
@@ -200,8 +200,6 @@ Note that if EPA/EPG is not available, this should NOT be used."
200 (const :tag "Save GPG-encrypted password tokens" gpg) 200 (const :tag "Save GPG-encrypted password tokens" gpg)
201 (const :tag "Don't encrypt tokens" never)))))) 201 (const :tag "Don't encrypt tokens" never))))))
202 202
203(defvar auth-source-magic "auth-source-magic ")
204
205(defcustom auth-source-do-cache t 203(defcustom auth-source-do-cache t
206 "Whether auth-source should cache information with `password-cache'." 204 "Whether auth-source should cache information with `password-cache'."
207 :group 'auth-source 205 :group 'auth-source
@@ -782,16 +780,16 @@ Returns the deleted entries."
782(defun auth-source-forget-all-cached () 780(defun auth-source-forget-all-cached ()
783 "Forget all cached auth-source data." 781 "Forget all cached auth-source data."
784 (interactive) 782 (interactive)
785 (cl-do-symbols (sym password-data) 783 (maphash (lambda (key _password)
786 ;; when the symbol name starts with auth-source-magic 784 (when (eq 'auth-source (car-safe key))
787 (when (string-match (concat "^" auth-source-magic) (symbol-name sym)) 785 ;; remove that key
788 ;; remove that key 786 (password-cache-remove key)))
789 (password-cache-remove (symbol-name sym)))) 787 password-data)
790 (setq auth-source-netrc-cache nil)) 788 (setq auth-source-netrc-cache nil))
791 789
792(defun auth-source-format-cache-entry (spec) 790(defun auth-source-format-cache-entry (spec)
793 "Format SPEC entry to put it in the password cache." 791 "Format SPEC entry to put it in the password cache."
794 (concat auth-source-magic (format "%S" spec))) 792 `(auth-source . ,spec))
795 793
796(defun auth-source-remember (spec found) 794(defun auth-source-remember (spec found)
797 "Remember FOUND search results for SPEC." 795 "Remember FOUND search results for SPEC."
@@ -822,18 +820,16 @@ This is not a full `auth-source-search' spec but works similarly.
822For instance, \(:host \"myhost\" \"yourhost\") would find all the 820For instance, \(:host \"myhost\" \"yourhost\") would find all the
823cached data that was found with a search for those two hosts, 821cached data that was found with a search for those two hosts,
824while \(:host t) would find all host entries." 822while \(:host t) would find all host entries."
825 (let ((count 0) 823 (let ((count 0))
826 sname) 824 (maphash
827 (cl-do-symbols (sym password-data) 825 (lambda (key _password)
828 ;; when the symbol name matches with auth-source-magic 826 (when (and (eq 'auth-source (car-safe key))
829 (when (and (setq sname (symbol-name sym)) 827 ;; and the spec matches what was stored in the cache
830 (string-match (concat "^" auth-source-magic "\\(.+\\)") 828 (auth-source-specmatchp spec (cdr key)))
831 sname) 829 ;; remove that key
832 ;; and the spec matches what was stored in the cache 830 (password-cache-remove key)
833 (auth-source-specmatchp spec (read (match-string 1 sname)))) 831 (cl-incf count)))
834 ;; remove that key 832 password-data)
835 (password-cache-remove sname)
836 (cl-incf count)))
837 count)) 833 count))
838 834
839(defun auth-source-specmatchp (spec stored) 835(defun auth-source-specmatchp (spec stored)
diff --git a/lisp/password-cache.el b/lisp/password-cache.el
index 7be3c6fdb6f..cbc248b9ecf 100644
--- a/lisp/password-cache.el
+++ b/lisp/password-cache.el
@@ -66,7 +66,7 @@ Whether passwords are cached at all is controlled by `password-cache'."
66 :type '(choice (const :tag "Never" nil) 66 :type '(choice (const :tag "Never" nil)
67 (integer :tag "Seconds"))) 67 (integer :tag "Seconds")))
68 68
69(defvar password-data (make-vector 7 0)) 69(defvar password-data (make-hash-table :test #'equal))
70 70
71(defun password-read-from-cache (key) 71(defun password-read-from-cache (key)
72 "Obtain passphrase for KEY from time-limited passphrase cache. 72 "Obtain passphrase for KEY from time-limited passphrase cache.
@@ -74,20 +74,20 @@ Custom variables `password-cache' and `password-cache-expiry'
74regulate cache behavior." 74regulate cache behavior."
75 (and password-cache 75 (and password-cache
76 key 76 key
77 (symbol-value (intern-soft key password-data)))) 77 (gethash key password-data)))
78 78
79;;;###autoload 79;;;###autoload
80(defun password-in-cache-p (key) 80(defun password-in-cache-p (key)
81 "Check if KEY is in the cache." 81 "Check if KEY is in the cache."
82 (and password-cache 82 (and password-cache
83 key 83 key
84 (intern-soft key password-data))) 84 (gethash key password-data)))
85 85
86(defun password-read (prompt &optional key) 86(defun password-read (prompt &optional key)
87 "Read password, for use with KEY, from user, or from cache if wanted. 87 "Read password, for use with KEY, from user, or from cache if wanted.
88KEY indicate the purpose of the password, so the cache can 88KEY indicate the purpose of the password, so the cache can
89separate passwords. The cache is not used if KEY is nil. It is 89separate passwords. The cache is not used if KEY is nil.
90typically a string. 90KEY is typically a string but can be anything (compared via `equal').
91The variable `password-cache' control whether the cache is used." 91The variable `password-cache' control whether the cache is used."
92 (or (password-read-from-cache key) 92 (or (password-read-from-cache key)
93 (read-passwd prompt))) 93 (read-passwd prompt)))
@@ -115,29 +115,27 @@ but can be invoked at any time to forcefully remove passwords
115from the cache. This may be useful when it has been detected 115from the cache. This may be useful when it has been detected
116that a password is invalid, so that `password-read' query the 116that a password is invalid, so that `password-read' query the
117user again." 117user again."
118 (let ((sym (intern-soft key password-data))) 118 (let ((password (gethash key password-data)))
119 (when sym 119 (when (stringp password)
120 (let ((password (symbol-value sym))) 120 (if (fboundp 'clear-string)
121 (when (stringp password) 121 (clear-string password)
122 (if (fboundp 'clear-string) 122 (fillarray password ?_)))
123 (clear-string password) 123 (remhash key password-data)))
124 (fillarray password ?_)))
125 (unintern key password-data)))))
126 124
127(defun password-cache-add (key password) 125(defun password-cache-add (key password)
128 "Add password to cache. 126 "Add password to cache.
129The password is removed by a timer after `password-cache-expiry' seconds." 127The password is removed by a timer after `password-cache-expiry' seconds."
130 (when (and password-cache-expiry (null (intern-soft key password-data))) 128 (when (and password-cache-expiry (null (gethash key password-data)))
131 (run-at-time password-cache-expiry nil 129 (run-at-time password-cache-expiry nil
132 #'password-cache-remove 130 #'password-cache-remove
133 key)) 131 key))
134 (set (intern key password-data) password) 132 (puthash key password password-data)
135 nil) 133 nil)
136 134
137(defun password-reset () 135(defun password-reset ()
138 "Clear the password cache." 136 "Clear the password cache."
139 (interactive) 137 (interactive)
140 (fillarray password-data 0)) 138 (clrhash password-data))
141 139
142(provide 'password-cache) 140(provide 'password-cache)
143 141
diff --git a/test/lisp/auth-source-tests.el b/test/lisp/auth-source-tests.el
index 2634777c7db..9753029f198 100644
--- a/test/lisp/auth-source-tests.el
+++ b/test/lisp/auth-source-tests.el
@@ -215,7 +215,7 @@
215 215
216(ert-deftest auth-source-test-remembrances-of-things-past () 216(ert-deftest auth-source-test-remembrances-of-things-past ()
217 (let ((password-cache t) 217 (let ((password-cache t)
218 (password-data (make-vector 7 0))) 218 (password-data (copy-hash-table password-data)))
219 (auth-source-remember '(:host "wedd") '(4 5 6)) 219 (auth-source-remember '(:host "wedd") '(4 5 6))
220 (should (auth-source-remembered-p '(:host "wedd"))) 220 (should (auth-source-remembered-p '(:host "wedd")))
221 (should-not (auth-source-remembered-p '(:host "xedd"))) 221 (should-not (auth-source-remembered-p '(:host "xedd")))