1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-26 07:33:47 +00:00
emacs/lisp/password-cache.el

129 lines
4.1 KiB
EmacsLisp
Raw Normal View History

;;; password-cache.el --- Read passwords, possibly using a password cache. -*- lexical-binding: t -*-
2007-12-04 04:09:15 +00:00
2024-01-02 01:47:10 +00:00
;; Copyright (C) 1999-2000, 2003-2024 Free Software Foundation, Inc.
2007-12-04 04:09:15 +00:00
;; Author: Simon Josefsson <simon@josefsson.org>
;; Created: 2003-12-21
;; Keywords: extensions
2007-12-04 04:09:15 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
2007-12-04 04:09:15 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
2007-12-04 04:09:15 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
2007-12-04 04:09:15 +00:00
;;; Commentary:
;; Greatly influenced by pgg.el written by Daiki Ueno, with timer
;; fixes for XEmacs by Katsumi Yamaoka. In fact, this is mostly just
;; a rip-off.
;;
;; (password-read "Password? " "test")
;; ;; Minibuffer prompt for password.
;; => "foo"
;;
;; (password-cache-add "test" (read-passwd "Password? "))
;; ;; Minibuffer prompt from read-passwd, which returns "foo".
2007-12-04 04:09:15 +00:00
;; => nil
;; (password-read "Password? " "test")
;; ;; No minibuffer prompt
;; => "foo"
;;
;; (password-read "Password? " "test")
;; ;; No minibuffer prompt
;; => "foo"
;;
;; ;; Wait `password-cache-expiry' seconds.
;;
;; (password-read "Password? " "test")
;; ;; Minibuffer prompt for password is back.
;; => "foo"
;;; Code:
Stop message.el from loading about 40 libraries it doesn't always need. The general approach is to autoload rather than require, and to require in the specific functions rather than the file. (Bug#5642) * url/url.el: Move mailcap require earlier in the file. * gnus/gmm-utils.el: Don't require wid-edit. (widget-create-child-value, widget-convert, widget-default-get): Autoload. * gnus/gnus-util.el: Don't require time-date, netrc. (message-fetch-field, gnus-group-name-decode): Declare rather than autoloading. (gnus-fetch-field): Require message. (gnus-decode-newsgroups): Require gnus-group. * gnus/ietf-drums.el: Don't require time-date. * gnus/message.el: Don't require hashcash, canlock, ecomplete. Do require mail-utils. Require nnheader only when compiling. (smtpmail-default-smtp-server): Remove declaration. (message-send-mail-function): Check smtpmail-default-smtp-server is bound rather than requiring smtpmail. (message-auto-save-directory, message-insert-signature): Use expand-file-name rather than nnheader-concat. (nnheader-insert-file-contents): Autoload. (hashcash-wait-async): Declare. (message-send-mail): Only call gnus-setup-posting-charset if gnus-group-posting-charset-alist is bound. Require hashcash if needed. (message-send-mail-with-sendmail): Require sendmail. (canlock-password, canlock-password-for-verify): Declare. (message-canlock-password): Require canlock. (nnheader-get-report): Autoload. (gnus-setup-posting-charset): Declare. (message-send-news): Require gnus-msg. (message-make-references, message-make-in-reply-to): Use mail-header-id rather than the alias mail-header-message-id. (ecomplete-add-item, ecomplete-save): Declare. (message-put-addresses-in-ecomplete): Require ecomplete. (ecomplete-display-matches): Autoload. * gnus/mm-decode.el: Don't require mailcap, gnus-util. (gnus-map-function, gnus-replace-in-string, gnus-read-shell-command) (message-fetch-field, mailcap-parse-mailcaps, mailcap-mime-info): Autoload. (mailcap-mime-extensions): Declare. * gnus/mm-encode.el: Don't require mailcap. (mailcap-extension-to-mime): Autoload. * gnus/mml-sec.el: Don't require password-cache. * gnus/mml.el (gnus-setup-posting-charset): Declare rather than autoload. (mailcap-parse-mimetypes, mailcap-mime-types): Declare. (mml-minibuffer-read-type): Require mailcap. (mml-preview): Require gnus-msg. * gnus/mml1991.el: Require password-cache. (password-cache-expiry): Remove declaration. * gnus/mml2015.el: Require password-cache. (password-cache-expiry): Remove declaration. * gnus/nneething.el (mailcap): Require mailcap. * gnus/nnheader.el: (declare-function): Add compatibility stub. (message-remove-header): Declare rather than autoload. (nnheader-replace-header): Require message. * gnus/nnimap.el (declare-function): Add compatibility stub. (netrc-parse, netrc-machine-user-or-password): Declare. (nnimap-open-connection): Require netrc. * gnus/nntp.el (declare-function): Add compatibility stub. (netrc-parse, netrc-machine, netrc-get): Declare. (nntp-send-authinfo): Require netrc. * gnus/rfc2047.el: Don't require qp. (quoted-printable-encode-region, quoted-printable-decode-string): Autoload. * gnus/sieve-mode.el: Don't require easymenu. (easy-menu-add-item): Autoload it. * gnus/spam-stat.el (time-to-number-of-days): Autoload it. * password-cache.el (password-cache, password-cache-expiry): Autoload.
2010-03-19 02:55:37 +00:00
;; Options are autoloaded since they are used by eg mml-sec.el.
;;;###autoload
2007-12-04 04:09:15 +00:00
(defcustom password-cache t
"Whether to cache passwords."
:group 'password
:type 'boolean)
Stop message.el from loading about 40 libraries it doesn't always need. The general approach is to autoload rather than require, and to require in the specific functions rather than the file. (Bug#5642) * url/url.el: Move mailcap require earlier in the file. * gnus/gmm-utils.el: Don't require wid-edit. (widget-create-child-value, widget-convert, widget-default-get): Autoload. * gnus/gnus-util.el: Don't require time-date, netrc. (message-fetch-field, gnus-group-name-decode): Declare rather than autoloading. (gnus-fetch-field): Require message. (gnus-decode-newsgroups): Require gnus-group. * gnus/ietf-drums.el: Don't require time-date. * gnus/message.el: Don't require hashcash, canlock, ecomplete. Do require mail-utils. Require nnheader only when compiling. (smtpmail-default-smtp-server): Remove declaration. (message-send-mail-function): Check smtpmail-default-smtp-server is bound rather than requiring smtpmail. (message-auto-save-directory, message-insert-signature): Use expand-file-name rather than nnheader-concat. (nnheader-insert-file-contents): Autoload. (hashcash-wait-async): Declare. (message-send-mail): Only call gnus-setup-posting-charset if gnus-group-posting-charset-alist is bound. Require hashcash if needed. (message-send-mail-with-sendmail): Require sendmail. (canlock-password, canlock-password-for-verify): Declare. (message-canlock-password): Require canlock. (nnheader-get-report): Autoload. (gnus-setup-posting-charset): Declare. (message-send-news): Require gnus-msg. (message-make-references, message-make-in-reply-to): Use mail-header-id rather than the alias mail-header-message-id. (ecomplete-add-item, ecomplete-save): Declare. (message-put-addresses-in-ecomplete): Require ecomplete. (ecomplete-display-matches): Autoload. * gnus/mm-decode.el: Don't require mailcap, gnus-util. (gnus-map-function, gnus-replace-in-string, gnus-read-shell-command) (message-fetch-field, mailcap-parse-mailcaps, mailcap-mime-info): Autoload. (mailcap-mime-extensions): Declare. * gnus/mm-encode.el: Don't require mailcap. (mailcap-extension-to-mime): Autoload. * gnus/mml-sec.el: Don't require password-cache. * gnus/mml.el (gnus-setup-posting-charset): Declare rather than autoload. (mailcap-parse-mimetypes, mailcap-mime-types): Declare. (mml-minibuffer-read-type): Require mailcap. (mml-preview): Require gnus-msg. * gnus/mml1991.el: Require password-cache. (password-cache-expiry): Remove declaration. * gnus/mml2015.el: Require password-cache. (password-cache-expiry): Remove declaration. * gnus/nneething.el (mailcap): Require mailcap. * gnus/nnheader.el: (declare-function): Add compatibility stub. (message-remove-header): Declare rather than autoload. (nnheader-replace-header): Require message. * gnus/nnimap.el (declare-function): Add compatibility stub. (netrc-parse, netrc-machine-user-or-password): Declare. (nnimap-open-connection): Require netrc. * gnus/nntp.el (declare-function): Add compatibility stub. (netrc-parse, netrc-machine, netrc-get): Declare. (nntp-send-authinfo): Require netrc. * gnus/rfc2047.el: Don't require qp. (quoted-printable-encode-region, quoted-printable-decode-string): Autoload. * gnus/sieve-mode.el: Don't require easymenu. (easy-menu-add-item): Autoload it. * gnus/spam-stat.el (time-to-number-of-days): Autoload it. * password-cache.el (password-cache, password-cache-expiry): Autoload.
2010-03-19 02:55:37 +00:00
;;;###autoload
2007-12-04 04:09:15 +00:00
(defcustom password-cache-expiry 16
"How many seconds passwords are cached, or nil to disable expiring.
Whether passwords are cached at all is controlled by `password-cache'."
:group 'password
:type '(choice (const :tag "Never" nil)
(integer :tag "Seconds")))
(defvar password-data (make-hash-table :test #'equal))
2007-12-04 04:09:15 +00:00
(defun password-read-from-cache (key)
"Obtain passphrase for KEY from time-limited passphrase cache.
Custom variables `password-cache' and `password-cache-expiry'
regulate cache behavior."
(and password-cache
key
(gethash key password-data)))
2007-12-04 04:09:15 +00:00
;;;###autoload
(defun password-in-cache-p (key)
"Check if KEY is in the cache."
(and password-cache
key
(not (eq (gethash key password-data 'password-cache-no-data)
'password-cache-no-data))))
2007-12-04 04:09:15 +00:00
(defun password-read (prompt &optional key)
"Read password, for use with KEY, from user, or from cache if wanted.
KEY indicate the purpose of the password, so the cache can
separate passwords. The cache is not used if KEY is nil.
KEY is typically a string but can be anything (compared via `equal').
2007-12-04 04:09:15 +00:00
The variable `password-cache' control whether the cache is used."
(or (password-read-from-cache key)
(read-passwd prompt)))
(defun password-cache-remove (key)
"Remove password indexed by KEY from password cache.
This is typically run by a timer setup from `password-cache-add',
2007-12-04 04:09:15 +00:00
but can be invoked at any time to forcefully remove passwords
from the cache. This may be useful when it has been detected
that a password is invalid, so that `password-read' query the
user again."
(let ((password (gethash key password-data)))
(when (stringp password)
(clear-string password))
(remhash key password-data)))
2007-12-04 04:09:15 +00:00
(defun password-cache-add (key password)
"Add password to cache.
The password is removed by a timer after `password-cache-expiry' seconds."
(when (and password-cache-expiry
(eq (gethash key password-data 'password-cache-no-data)
'password-cache-no-data))
2007-12-04 04:09:15 +00:00
(run-at-time password-cache-expiry nil
#'password-cache-remove
key))
(puthash key password password-data)
2007-12-04 04:09:15 +00:00
nil)
(defun password-reset ()
"Clear the password cache."
(interactive)
(clrhash password-data))
2007-12-04 04:09:15 +00:00
(provide 'password-cache)
;;; password-cache.el ends here