##// END OF EJS Templates
Changed auth basic handler only for mercurial request.
marcink -
r177:93bd77e1 default
parent child Browse files
Show More
@@ -1,72 +1,70 b''
1 1 """Pylons middleware initialization"""
2 2 from beaker.middleware import SessionMiddleware
3 3 from paste.cascade import Cascade
4 4 from paste.registry import RegistryManager
5 5 from paste.urlparser import StaticURLParser
6 6 from paste.deploy.converters import asbool
7 7 from pylons.middleware import ErrorHandler, StatusCodeRedirect
8 8 from pylons.wsgiapp import PylonsApp
9 9 from routes.middleware import RoutesMiddleware
10 10 from paste.auth.basic import AuthBasicHandler
11 11 from pylons_app.lib.simplehg import SimpleHg
12 12 from pylons_app.config.environment import load_environment
13 from pylons_app.lib.auth import authfunc
14 13
15 14 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
16 15 """Create a Pylons WSGI application and return it
17 16
18 17 ``global_conf``
19 18 The inherited configuration for this application. Normally from
20 19 the [DEFAULT] section of the Paste ini file.
21 20
22 21 ``full_stack``
23 22 Whether or not this application provides a full WSGI stack (by
24 23 default, meaning it handles its own exceptions and errors).
25 24 Disable full_stack when this application is "managed" by
26 25 another WSGI middleware.
27 26
28 27 ``app_conf``
29 28 The application's local configuration. Normally specified in
30 29 the [app:<name>] section of the Paste ini file (where <name>
31 30 defaults to main).
32 31
33 32 """
34 33 # Configure the Pylons environment
35 34 config = load_environment(global_conf, app_conf)
36 35
37 36
38 37 # The Pylons WSGI app
39 38 app = PylonsApp(config=config)
40 39
41 40
42 41 # Routing/Session/Cache Middleware
43 42 app = RoutesMiddleware(app, config['routes.map'])
44 43 app = SessionMiddleware(app, config)
45 44
46 45 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
47 46 app = SimpleHg(app, config)
48 app = AuthBasicHandler(app, config['repos_name'] + ' mercurial repository', authfunc)
49 47
50 48 if asbool(full_stack):
51 49 # Handle Python exceptions
52 50 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
53 51
54 52 # Display error documents for 401, 403, 404 status codes (and
55 53 # 500 when debug is disabled)
56 54 if asbool(config['debug']):
57 55 app = StatusCodeRedirect(app)
58 56 else:
59 57 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
60 58
61 59 # Establish the Registry for this application
62 60 app = RegistryManager(app)
63 61
64 62 if asbool(static_files):
65 63 # Serve static files
66 64 static_app = StaticURLParser(config['pylons.paths']['static_files'])
67 65 app = Cascade([static_app, app])
68 66
69 67 app.config = config
70 68
71 69 return app
72 70
@@ -1,64 +1,85 b''
1 import os
2 1 from mercurial.hgweb import hgweb
3 2 from mercurial.hgweb.request import wsgiapplication
4 from pylons_app.lib.utils import make_ui, invalidate_cache
3 from paste.auth.basic import AuthBasicAuthenticator
4 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
5 5 from pylons.controllers.util import abort
6 from pylons_app.lib.auth import authfunc
7 from pylons_app.lib.utils import make_ui, invalidate_cache
6 8 from webob.exc import HTTPNotFound
9 import os
10
7 11 class SimpleHg(object):
8 12
9 13 def __init__(self, application, config):
10 14 self.application = application
11 15 self.config = config
16 #authenticate this mercurial request using
17 realm = '%s %s' % (config['repos_name'], 'mercurial repository')
18 self.authenticate = AuthBasicAuthenticator(realm, authfunc)
12 19
13 20 def __call__(self, environ, start_response):
14 21 if not is_mercurial(environ):
15 22 return self.application(environ, start_response)
16 23 else:
24 #===================================================================
25 # AUTHENTICATE THIS MERCURIAL REQUEST
26 #===================================================================
27 username = REMOTE_USER(environ)
28 if not username:
29 result = self.authenticate(environ)
30 if isinstance(result, str):
31 AUTH_TYPE.update(environ, 'basic')
32 REMOTE_USER.update(environ, result)
33 else:
34 return result.wsgi_application(environ, start_response)
35
17 36 try:
18 37 repo_name = environ['PATH_INFO'].split('/')[1]
19 38 except:
20 39 return HTTPNotFound()(environ, start_response)
21 40
22 41 #since we wrap into hgweb, just reset the path
23 42 environ['PATH_INFO'] = '/'
24 43 self.baseui = make_ui()
25 44 self.basepath = self.baseui.configitems('paths')[0][1]\
26 45 .replace('*', '')
27 46 self.repo_path = os.path.join(self.basepath, repo_name)
28 47 try:
29 48 app = wsgiapplication(self._make_app)
30 49 except Exception as e:
31 50 return HTTPNotFound()(environ, start_response)
32 51
33 52 """we know that some change was made to repositories and we should
34 53 invalidate the cache to see the changes right away"""
35 54 invalidate_cache('full_changelog', repo_name)
36 55 return app(environ, start_response)
37 56
38 57 def _make_app(self):
39 58 hgserve = hgweb(self.repo_path)
40 59 return self.load_web_settings(hgserve)
41 60
42 61
43 62 def load_web_settings(self, hgserve):
44 63 repoui = make_ui(os.path.join(self.repo_path, '.hg', 'hgrc'), False)
45 64 #set the global ui for hgserve
46 65 hgserve.repo.ui = self.baseui
47 66
48 67 if repoui:
49 68 #set the repository based config
50 69 hgserve.repo.ui = repoui
51 70
52 71 return hgserve
72
73
53 74
54 75 def is_mercurial(environ):
55 76 """
56 77 Returns True if request's target is mercurial server - header
57 78 ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``.
58 79 """
59 80 http_accept = environ.get('HTTP_ACCEPT')
60 81 if http_accept and http_accept.startswith('application/mercurial'):
61 82 return True
62 83 return False
63 84
64 85
General Comments 0
You need to be logged in to leave comments. Login now