-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1efdaa
commit 0a41541
Showing
12 changed files
with
94 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ Configuration | |
database | ||
broker | ||
logging | ||
session | ||
cors | ||
debug | ||
monitoring | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.. _backend-configuration-server-session: | ||
|
||
Session settings | ||
================ | ||
|
||
These settings used to control `Session <https://developer.mozilla.org/en-US/docs/Web/HTTP/Session>`_ options. | ||
|
||
.. autopydantic_model:: syncmaster.backend.settings.server.session.SessionSettings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import secrets | ||
|
||
from fastapi import FastAPI | ||
from starlette.middleware.sessions import SessionMiddleware | ||
|
||
from syncmaster.backend.settings.server.session import SessionSettings | ||
|
||
def apply_session_middleware(app: FastAPI) -> FastAPI: | ||
|
||
def apply_session_middleware(app: FastAPI, settings: SessionSettings) -> FastAPI: | ||
"""Add SessionMiddleware middleware to the application.""" | ||
secret_key = secrets.token_urlsafe(32) | ||
app.add_middleware(SessionMiddleware, secret_key=secret_key) | ||
|
||
app.add_middleware( | ||
SessionMiddleware, | ||
**settings.dict(), | ||
) | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
class SessionSettings(BaseModel): | ||
"""Session Middleware Settings. | ||
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 | ||
-------- | ||
For development environment: | ||
.. code-block:: bash | ||
SYNCMASTER__SERVER__SESSION__SECRET_KEY=secret | ||
SYNCMASTER__SERVER__SESSION__SESSION_COOKIE=custom_cookie_name | ||
SYNCMASTER__SERVER__SESSION__MAX_AGE=None # cookie will last as long as the browser session | ||
SYNCMASTER__SERVER__SESSION__SAME_SITE=strict | ||
SYNCMASTER__SERVER__SESSION__HTTPS_ONLY=True | ||
SYNCMASTER__SERVER__SESSION__DOMAIN=example.com | ||
For production environment: | ||
.. code-block:: bash | ||
SYNCMASTER__SERVER__SESSION__SECRET_KEY=secret | ||
SYNCMASTER__SERVER__SESSION__HTTPS_ONLY=True | ||
""" | ||
|
||
secret_key: str = Field(description="A random string for signing cookies.") | ||
session_cookie: str | None = Field(default="session", description="Name of the session cookie.") | ||
max_age: int | None = Field(default=1209600, 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.", | ||
) | ||
|
||
class Config: | ||
extra = "allow" |