How do I use a cookie session with pyramid & beaker to properly store a session_id?
<p>I am using the pyramid framework with beaker as a back-end for session management, and I want to store a session_id within a signed cookie. The session-id is associated with a real user_id, and the association mappings will be stored in something like redis. Can I simply use the configuration below to achieve this? The documentation doesn't provide a full implementation and only shows an example using an unsecure session factory.  </p>

<p>Here is my configuration file, redacted to session details:</p>

<pre><code>session.type = cookie
session.data_dir = %(here)s/data/sessions/data
session.lock_dir = %(here)s/data/sessions/lock
session.key = session_key
session.secret = as98&amp;$Hh94
session.cookie_on_exception = true
</code></pre>

<p>Here is my <strong>init</strong>.py:</p>

<pre><code>config = Configurator(settings=settings)
config.include('pyramid_jinja2')
config.include('pyramid_beaker')
config.include('.models')
config.include('.routes')
config.include('polatick.models')
config.scan()
return config.make_wsgi_app()
</code></pre>

<p>Do I simply use the forget and remember functions in pyramid to do this properly?</p>

<p>I was thinking of the process going something along the lines of something simple like this:</p>

<pre><code>if user_authenticates():
   session_id = create_session_id()
   redis.put_entry(session_id, user_id)
   request.session['session_id'] = session_id
   headers = remember(request, login)
   return HTTPFound('/', headers=headers)
</code></pre>

<p>I've been following this documentation:<br>
<a href="http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/pylons/sessions.html" rel="nofollow">http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/pylons/sessions.html</a></p>

<p>The Pyramid Community Cookbook is not official documentation. It is a collection of user-contributed recipes. That one in particular is targeted toward users of the web framework Pylons who are migrating solutions to Pyramid.</p>

<p>Instead you should look at the official documentation on <a href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/sessions.html" rel="nofollow">Sessions</a>. This provides session implementation out of the box, and allows you to implement your own session factory.</p>

<p>Following that, there is an implementation of sessions with authentication (and later on with authorization) in the <a href="http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/authentication.html" rel="nofollow">wiki tutorial</a>.</p>

<p><a href="http://docs.pylonsproject.org/projects/pyramid-nacl-session/en/latest/" rel="nofollow">pyramid_nacl_session</a> defines an encrypting, pickle-based cookie serializer, using PyNaCl to generate the symmetric encryption for the cookie state.</p>

<p>There is also <a href="http://pyramid-redis-sessions.readthedocs.io/en/latest/index.html" rel="nofollow">pyramid_redis_sessions</a>, a Pyramid add-on, which implements Pyramid's ISession interface, using Redis as its backend.</p>

<p>Finally for a list of packages that provide sessions, authorization, or authentication, see <a href="https://trypyramid.com/resources-extending-pyramid.html" rel="nofollow">Try Pyramid - Extending Pyramid</a>.</p>

python session pyramid beaker