From 11b99faaf71f3f35531aee0fd5b30eb6cea31beb Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 9 Sep 2008 19:06:25 +0200 Subject: made error page for BasicAuthMiddleware configurable --- wsgitools/middlewares.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'wsgitools/middlewares.py') diff --git a/wsgitools/middlewares.py b/wsgitools/middlewares.py index 57e5028..7df21a9 100644 --- a/wsgitools/middlewares.py +++ b/wsgitools/middlewares.py @@ -272,7 +272,8 @@ class DictAuthChecker: __all__.append("BasicAuthMiddleware") class BasicAuthMiddleware: """Middleware implementing HTTP Basic Auth.""" - def __init__(self, app, check_function, realm='www'): + def __init__(self, app, check_function, realm='www', html401=None, + status401=None): """ @param app: is a WSGI application. @param check_function: is a function taking three arguments username, @@ -280,10 +281,25 @@ 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 """ 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 def __call__(self, environ, start_response): """wsgi interface @@ -314,16 +330,13 @@ class BasicAuthMiddleware: """wsgi application for indicating authorization is required. @type environ: {str: str} """ - 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) + ("Content-length", str(len(self.html401)))] + start_response(self.status401, headers) if environ["REQUEST_METHOD"].upper() == "HEAD": return [] - return [html] + return [self.html401] __all__.append("TracebackMiddleware") class TracebackMiddleware: -- cgit v1.2.3