annotate lisp/w3/url-auth.el @ 93:486ff617c2a1

Added tag r20-0test1 for changeset 6a43545367ac
author cvs
date Mon, 13 Aug 2007 09:11:41 +0200
parents 0293115a14e9
children 441bb1e64a06
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
1 ;;; url-auth.el --- Uniform Resource Locator authorization modules
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
2 ;; Author: wmperry
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
3 ;; Created: 1997/01/19 01:17:29
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
4 ;; Version: 1.5
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
5 ;; Keywords: comm, data, processes, hypermedia
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
6
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
8 ;;; Copyright (c) 1993-1996 by William M. Perry (wmperry@cs.indiana.edu)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
10 ;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
12 ;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
14 ;;; it under the terms of the GNU General Public License as published by
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
15 ;;; the Free Software Foundation; either version 2, or (at your option)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
16 ;;; any later version.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
17 ;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
21 ;;; GNU General Public License for more details.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
22 ;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
23 ;;; You should have received a copy of the GNU General Public License
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
26 ;;; Boston, MA 02111-1307, USA.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
28
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
29 (require 'url-vars)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
30 (require 'url-parse)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
32 ;;; Basic authorization code
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
33 ;;; ------------------------
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
34 ;;; This implements the BASIC authorization type. See the online
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
35 ;;; documentation at
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
36 ;;; http://www.w3.org/hypertext/WWW/AccessAuthorization/Basic.html
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
37 ;;; for the complete documentation on this type.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
38 ;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
39 ;;; This is very insecure, but it works as a proof-of-concept
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
41 (defvar url-basic-auth-storage nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
42 "Where usernames and passwords are stored. Its value is an assoc list of
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
43 assoc lists. The first assoc list is keyed by the server name. The cdr of
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
44 this is an assoc list based on the 'directory' specified by the url we are
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
45 looking up.")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
46
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
47 (defun url-basic-auth (url &optional prompt overwrite realm args)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
48 "Get the username/password for the specified URL.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
49 If optional argument PROMPT is non-nil, ask for the username/password
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
50 to use for the url and its descendants. If optional third argument
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
51 OVERWRITE is non-nil, overwrite the old username/password pair if it
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
52 is found in the assoc list. If REALM is specified, use that as the realm
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
53 instead of the pathname inheritance method."
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
54 (let* ((href (if (stringp url)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
55 (url-generic-parse-url url)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
56 url))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
57 (server (or (url-host href) url-current-server))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
58 (port (or (url-port href) "80"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
59 (path (url-filename href))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
60 user pass byserv retval data)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
61 (setq server (concat server ":" port)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
62 path (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
63 (realm realm)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
64 ((string-match "/$" path) path)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
65 (t (url-basepath path)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
66 byserv (cdr-safe (assoc server url-basic-auth-storage)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
67 (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
68 ((and prompt (not byserv))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
69 (setq user (read-string "Username: " (user-real-login-name))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
70 pass (funcall url-passwd-entry-func "Password: ")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
71 url-basic-auth-storage
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
72 (cons (list server
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
73 (cons path
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
74 (setq retval
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
75 (base64-encode
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
76 (format "%s:%s" user pass)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
77 url-basic-auth-storage)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
78 (byserv
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
79 (setq retval (cdr-safe (assoc path byserv)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
80 (if (and (not retval)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
81 (string-match "/" path))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
82 (while (and byserv (not retval))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
83 (setq data (car (car byserv)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
84 (if (or (not (string-match "/" data)) ; Its a realm - take it!
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
85 (and
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
86 (>= (length path) (length data))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
87 (string= data (substring path 0 (length data)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
88 (setq retval (cdr (car byserv))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
89 (setq byserv (cdr byserv))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
90 (if (or (and (not retval) prompt) overwrite)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
91 (progn
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
92 (setq user (read-string "Username: " (user-real-login-name))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
93 pass (funcall url-passwd-entry-func "Password: ")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
94 retval (base64-encode (format "%s:%s" user pass))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
95 byserv (assoc server url-basic-auth-storage))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
96 (setcdr byserv
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
97 (cons (cons path retval) (cdr byserv))))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
98 (t (setq retval nil)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
99 (if retval (setq retval (concat "Basic " retval)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
100 retval))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
101
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
103 ;;; Digest authorization code
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
104 ;;; ------------------------
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
105 ;;; This implements the DIGEST authorization type. See the internet draft
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
106 ;;; ftp://ds.internic.net/internet-drafts/draft-ietf-http-digest-aa-01.txt
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
107 ;;; for the complete documentation on this type.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
108 ;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
109 ;;; This is very secure
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
111 (defvar url-digest-auth-storage nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
112 "Where usernames and passwords are stored. Its value is an assoc list of
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
113 assoc lists. The first assoc list is keyed by the server name. The cdr of
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
114 this is an assoc list based on the 'directory' specified by the url we are
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
115 looking up.")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
116
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
117 (defun url-digest-auth-create-key (username password realm method uri)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
118 "Create a key for digest authentication method"
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
119 (let* ((info (if (stringp uri)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
120 (url-generic-parse-url uri)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
121 uri))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
122 (a1 (md5 (concat username ":" realm ":" password)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
123 (a2 (md5 (concat method ":" (url-filename info)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
124 (list a1 a2)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
125
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
126 (defun url-digest-auth (url &optional prompt overwrite realm args)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
127 "Get the username/password for the specified URL.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
128 If optional argument PROMPT is non-nil, ask for the username/password
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
129 to use for the url and its descendants. If optional third argument
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
130 OVERWRITE is non-nil, overwrite the old username/password pair if it
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
131 is found in the assoc list. If REALM is specified, use that as the realm
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
132 instead of hostname:portnum."
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
133 (if args
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
134 (let* ((href (if (stringp url)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
135 (url-generic-parse-url url)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
136 url))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
137 (server (or (url-host href) url-current-server))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
138 (port (or (url-port href) "80"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
139 (path (url-filename href))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
140 user pass byserv retval data)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
141 (setq path (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
142 (realm realm)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
143 ((string-match "/$" path) path)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
144 (t (url-basepath path)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
145 server (concat server ":" port)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
146 byserv (cdr-safe (assoc server url-digest-auth-storage)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
147 (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
148 ((and prompt (not byserv))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
149 (setq user (read-string "Username: " (user-real-login-name))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
150 pass (funcall url-passwd-entry-func "Password: ")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
151 url-digest-auth-storage
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
152 (cons (list server
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
153 (cons path
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
154 (setq retval
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
155 (cons user
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
156 (url-digest-auth-create-key
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
157 user pass realm
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
158 (or url-request-method "GET")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
159 url)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
160 url-digest-auth-storage)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
161 (byserv
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
162 (setq retval (cdr-safe (assoc path byserv)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
163 (if (and (not retval) ; no exact match, check directories
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
164 (string-match "/" path)) ; not looking for a realm
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
165 (while (and byserv (not retval))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
166 (setq data (car (car byserv)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
167 (if (or (not (string-match "/" data))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
168 (and
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
169 (>= (length path) (length data))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
170 (string= data (substring path 0 (length data)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
171 (setq retval (cdr (car byserv))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
172 (setq byserv (cdr byserv))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
173 (if (or (and (not retval) prompt) overwrite)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
174 (progn
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
175 (setq user (read-string "Username: " (user-real-login-name))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
176 pass (funcall url-passwd-entry-func "Password: ")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
177 retval (setq retval
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
178 (cons user
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
179 (url-digest-auth-create-key
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
180 user pass realm
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
181 (or url-request-method "GET")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
182 url)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
183 byserv (assoc server url-digest-auth-storage))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
184 (setcdr byserv
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
185 (cons (cons path retval) (cdr byserv))))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
186 (t (setq retval nil)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
187 (if retval
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
188 (let ((nonce (or (cdr-safe (assoc "nonce" args)) "nonegiven"))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
189 (opaque (or (cdr-safe (assoc "opaque" args)) "nonegiven")))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
190 (format
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
191 (concat "Digest username=\"%s\", realm=\"%s\","
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
192 "nonce=\"%s\", uri=\"%s\","
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
193 "response=\"%s\", opaque=\"%s\"")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
194 (nth 0 retval) realm nonce (url-filename href)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
195 (md5 (concat (nth 1 retval) ":" nonce ":"
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
196 (nth 2 retval))) opaque))))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
197
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
198 (defvar url-registered-auth-schemes nil
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
199 "A list of the registered authorization schemes and various and sundry
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
200 information associated with them.")
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
201
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
202 ;;###autoload
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
203 (defun url-get-authentication (url realm type prompt &optional args)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
204 "Return an authorization string suitable for use in the WWW-Authenticate
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
205 header in an HTTP/1.0 request.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
206
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
207 URL is the url you are requesting authorization to. This can be either a
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
208 string representing the URL, or the parsed representation returned by
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
209 `url-generic-parse-url'
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
210 REALM is the realm at a specific site we are looking for. This should be a
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
211 string specifying the exact realm, or nil or the symbol 'any' to
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
212 specify that the filename portion of the URL should be used as the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
213 realm
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
214 TYPE is the type of authentication to be returned. This is either a string
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
215 representing the type (basic, digest, etc), or nil or the symbol 'any'
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
216 to specify that any authentication is acceptable. If requesting 'any'
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
217 the strongest matching authentication will be returned. If this is
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
218 wrong, its no big deal, the error from the server will specify exactly
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
219 what type of auth to use
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
220 PROMPT is boolean - specifies whether to ask the user for a username/password
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
221 if one cannot be found in the cache"
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
222 (if (not realm)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
223 (setq realm (cdr-safe (assoc "realm" args))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
224 (if (stringp url)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
225 (setq url (url-generic-parse-url url)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
226 (if (or (null type) (eq type 'any))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
227 ;; Whooo doogies!
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
228 ;; Go through and get _all_ the authorization strings that could apply
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
229 ;; to this URL, store them along with the 'rating' we have in the list
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
230 ;; of schemes, then sort them so that the 'best' is at the front of the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
231 ;; list, then get the car, then get the cdr.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
232 ;; Zooom zooom zoooooom
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
233 (cdr-safe
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
234 (car-safe
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
235 (sort
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
236 (mapcar
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
237 (function
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
238 (lambda (scheme)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
239 (if (fboundp (car (cdr scheme)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
240 (cons (cdr (cdr scheme))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
241 (funcall (car (cdr scheme)) url nil nil realm))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
242 (cons 0 nil))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
243 url-registered-auth-schemes)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
244 (function
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
245 (lambda (x y)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
246 (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
247 ((null (cdr x)) nil)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
248 ((and (cdr x) (null (cdr y))) t)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
249 ((and (cdr x) (cdr y))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
250 (>= (car x) (car y)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
251 (t nil)))))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
252 (if (symbolp type) (setq type (symbol-name type)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
253 (let* ((scheme (car-safe
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
254 (cdr-safe (assoc (downcase type)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
255 url-registered-auth-schemes)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
256 (if (and scheme (fboundp scheme))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
257 (funcall scheme url prompt
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
258 (and prompt
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
259 (funcall scheme url nil nil realm args))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
260 realm args)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
261
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
262 ;;###autoload
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
263 (defun url-register-auth-scheme (type &optional function rating)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
264 "Register an HTTP authentication method.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
265
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
266 TYPE is a string or symbol specifying the name of the method. This
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
267 should be the same thing you expect to get returned in an Authenticate
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
268 header in HTTP/1.0 - it will be downcased.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
269 FUNCTION is the function to call to get the authorization information. This
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
270 defaults to `url-?-auth', where ? is TYPE
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
271 RATING a rating between 1 and 10 of the strength of the authentication.
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
272 This is used when asking for the best authentication for a specific
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
273 URL. The item with the highest rating is returned."
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
274 (let* ((type (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
275 ((stringp type) (downcase type))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
276 ((symbolp type) (downcase (symbol-name type)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
277 (t (error "Bad call to `url-register-auth-scheme'"))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
278 (function (or function (intern (concat "url-" type "-auth"))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
279 (rating (cond
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
280 ((null rating) 2)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
281 ((stringp rating) (string-to-int rating))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
282 (t rating)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
283 (node (assoc type url-registered-auth-schemes)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
284 (if (not (fboundp function))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
285 (url-warn 'security
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
286 (format (eval-when-compile
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
287 "Tried to register `%s' as an auth scheme"
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
288 ", but it is not a function!") function)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
289
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
290 (if node
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
291 (progn
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
292 (setcdr node (cons function rating))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
293 (url-warn 'security
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
294 (format
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
295 "Replacing authorization method `%s' - this could be bad."
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
296 type)))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
297 (setq url-registered-auth-schemes
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
298 (cons (cons type (cons function rating))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
299 url-registered-auth-schemes)))))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
300
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
301 (defun url-auth-registered (scheme)
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
302 ;; Return non-nil iff SCHEME is registered as an auth type
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
303 (assoc scheme url-registered-auth-schemes))
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
304
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents:
diff changeset
305 (provide 'urlauth)