Skip to content

Instantly share code, notes, and snippets.

@flibustenet
Forked from mmerickel/gist:8035611
Last active August 29, 2015 14:00
Show Gist options
  • Save flibustenet/11248284 to your computer and use it in GitHub Desktop.
Save flibustenet/11248284 to your computer and use it in GitHub Desktop.
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