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