54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
1 ;;; url-auth.el --- Uniform Resource Locator authorization modules
|
57612
|
2
|
64748
|
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
|
75347
|
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
|
57612
|
5
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
6 ;; Keywords: comm, data, processes, hypermedia
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
7
|
57612
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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
|
78222
|
12 ;; the Free Software Foundation; either version 3, or (at your option)
|
57612
|
13 ;; any later version.
|
|
14
|
|
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.
|
|
19
|
|
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
|
64084
|
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
23 ;; Boston, MA 02110-1301, USA.
|
57612
|
24
|
|
25 ;;; Code:
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
26
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
27 (require 'url-vars)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
28 (require 'url-parse)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
29 (autoload 'url-warn "url")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
30
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
31 (defsubst url-auth-user-prompt (url realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
32 "String to usefully prompt for a username."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
33 (concat "Username [for "
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
34 (or realm (url-truncate-url-for-viewing
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
35 (url-recreate-url url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
36 (- (window-width) 10 20)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
37 "]: "))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
38
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
40 ;;; Basic authorization code
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
41 ;;; ------------------------
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
42 ;;; This implements the BASIC authorization type. See the online
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
43 ;;; documentation at
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
44 ;;; http://www.w3.org/hypertext/WWW/AccessAuthorization/Basic.html
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
45 ;;; for the complete documentation on this type.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
46 ;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
47 ;;; This is very insecure, but it works as a proof-of-concept
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
49 (defvar url-basic-auth-storage 'url-http-real-basic-auth-storage
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
50 "Where usernames and passwords are stored.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
51
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
52 Must be a symbol pointing to another variable that will actually store
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
53 the information. The value of this variable is an assoc list of assoc
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
54 lists. The first assoc list is keyed by the server name. The cdr of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
55 this is an assoc list based on the 'directory' specified by the url we
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
56 are looking up.")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
57
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
58 (defun url-basic-auth (url &optional prompt overwrite realm args)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
59 "Get the username/password for the specified URL.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
60 If optional argument PROMPT is non-nil, ask for the username/password
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
61 to use for the url and its descendants. If optional third argument
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
62 OVERWRITE is non-nil, overwrite the old username/password pair if it
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
63 is found in the assoc list. If REALM is specified, use that as the realm
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
64 instead of the pathname inheritance method."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
65 (let* ((href (if (stringp url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
66 (url-generic-parse-url url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
67 url))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
68 (server (url-host href))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
69 (port (url-port href))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
70 (path (url-filename href))
|
78514
|
71 (user (url-user href))
|
|
72 (pass (url-password href))
|
|
73 byserv retval data)
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
74 (setq server (format "%s:%d" server port)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
75 path (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
76 (realm realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
77 ((string-match "/$" path) path)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
78 (t (url-basepath path)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
79 byserv (cdr-safe (assoc server
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
80 (symbol-value url-basic-auth-storage))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
81 (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
82 ((and prompt (not byserv))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
83 (setq user (read-string (url-auth-user-prompt url realm)
|
78514
|
84 (or user (user-real-login-name)))
|
|
85 pass (read-passwd "Password: " nil (or pass "")))
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
86 (set url-basic-auth-storage
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
87 (cons (list server
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
88 (cons path
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
89 (setq retval
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
90 (base64-encode-string
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
91 (format "%s:%s" user pass)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
92 (symbol-value url-basic-auth-storage))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
93 (byserv
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
94 (setq retval (cdr-safe (assoc path byserv)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
95 (if (and (not retval)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
96 (string-match "/" path))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
97 (while (and byserv (not retval))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
98 (setq data (car (car byserv)))
|
75519
|
99 (if (or (not (string-match "/" data)) ; It's a realm - take it!
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
100 (and
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
101 (>= (length path) (length data))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
102 (string= data (substring path 0 (length data)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
103 (setq retval (cdr (car byserv))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
104 (setq byserv (cdr byserv))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
105 (if (or (and (not retval) prompt) overwrite)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
106 (progn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
107 (setq user (read-string (url-auth-user-prompt url realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
108 (user-real-login-name))
|
57509
|
109 pass (read-passwd "Password: ")
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
110 retval (base64-encode-string (format "%s:%s" user pass))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
111 byserv (assoc server (symbol-value url-basic-auth-storage)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
112 (setcdr byserv
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
113 (cons (cons path retval) (cdr byserv))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
114 (t (setq retval nil)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
115 (if retval (setq retval (concat "Basic " retval)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
116 retval))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
117
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
118 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
119 ;;; Digest authorization code
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
120 ;;; ------------------------
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
121 ;;; This implements the DIGEST authorization type. See the internet draft
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
122 ;;; ftp://ds.internic.net/internet-drafts/draft-ietf-http-digest-aa-01.txt
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
123 ;;; for the complete documentation on this type.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
124 ;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
125 ;;; This is very secure
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
127 (defvar url-digest-auth-storage nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
128 "Where usernames and passwords are stored. Its value is an assoc list of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
129 assoc lists. The first assoc list is keyed by the server name. The cdr of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
130 this is an assoc list based on the 'directory' specified by the url we are
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
131 looking up.")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
132
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
133 (defun url-digest-auth-create-key (username password realm method uri)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
134 "Create a key for digest authentication method"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
135 (let* ((info (if (stringp uri)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
136 (url-generic-parse-url uri)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
137 uri))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
138 (a1 (md5 (concat username ":" realm ":" password)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
139 (a2 (md5 (concat method ":" (url-filename info)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
140 (list a1 a2)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
141
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
142 (defun url-digest-auth (url &optional prompt overwrite realm args)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
143 "Get the username/password for the specified URL.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
144 If optional argument PROMPT is non-nil, ask for the username/password
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
145 to use for the url and its descendants. If optional third argument
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
146 OVERWRITE is non-nil, overwrite the old username/password pair if it
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
147 is found in the assoc list. If REALM is specified, use that as the realm
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
148 instead of hostname:portnum."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
149 (if args
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
150 (let* ((href (if (stringp url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
151 (url-generic-parse-url url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
152 url))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
153 (server (url-host href))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
154 (port (url-port href))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
155 (path (url-filename href))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
156 user pass byserv retval data)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
157 (setq path (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
158 (realm realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
159 ((string-match "/$" path) path)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
160 (t (url-basepath path)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
161 server (format "%s:%d" server port)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
162 byserv (cdr-safe (assoc server url-digest-auth-storage)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
163 (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
164 ((and prompt (not byserv))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
165 (setq user (read-string (url-auth-user-prompt url realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
166 (user-real-login-name))
|
57509
|
167 pass (read-passwd "Password: ")
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
168 url-digest-auth-storage
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
169 (cons (list server
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
170 (cons path
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
171 (setq retval
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
172 (cons user
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
173 (url-digest-auth-create-key
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
174 user pass realm
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
175 (or url-request-method "GET")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
176 url)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
177 url-digest-auth-storage)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
178 (byserv
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
179 (setq retval (cdr-safe (assoc path byserv)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
180 (if (and (not retval) ; no exact match, check directories
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
181 (string-match "/" path)) ; not looking for a realm
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
182 (while (and byserv (not retval))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
183 (setq data (car (car byserv)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
184 (if (or (not (string-match "/" data))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
185 (and
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
186 (>= (length path) (length data))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
187 (string= data (substring path 0 (length data)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
188 (setq retval (cdr (car byserv))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
189 (setq byserv (cdr byserv))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
190 (if (or (and (not retval) prompt) overwrite)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
191 (progn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
192 (setq user (read-string (url-auth-user-prompt url realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
193 (user-real-login-name))
|
57509
|
194 pass (read-passwd "Password: ")
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
195 retval (setq retval
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
196 (cons user
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
197 (url-digest-auth-create-key
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
198 user pass realm
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
199 (or url-request-method "GET")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
200 url)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
201 byserv (assoc server url-digest-auth-storage))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
202 (setcdr byserv
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
203 (cons (cons path retval) (cdr byserv))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
204 (t (setq retval nil)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
205 (if retval
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
206 (let ((nonce (or (cdr-safe (assoc "nonce" args)) "nonegiven"))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
207 (opaque (or (cdr-safe (assoc "opaque" args)) "nonegiven")))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
208 (format
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
209 (concat "Digest username=\"%s\", realm=\"%s\","
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
210 "nonce=\"%s\", uri=\"%s\","
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
211 "response=\"%s\", opaque=\"%s\"")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
212 (nth 0 retval) realm nonce (url-filename href)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
213 (md5 (concat (nth 1 retval) ":" nonce ":"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
214 (nth 2 retval))) opaque))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
215
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
216 (defvar url-registered-auth-schemes nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
217 "A list of the registered authorization schemes and various and sundry
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
218 information associated with them.")
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
219
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
220 ;;;###autoload
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
221 (defun url-get-authentication (url realm type prompt &optional args)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
222 "Return an authorization string suitable for use in the WWW-Authenticate
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
223 header in an HTTP/1.0 request.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
224
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
225 URL is the url you are requesting authorization to. This can be either a
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
226 string representing the URL, or the parsed representation returned by
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
227 `url-generic-parse-url'
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
228 REALM is the realm at a specific site we are looking for. This should be a
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
229 string specifying the exact realm, or nil or the symbol 'any' to
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
230 specify that the filename portion of the URL should be used as the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
231 realm
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
232 TYPE is the type of authentication to be returned. This is either a string
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
233 representing the type (basic, digest, etc), or nil or the symbol 'any'
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
234 to specify that any authentication is acceptable. If requesting 'any'
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
235 the strongest matching authentication will be returned. If this is
|
75519
|
236 wrong, it's no big deal, the error from the server will specify exactly
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
237 what type of auth to use
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
238 PROMPT is boolean - specifies whether to ask the user for a username/password
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
239 if one cannot be found in the cache"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
240 (if (not realm)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
241 (setq realm (cdr-safe (assoc "realm" args))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
242 (if (stringp url)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
243 (setq url (url-generic-parse-url url)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
244 (if (or (null type) (eq type 'any))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
245 ;; Whooo doogies!
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
246 ;; Go through and get _all_ the authorization strings that could apply
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
247 ;; to this URL, store them along with the 'rating' we have in the list
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
248 ;; of schemes, then sort them so that the 'best' is at the front of the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
249 ;; list, then get the car, then get the cdr.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
250 ;; Zooom zooom zoooooom
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
251 (cdr-safe
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
252 (car-safe
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
253 (sort
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
254 (mapcar
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
255 (function
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
256 (lambda (scheme)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
257 (if (fboundp (car (cdr scheme)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
258 (cons (cdr (cdr scheme))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
259 (funcall (car (cdr scheme)) url nil nil realm))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
260 (cons 0 nil))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
261 url-registered-auth-schemes)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
262 (function
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
263 (lambda (x y)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
264 (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
265 ((null (cdr x)) nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
266 ((and (cdr x) (null (cdr y))) t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
267 ((and (cdr x) (cdr y))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
268 (>= (car x) (car y)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
269 (t nil)))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
270 (if (symbolp type) (setq type (symbol-name type)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
271 (let* ((scheme (car-safe
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
272 (cdr-safe (assoc (downcase type)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
273 url-registered-auth-schemes)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
274 (if (and scheme (fboundp scheme))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
275 (funcall scheme url prompt
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
276 (and prompt
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
277 (funcall scheme url nil nil realm args))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
278 realm args)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
279
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
280 ;;;###autoload
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
281 (defun url-register-auth-scheme (type &optional function rating)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
282 "Register an HTTP authentication method.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
283
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
284 TYPE is a string or symbol specifying the name of the method. This
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
285 should be the same thing you expect to get returned in an Authenticate
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
286 header in HTTP/1.0 - it will be downcased.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
287 FUNCTION is the function to call to get the authorization information. This
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
288 defaults to `url-?-auth', where ? is TYPE
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
289 RATING a rating between 1 and 10 of the strength of the authentication.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
290 This is used when asking for the best authentication for a specific
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
291 URL. The item with the highest rating is returned."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
292 (let* ((type (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
293 ((stringp type) (downcase type))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
294 ((symbolp type) (downcase (symbol-name type)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
295 (t (error "Bad call to `url-register-auth-scheme'"))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
296 (function (or function (intern (concat "url-" type "-auth"))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
297 (rating (cond
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
298 ((null rating) 2)
|
62400
|
299 ((stringp rating) (string-to-number rating))
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
300 (t rating)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
301 (node (assoc type url-registered-auth-schemes)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
302 (if (not (fboundp function))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
303 (url-warn 'security
|
54792
|
304 (format (concat
|
|
305 "Tried to register `%s' as an auth scheme"
|
|
306 ", but it is not a function!") function)))
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
307
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
308 (if node
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
309 (setcdr node (cons function rating))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
310 (setq url-registered-auth-schemes
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
311 (cons (cons type (cons function rating))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
312 url-registered-auth-schemes)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
313
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
314 (defun url-auth-registered (scheme)
|
78481
|
315 "Return non-nil if SCHEME is registered as an auth type."
|
54695
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
316 (assoc scheme url-registered-auth-schemes))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
317
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
318 (provide 'url-auth)
|
54699
|
319
|
|
320 ;;; arch-tag: 04058625-616d-44e4-9dbf-4b46b00b2a91
|
57612
|
321 ;;; url-auth.el ends here
|