[PATCH] allow http authentication information to be specified in the configuration

Benoit Boissinot benoit.boissinot at ens-lyon.org
Mon Apr 6 16:12:54 CDT 2009


[after looking at the code I think I like this version better,
 sorry for the wrong advise]

On Sun, Apr 05, 2009 at 10:17:26PM +0200, Sune Foldager wrote:
> I discussed this with djc on IRC some time ago. It adds an [auth] 
> section to the configuration, where you can put lines like this:
> 
> urlprefix = username:password
> 
> urlprefix is matched against the url being operated on (excluding the 
> schema).

What is the reason to exclude the scheme ?

>  The longest prefix wins, and there is a special prefix, *, 
> which matches all urls (with match length 1, so any more specific prefix 
> will win). The username/password pair is then used to authenticate 
> against the remote http. All this is only activated if no 
> username/password is given as part of the URL.

I've corrected the code to correctly encode the password, now it should be
in the same format as when it's embedded in the url.

I won't push that until there's some doc against doc/hgrc.txt

Please test and review.

# HG changeset patch
# User Sune Foldager <cryo at cyanite.org>
# Date 1238961876 -7200
# Node ID d2632947ec737a0f0e29ba07d2facb11c29b3797
# Parent  685ce2f7ee35dfe29d296f80ba2dff124a1caa63
allow http authentication information to be specified in the configuration

diff --git a/mercurial/httprepo.py b/mercurial/httprepo.py
--- a/mercurial/httprepo.py
+++ b/mercurial/httprepo.py
@@ -31,7 +31,7 @@
                              (query or frag))
 
         # urllib cannot handle URLs with embedded user or passwd
-        self._url, authinfo = url.getauthinfo(path)
+        self._url, authinfo = url.getauthinfo(ui, path)
 
         self.ui = ui
         self.ui.debug(_('using %s\n') % self._url)
diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py
--- a/mercurial/statichttprepo.py
+++ b/mercurial/statichttprepo.py
@@ -60,7 +60,8 @@
         self._url = path
         self.ui = ui
 
-        self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
+        path = path.rstrip('/') + "/.hg"
+        self.path, authinfo = url.getauthinfo(ui, path)
 
         opener = build_opener(ui, authinfo)
         self.opener = opener(self.path)
diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -8,6 +8,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 import urllib, urllib2, urlparse, httplib, os, re
+import bisect
 from i18n import _
 import keepalive, util
 
@@ -246,7 +247,7 @@
                 return
             raise
 
-def getauthinfo(path):
+def urlauthinfo(path):
     scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
     if not urlpath:
         urlpath = '/'
@@ -271,6 +272,30 @@
         authinfo = None
     return url, authinfo
 
+def getauthinfo(ui, path):
+    path, authinfo = urlauthinfo(path)
+    if authinfo:
+        return path, authinfo
+
+    def splituserpass(userpass):
+        return netlocsplit(userpass + '@localhost')[2:4]
+
+    fuzzyauthn = dict(ui.configitems('auth'))
+
+    default_auth = fuzzyauthn.pop('*', None)
+    if default_auth:
+        default_auth = splituserpass(default_auth)
+
+    fuzzyauthn = [(url, splituserpass(userpass))
+                  for url, userpass in fuzzyauthn.iteritems()]
+    fuzzyauthn.sort()
+    # find closest matching prefix
+    i = bisect.bisect(fuzzyauthn, path)
+    if path.startswith(fuzzyauthn[i][0]):
+        return path, fuzzyauthn[i][1]
+
+    return path, default_auth
+
 def opener(ui, authinfo=None):
     '''
     construct an opener suitable for urllib2
@@ -310,5 +335,5 @@
         url = 'file://' + urllib.pathname2url(path)
         authinfo = None
     else:
-        url, authinfo = getauthinfo(url)
+        url, authinfo = getauthinfo(ui, url)
     return opener(ui, authinfo).open(url, data)

-- 
:wq


More information about the Mercurial-devel mailing list