1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This is the auth-source.el package. It lets users tell Gnus how to
28 ;; authenticate in a single place. Simplicity is the goal. Instead
29 ;; of providing 5000 options, we'll stick to simple, easy to
30 ;; understand options.
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'netrc))
36 (defgroup auth-source nil
37 "Authentication sources."
38 :version "23.1" ;; No Gnus
41 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
42 (pop3 "pop3" "pop" "pop3s" "110" "995")
46 "List of authentication protocols and their names"
49 :version "23.1" ;; No Gnus
50 :type '(repeat :tag "Authentication Protocols"
51 (cons :tag "Protocol Entry"
52 (symbol :tag "Protocol")
54 (string :tag "Name")))))
56 ;;; generate all the protocols in a format Customize can use
57 (defconst auth-source-protocols-customize
59 (let ((p (car-safe a)))
61 :tag (upcase (symbol-name p))
63 auth-source-protocols))
65 ;;; this default will be changed to ~/.authinfo.gpg
66 (defcustom auth-source-choices '((:source "~/.authinfo.enc" :host t :protocol t))
67 "List of authentication sources.
69 Each entry is the authentication type with optional properties."
71 :version "23.1" ;; No Gnus
72 :type `(repeat :tag "Authentication Sources"
73 (list :tag "Source definition"
74 (const :format "" :value :source)
75 (string :tag "Authentication Source")
76 (const :format "" :value :host)
77 (choice :tag "Host choice"
79 (regexp :tag "Host regular expression (TODO)")
80 (const :tag "Fallback" nil))
81 (const :format "" :value :protocol)
82 (choice :tag "Protocol"
84 (const :tag "Fallback" nil)
85 ,@auth-source-protocols-customize))))
88 ;; (unintern 'auth-source-protocols)
89 ;; (unintern 'auth-source-choices)
90 ;; (customize-variable 'auth-source-choices)
91 ;; (setq auth-source-choices nil)
92 ;; (format "%S" auth-source-choices)
93 ;; (customize-variable 'auth-source-protocols)
94 ;; (setq auth-source-protocols nil)
95 ;; (format "%S" auth-source-protocols)
96 ;; (auth-source-pick "a" 'imap)
97 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
98 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
99 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
100 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
101 ;; (auth-source-protocol-defaults 'imap)
103 (defun auth-source-pick (host protocol &optional fallback)
104 "Parse `auth-source-choices' for HOST and PROTOCOL matches.
106 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
107 (interactive "sHost: \nsProtocol: \n") ;for testing
109 (dolist (choice auth-source-choices)
110 (let ((h (plist-get choice :host))
111 (p (plist-get choice :protocol)))
114 (and (stringp h) (string-match h host))
115 (and fallback (equal h nil)))
117 (and (symbolp p) (equal p protocol))
118 (and fallback (equal p nil))))
119 (push choice choices))))
123 (auth-source-pick host protocol t)))))
125 (defun auth-source-user-or-password (mode host protocol)
126 "Find user or password (from the string MODE) matching HOST and PROTOCOL."
128 (dolist (choice (auth-source-pick host protocol))
129 (setq found (netrc-machine-user-or-password
131 (plist-get choice :source)
133 (list (format "%s" protocol))
134 (auth-source-protocol-defaults protocol)))
138 (defun auth-source-protocol-defaults (protocol)
139 "Return a list of default ports and names for PROTOCOL."
140 (cdr-safe (assoc protocol auth-source-protocols)))
142 (defun auth-source-user-or-password-imap (mode host)
143 (auth-source-user-or-password mode host 'imap))
145 (defun auth-source-user-or-password-pop3 (mode host)
146 (auth-source-user-or-password mode host 'pop3))
148 (defun auth-source-user-or-password-ssh (mode host)
149 (auth-source-user-or-password mode host 'ssh))
151 (defun auth-source-user-or-password-sftp (mode host)
152 (auth-source-user-or-password mode host 'sftp))
154 (defun auth-source-user-or-password-smtp (mode host)
155 (auth-source-user-or-password mode host 'smtp))
157 (provide 'auth-source)
159 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
160 ;;; auth-source.el ends here