Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1770

Show
Ignore:
Timestamp:
10/26/07 21:23:02
Author:
fumanchu
Message:

Fix for #710 (Allow forcing a new session id).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cptools.py

    r1728 r1770  
    243243        hooks.attach('before_finalize', _sessions.save) 
    244244        hooks.attach('on_end_request', _sessions.close) 
     245         
     246    def regenerate(self): 
     247        """Drop the current session and make a new one (with a new id).""" 
     248        sess = cherrypy.serving.session 
     249        sess.regenerate() 
     250         
     251        # Grab cookie-relevant tool args 
     252        conf = dict([(k, v) for k, v in self._merged_args().iteritems() 
     253                     if k in ('path', 'path_header', 'name', 'timeout', 
     254                              'domain', 'secure')]) 
     255        _sessions.set_response_cookie(**conf) 
     256 
     257 
    245258 
    246259 
  • trunk/cherrypy/lib/sessions.py

    r1752 r1770  
    5959            setattr(self, k, v) 
    6060         
    61         self.id = id 
     61        if id is None: 
     62            self.regenerate() 
     63        else: 
     64            self.id = id 
     65     
     66    def regenerate(self): 
     67        """Replace the current session (with a new id).""" 
     68        if self.id is not None: 
     69            self.delete() 
     70         
     71        old_session_was_locked = self.locked 
     72        if old_session_was_locked: 
     73            self.release_lock() 
     74         
     75        self.id = None 
    6276        while self.id is None: 
    6377            self.id = self.generate_id() 
     
    6579            if self._load() is not None: 
    6680                self.id = None 
     81         
     82        if old_session_was_locked: 
     83            self.acquire_lock() 
    6784     
    6885    def clean_up(self): 
     
    490507        cookie 'path' will be pulled from request.headers[path_header]. 
    491508    name: the name of the cookie. 
    492     timeout: the expiration timeout for the cookie. 
     509    timeout: the expiration timeout (in minutes) for both the cookie and 
     510        stored session data. 
    493511    domain: the cookie domain. 
    494512    secure: if False (the default) the cookie 'secure' value will not 
     
    531549        cherrypy.session = cherrypy._ThreadLocalProxy('session') 
    532550     
     551    set_response_cookie(path=path, path_header=path_header, name=name, 
     552                        timeout=timeout, domain=domain, secure=secure) 
     553 
     554 
     555def set_response_cookie(path=None, path_header=None, name='session_id', 
     556                        timeout=60, domain=None, secure=False): 
     557    """Set a response cookie for the client. 
     558     
     559    path: the 'path' value to stick in the response cookie metadata. 
     560    path_header: if 'path' is None (the default), then the response 
     561        cookie 'path' will be pulled from request.headers[path_header]. 
     562    name: the name of the cookie. 
     563    timeout: the expiration timeout for the cookie. 
     564    domain: the cookie domain. 
     565    secure: if False (the default) the cookie 'secure' value will not 
     566        be set. If True, the cookie 'secure' value will be set (to 1). 
     567    """ 
    533568    # Set response cookie 
    534569    cookie = cherrypy.response.cookie 
    535     cookie[name] = sess.id 
    536     cookie[name]['path'] = path or request.headers.get(path_header) or '/' 
     570    cookie[name] = cherrypy.serving.session.id 
     571    cookie[name]['path'] = (path or cherrypy.request.headers.get(path_header) 
     572                            or '/') 
    537573     
    538574    # We'd like to use the "max-age" param as indicated in 
     
    557593    cherrypy.response.cookie[name]['expires'] = t 
    558594 
     595 
  • trunk/cherrypy/test/test_session.py

    r1742 r1770  
    8484            return cherrypy.request.method 
    8585        restricted.exposed = True 
     86         
     87        def regen(self): 
     88            cherrypy.tools.sessions.regenerate() 
     89            return "logged in" 
     90        regen.exposed = True 
    8691     
    8792    cherrypy.tree.mount(Root()) 
     
    203208        self.getPage('/restricted', self.cookies, method='POST') 
    204209        self.assertErrorPage(405, "Specified method is invalid for this server.") 
     210     
     211    def test_6_regenerate(self): 
     212        self.getPage('/testStr') 
     213        # grab the cookie ID 
     214        id1 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1] 
     215        self.getPage('/regen') 
     216        self.assertBody('logged in') 
     217        id2 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1] 
     218        self.assertNotEqual(id1, id2) 
    205219 
    206220 

Hosted by WebFaction

Log in as guest/cpguest to create tickets