urllib2 authentication to Trac resources

85 views
Skip to first unread message

Jason Miller

unread,
Oct 3, 2012, 6:47:54 PM10/3/12
to trac-...@googlegroups.com
All,

I have need of creating a small python script which would obtain the current build page (bitten) and return a dictionary of status (and I would be happy to relinquish this small code on demand). In order for this to work, I need to authenticate to any Trac resource beyond the main page. I have yet to successfully do so. Using the examples on the following site: http://docs.python.org/howto/urllib2.html works fine when retrieving resources from an LDAP protected location. But not to a Trac resource. 

My Trac/Apache configuration (urllib2 fails):
########################
# WSGI trac_test Trac page here:
WSGIScriptAlias /trac_test /data/trac/trac_test/htdocs/trac_init.wsgi
<Directory /data/trac/trac_test/htdocs>
 Options -Indexes
 WSGIApplicationGroup %{GLOBAL}
 Options FollowSymLinks
 AllowOverride None
 Order deny,allow
 Allow from all
</Directory>
<Location '/trac_test/login'>
  AuthType Basic
  AuthName "Trac Test Site"
  AuthBasicProvider ldap
  AuthLDAPURL "ldap://<someserver>/dc=sub,dc=com?uid"
  AuthzLDAPAuthoritative off
  require valid-user
  Allow from all
</Location>
########################



An Apache resource configuration (urllib2 succeeds):
########################
<Directory /srv/www/ssl/somelocation>
 AuthType Basic
 AuthGroupFile /data/authz_generator/apache_groups
 AuthName "Doxygen Access"
 AuthBasicProvider ldap
 AuthLDAPURL "ldap://<someserver>/dc=sub,dc=com?uid"
 AuthzLDAPAuthoritative off
 require group <somegroup>
 Allow from all
</Directory>
########################

When I attempt to connect to /trac_test/login (or any Trac resource) I receive:
urllib2.HTTPError: HTTP Error 401: Authorization Required

I realize I may have to fiddle with cookielib (maybe not). And that will be an issue for another time.
Is there an urllib2.Request.add_header() method I should be calling specifically for wsgi Trac resources? When I look at the error logs, the authentication error headers look identical... so I am not sure what I should be asking for. I have yet to peak into the way bitten_slave authenticates... And I suppose I may have to, unless someone here has a quick "Yeah... you just add_header("blah we's need special trac wsgi access: %s" % hash)" :)

Thanks in advance!
Jason Miller

Jason Miller

unread,
Oct 3, 2012, 7:35:30 PM10/3/12
to trac-...@googlegroups.com
I posted in ernst :) 


def _get_opener(self):
  opener = urllib2.build_opener(urllib2.HTTPErrorProcessor())
  opener.add_handler(HTTPBasicAuthHandler(self.password_mgr))
  opener.add_handler(urllib2.HTTPDigestAuthHandler(self.password_mgr))
  opener.add_handler(urllib2.HTTPCookieProcessor(self.cookiejar))
  return opener

Thanks all!
Jason
Reply all
Reply to author
Forward
0 new messages