cookie_backend#

class starlite.middleware.session.cookie_backend.CookieBackend#

Bases: BaseSessionBackend[CookieBackendConfig]

Cookie backend for SessionMiddleware.

__init__(config: CookieBackendConfig) None#

Initialize CookieBackend.

Parameters:

config – SessionCookieConfig instance.

dump_data(data: Any, scope: Optional[Scope] = None) List[bytes]#

Given serializable data, including pydantic models and numpy types, dump it into a bytes string, encrypt, encode and split it into chunks of the desirable size.

Parameters:
  • data – Data to serialize, encrypt, encode and chunk.

  • scope – The ASGI connection scope.

Notes

  • The returned list is composed of a chunks of a single base64 encoded string that is encrypted using AES-CGM.

Returns:

List of encoded bytes string of a maximum length equal to the CHUNK_SIZE constant.

load_data(data: List[bytes]) Dict[str, Any]#

Given a list of strings, decodes them into the session object.

Parameters:

data – A list of strings derived from the request’s session cookie(s).

Returns:

A deserialized session value.

Return a list of cookie-keys from the connection if they match the session-cookie pattern.

Parameters:

connection – An ASGIConnection instance

Returns:

A list of session-cookie keys

async store_in_message(scope_session: ScopeSession, message: Message, connection: ASGIConnection) None#

Store data from scope_session in Message in the form of cookies. If the contents of scope_session are too large to fit a single cookie, it will be split across several cookies, following the naming scheme of <cookie key>-<n>. If the session is empty or shrinks, cookies will be cleared by setting their value to "null"

Parameters:
  • scope_session – Current session to store

  • message – Outgoing send-message

  • connection – Originating ASGIConnection containing the scope

Returns:

None

async load_from_connection(connection: ASGIConnection) Dict[str, Any]#

Load session data from a connection’s session-cookies and return it as a dictionary.

Parameters:

connection – Originating ASGIConnection

Returns:

The session data

class starlite.middleware.session.cookie_backend.CookieBackendConfig#

Bases: BaseBackendConfig

Configuration for [SessionMiddleware] middleware.

secret: SecretBytes#

A secret key to use for generating an encryption key.

Must have a length of 16 (128 bits), 24 (192 bits) or 32 (256 bits) characters.

classmethod validate_secret(value: SecretBytes) SecretBytes#

Ensure that the secret value is 128, 192 or 256 bits.

Parameters:

value – A bytes string.

Raises:

ValueError – if the bytes string is of incorrect length.

Returns:

A bytes string.