changeset 1931:7f87f9d0159e

move cookie parsing to request and pass the cookie object (or None) to the auth handlers
author Johannes Berg <johannes AT sipsolutions DOT net>
date Tue, 03 Apr 2007 18:20:18 +0200
parents 3b25f0f60ede
children 8916520c8314
files MoinMoin/auth/__init__.py MoinMoin/auth/php_session.py MoinMoin/request/__init__.py
diffstat 3 files changed, 16 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/auth/__init__.py	Tue Apr 03 18:19:48 2007 +0200
+++ b/MoinMoin/auth/__init__.py	Tue Apr 03 18:20:18 2007 +0200
@@ -12,6 +12,8 @@
        logout: True if user has clicked on Logout button
        user_obj: the user_obj we have until now (user_obj returned from
                  previous auth method or None for first auth method)
+       cookie: a Cookie.SimpleCookie instance containing the cookies for
+               this request, or None if no (valid) cookies were set
        (we maybe add some more here)
 
     Use code like this to get them:
@@ -19,6 +21,7 @@
         password = kw.get('password') or ''
         login = kw.get('login')
         logout = kw.get('logout')
+        cookie = kw.get('cookie')
         request.log("got name=%s len(password)=%d login=%r logout=%r" % (name, len(password), login, logout))
     
     The called auth method then must return a tuple (user_obj, continue_flag).
@@ -276,6 +279,7 @@
     login = kw.get('login')
     logout = kw.get('logout')
     user_obj = kw.get('user_obj')
+    cookie = kw.get('cookie')
 
     cfg = request.cfg
     verbose = False
@@ -302,14 +306,6 @@
             if verbose: request.log("moin_session did not get valid user from previous auth method, doing nothing")
             return user_obj, True
 
-    try:
-        if verbose: request.log("trying to get cookie...")
-        cookie = Cookie.SimpleCookie(request.saved_cookie)
-    except Cookie.CookieError:
-        # ignore invalid cookies, else user can't relogin
-        if verbose: request.log("caught Cookie.CookieError")
-        cookie = None
-
     if not (cookie is not None and cookie_name in cookie):
         # No valid cookie
         if verbose: request.log("either no cookie or no %s key" % cookie_name)
--- a/MoinMoin/auth/php_session.py	Tue Apr 03 18:19:48 2007 +0200
+++ b/MoinMoin/auth/php_session.py	Tue Apr 03 18:20:18 2007 +0200
@@ -47,11 +47,8 @@
             return dec(username), dec(email), dec(name)
 
         user_obj = kw.get('user_obj')
-        try:
-            cookie = Cookie.SimpleCookie(request.saved_cookie)
-        except Cookie.CookieError: # ignore invalid cookies
-            cookie = None
-        if cookie:
+        cookie = kw.get('cookie')
+        if not cookie is None:
             for cookiename in cookie:
                 cookievalue = urllib.unquote(cookie[cookiename].value).decode('iso-8859-1')
                 session = _PHPsessionParser.loadSession(cookievalue, path=self.s_path, prefix=self.s_prefix)
--- a/MoinMoin/request/__init__.py	Tue Apr 03 18:19:48 2007 +0200
+++ b/MoinMoin/request/__init__.py	Tue Apr 03 18:20:18 2007 +0200
@@ -10,6 +10,7 @@
 import os, re, time, sys, cgi, StringIO
 import logging
 import copy
+import Cookie
 
 try:
     set
@@ -569,6 +570,12 @@
                                           user_obj=None)
         return u
 
+    def parse_cookie(self):
+        try:
+            return Cookie.SimpleCookie(self.saved_cookie)
+        except Cookie.CookieError:
+            return None
+
     def get_user_default_unknown(self, **kw):
         """ call do_auth and if it doesnt return a user object, make some "Unknown User" """
         user_obj = self.get_user_default_None(**kw)
@@ -583,9 +590,11 @@
         login = kw.get('login')
         logout = kw.get('logout')
         user_obj = kw.get('user_obj')
+        cookie = self.parse_cookie()
         for auth in self.cfg.auth:
             user_obj, continue_flag = auth(self, name=name, password=password,
-                                           login=login, logout=logout, user_obj=user_obj)
+                                           login=login, logout=logout, user_obj=user_obj,
+                                           cookie=cookie)
             if not continue_flag:
                 break
         return user_obj