client_side

members:

class litestar.middleware.session.client_side.ClientSideSessionBackend

基类:BaseSessionBackend[CookieBackendConfig]

SessionMiddleware 的 Cookie 后端。

__init__(config: CookieBackendConfig) None

初始化 ClientSideSessionBackend。 Args: config: SessionCookieConfig 实例。

dump_data(data: Any, scope: Scope | None = None) list[bytes]

给定可序列化的数据(包括 pydantic 模型和 numpy 类型),将其转储为字节字符串,进行加密、编码,并分割为指定大小的块。 Args: data: 待序列化、加密、编码和分块的数据。 scope: ASGI 连接作用域。 Notes: - 返回的列表由单个 base64 编码字符串的块组成,该字符串使用 AES-CGM 进行加密。 Returns: 编码后的字节字符串列表,每个字符串的最大长度等于 CHUNK_SIZE 常量。

load_data(data: list[bytes]) dict[str, Any]

给定一个字符串列表,将其解码为会话对象。 Args: data: 源自请求会话 cookie 的字符串列表。 Returns: 反序列化的会话值。

如果匹配会话 cookie 模式,则从连接中返回 cookie 键列表。 Args: connection: 一个 ASGIConnection 实例 Returns: 会话 cookie 键的列表

如果匹配会话 cookie 模式,则从连接中返回一组 cookie 键。 .. versionadded:: 2.8.3 Args: connection: 一个 ASGIConnection 实例 Returns: 一组会话 cookie 键

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

scope_session 中的数据以 Cookie 的形式存储在 Message 中。如果 scope_session 的内容过大,无法放入单个 Cookie,则会按照 <cookie key>-<n> 的命名规则拆分为多个 Cookie。如果会话为空或缩小,将通过将其值设置为 "null" 来清除相应的 Cookie。 Args: scope_session: 当前要存储的会话 message: 待发送的 outgoing send-message connection: 包含作用域(scope)的原始 ASGIConnection Returns: None

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

从连接的会话 cookie 中加载会话数据,并将其作为字典返回。 Args: connection: 源 ASGIConnection Returns: 会话数据

get_session_id(connection: ASGIConnection) str | None

Try to fetch session id from connection ScopeState. If one does not exist, generate one.

参数:

connection -- Originating ASGIConnection containing the scope

返回:

Session id str or None if the concept of a session id does not apply.

class litestar.middleware.session.client_side.CookieBackendConfig

基类:BaseBackendConfig[ClientSideSessionBackend]

[SessionMiddleware] 中间件的配置。

secret: bytes

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.

key: str = 'session'

Key to use for the cookie inside the header, e.g. session=<data> where session is the cookie key and <data> is the session data.

备注

  • If a session cookie exceeds 4KB in size it is split. In this case the key will be of the format session-{segment number}.

max_age: int = 1209600

Maximal age of the cookie before its invalidated.

path: str = '/'

Path fragment that must exist in the request url for the cookie to be valid.

Defaults to '/'.

domain: str | None = None

Domain for which the cookie is valid.

__init__(secret: bytes, key: str = 'session', max_age: int = 1209600, scopes: set[~typing.Literal[ScopeType.HTTP, ScopeType.WEBSOCKET]]=<factory>, path: str = '/', domain: str | None = None, secure: bool = False, httponly: bool = True, samesite: Literal['lax', 'strict', 'none']='lax', exclude: str | list[str] | None = None, exclude_opt_key: str = 'skip_session') None
secure: bool = False

Https is required for the cookie.

httponly: bool = True

Forbids javascript to access the cookie via 'Document.cookie'.

samesite: Literal['lax', 'strict', 'none'] = 'lax'

Controls whether or not a cookie is sent with cross-site requests.

Defaults to lax.

exclude: str | list[str] | None = None

A pattern or list of patterns to skip in the session middleware.

exclude_opt_key: str = 'skip_session'

An identifier to use on routes to disable the session middleware for a particular route.