annotate lisp/url/urlauth.el @ 7:c153ca296910

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