1 ;;; password.el --- Read passwords from user, possibly using a password cache.
3 ;; Copyright (C) 1999, 2000, 2003 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 ;; (password-read "Password? " "test")
40 ;; ;; No minibuffer prompt
43 ;; (password-read "Password? " "test")
44 ;; ;; No minibuffer prompt
47 ;; ;; Wait `password-cache-expiry' seconds.
49 ;; (password-read "Password? " "test")
50 ;; ;; Minibuffer prompt for password is back.
55 (if (featurep 'xemacs)
56 (require 'run-at-time)
57 (autoload 'run-at-time "timer"))
62 (defcustom password-cache t
63 "Whether to cache passwords."
67 (defcustom password-cache-expiry 16
68 "How many seconds passwords are cached, or nil to disable expiring.
69 Whether passwords are cached at all is controlled by `password-cache'."
71 :type '(choice (const :tag "Never" nil)
72 (integer :tag "Seconds")))
74 (defvar password-data (make-vector 7 0))
76 (defun password-read (prompt &optional key)
77 "Read password, for use with KEY, from user, or from cache if wanted.
78 KEY indicate the purpose of the password, so the cache can
79 separate passwords. The cache is not used if KEY is nil. It is
81 The variable `password-cache' control whether the cache is used."
82 (or (and password-cache
84 (symbol-value (intern-soft key password-data)))
85 (read-passwd prompt)))
87 (defun password-cache-remove (key)
88 "Remove password indexed by KEY from password cache.
89 This is typically run be a timer setup from `password-cache-add',
90 but can be invoked at any time to forcefully remove passwords
91 from the cache. This may be useful when it has been detected
92 that a password is invalid, so that `password-read' query the
94 (let ((password (symbol-value (intern-soft key password-data))))
96 (fillarray password ?_)
97 (unintern key password-data))))
99 (defun password-cache-add (key password)
100 "Add password to cache.
101 The password is removed by a timer after `password-cache-expiry'
103 (set (intern key password-data) password)
104 (when password-cache-expiry
105 (run-at-time password-cache-expiry nil
106 #'password-cache-remove
112 ;;; password.el ends here