Created
December 19, 2013 07:25
-
-
Save mmerickel/8035611 to your computer and use it in GitHub Desktop.
backend-based session storage in pyramid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import sha256 | |
import os | |
from pyramid.session import SignedCookieSessionFactory | |
def make_session_id(): | |
rand = os.urandom() | |
return sha256(sha256(rand).digest()).hexdigest() | |
class MemorySessionSerializer(object): | |
def __init__(self): | |
self.sessions = {} | |
def loads(self, bstruct): | |
session_id = bstruct.decode('utf-8') | |
try: | |
return self.sessions[session_id] | |
except KeyError: | |
raise ValueError | |
def dumps(self, appstruct): | |
# damn, we have no unhacky way to reuse the old session id | |
session_id = make_session_id() | |
self.sessions[session_id] = appstruct | |
return session_id.encode('utf-8') | |
def main(global_config, **settings): | |
config = Configurator(settings=settings) | |
serializer = MemorySessionSerializer() | |
session_factory = SignedCookieSessionFactory( | |
settings['session.secret'], | |
serializer=serializer, | |
) | |
config.set_session_factory(session_factory) | |
return config.make_wsgi_app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment