Skip to content

Настройки cookie для сессий

Эти настройки используются для управления параметрами cookie, используемых для хранения данных пользовательской сессии.

Bases: BaseModel

Session Middleware Settings.

Required for :ref:auth-server-keycloak.

See SessionMiddleware <https://www.starlette.io/middleware/#sessionmiddleware>_ documentation.

.. note::

You can pass here any extra option supported by ``SessionMiddleware``,
even if it is not mentioned in documentation.

Examples

.. code-block:: bash

DATA_RENTGEN__SERVER__SESSION__ENABLED=True
DATA_RENTGEN__SERVER__SESSION__SECRET_KEY=secret
DATA_RENTGEN__SERVER__SESSION__SESSION_COOKIE=custom_cookie_name
DATA_RENTGEN__SERVER__SESSION__MAX_AGE=None  # cookie will last as long as the browser session
DATA_RENTGEN__SERVER__SESSION__SAME_SITE=strict
DATA_RENTGEN__SERVER__SESSION__HTTPS_ONLY=True
DATA_RENTGEN__SERVER__SESSION__DOMAIN=example.com
Source code in data_rentgen/server/settings/session.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class SessionSettings(BaseModel):
    """Session Middleware Settings.

    Required for :ref:`auth-server-keycloak`.

    See `SessionMiddleware <https://www.starlette.io/middleware/#sessionmiddleware>`_ documentation.

    .. note::

        You can pass here any extra option supported by ``SessionMiddleware``,
        even if it is not mentioned in documentation.

    Examples
    --------

    .. code-block:: bash

        DATA_RENTGEN__SERVER__SESSION__ENABLED=True
        DATA_RENTGEN__SERVER__SESSION__SECRET_KEY=secret
        DATA_RENTGEN__SERVER__SESSION__SESSION_COOKIE=custom_cookie_name
        DATA_RENTGEN__SERVER__SESSION__MAX_AGE=None  # cookie will last as long as the browser session
        DATA_RENTGEN__SERVER__SESSION__SAME_SITE=strict
        DATA_RENTGEN__SERVER__SESSION__HTTPS_ONLY=True
        DATA_RENTGEN__SERVER__SESSION__DOMAIN=example.com

    """

    enabled: bool = Field(
        default=True,
        description="Set to ``True`` to enable SessionMiddleware",
    )
    secret_key: SecretStr | None = Field(
        default=None,
        description=textwrap.dedent(
            """
            Secret key for encrypting cookies.

            Can be any string. It is recommended to generate random value for every application instance, e.g.:

            .. code:: shell

                pwgen 32 1
            """,
        ),
    )
    session_cookie: str | None = Field(
        default="session",
        description="Name of the session cookie. Change this if there are multiple application under the same domain.",
    )
    max_age: int | None = Field(
        default=DEFAULT_MAX_AGE,
        description="Session expiry time in seconds. Defaults to 2 weeks.",
    )
    same_site: str | None = Field(
        default="lax",
        description="Prevents cookie from being sent with cross-site requests.",
    )
    path: str | None = Field(default="/", description="Path to restrict session cookie access.")
    https_only: bool = Field(default=False, description="Secure flag for HTTPS-only access.")
    domain: str | None = Field(
        default=None,
        description="Domain for sharing cookies between subdomains or cross-domains.",
    )

    model_config = ConfigDict(extra="allow")

    @field_validator("secret_key", mode="after")
    @classmethod
    def _validate_secret_key(cls, value: SecretStr | None, info: ValidationInfo) -> SecretStr | None:
        if not value and info.data.get("enabled"):
            msg = "secret_key is required"
            raise ValueError(msg)
        return value