summaryrefslogtreecommitdiff
path: root/wsgitools/middlewares.py
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2008-09-09 19:06:25 +0200
committerHelmut Grohne <helmut@subdivi.de>2008-09-09 19:06:25 +0200
commit11b99faaf71f3f35531aee0fd5b30eb6cea31beb (patch)
treea660632da731543af837f1587c5db4483f47e7d3 /wsgitools/middlewares.py
parent8d739dfd35ec93587c0dbc7da99f62af9e897851 (diff)
downloadwsgitools-11b99faaf71f3f35531aee0fd5b30eb6cea31beb.tar.gz
made error page for BasicAuthMiddleware configurable
Diffstat (limited to 'wsgitools/middlewares.py')
-rw-r--r--wsgitools/middlewares.py27
1 files changed, 20 insertions, 7 deletions
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 = "<html><head><title>Authorization required" + \
+ "</title></head><body><h1>Authorization required</h1>" \
+ "</body></html>\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 = "<html><head><title>Authorization required</title></head>" + \
- "<body><h1>Authorization required</h1></body></html>\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: