-
-
Save flibustenet/11248284 to your computer and use it in GitHub Desktop.
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(16) | |
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): | |
# appstruct = (self.accessed, self.created, dict(self)) | |
d = appstruct[2] | |
if 'session_id' in d: | |
session_id = d['session_id'] | |
else: | |
d['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