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

Ticket #710: sessregen.patch

  • _cptools.py

    old new  
    241241         
    242242        hooks.attach('before_finalize', _sessions.save) 
    243243        hooks.attach('on_end_request', _sessions.close) 
     244         
     245    def regenerate(self): 
     246        """Drop the current session and make a new one (with a new id).""" 
     247        sess = cherrypy._serving.session 
     248        sess.regenerate() 
     249         
     250        # Grab cookie-relevant tool args 
     251        conf = dict([(k, v) for k, v in self._merged_args().iteritems() 
     252                     if k in ('path', 'path_header', 'name', 'timeout', 
     253                              'domain', 'secure')]) 
     254        _sessions.set_response_cookie(**conf) 
    244255 
    245256 
     257 
     258 
    246259class XMLRPCController(object): 
    247260    """A Controller (page handler collection) for XML-RPC. 
    248261     
  • lib/sessions.py

    old new  
    5858        for k, v in kwargs.iteritems(): 
    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() 
    6478            # Assert that the generated id is not already stored. 
    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): 
    6986        """Clean up expired sessions.""" 
     
    441458    path_header: if 'path' is None (the default), then the response 
    442459        cookie 'path' will be pulled from request.headers[path_header]. 
    443460    name: the name of the cookie. 
    444     timeout: the expiration timeout for the cookie. 
     461    timeout: the expiration timeout (in minutes) for both the cookie and 
     462        stored session data. 
    445463    domain: the cookie domain. 
    446464    secure: if False (the default) the cookie 'secure' value will not 
    447465        be set. If True, the cookie 'secure' value will be set (to 1). 
     
    478496        if hasattr(sess, "setup"): 
    479497            sess.setup() 
    480498     
     499    set_response_cookie(path=path, path_header=path_header, name=name, 
     500                        timeout=timeout, domain=domain, secure=secure) 
     501 
     502 
     503def set_response_cookie(path=None, path_header=None, name='session_id', 
     504                        timeout=60, domain=None, secure=False): 
     505    """Set a response cookie for the client. 
     506     
     507    path: the 'path' value to stick in the response cookie metadata. 
     508    path_header: if 'path' is None (the default), then the response 
     509        cookie 'path' will be pulled from request.headers[path_header]. 
     510    name: the name of the cookie. 
     511    timeout: the expiration timeout for the cookie. 
     512    domain: the cookie domain. 
     513    secure: if False (the default) the cookie 'secure' value will not 
     514        be set. If True, the cookie 'secure' value will be set (to 1). 
     515    """ 
    481516    # Set response cookie 
    482517    cookie = cherrypy.response.cookie 
    483     cookie[name] = sess.id 
    484     cookie[name]['path'] = path or request.headers.get(path_header) or '/' 
     518    cookie[name] = cherrypy._serving.session.id 
     519    cookie[name]['path'] = (path or cherrypy.request.headers.get(path_header) 
     520                            or '/') 
    485521     
    486522    # We'd like to use the "max-age" param as indicated in 
    487523    # http://www.faqs.org/rfcs/rfc2109.html but IE doesn't 
  • test/test_session.py

    old new  
    6767        def iredir(self): 
    6868            raise cherrypy.InternalRedirect('/blah') 
    6969        iredir.exposed = True 
     70         
     71        def regen(self): 
     72            cherrypy.tools.sessions.regenerate() 
     73            return "logged in" 
     74        regen.exposed = True 
    7075     
    7176    cherrypy.tree.mount(Root()) 
    7277    cherrypy.config.update({'environment': 'test_suite'}) 
     
    174179        path = os.path.join(localDir, "session-" + id) 
    175180        os.unlink(path) 
    176181        self.getPage('/testStr', self.cookies) 
     182     
     183    def test_5_regenerate(self): 
     184        self.getPage('/testStr') 
     185        # grab the cookie ID 
     186        id1 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1] 
     187        self.getPage('/regen') 
     188        self.assertBody('logged in') 
     189        id2 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1] 
     190        self.assertNotEqual(id1, id2) 
    177191 
    178192 
    179193 

Hosted by WebFaction

Log in as guest/cpguest to create tickets