44
55import textwrap
66
7- from pydantic import BaseModel , ConfigDict , Field , SecretStr
7+ from pydantic import BaseModel , ConfigDict , Field , SecretStr , ValidationInfo , field_validator
88
99DEFAULT_MAX_AGE = 1_209_600
1010
1111
1212class SessionSettings (BaseModel ):
1313 """Session Middleware Settings.
1414
15+ Required for :ref:`keycloak-auth-provider`.
16+
1517 See `SessionMiddleware <https://www.starlette.io/middleware/#sessionmiddleware>`_ documentation.
1618
1719 .. note::
@@ -24,6 +26,7 @@ class SessionSettings(BaseModel):
2426
2527 .. code-block:: bash
2628
29+ DATA_RENTGEN__SERVER__SESSION__ENABLED=True
2730 DATA_RENTGEN__SERVER__SESSION__SECRET_KEY=secret
2831 DATA_RENTGEN__SERVER__SESSION__SESSION_COOKIE=custom_cookie_name
2932 DATA_RENTGEN__SERVER__SESSION__MAX_AGE=None # cookie will last as long as the browser session
@@ -33,7 +36,12 @@ class SessionSettings(BaseModel):
3336
3437 """
3538
36- secret_key : SecretStr = Field (
39+ enabled : bool = Field (
40+ default = False ,
41+ description = "Set to ``True`` to enable SessionMiddleware" ,
42+ )
43+ secret_key : SecretStr | None = Field (
44+ default = None ,
3745 description = textwrap .dedent (
3846 """
3947 Secret key for encrypting cookies.
@@ -66,3 +74,11 @@ class SessionSettings(BaseModel):
6674 )
6775
6876 model_config = ConfigDict (extra = "allow" )
77+
78+ @field_validator ("secret_key" )
79+ @classmethod
80+ def _validate_secret_key (cls , value : SecretStr | None , info : ValidationInfo ) -> SecretStr | None :
81+ if not value and info .data .get ("enabled" ):
82+ msg = "secret_key is required"
83+ raise ValueError (msg )
84+ return value
0 commit comments