]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Annotate `samesite` parameter on `set_cookie` (#1590)
authorMarcelo Trylesinski <marcelotryle@gmail.com>
Tue, 26 Apr 2022 15:13:12 +0000 (17:13 +0200)
committerGitHub <noreply@github.com>
Tue, 26 Apr 2022 15:13:12 +0000 (17:13 +0200)
* Annotate `samesite` parameter on `set_cookie`

* Add version conditional

* Add pragma

starlette/middleware/sessions.py
starlette/responses.py

index 597de38a2d4a6e47fa686879bbb6e4082428c3a4..b1e32ec16109987d25195f9c76613ebed19dc9ed 100644 (file)
@@ -1,4 +1,5 @@
 import json
+import sys
 import typing
 from base64 import b64decode, b64encode
 
@@ -9,6 +10,11 @@ from starlette.datastructures import MutableHeaders, Secret
 from starlette.requests import HTTPConnection
 from starlette.types import ASGIApp, Message, Receive, Scope, Send
 
+if sys.version_info >= (3, 8):  # pragma: no cover
+    from typing import Literal
+else:  # pragma: no cover
+    from typing_extensions import Literal
+
 
 class SessionMiddleware:
     def __init__(
@@ -18,7 +24,7 @@ class SessionMiddleware:
         session_cookie: str = "session",
         max_age: typing.Optional[int] = 14 * 24 * 60 * 60,  # 14 days, in seconds
         path: str = "/",
-        same_site: str = "lax",
+        same_site: Literal["lax", "strict", "none"] = "lax",
         https_only: bool = False,
     ) -> None:
         self.app = app
index bc73cb1561b03dacd6e7b7257d2bbd3f71dba809..98c8caf1b51927a4e5c88f74467f18c3fd89476d 100644 (file)
@@ -17,6 +17,11 @@ from starlette.concurrency import iterate_in_threadpool
 from starlette.datastructures import URL, MutableHeaders
 from starlette.types import Receive, Scope, Send
 
+if sys.version_info >= (3, 8):  # pragma: no cover
+    from typing import Literal
+else:  # pragma: no cover
+    from typing_extensions import Literal
+
 # Workaround for adding samesite support to pre 3.8 python
 http.cookies.Morsel._reserved["samesite"] = "SameSite"  # type: ignore
 
@@ -105,7 +110,7 @@ class Response:
         domain: typing.Optional[str] = None,
         secure: bool = False,
         httponly: bool = False,
-        samesite: str = "lax",
+        samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax",
     ) -> None:
         cookie: http.cookies.BaseCookie = http.cookies.SimpleCookie()
         cookie[key] = value
@@ -138,7 +143,7 @@ class Response:
         domain: typing.Optional[str] = None,
         secure: bool = False,
         httponly: bool = False,
-        samesite: str = "lax",
+        samesite: typing.Optional[Literal["lax", "strict", "none"]] = "lax",
     ) -> None:
         self.set_cookie(
             key,