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