1.1 --- a/examples/Common/Login/__init__.py Sun May 30 15:34:05 2004 +0000
1.2 +++ b/examples/Common/Login/__init__.py Sun May 30 15:58:18 2004 +0000
1.3 @@ -9,23 +9,16 @@
1.4
1.5 "A resource providing a login screen."
1.6
1.7 - def __init__(self, authenticator, anonymous_parameter_name=None, anonymous_username="anonymous", use_redirect=1):
1.8 + def __init__(self, authenticator, use_redirect=1):
1.9
1.10 """
1.11 Initialise the resource with an 'authenticator'.
1.12
1.13 - If the optional 'anonymous_parameter_name' is set, clients providing a parameter
1.14 - of that name in the URL will not be authenticated, but then such clients will not
1.15 - get a user identity associated with them. The optional 'anonymous_username' is the
1.16 - username appearing as the identity of anonymous users.
1.17 -
1.18 If the optional 'use_redirect' flag is set to 0, a confirmation screen is given
1.19 instead of redirecting the user back to the original application.
1.20 """
1.21
1.22 self.authenticator = authenticator
1.23 - self.anonymous_parameter_name = anonymous_parameter_name
1.24 - self.anonymous_username = anonymous_username
1.25 self.use_redirect = use_redirect
1.26
1.27 def respond(self, trans):
1.28 @@ -44,19 +37,9 @@
1.29 else:
1.30 redirect = ""
1.31
1.32 - # Check for the anonymous parameter, if appropriate.
1.33 -
1.34 - if self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name):
1.35 -
1.36 - # Make a special cookie token.
1.37 + # Check for a submitted login form.
1.38
1.39 - self.authenticator.set_token(trans, self.anonymous_username)
1.40 - self._redirect(trans, redirect)
1.41 - return
1.42 -
1.43 - # Otherwise, check for a submitted login form.
1.44 -
1.45 - elif fields_body.has_key("login"):
1.46 + if fields_body.has_key("login"):
1.47 if self.authenticator.authenticate(trans):
1.48 self._redirect(trans, redirect)
1.49
2.1 --- a/examples/Common/LoginRedirect/__init__.py Sun May 30 15:34:05 2004 +0000
2.2 +++ b/examples/Common/LoginRedirect/__init__.py Sun May 30 15:58:18 2004 +0000
2.3 @@ -8,15 +8,17 @@
2.4
2.5 "A resource redirecting to a login URL."
2.6
2.7 - def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None):
2.8 + def __init__(self, login_url, app_url, resource, authenticator, anonymous_parameter_name=None,
2.9 + anonymous_username="anonymous"):
2.10
2.11 """
2.12 Initialise the resource with a 'login_url', an 'app_url' where the 'resource' for
2.13 the application being protected should be reachable, and an 'authenticator'.
2.14
2.15 If the optional 'anonymous_parameter_name' is set, clients providing a parameter
2.16 - of that name in the URL will have that parameter sent specially to the login
2.17 - application.
2.18 + of that name in the URL will not be authenticated, but then such clients will get
2.19 + a predefined user identity associated with them, configurable using the optional
2.20 + 'anonymous_username'.
2.21 """
2.22
2.23 self.login_url = login_url
2.24 @@ -24,27 +26,35 @@
2.25 self.resource = resource
2.26 self.authenticator = authenticator
2.27 self.anonymous_parameter_name = anonymous_parameter_name
2.28 + self.anonymous_username = anonymous_username
2.29
2.30 def respond(self, trans):
2.31
2.32 + fields_path = trans.get_fields_from_path()
2.33 +
2.34 # Check the authentication details with the specified authenticator.
2.35
2.36 if self.authenticator.authenticate(trans):
2.37 +
2.38 + # If successful, pass on the transaction.
2.39 +
2.40 self.resource.respond(trans)
2.41 - else:
2.42 - # Define anonymous mode, if appropriate.
2.43 +
2.44 + # Check for the anonymous parameter, if appropriate.
2.45
2.46 - fields_path = trans.get_fields_from_path()
2.47 + elif self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name):
2.48 +
2.49 + # Make a special cookie token, then pass on the transaction.
2.50
2.51 - if self.anonymous_parameter_name is not None and fields_path.has_key(self.anonymous_parameter_name):
2.52 - anonymous_parameter = "%s=%s&" % (self.anonymous_parameter_name, fields_path[self.anonymous_parameter_name][0])
2.53 - else:
2.54 - anonymous_parameter = ""
2.55 + self.authenticator.set_token(trans, self.anonymous_username)
2.56 + self.resource.respond(trans)
2.57 +
2.58 + else:
2.59
2.60 # Redirect to the login URL.
2.61
2.62 - trans.set_header_value("Location", "%s?%sredirect=%s%s" % (
2.63 - self.login_url, anonymous_parameter, self.app_url, self._encode(trans.get_path()))
2.64 + trans.set_header_value("Location", "%s?redirect=%s%s" % (
2.65 + self.login_url, self.app_url, self._encode(trans.get_path()))
2.66 )
2.67 trans.set_response_code(307)
2.68
2.69 @@ -88,4 +98,17 @@
2.70 else:
2.71 return 0
2.72
2.73 + def set_token(self, trans, username):
2.74 +
2.75 + "Set an authentication in the 'trans' with the given 'username'."
2.76 +
2.77 + trans.set_cookie_value(
2.78 + self.cookie_name,
2.79 + get_token(username, self.secret_key)
2.80 + )
2.81 +
2.82 + # Update the transaction with the user details.
2.83 +
2.84 + trans.set_user(username)
2.85 +
2.86 # vim: tabstop=4 expandtab shiftwidth=4