SessionInterface Class — flask Architecture
Architecture documentation for the SessionInterface class in sessions.py from the flask codebase.
Entity Profile
Dependency Diagram
graph TD bbf56873_bd71_6e70_f44c_ca8a85b0a007["SessionInterface"] f793a407_79ea_667a_f29e_29bbf57c781f["sessions.py"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|defined in| f793a407_79ea_667a_f29e_29bbf57c781f a3a36a1e_dcc4_1f61_b5f5_bce1cf9ca5df["make_null_session()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| a3a36a1e_dcc4_1f61_b5f5_bce1cf9ca5df 1b3ce05d_9f5d_ca0f_d90c_87f0a25a18ba["is_null_session()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| 1b3ce05d_9f5d_ca0f_d90c_87f0a25a18ba 6255d447_9270_b32e_3537_d10688208a06["get_cookie_name()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| 6255d447_9270_b32e_3537_d10688208a06 522c0d60_ef75_10cc_88d3_4d9ccebd6a54["get_cookie_domain()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| 522c0d60_ef75_10cc_88d3_4d9ccebd6a54 b6687bcd_1618_cb97_0b08_4df52250e96d["get_cookie_path()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| b6687bcd_1618_cb97_0b08_4df52250e96d e21674ac_874b_e0a2_10a3_9f42073cd29e["get_cookie_httponly()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| e21674ac_874b_e0a2_10a3_9f42073cd29e def4e514_2812_db8f_9133_e44c29c504ff["get_cookie_secure()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| def4e514_2812_db8f_9133_e44c29c504ff f7e98be5_31d8_e5f5_ca82_ecbda038c0e5["get_cookie_samesite()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| f7e98be5_31d8_e5f5_ca82_ecbda038c0e5 8664806b_8ac2_5d8d_7c97_c0f8d164f24e["get_cookie_partitioned()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| 8664806b_8ac2_5d8d_7c97_c0f8d164f24e e9cd7e54_4bc9_3d51_b001_5bb10f298bfb["get_expiration_time()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| e9cd7e54_4bc9_3d51_b001_5bb10f298bfb 2cf7467e_84a3_e5f7_ca7c_32bde50158e9["should_set_cookie()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| 2cf7467e_84a3_e5f7_ca7c_32bde50158e9 f07e3f84_ffc5_9004_1d81_703a5234af84["open_session()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| f07e3f84_ffc5_9004_1d81_703a5234af84 22e64eec_34fe_f9c3_c75a_bbd842b167e0["save_session()"] bbf56873_bd71_6e70_f44c_ca8a85b0a007 -->|method| 22e64eec_34fe_f9c3_c75a_bbd842b167e0
Relationship Graph
Source Code
src/flask/sessions.py lines 114–284
class 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 are
:meth:`open_session` and :meth:`save_session`, the others have
useful defaults which you don't need to change.
The session object returned by the :meth:`open_session` method has to
provide a dictionary like interface plus the properties and methods
from the :class:`SessionMixin`. We recommend just subclassing a dict
and adding that mixin::
class Session(dict, SessionMixin):
pass
If :meth:`open_session` returns ``None`` Flask will call into
:meth:`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 :class:`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 :attr:`flask.Flask.session_interface`::
app = Flask(__name__)
app.session_interface = MySessionInterface()
Multiple requests with the same session may be sent and handled
concurrently. When implementing a new session interface, consider
whether reads or writes to the backing store must be synchronized.
There is no guarantee on the order in which the session for each
request is opened or saved, it will occur in the order that requests
begin and end processing.
.. versionadded:: 0.8
"""
#: :meth:`make_null_session` will look here for the class that should
#: be created when a null session is requested. Likewise the
#: :meth:`is_null_session` method will perform a typecheck against
#: this type.
null_session_class = NullSession
#: 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.
#:
#: .. versionadded:: 0.10
pickle_based = False
def make_null_session(self, app: Flask) -> NullSession:
"""Creates a null session which acts as a replacement object if the
real session support could not be loaded due to a configuration
error. This mainly aids the user experience because the job of the
null session is to still support lookup without complaining but
modifications are answered with a helpful error message of what
failed.
This creates an instance of :attr:`null_session_class` by default.
"""
return self.null_session_class()
def is_null_session(self, obj: object) -> bool:
"""Checks if a given object is a null session. Null sessions are
not asked to be saved.
This checks if the object is an instance of :attr:`null_session_class`
by default.
"""
return isinstance(obj, self.null_session_class)
def get_cookie_name(self, app: Flask) -> str:
"""The name of the session cookie. Uses``app.config["SESSION_COOKIE_NAME"]``."""
return app.config["SESSION_COOKIE_NAME"] # type: ignore[no-any-return]
def get_cookie_domain(self, app: Flask) -> str | None:
"""The value of the ``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 :data:`SESSION_COOKIE_DOMAIN` config.
Domain
Defined In
Source
Frequently Asked Questions
What is the SessionInterface class?
SessionInterface is a class in the flask codebase, defined in src/flask/sessions.py.
Where is SessionInterface defined?
SessionInterface is defined in src/flask/sessions.py at line 114.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free