From c02db04f584785adf9e00f9385eaf3aa46ef6436 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Wed, 10 Sep 2008 13:16:43 +0200 Subject: change BasicAuthMiddleware again --- wsgitools/middlewares.py | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'wsgitools/middlewares.py') diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py index 7df21a9..f9362b7 100644 --- a/wsgitools/middlewares.py +++ b/wsgitools/middlewares.py @@ -272,8 +272,7 @@ class DictAuthChecker: __all__.append("BasicAuthMiddleware") class BasicAuthMiddleware: """Middleware implementing HTTP Basic Auth.""" - def __init__(self, app, check_function, realm='www', html401=None, - status401=None): + def __init__(self, app, check_function, realm='www', app401=None): """ @param app: is a WSGI application. @param check_function: is a function taking three arguments username, @@ -281,25 +280,13 @@ class BasicAuthMiddleware: request may is allowed. The older interface of taking only the first two arguments is still supported via catching a TypeError. @type realm: str - @type html401: str or None - @param html401: is to be delivered on authentication failures instead of - the default - @param status401: is to be delivered on authentication failures instead - of the default + @param app401: is an optional WSGI application to be used for error + messages """ self.app = app self.check_function = check_function self.realm = realm - if status401 is None: - self.status401 = "401 Authorization required" - else: - self.status401 = status401 - if html401 is None: - self.html401 = "Authorization required" + \ - "

Authorization required

" \ - "\n" - else: - self.html401 = html401 + self.app401 = app401 def __call__(self, environ, start_response): """wsgi interface @@ -330,13 +317,18 @@ class BasicAuthMiddleware: """wsgi application for indicating authorization is required. @type environ: {str: str} """ - headers = [('Content-type', 'text/html'), - ('WWW-Authenticate', 'Basic realm="%s"' % self.realm), - ("Content-length", str(len(self.html401)))] - start_response(self.status401, headers) - if environ["REQUEST_METHOD"].upper() == "HEAD": - return [] - return [self.html401] + if self.app401 is None: + status = "401 Authorization required" + html = "Authorization required" \ + "

Authorization required

\n" + headers = [('Content-type', 'text/html'), + ('WWW-Authenticate', 'Basic realm="%s"' % self.realm), + ("Content-length", str(len(html)))] + start_response(status, headers) + if environ["REQUEST_METHOD"].upper() == "HEAD": + return [] + return [html] + return self.app401(environ, start_response) __all__.append("TracebackMiddleware") class TracebackMiddleware: -- cgit v1.2.3