Opened 11 years ago

Last modified 10 years ago

#668 closed enhancement

Replace beaker sessions with itsdangerous based sessions — at Version 2

Reported by: Elrond Owned by:
Priority: major Milestone:
Component: programming Keywords: sprint
Cc: Christopher Allan Webber Parent Tickets:

Description (last modified by Elrond)

We want to switch from beaker sessions to itsdangerous based sessions.
Why? We should come up with a better list of reaasons than "The core developers like this idea".

What's needed?

  1. Remove beaker sessions from paste*.ini
  2. Create a class that will be our new request.session
  3. Create code to create such an instance from a received cookie.
  4. Create code to send a (modified) session as a cookie to the client.

Some more Details

Create a class that will be our new request.session

Some pseudo code:

class MGSession(dict):

    def save(self):
        self.send_new_cookie = True

    def delete(self):
        self.clear()
        self.save()

Create Session Instance from cookie

Here's some pseudo code to do that. It includes code for future "revocation support". Because we do not store anything locally any more, one can only forcibly block sessions by blacklisting. In the first step, its okay to document the needed approach in the code.

class SessionManager(object):
    def load_session_from_cookie(request, cookie_name):
        cookie = request.get_cookie(cookie_name)
        if not cookie:
            request.session = MGSession()
            return
        m = BadCookie.query.filter_by(cookie = cookie)
        if m:
            _log.warn("Bad cookie received: %s", m.reason)
            raise BadRequest()
        parsed_dict = self.signer.loads(cookie, max_age=...)
        requestion.session = MGSession(parsed_dict)

Send cookie

  • Only send a cookie, if an update is needed
  • If the session is empty delete the cookie on the client (MGSession.delete() was called probably)
  • Bonus points: Consider sending a new cookie, if the old one is going to expire soon

Change History (2)

comment:1 by Elrond, 11 years ago

Type: defectenhancement

From #580:
This might be helpful too: http://flask.pocoo.org/snippets/51/

comment:2 by Elrond, 11 years ago

Description: modified (diff)

some small changes to example pseudo code, mostly suggesting a SessionManager class.

Note: See TracTickets for help on using tickets.