1 ;;; password.el --- Read passwords from user, possibly using a password cache.
3 ;; Copyright (C) 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: password cache passphrase key
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Greatly influenced by pgg.el written by Daiki Ueno, with timer
29 ;; fixes for XEmacs by Katsumi Yamaoka. In fact, this is mostly just
32 ;; (password-read "Password? " "test")
33 ;; ;; Minibuffer prompt for password.
36 ;; (password-cache-add "test" "foo")
39 ;; Note the previous two can be replaced with:
40 ;; (password-read-and-add "Password? " "test")
41 ;; ;; Minibuffer prompt for password.
43 ;; ;; "foo" is now cached with key "test"
46 ;; (password-read "Password? " "test")
47 ;; ;; No minibuffer prompt
50 ;; (password-read "Password? " "test")
51 ;; ;; No minibuffer prompt
54 ;; ;; Wait `password-cache-expiry' seconds.
56 ;; (password-read "Password? " "test")
57 ;; ;; Minibuffer prompt for password is back.
62 (when (featurep 'xemacs)
63 (require 'run-at-time))
68 (defcustom password-cache t
69 "Whether to cache passwords."
73 (defcustom password-cache-expiry 16
74 "How many seconds passwords are cached, or nil to disable expiring.
75 Whether passwords are cached at all is controlled by `password-cache'."
77 :type '(choice (const :tag "Never" nil)
78 (integer :tag "Seconds")))
80 (defvar password-data (make-vector 7 0))
82 (defun password-read (prompt &optional key)
83 "Read password, for use with KEY, from user, or from cache if wanted.
84 KEY indicate the purpose of the password, so the cache can
85 separate passwords. The cache is not used if KEY is nil. It is
87 The variable `password-cache' control whether the cache is used."
88 (or (and password-cache
90 (symbol-value (intern-soft key password-data)))
91 (read-passwd prompt)))
93 (defun password-read-and-add (prompt &optional key)
94 "Read password, for use with KEY, from user, or from cache if wanted.
95 Then store the password in the cache. Uses `password-read' and
96 `password-cache-add'."
97 (let ((password (password-read prompt key)))
98 (when (and password key)
99 (password-cache-add key password))
102 (defun password-cache-remove (key)
103 "Remove password indexed by KEY from password cache.
104 This is typically run be a timer setup from `password-cache-add',
105 but can be invoked at any time to forcefully remove passwords
106 from the cache. This may be useful when it has been detected
107 that a password is invalid, so that `password-read' query the
109 (let ((password (symbol-value (intern-soft key password-data))))
111 (fillarray password ?_)
112 (unintern key password-data))))
114 (defun password-cache-add (key password)
115 "Add password to cache.
116 The password is removed by a timer after `password-cache-expiry'
118 (set (intern key password-data) password)
119 (when password-cache-expiry
120 (run-at-time password-cache-expiry nil
121 #'password-cache-remove
127 ;;; password.el ends here