1.1 --- a/WebStack/Adapters/BaseHTTPRequestHandler.py Sun Feb 29 00:04:53 2004 +0000
1.2 +++ b/WebStack/Adapters/BaseHTTPRequestHandler.py Sun Feb 29 00:05:02 2004 +0000
1.3 @@ -11,28 +11,36 @@
1.4
1.5 "A factory class creating WebStack dispatcher objects."
1.6
1.7 - def __init__(self, resource):
1.8 + def __init__(self, resource, authenticator=None):
1.9
1.10 - "Initialise the root application-specific 'resource'."
1.11 + """
1.12 + Initialise the root application-specific 'resource' and optional
1.13 + 'authenticator'.
1.14 + """
1.15
1.16 self.webstack_resource = resource
1.17 + self.webstack_authenticator = authenticator
1.18
1.19 def __call__(self, request, client_address, server):
1.20
1.21 "Act as a factory for the server objects."
1.22
1.23 - handler = Handler(request, client_address, server, self.webstack_resource)
1.24 + handler = Handler(request, client_address, server, self.webstack_resource,
1.25 + self.webstack_authenticator)
1.26 return handler
1.27
1.28 class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
1.29
1.30 "A class dispatching requests to WebStack resources."
1.31
1.32 - def __init__(self, request, client_address, server, resource):
1.33 + def __init__(self, request, client_address, server, resource, authenticator):
1.34
1.35 - "Initialise the root application-specific 'resource'."
1.36 + """
1.37 + Initialise the root application-specific 'resource' and 'authenticator'.
1.38 + """
1.39
1.40 self.webstack_resource = resource
1.41 + self.webstack_authenticator = authenticator
1.42 BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)
1.43
1.44 def handle(self):
1.45 @@ -46,7 +54,13 @@
1.46 return
1.47
1.48 trans = WebStack.BaseHTTPRequestHandler.Transaction(self)
1.49 - self.webstack_resource.respond(trans)
1.50 + if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans):
1.51 + self.webstack_resource.respond(trans)
1.52 + else:
1.53 + trans.set_response_code(401) # Unauthorized
1.54 + trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
1.55 + self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm()))
1.56 +
1.57 trans.commit()
1.58
1.59 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/WebStack/Adapters/ModPython.py Sun Feb 29 00:04:53 2004 +0000
2.2 +++ b/WebStack/Adapters/ModPython.py Sun Feb 29 00:05:02 2004 +0000
2.3 @@ -7,7 +7,7 @@
2.4 import WebStack.ModPython
2.5 from mod_python import apache
2.6
2.7 -def dispatch(request, resource, debug=0):
2.8 +def respond(request, resource, debug=0):
2.9
2.10 """
2.11 Dispatch the given 'request' to the root application-specific 'resource'.
2.12 @@ -33,4 +33,32 @@
2.13 else:
2.14 raise
2.15
2.16 +def authenticate(request, authenticator, debug=0):
2.17 +
2.18 + """
2.19 + Dispatch the given 'request' to the application-specific 'authenticator'.
2.20 + The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in
2.21 + the browser.
2.22 + """
2.23 +
2.24 + trans = WebStack.ModPython.Transaction(request)
2.25 +
2.26 + # NOTE: Resource pooling may be appropriate.
2.27 + # NOTE: Forbidden access is not yet considered here.
2.28 +
2.29 + try:
2.30 + if authenticator.authenticate(trans):
2.31 + return apache.OK
2.32 + else:
2.33 + return apache.HTTP_UNAUTHORIZED
2.34 + except:
2.35 +
2.36 + # NOTE: Error conditions should be investigated further, along with
2.37 + # NOTE: other response states.
2.38 +
2.39 + if not debug:
2.40 + return apache.HTTP_INTERNAL_SERVER_ERROR
2.41 + else:
2.42 + raise
2.43 +
2.44 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/WebStack/Adapters/Twisted.py Sun Feb 29 00:04:53 2004 +0000
3.2 +++ b/WebStack/Adapters/Twisted.py Sun Feb 29 00:05:02 2004 +0000
3.3 @@ -13,12 +13,16 @@
3.4
3.5 isLeaf = 1
3.6
3.7 - def __init__(self, resource):
3.8 + def __init__(self, resource, authenticator=None):
3.9
3.10 - "Initialise the root application-specific 'resource'."
3.11 + """
3.12 + Initialise the root application-specific 'resource' and optional
3.13 + 'authenticator'.
3.14 + """
3.15
3.16 twisted.web.resource.Resource.__init__(self)
3.17 self.webstack_resource = resource
3.18 + self.webstack_authenticator = authenticator
3.19
3.20 def getChild(self, path, request):
3.21 raise NotImplementedError, "getChild"
3.22 @@ -28,7 +32,13 @@
3.23 "Dispatch the given 'request' to the root application-specific resource."
3.24
3.25 trans = WebStack.Twisted.Transaction(request)
3.26 - self.webstack_resource.respond(trans)
3.27 + if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(trans):
3.28 + self.webstack_resource.respond(trans)
3.29 + else:
3.30 + trans.set_response_code(401) # Unauthorized
3.31 + trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
3.32 + self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm()))
3.33 +
3.34 trans.commit()
3.35 request.finish()
3.36 return twisted.web.server.NOT_DONE_YET
4.1 --- a/WebStack/Adapters/Webware.py Sun Feb 29 00:04:53 2004 +0000
4.2 +++ b/WebStack/Adapters/Webware.py Sun Feb 29 00:05:02 2004 +0000
4.3 @@ -28,14 +28,15 @@
4.4 instantiation.
4.5 """
4.6
4.7 - def __init__(self, resource):
4.8 + def __init__(self, resource, authenticator=None):
4.9
4.10 """
4.11 Initialise the parser object with the given root application-specific
4.12 - 'resource'.
4.13 + 'resource' and optional 'authenticator'.
4.14 """
4.15
4.16 self.webstack_resource = resource
4.17 + self.webstack_authenticator = authenticator
4.18
4.19 def parse(self, trans, requestPath):
4.20
4.21 @@ -50,7 +51,7 @@
4.22 should be available elsewhere.
4.23 """
4.24
4.25 - return WebStackServlet(self.webstack_resource)
4.26 + return WebStackServlet(self.webstack_resource, self.webstack_authenticator)
4.27
4.28 # For Webware 0.8.1 and earlier, employ servlet factories and servlets.
4.29
4.30 @@ -64,17 +65,19 @@
4.31 application-specific resources.
4.32 """
4.33
4.34 - def __init__(self, application, resource, file_extensions):
4.35 + def __init__(self, application, resource, file_extensions, authenticator=None):
4.36
4.37 """
4.38 Initialise the servlet factory with the Webware 'application' and the
4.39 WebStack root application-specific 'resource'. The 'file_extensions'
4.40 - specified indicate for which files this factory is invoked.
4.41 + specified indicate for which files this factory is invoked. An optional
4.42 + 'authenticator' is used to control access to the resource.
4.43 """
4.44
4.45 ServletFactory.__init__(self, application)
4.46 self.webstack_resource = resource
4.47 self.file_extensions = file_extensions
4.48 + self.webstack_authenticator = authenticator
4.49
4.50 def uniqueness(self):
4.51
4.52 @@ -101,7 +104,7 @@
4.53 method is invoked on the servlet.
4.54 """
4.55
4.56 - return WebStackServlet(self.webstack_resource)
4.57 + return WebStackServlet(self.webstack_resource, self.webstack_authenticator)
4.58
4.59 # Servlets are common to both solutions.
4.60
4.61 @@ -109,12 +112,16 @@
4.62
4.63 "A servlet which dispatches transactions to application-specific resources."
4.64
4.65 - def __init__(self, resource):
4.66 + def __init__(self, resource, authenticator):
4.67
4.68 - "Initialise the servlet with an application-specific 'resource'."
4.69 + """
4.70 + Initialise the servlet with an application-specific 'resource' and
4.71 + 'authenticator'.
4.72 + """
4.73
4.74 Servlet.__init__(self)
4.75 self.webstack_resource = resource
4.76 + self.webstack_authenticator = authenticator
4.77
4.78 def respond(self, trans):
4.79
4.80 @@ -124,7 +131,13 @@
4.81 """
4.82
4.83 new_trans = WebStack.Webware.Transaction(trans)
4.84 - self.webstack_resource.respond(new_trans)
4.85 + if self.webstack_authenticator is None or self.webstack_authenticator.authenticate(new_trans):
4.86 + self.webstack_resource.respond(new_trans)
4.87 + else:
4.88 + new_trans.set_response_code(401) # Unauthorized
4.89 + new_trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % (
4.90 + self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm()))
4.91 +
4.92 new_trans.commit()
4.93
4.94 # vim: tabstop=4 expandtab shiftwidth=4