Flask Session Interface API
SessionInterface is the low-level abstraction of Session, allowing you to replace Flask's default cookie storage with external storage such as Redis or a database.
A custom implementation only needs to inherit from SessionInterface and implement two methods.
SessionInterface abstract base class
| Attribute / method | Description |
|---|---|
| null_session_class | The class used when Session is unavailable. Defaults to NullSession |
| pickle_based | bool, whether Session is serialized based on pickle. Defaults to False |
| open_session(app, request) | Read Session data from the request. Returns a SessionMixin instance or None |
| save_session(app, session, response) | Save Session data to the response |
| make_null_session(app) | Create an empty Session (automatically called when open_session returns None) |
| is_null_session(obj) | Determine whether it is a NullSession instance |
Cookie option methods
The following methods are used to obtain the various attributes of the Session cookie and can be overridden:
| Method | Description |
|---|---|
| get_cookie_name(app) | Get the cookie name. Reads the SESSION_COOKIE_NAME config by default |
| get_cookie_domain(app) | Get the cookie Domain. Reads the SESSION_COOKIE_DOMAIN config by default |
| get_cookie_path(app) | Get the cookie Path. Reads SESSION_COOKIE_PATH or APPLICATION_ROOT by default |
| get_cookie_httponly(app) | Get the HttpOnly flag. Reads SESSION_COOKIE_HTTPONLY by default |
| get_cookie_secure(app) | Get the Secure flag. Reads SESSION_COOKIE_SECURE by default |
| get_cookie_samesite(app) | Get the SameSite value. Reads SESSION_COOKIE_SAMESITE by default |
| get_cookie_partitioned(app) | Get the Partitioned flag. Reads SESSION_COOKIE_PARTITIONED by default (v3.1+) |
| get_expiration_time(app, session) | Get the Session expiration time. A permanent Session returns now + lifetime |
| should_set_cookie(app, session) | Determine whether a cookie needs to be set. Returns True when the Session is modified or permanent + SESSION_REFRESH_EACH_REQUEST |
Built-in implementations
| class | Description |
|---|---|
| SecureCookieSessionInterface | Default implementation. Uses itsdangerous to sign cookies for storing Session data. key_derivation="hmac", digest_method=sha1 |
| SecureCookieSession | A Session class based on CallbackDict. Detects modifications to top-level keys and automatically sets modified=True |
| NullSession | A placeholder Session used when SECRET_KEY is not set. Reads work normally, but modifications raise an error |
| SessionMixin | A Mixin for the Session class. Provides the permanent, new, modified, and accessed attributes |
Replace the default Session implementation
Example
from
flask
import
Flask
from
flask.
sessions
import
SessionInterface
,
SessionMixin
Custom Session class
class
MySession
(
dict
,
SessionMixin
)
:
pass
Custom Session interface
class
MySessionInterface
(
SessionInterface
)
:
def
open_session
(
self
,
app
,
request
)
:
Load the Session from the request
user_id
=
request.
headers
.
get
(
"X-User-ID"
)
if
user_id:
return
MySession
(
user_id
=
user_id
)
return
MySession
(
)
def
save_session
(
self
,
app
,
session
,
response
)
:
Save the Session to the response
if
"user_id"
in
session:
response.
headers
[
"X-Session-ID"
]
=
session
[
"user_id"
]
app
=
Flask
(
name
)
Replace the default Session implementation
app.
session_interface
=
MySessionInterface
(
)