-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_cookies.py
More file actions
43 lines (36 loc) · 1.28 KB
/
Copy pathsession_cookies.py
File metadata and controls
43 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
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
"""Helpers for auth cookies used by the browser refresh flow."""
from __future__ import annotations
from starlette.responses import Response
from app.core.config import settings
def set_auth_cookies(
response: Response, *, refresh_token: str, csrf_token: str
) -> None:
"""Attach refresh and CSRF cookies to a response."""
response.set_cookie(
key=settings.REFRESH_COOKIE_NAME,
value=refresh_token,
httponly=True,
secure=settings.AUTH_COOKIE_SECURE,
samesite=settings.AUTH_COOKIE_SAMESITE,
path=settings.AUTH_COOKIE_PATH,
max_age=settings.REFRESH_TOKEN_EXPIRE_DAYS * 24 * 60 * 60,
)
response.set_cookie(
key=settings.CSRF_COOKIE_NAME,
value=csrf_token,
httponly=False,
secure=settings.AUTH_COOKIE_SECURE,
samesite=settings.AUTH_COOKIE_SAMESITE,
path=settings.CSRF_COOKIE_PATH,
max_age=settings.REFRESH_TOKEN_EXPIRE_DAYS * 24 * 60 * 60,
)
def clear_auth_cookies(response: Response) -> None:
"""Expire both auth cookies on a response."""
response.delete_cookie(
key=settings.REFRESH_COOKIE_NAME,
path=settings.AUTH_COOKIE_PATH,
)
response.delete_cookie(
key=settings.CSRF_COOKIE_NAME,
path=settings.CSRF_COOKIE_PATH,
)