[PATCH] http auth user & password lookup
Brad Schick
schickb at gmail.com
Wed Aug 15 16:01:32 CDT 2007
# HG changeset patch
# User Brad Schick <schickb at gmail.com>
# Date 1187211013 25200
# Node ID 37b01c92a63ca144a15f728be8712c8a2488a2a2
# Parent dc38a08557bc34493221683453d14717da0b82e2
added user & password lookup from [http_auth] config section
For secure systems, this lets people store user and optional
password values for http auth realms. The format is:
[http_auth]
realm = user:pass
* = user:pass
A realm name of '*' is an optional default used when no
matching realm name is found.
diff -r dc38a08557bc -r 37b01c92a63c doc/hgrc.5.txt
--- a/doc/hgrc.5.txt Tue Aug 14 15:09:30 2007 -0700
+++ b/doc/hgrc.5.txt Wed Aug 15 13:50:13 2007 -0700
@@ -345,6 +345,22 @@ hooks::
If a Python hook returns a "true" value or raises an exception, this
is treated as failure of the hook.
+
+http_auth::
+ Used to store user name and password values for specified http digest
+ and basic authentication realms.
+ realm-name;;
+ User name and optional password for "realm-name" separated by a
+ colon.
+ *;;
+ Optional. Default user name and optional password used when no
+ matching realm is found.
+
+ http_auth example:
+
+ [http_auth]
+ My Realm = joe:badpasswd
+ * = joe:otherpasswd
http_proxy::
Used to access web-based Mercurial repositories through a HTTP
diff -r dc38a08557bc -r 37b01c92a63c mercurial/httprepo.py
--- a/mercurial/httprepo.py Tue Aug 14 15:09:30 2007 -0700
+++ b/mercurial/httprepo.py Wed Aug 15 13:50:13 2007 -0700
@@ -24,7 +24,19 @@ class passwordmgr(urllib2.HTTPPasswordMg
if user and passwd:
return (user, passwd)
- if not self.ui.interactive:
+ configval = self.ui.config('http_auth', realm)
+ if configval is None:
+ configval = self.ui.config('http_auth', '*')
+
+ self.ui.debug(_('found http_auth: %s\n') % configval)
+
+ if configval:
+ parts = configval.split(':', 1)
+ user = parts[0]
+ if len(parts) == 2:
+ passwd = parts[1]
+
+ if not self.ui.interactive and (not user or not passwd):
raise util.Abort(_('http authorization required'))
self.ui.write(_("http authorization required\n"))
diff -r dc38a08557bc -r 37b01c92a63c tests/test-passman.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-passman.py Wed Aug 15 13:50:13 2007 -0700
@@ -0,0 +1,45 @@
+# Test http_auth user/password lookup
+
+import os
+from mercurial import ui, util, httprepo
+
+hgrc = os.environ['HGRCPATH']
+
+# write a hgrc with the various values
+f = open(hgrc, 'w')
+f.write('[http_auth]\n')
+f.write('Realm 1=user1:pass1\n')
+f.write('Realm 2=user2\n')
+f.write('Realm 3=\n')
+f.write('Realm 4=user4:\n')
+f.write('*=userN:passN\n')
+f.close()
+
+def test(realm):
+ passmgr = httprepo.passwordmgr(u)
+ print passmgr.find_user_password(realm, 'bogus')
+
+u = ui.ui()
+u.interactive = False
+u.debugflag = True
+
+test("Realm 1")
+test("Realm 1")
+
+try:
+ test("Realm 2")
+except util.Abort, e:
+ print "r2 abort: ", e
+
+try:
+ test("Realm 3")
+except util.Abort, e:
+ print "r3 abort: ", e
+
+try:
+ test("Realm 4")
+except util.Abort, e:
+ print "r4 abort: ", e
+
+test("lala")
+
diff -r dc38a08557bc -r 37b01c92a63c tests/test-passman.py.out
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-passman.py.out Wed Aug 15 13:50:13 2007 -0700
@@ -0,0 +1,21 @@
+found http_auth: user1:pass1
+http authorization required
+realm: Realm 1
+user: user1
+('user1', 'pass1')
+found http_auth: user1:pass1
+http authorization required
+realm: Realm 1
+user: user1
+('user1', 'pass1')
+found http_auth: user2
+r2 abort: http authorization required
+found http_auth:
+r3 abort: http authorization required
+found http_auth: user4:
+r4 abort: http authorization required
+found http_auth: userN:passN
+http authorization required
+realm: lala
+user: userN
+('userN', 'passN')
More information about the Mercurial-devel
mailing list