1.1 --- a/WebStack/Resources/Selectors.py Sun Aug 24 20:51:10 2008 +0200
1.2 +++ b/WebStack/Resources/Selectors.py Sat Nov 15 02:33:06 2008 +0100
1.3 @@ -4,7 +4,7 @@
1.4 Resources which "select" other resources, sometimes causing desirable
1.5 side-effects.
1.6
1.7 -Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -138,4 +138,62 @@
1.13 finally:
1.14 self.store.rollback()
1.15
1.16 +class ConditionalAuthSelector:
1.17 +
1.18 + """
1.19 + Test for the presence of authenticated user information, dispatching to the
1.20 + authenticating resource where no such information can be found, dispatching
1.21 + beyond the authenticating resource otherwise.
1.22 + """
1.23 +
1.24 + def __init__(self, resource):
1.25 +
1.26 + """
1.27 + Initialise the selector with the given authenticating 'resource' which
1.28 + would normally conduct an authentication process unconditionally. This
1.29 + 'resource' must itself have an attribute called 'resource' in order to
1.30 + support dispatch beyond the authentication mechanisms where
1.31 + authentication has already taken place.
1.32 + """
1.33 +
1.34 + self.resource = resource
1.35 +
1.36 + def respond(self, trans):
1.37 +
1.38 + """
1.39 + Respond to the transaction 'trans', either dispatching to this object's
1.40 + resource for authentication, or if authentication has already taken
1.41 + place and a user has been set, dispatching to the target of the resource
1.42 + - the resource wrapped by the authenticating resource.
1.43 + """
1.44 +
1.45 + if trans.get_user() is None:
1.46 + self.resource.respond(trans)
1.47 + else:
1.48 + self.resource.resource.respond(trans)
1.49 +
1.50 +class AuthInfoSelector:
1.51 +
1.52 + """
1.53 + A selector which ensures that any information set by authenticators is made
1.54 + available.
1.55 + """
1.56 +
1.57 + def __init__(self, resource, authenticator):
1.58 +
1.59 + "Initialise the selector with the given 'resource' and 'authenticator'."
1.60 +
1.61 + self.resource = resource
1.62 + self.authenticator = authenticator
1.63 +
1.64 + def respond(self, trans):
1.65 +
1.66 + """
1.67 + Respond to 'trans' by performing authentication and forwarding the
1.68 + request.
1.69 + """
1.70 +
1.71 + self.authenticator.authenticate(trans)
1.72 + self.resource.respond(trans)
1.73 +
1.74 # vim: tabstop=4 expandtab shiftwidth=4