pyramid.session¶
- SignedCookieSessionFactory(secret, cookie_name='session', max_age=None, path='/', domain=None, secure=False, httponly=False, samesite='Lax', set_on_exception=True, timeout=1200, reissue_time=0, hashalg='sha512', salt='pyramid.session.', serializer=None)[source]¶
 New in version 1.5.
Configure a session factory which will provide signed cookie-based sessions. The return value of this function is a session factory, which may be provided as the
session_factoryargument of apyramid.config.Configuratorconstructor, or used as thesession_factoryargument of thepyramid.config.Configurator.set_session_factory()method.The session factory returned by this function will create sessions which are limited to storing fewer than 4000 bytes of data (as the payload must fit into a single cookie).
Parameters:
secretA string which is used to sign the cookie. The secret should be at least as long as the block size of the selected hash algorithm. For
sha512this would mean a 512 bit (64 character) secret. It should be unique within the set of secret values provided to Pyramid for its various subsystems (see Admonishment Against Secret-Sharing).hashalgThe HMAC digest algorithm to use for signing. The algorithm must be supported by the
hashliblibrary. Default:'sha512'.saltA namespace to avoid collisions between different uses of a shared secret. Reusing a secret for different parts of an application is strongly discouraged (see Admonishment Against Secret-Sharing). Default:
'pyramid.session.'.cookie_nameThe name of the cookie used for sessioning. Default:
'session'.max_ageThe maximum age of the cookie used for sessioning (in seconds). Default:
None(browser scope).pathThe path used for the session cookie. Default:
'/'.domainThe domain used for the session cookie. Default:
None(no domain).secureThe 'secure' flag of the session cookie. Default:
False.httponlyHide the cookie from Javascript by setting the 'HttpOnly' flag of the session cookie. Default:
False.samesiteThe 'samesite' option of the session cookie. Set the value to
Noneto turn off the samesite option. Default:'Lax'.timeoutA number of seconds of inactivity before a session times out. If
Nonethen the cookie never expires. This lifetime only applies to the value within the cookie. Meaning that if the cookie expires due to a lowermax_age, then this setting has no effect. Default:1200.reissue_timeThe number of seconds that must pass before the cookie is automatically reissued as the result of accessing the session. The duration is measured as the number of seconds since the last session cookie was issued and 'now'. If this value is
0, a new cookie will be reissued on every request accessing the session. IfNonethen the cookie's lifetime will never be extended.A good rule of thumb: if you want auto-expired cookies based on inactivity: set the
timeoutvalue to 1200 (20 mins) and set thereissue_timevalue to perhaps a tenth of thetimeoutvalue (120 or 2 mins). It's nonsensical to set thetimeoutvalue lower than thereissue_timevalue, as the ticket will never be reissued. However, such a configuration is not explicitly prevented.Default:
0.set_on_exceptionIf
True, set a session cookie even if an exception occurs while rendering a view. Default:True.serializerAn object with two methods:
loadsanddumps. Theloadsmethod should accept bytes and return a Python object. Thedumpsmethod should accept a Python object and return bytes. AValueErrorshould be raised for malformed inputs. If a serializer is not passed, thepyramid.session.PickleSerializerserializer will be used.
Warning
In Pyramid 2.0 the default
serializeroption will change to usepyramid.session.JSONSerializer. See Upcoming Changes to ISession in Pyramid 2.0 for more information about why this change is being made.
- BaseCookieSessionFactory(serializer, cookie_name='session', max_age=None, path='/', domain=None, secure=False, httponly=False, samesite='Lax', timeout=1200, reissue_time=0, set_on_exception=True)[source]¶
 Configure a session factory which will provide cookie-based sessions. The return value of this function is a session factory, which may be provided as the
session_factoryargument of apyramid.config.Configuratorconstructor, or used as thesession_factoryargument of thepyramid.config.Configurator.set_session_factory()method.The session factory returned by this function will create sessions which are limited to storing fewer than 4000 bytes of data (as the payload must fit into a single cookie).
Parameters:
serializerAn object with two methods:
loadsanddumps. Theloadsmethod should accept bytes and return a Python object. Thedumpsmethod should accept a Python object and return bytes. AValueErrorshould be raised for malformed inputs.cookie_nameThe name of the cookie used for sessioning. Default:
'session'.max_ageThe maximum age of the cookie used for sessioning (in seconds). Default:
None(browser scope).pathThe path used for the session cookie. Default:
'/'.domainThe domain used for the session cookie. Default:
None(no domain).secureThe 'secure' flag of the session cookie. Default:
False.httponlyHide the cookie from Javascript by setting the 'HttpOnly' flag of the session cookie. Default:
False.samesiteThe 'samesite' option of the session cookie. Set the value to
Noneto turn off the samesite option. Default:'Lax'.timeoutA number of seconds of inactivity before a session times out. If
Nonethen the cookie never expires. This lifetime only applies to the value within the cookie. Meaning that if the cookie expires due to a lowermax_age, then this setting has no effect. Default:1200.reissue_timeThe number of seconds that must pass before the cookie is automatically reissued as the result of a request which accesses the session. The duration is measured as the number of seconds since the last session cookie was issued and 'now'. If this value is
0, a new cookie will be reissued on every request accessing the session. IfNonethen the cookie's lifetime will never be extended.A good rule of thumb: if you want auto-expired cookies based on inactivity: set the
timeoutvalue to 1200 (20 mins) and set thereissue_timevalue to perhaps a tenth of thetimeoutvalue (120 or 2 mins). It's nonsensical to set thetimeoutvalue lower than thereissue_timevalue, as the ticket will never be reissued. However, such a configuration is not explicitly prevented.Default:
0.set_on_exceptionIf
True, set a session cookie even if an exception occurs while rendering a view. Default:True.