annotate lisp/url/url-auth.el @ 99126:b7fa14d891a6

(Current Buffer): Reword set-buffer and with-current-buffer documentations. (Creating Buffers): Reword documentation of get-buffer-create.
author Martin Rudalics <rudalics@gmx.at>
date Sat, 25 Oct 2008 09:57:49 +0000
parents 1fa392febf9a
children a9dc0e7c3f2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1 ;;; url-auth.el --- Uniform Resource Locator authorization modules
57612
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
5
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
6 ;; Keywords: comm, data, processes, hypermedia
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
7
57612
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
8 ;; This file is part of GNU Emacs.
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
14
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
18 ;; GNU General Public License for more details.
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
19
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
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
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
22
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
23 ;;; Code:
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
24
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
25 (require 'url-vars)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
26 (require 'url-parse)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
30 (defsubst url-auth-user-prompt (url realm)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
31 "String to usefully prompt for a username."
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
32 (concat "Username [for "
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
33 (or realm (url-truncate-url-for-viewing
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
34 (url-recreate-url url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
35 (- (window-width) 10 20)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
36 "]: "))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
37
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
39 ;;; Basic authorization code
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
40 ;;; ------------------------
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
41 ;;; This implements the BASIC authorization type. See the online
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
42 ;;; documentation at
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
43 ;;; http://www.w3.org/hypertext/WWW/AccessAuthorization/Basic.html
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
44 ;;; for the complete documentation on this type.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
45 ;;;
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
46 ;;; This is very insecure, but it works as a proof-of-concept
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
48 (defvar url-basic-auth-storage 'url-http-real-basic-auth-storage
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
49 "Where usernames and passwords are stored.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
50
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
51 Must be a symbol pointing to another variable that will actually store
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
52 the information. The value of this variable is an assoc list of assoc
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
55 are looking up.")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
56
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
57 (defun url-basic-auth (url &optional prompt overwrite realm args)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
58 "Get the username/password for the specified URL.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
59 If optional argument PROMPT is non-nil, ask for the username/password
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
60 to use for the url and its descendants. If optional third argument
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
61 OVERWRITE is non-nil, overwrite the old username/password pair if it
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
64 (let* ((href (if (stringp url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
65 (url-generic-parse-url url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
66 url))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
80 byserv (cdr-safe (assoc server
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
81 (symbol-value url-basic-auth-storage))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
82 (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
91 (set url-basic-auth-storage
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
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
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
94 (setq retval
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
95 (base64-encode-string
98938
1fa392febf9a (url-basic-auth): Encode password string.
Chong Yidong <cyd@stupidchicken.com>
parents: 96486
diff changeset
96 (format "%s:%s" user
1fa392febf9a (url-basic-auth): Encode password string.
Chong Yidong <cyd@stupidchicken.com>
parents: 96486
diff changeset
97 (encode-coding-string pass 'utf-8))))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
98 (symbol-value url-basic-auth-storage))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
99 (byserv
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
100 (setq retval (cdr-safe (assoc file byserv)))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
101 (if (and (not retval)
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
102 (string-match "/" file))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
103 (while (and byserv (not retval))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
104 (setq data (car (car byserv)))
75519
b271481fb8d2 (url-get-authentication): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents: 75347
diff changeset
105 (if (or (not (string-match "/" data)) ; It's a realm - take it!
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
106 (and
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
107 (>= (length file) (length data))
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
108 (string= data (substring file 0 (length data)))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
109 (setq retval (cdr (car byserv))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
110 (setq byserv (cdr byserv))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
111 (if (or (and (not retval) prompt) overwrite)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
112 (progn
95612
26c59cfcaeff (auth-source-user-or-password): Remove unnecessary eval-and-compile.
Glenn Morris <rgm@gnu.org>
parents: 94879
diff changeset
113 (setq user (or
94879
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
114 (auth-source-user-or-password "login" server type)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
115 (read-string (url-auth-user-prompt url realm)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
116 (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
117 pass (or
94879
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
118 (auth-source-user-or-password "password" server type)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
119 (read-passwd "Password: "))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
120 retval (base64-encode-string (format "%s:%s" user pass))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
121 byserv (assoc server (symbol-value url-basic-auth-storage)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
122 (setcdr byserv
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
123 (cons (cons file retval) (cdr byserv))))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
124 (t (setq retval nil)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
125 (if retval (setq retval (concat "Basic " retval)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
126 retval))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
127
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
129 ;;; Digest authorization code
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
130 ;;; ------------------------
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
131 ;;; This implements the DIGEST authorization type. See the internet draft
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
132 ;;; ftp://ds.internic.net/internet-drafts/draft-ietf-http-digest-aa-01.txt
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
133 ;;; for the complete documentation on this type.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
134 ;;;
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
135 ;;; This is very secure
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
137 (defvar url-digest-auth-storage nil
96486
7369ded3b436 Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents: 95612
diff changeset
138 "Where usernames and passwords are stored.
7369ded3b436 Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents: 95612
diff changeset
139 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
140 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
141 on the 'directory' specified by the url we are looking up.")
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
142
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
143 (defun url-digest-auth-create-key (username password realm method uri)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
144 "Create a key for digest authentication method"
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
145 (let* ((info (if (stringp uri)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
146 (url-generic-parse-url uri)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
147 uri))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
148 (a1 (md5 (concat username ":" realm ":" password)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
149 (a2 (md5 (concat method ":" (url-filename info)))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
150 (list a1 a2)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
151
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
152 (defun url-digest-auth (url &optional prompt overwrite realm args)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
153 "Get the username/password for the specified URL.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
154 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
155 to use for the URL and its descendants. If optional third argument
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
156 OVERWRITE is non-nil, overwrite the old username/password pair if it
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
157 is found in the assoc list. If REALM is specified, use that as the realm
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
158 instead of hostname:portnum."
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
159 (if args
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
160 (let* ((href (if (stringp url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
161 (url-generic-parse-url url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
162 url))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
163 (server (url-host href))
94879
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
164 (type (url-type href))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
165 (port (url-port href))
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
166 (file (url-filename href))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
167 user pass byserv retval data)
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
168 (setq file (cond
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
169 (realm realm)
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
170 ((string-match "/$" file) file)
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
171 (t (url-file-directory file)))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
172 server (format "%s:%d" server port)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
173 byserv (cdr-safe (assoc server url-digest-auth-storage)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
174 (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
175 ((and prompt (not byserv))
94879
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
176 (setq user (or
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
177 (auth-source-user-or-password "login" server type)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
178 (read-string (url-auth-user-prompt url realm)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
179 (user-real-login-name)))
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
180 pass (or
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
181 (auth-source-user-or-password "password" server type)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
182 (read-passwd "Password: "))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
183 url-digest-auth-storage
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
184 (cons (list server
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
185 (cons file
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
186 (setq retval
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
187 (cons user
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
188 (url-digest-auth-create-key
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
189 user pass realm
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
190 (or url-request-method "GET")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
191 url)))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
192 url-digest-auth-storage)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
193 (byserv
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
194 (setq retval (cdr-safe (assoc file byserv)))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
195 (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
196 (string-match "/" file)) ; not looking for a realm
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
197 (while (and byserv (not retval))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
198 (setq data (car (car byserv)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
199 (if (or (not (string-match "/" data))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
200 (and
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
201 (>= (length file) (length data))
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
202 (string= data (substring file 0 (length data)))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
203 (setq retval (cdr (car byserv))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
204 (setq byserv (cdr byserv))))
88054
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
205 (if overwrite
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
206 (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
207 (setq user (or
94879
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
208 (auth-source-user-or-password "login" server type)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
209 (read-string (url-auth-user-prompt url realm)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
210 (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
211 pass (or
94879
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
212 (auth-source-user-or-password "password" server type)
fc66ed3d9938 Add autoload cookie for
Teodor Zlatanov <tzz@lifelogs.com>
parents: 94668
diff changeset
213 (read-passwd "Password: "))
88054
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
214 retval (setq retval
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
215 (cons user
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
216 (url-digest-auth-create-key
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
217 user pass realm
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
218 (or url-request-method "GET")
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
219 url)))
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
220 byserv (assoc server url-digest-auth-storage))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
221 (setcdr byserv
79065
de62e8014967 (url-digest-auth, url-basic-auth):
Richard M. Stallman <rms@gnu.org>
parents: 79056
diff changeset
222 (cons (cons file retval) (cdr byserv))))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
223 (t (setq retval nil)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
224 (if retval
88054
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
225 (if (cdr-safe (assoc "opaque" args))
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
226 (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
227 (opaque (cdr-safe (assoc "opaque" args))))
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
228 (format
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
229 (concat "Digest username=\"%s\", realm=\"%s\","
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
230 "nonce=\"%s\", uri=\"%s\","
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
231 "response=\"%s\", opaque=\"%s\"")
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
232 (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
233 (md5 (concat (nth 1 retval) ":" nonce ":"
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
234 (nth 2 retval))) opaque))
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
235 (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
236 (format
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
237 (concat "Digest username=\"%s\", realm=\"%s\","
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
238 "nonce=\"%s\", uri=\"%s\","
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
239 "response=\"%s\"")
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
240 (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
241 (md5 (concat (nth 1 retval) ":" nonce ":"
eae3aec0f807 2008-01-29 John Wiegley <johnw@newartisans.com>
John Wiegley <johnw@newartisans.com>
parents: 87649
diff changeset
242 (nth 2 retval))))))))))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
243
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
244 (defvar url-registered-auth-schemes nil
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
245 "A list of the registered authorization schemes and various and sundry
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
246 information associated with them.")
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
247
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
248 ;;;###autoload
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
249 (defun url-get-authentication (url realm type prompt &optional args)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
250 "Return an authorization string suitable for use in the WWW-Authenticate
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
251 header in an HTTP/1.0 request.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
252
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
253 URL is the url you are requesting authorization to. This can be either a
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
254 string representing the URL, or the parsed representation returned by
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
255 `url-generic-parse-url'
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
256 REALM is the realm at a specific site we are looking for. This should be a
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
257 string specifying the exact realm, or nil or the symbol 'any' to
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
258 specify that the filename portion of the URL should be used as the
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
259 realm
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
260 TYPE is the type of authentication to be returned. This is either a string
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
261 representing the type (basic, digest, etc), or nil or the symbol 'any'
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
262 to specify that any authentication is acceptable. If requesting 'any'
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
263 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
264 wrong, it's no big deal, the error from the server will specify exactly
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
265 what type of auth to use
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
266 PROMPT is boolean - specifies whether to ask the user for a username/password
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
267 if one cannot be found in the cache"
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
268 (if (not realm)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
269 (setq realm (cdr-safe (assoc "realm" args))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
270 (if (stringp url)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
271 (setq url (url-generic-parse-url url)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
272 (if (or (null type) (eq type 'any))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
273 ;; Whooo doogies!
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
274 ;; Go through and get _all_ the authorization strings that could apply
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
275 ;; to this URL, store them along with the 'rating' we have in the list
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
276 ;; of schemes, then sort them so that the 'best' is at the front of the
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
277 ;; list, then get the car, then get the cdr.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
278 ;; Zooom zooom zoooooom
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
279 (cdr-safe
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
280 (car-safe
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
281 (sort
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
282 (mapcar
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
283 (function
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
284 (lambda (scheme)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
285 (if (fboundp (car (cdr scheme)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
286 (cons (cdr (cdr scheme))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
287 (funcall (car (cdr scheme)) url nil nil realm))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
288 (cons 0 nil))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
289 url-registered-auth-schemes)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
290 (function
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
291 (lambda (x y)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
292 (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
293 ((null (cdr x)) nil)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
294 ((and (cdr x) (null (cdr y))) t)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
295 ((and (cdr x) (cdr y))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
296 (>= (car x) (car y)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
297 (t nil)))))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
298 (if (symbolp type) (setq type (symbol-name type)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
299 (let* ((scheme (car-safe
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
300 (cdr-safe (assoc (downcase type)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
301 url-registered-auth-schemes)))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
302 (if (and scheme (fboundp scheme))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
303 (funcall scheme url prompt
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
304 (and prompt
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
305 (funcall scheme url nil nil realm args))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
306 realm args)))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
307
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
308 ;;;###autoload
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
309 (defun url-register-auth-scheme (type &optional function rating)
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
310 "Register an HTTP authentication method.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
311
96486
7369ded3b436 Typo and docstring fixes.
Juanma Barranquero <lekktu@gmail.com>
parents: 95612
diff changeset
312 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
313 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
314 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
315 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
316 This defaults to `url-?-auth', where ? is TYPE.
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
317 RATING a rating between 1 and 10 of the strength of the authentication.
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
318 This is used when asking for the best authentication for a specific
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
319 URL. The item with the highest rating is returned."
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
320 (let* ((type (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
321 ((stringp type) (downcase type))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
322 ((symbolp type) (downcase (symbol-name type)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
323 (t (error "Bad call to `url-register-auth-scheme'"))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
324 (function (or function (intern (concat "url-" type "-auth"))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
325 (rating (cond
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
326 ((null rating) 2)
62400
e30c08177a3b Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents: 57612
diff changeset
327 ((stringp rating) (string-to-number rating))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
328 (t rating)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
329 (node (assoc type url-registered-auth-schemes)))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
330 (if (not (fboundp function))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
331 (url-warn 'security
54792
369ef3f04d8e (url-register-auth-scheme): Fix `format' call.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54770
diff changeset
332 (format (concat
369ef3f04d8e (url-register-auth-scheme): Fix `format' call.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 54770
diff changeset
333 "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
334 ", but it is not a function!") function)))
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
335
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
336 (if node
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
337 (setcdr node (cons function rating))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
338 (setq url-registered-auth-schemes
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
339 (cons (cons type (cons function rating))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
340 url-registered-auth-schemes)))))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
341
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
342 (defun url-auth-registered (scheme)
78481
bc53aa750f3b Replace `iff' in doc-strings and comments.
Glenn Morris <rgm@gnu.org>
parents: 78222
diff changeset
343 "Return non-nil if SCHEME is registered as an auth type."
54695
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
344 (assoc scheme url-registered-auth-schemes))
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
345
3fb37923e567 Initial revision
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
346 (provide 'url-auth)
54699
7784ae10206d Resolve CVS conflicts
Miles Bader <miles@gnu.org>
parents: 54695
diff changeset
347
84378
3c9ba55f960b Comment style.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 82365
diff changeset
348 ;; arch-tag: 04058625-616d-44e4-9dbf-4b46b00b2a91
57612
2954cb243379 Update header and footer.
Lars Hansen <larsh@soem.dk>
parents: 57509
diff changeset
349 ;;; url-auth.el ends here