Skip to content

Commit b8ddfb4

Browse files
committed
refactor
1 parent b982db7 commit b8ddfb4

File tree

2 files changed

+58
-51
lines changed

2 files changed

+58
-51
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import logging
2+
import time
3+
4+
import aiohttp_session
5+
from aiohttp import web
6+
from aiohttp_session.cookie_storage import EncryptedCookieStorage
7+
8+
_logger = logging.getLogger(__name__)
9+
10+
11+
def _share_cookie_across_all_subdomains(
12+
response: web.StreamResponse, params: aiohttp_session._CookieParams
13+
) -> aiohttp_session._CookieParams:
14+
# share cookie across all subdomains, by appending a dot (`.`) in front of the domain name
15+
# overwrite domain from `None` (browser sets `example.com`) to `.example.com`
16+
request = response._req # pylint:disable=protected-access # noqa: SLF001
17+
assert isinstance(request, web.Request) # nosec
18+
params["domain"] = f".{request.url.host}"
19+
return params
20+
21+
22+
class SharedCookieEncryptedCookieStorage(EncryptedCookieStorage):
23+
async def save_session(
24+
self,
25+
request: web.Request,
26+
response: web.StreamResponse,
27+
session: aiohttp_session.Session,
28+
) -> None:
29+
# link response to originating request (allows to detect the orginal request url)
30+
response._req = request # pylint:disable=protected-access # noqa: SLF001
31+
32+
await super().save_session(request, response, session)
33+
34+
def save_cookie(
35+
self,
36+
response: web.StreamResponse,
37+
cookie_data: str,
38+
*,
39+
max_age: int | None = None,
40+
) -> None:
41+
# NOTE: WARNING: the only difference between the superclass and this implementation
42+
# is the statement below where the domain name is set. Adjust in case the base library changes.
43+
params = _share_cookie_across_all_subdomains(
44+
response, self._cookie_params.copy()
45+
)
46+
47+
if max_age is not None:
48+
params["max_age"] = max_age
49+
t = time.gmtime(time.time() + max_age)
50+
params["expires"] = time.strftime("%a, %d-%b-%Y %T GMT", t)
51+
52+
if not cookie_data:
53+
response.del_cookie(
54+
self._cookie_name, domain=params["domain"], path=params["path"]
55+
)
56+
else:
57+
response.set_cookie(self._cookie_name, cookie_data, **params)

services/web/server/src/simcore_service_webserver/session/plugin.py

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,18 @@
33
"""
44

55
import logging
6-
import time
76

87
import aiohttp_session
98
from aiohttp import web
10-
from aiohttp_session.cookie_storage import EncryptedCookieStorage
119
from servicelib.aiohttp.application_setup import ModuleCategory, app_module_setup
1210
from settings_library.utils_session import DEFAULT_SESSION_COOKIE_NAME
1311

12+
from ._cookie_storage import SharedCookieEncryptedCookieStorage
1413
from .settings import SessionSettings, get_plugin_settings
1514

1615
_logger = logging.getLogger(__name__)
1716

1817

19-
def _share_cookie_across_all_subdomains(
20-
response: web.StreamResponse, params: aiohttp_session._CookieParams
21-
) -> aiohttp_session._CookieParams:
22-
# share cookie across all subdomains, by appending a dot (`.`) in front of the domain name
23-
# overwrite domain from `None` (browser sets `example.com`) to `.example.com`
24-
request = response._req # pylint:disable=protected-access # noqa: SLF001
25-
assert isinstance(request, web.Request) # nosec
26-
params["domain"] = f".{request.url.host}"
27-
return params
28-
29-
30-
class SharedCookieEncryptedCookieStorage(EncryptedCookieStorage):
31-
async def save_session(
32-
self,
33-
request: web.Request,
34-
response: web.StreamResponse,
35-
session: aiohttp_session.Session,
36-
) -> None:
37-
# link response to originating request (allows to detect the orginal request url)
38-
response._req = request # pylint:disable=protected-access # noqa: SLF001
39-
40-
await super().save_session(request, response, session)
41-
42-
def save_cookie(
43-
self,
44-
response: web.StreamResponse,
45-
cookie_data: str,
46-
*,
47-
max_age: int | None = None,
48-
) -> None:
49-
# NOTE: WARNING: the only difference between the superclass and this implementation
50-
# is the statement below where the domain name is set. Adjust in case the base library changes.
51-
params = _share_cookie_across_all_subdomains(
52-
response, self._cookie_params.copy()
53-
)
54-
55-
if max_age is not None:
56-
params["max_age"] = max_age
57-
t = time.gmtime(time.time() + max_age)
58-
params["expires"] = time.strftime("%a, %d-%b-%Y %T GMT", t)
59-
if not cookie_data:
60-
61-
response.del_cookie(
62-
self._cookie_name, domain=params["domain"], path=params["path"]
63-
)
64-
else:
65-
response.set_cookie(self._cookie_name, cookie_data, **params)
66-
67-
6818
@app_module_setup(
6919
__name__, ModuleCategory.ADDON, settings_name="WEBSERVER_SESSION", logger=_logger
7020
)

0 commit comments

Comments
 (0)