SecureCookieSession
Base class for sessions based on signed cookies. This session backend will set themodified and accessed attributes. It cannot reliably track whether a session is new (vs. empty), so new remains hard coded to False.
Attributes
modified
When data is changed, this is set toTrue. Only the session dictionary itself is tracked; if the session contains mutable data (for example a nested dict) then this must be set to True manually when modifying that data. The session cookie will only be written to the response if this is True.
- Type:
bool - Default:
False
Constructor
Initial data for the session
SessionMixin
Expands a basic dictionary with session attributes. This is the base for all session implementations.Properties
permanent
'_permanent' key in the dict. When set to True, the session will use the PERMANENT_SESSION_LIFETIME config value for expiration.
new
Some implementations can detect whether a session is newly created, but that is not guaranteed. Use with caution. The mixin default is hard-codedFalse.
- Type:
bool - Default:
False
modified
Some implementations can detect changes to the session and set this when that happens. The mixin default is hard coded toTrue.
- Type:
bool - Default:
True
accessed
Indicates if the session was accessed, even if it was not modified. This is set when the session object is accessed through the request context, including the globalsession proxy. A Vary: cookie header will be added if this is True.
- Type:
bool - Default:
False
SessionInterface
The basic interface you have to implement in order to replace the default session interface which uses werkzeug’s securecookie implementation. The only methods you have to implement areopen_session and save_session, the others have useful defaults which you don’t need to change.
The session object returned by the open_session method has to provide a dictionary like interface plus the properties and methods from the SessionMixin. We recommend just subclassing a dict and adding that mixin:
open_session returns None Flask will call into make_null_session to create a session that acts as replacement if the session support cannot work because some requirement is not fulfilled. The default NullSession class that is created will complain that the secret key was not set.
To replace the session interface on an application all you have to do is to assign flask.Flask.session_interface:
Class Attributes
null_session_class
make_null_session() will look here for the class that should be created when a null session is requested. Likewise the is_null_session() method will perform a typecheck against this type.
- Type:
type[NullSession] - Default:
NullSession
pickle_based
A flag that indicates if the session interface is pickle based. This can be used by Flask extensions to make a decision in regards to how to deal with the session object.- Type:
bool - Default:
False - Added in: 0.10
Methods
make_null_session()
null_session_class by default.
The Flask application instance
is_null_session()
null_session_class by default.
The object to check
get_cookie_name()
app.config["SESSION_COOKIE_NAME"].
The Flask application instance
get_cookie_domain()
Domain parameter on the session cookie. If not set, browsers will only send the cookie to the exact domain it was set from. Otherwise, they will send it to any subdomain of the given value as well.
Uses the SESSION_COOKIE_DOMAIN config.
Changed in 2.3: Not set by default, does not fall back to SERVER_NAME.
The Flask application instance
get_cookie_path()
SESSION_COOKIE_PATH config var if it’s set, and falls back to APPLICATION_ROOT or uses / if it’s None.
The Flask application instance
get_cookie_httponly()
SESSION_COOKIE_HTTPONLY config var.
The Flask application instance
get_cookie_secure()
SESSION_COOKIE_SECURE setting.
The Flask application instance
get_cookie_samesite()
'Strict' or 'Lax' if the cookie should use the SameSite attribute. This currently just returns the value of the SESSION_COOKIE_SAMESITE setting.
The Flask application instance
get_cookie_partitioned()
SESSION_COOKIE_PARTITIONED.
Added in: 3.1
The Flask application instance
get_expiration_time()
None if the session is linked to the browser session. The default implementation returns now + the permanent session lifetime configured on the application.
The Flask application instance
The session object
should_set_cookie()
Set-Cookie header should be set for this session cookie for this response. If the session has been modified, the cookie is set. If the session is permanent and the SESSION_REFRESH_EACH_REQUEST config is true, the cookie is always set.
This check is usually skipped if the session was deleted.
Added in: 0.11
The Flask application instance
The session object
open_session()
SessionMixin interface.
This will return None to indicate that loading failed in some way that is not immediately an error. The request context will fall back to using make_null_session in this case.
The Flask application instance
The request object
save_session()
is_null_session returns True.
The Flask application instance
The session object
The response object
SecureCookieSessionInterface
The default session interface that stores sessions in signed cookies through theitsdangerous module.
Class Attributes
salt
The salt that should be applied on top of the secret key for the signing of cookie based sessions.- Type:
str - Default:
"cookie-session"
digest_method
The hash function to use for the signature.- Type:
Callable - Default:
sha1
key_derivation
The name of the itsdangerous supported key derivation.- Type:
str - Default:
"hmac"
serializer
A python serializer for the payload. The default is a compact JSON derived serializer with support for some extra Python types such as datetime objects or tuples.- Type:
TaggedJSONSerializer
session_class
The session class to use.- Type:
type[SecureCookieSession] - Default:
SecureCookieSession
Methods
get_signing_serializer()
None if no secret key is configured.
The Flask application instance
