changeset 177:93bd77e1f3c1

Changed auth basic handler only for mercurial request.
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 21 May 2010 20:50:03 +0200
parents 8dd7305fbc2d
children 24dbf4bc57aa
files pylons_app/config/middleware.py pylons_app/lib/simplehg.py
diffstat 2 files changed, 23 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pylons_app/config/middleware.py	Fri May 21 20:37:34 2010 +0200
+++ b/pylons_app/config/middleware.py	Fri May 21 20:50:03 2010 +0200
@@ -10,7 +10,6 @@
 from paste.auth.basic import AuthBasicHandler
 from pylons_app.lib.simplehg import SimpleHg
 from pylons_app.config.environment import load_environment
-from pylons_app.lib.auth import authfunc 
 
 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
     """Create a Pylons WSGI application and return it
@@ -45,7 +44,6 @@
     
     # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)    
     app = SimpleHg(app, config)
-    app = AuthBasicHandler(app, config['repos_name'] + ' mercurial repository', authfunc)    
     
     if asbool(full_stack):
         # Handle Python exceptions
--- a/pylons_app/lib/simplehg.py	Fri May 21 20:37:34 2010 +0200
+++ b/pylons_app/lib/simplehg.py	Fri May 21 20:50:03 2010 +0200
@@ -1,19 +1,38 @@
-import os
 from mercurial.hgweb import hgweb
 from mercurial.hgweb.request import wsgiapplication
-from pylons_app.lib.utils import make_ui, invalidate_cache
+from paste.auth.basic import AuthBasicAuthenticator
+from paste.httpheaders import REMOTE_USER, AUTH_TYPE
 from pylons.controllers.util import abort
+from pylons_app.lib.auth import authfunc
+from pylons_app.lib.utils import make_ui, invalidate_cache
 from webob.exc import HTTPNotFound
+import os
+
 class SimpleHg(object):
 
     def __init__(self, application, config):
         self.application = application
         self.config = config
+        #authenticate this mercurial request using 
+        realm = '%s %s' % (config['repos_name'], 'mercurial repository')
+        self.authenticate = AuthBasicAuthenticator(realm, authfunc)
         
     def __call__(self, environ, start_response):
         if not is_mercurial(environ):
             return self.application(environ, start_response)
         else:
+            #===================================================================
+            # AUTHENTICATE THIS MERCURIAL REQUEST
+            #===================================================================
+            username = REMOTE_USER(environ)
+            if not username:
+                result = self.authenticate(environ)
+                if isinstance(result, str):
+                    AUTH_TYPE.update(environ, 'basic')
+                    REMOTE_USER.update(environ, result)
+                else:
+                    return result.wsgi_application(environ, start_response)
+            
             try:
                 repo_name = environ['PATH_INFO'].split('/')[1]
             except:
@@ -50,6 +69,8 @@
             hgserve.repo.ui = repoui
             
         return hgserve
+
+
                                 
 def is_mercurial(environ):
     """