Changeset - 93bd77e1f3c1
[Not reviewed]
default
0 2 0
Marcin Kuzminski - 15 years ago 2010-05-21 20:50:03
marcin@python-works.com
Changed auth basic handler only for mercurial request.
2 files changed with 23 insertions and 4 deletions:
0 comments (0 inline, 0 general)
pylons_app/config/middleware.py
Show inline comments
 
@@ -7,13 +7,12 @@ from paste.deploy.converters import asbo
 
from pylons.middleware import ErrorHandler, StatusCodeRedirect
 
from pylons.wsgiapp import PylonsApp
 
from routes.middleware import RoutesMiddleware
 
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
 

	
 
    ``global_conf``
 
        The inherited configuration for this application. Normally from
 
@@ -42,13 +41,12 @@ def make_app(global_conf, full_stack=Tru
 
    # Routing/Session/Cache Middleware
 
    app = RoutesMiddleware(app, config['routes.map'])
 
    app = SessionMiddleware(app, config)
 
    
 
    # 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
 
        app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
 

	
 
        # Display error documents for 401, 403, 404 status codes (and
pylons_app/lib/simplehg.py
Show inline comments
 
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:
 
                return HTTPNotFound()(environ, start_response)
 
            
 
            #since we wrap into hgweb, just reset the path
 
@@ -47,12 +66,14 @@ class SimpleHg(object):
 
        
 
        if repoui:
 
            #set the repository based config
 
            hgserve.repo.ui = repoui
 
            
 
        return hgserve
 

	
 

	
 
                                
 
def is_mercurial(environ):
 
    """
 
    Returns True if request's target is mercurial server - header
 
    ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``.
 
    """
0 comments (0 inline, 0 general)