-
Notifications
You must be signed in to change notification settings - Fork 1
feat: set cookie #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: set cookie #96
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,7 +217,7 @@ class Session: | |
| Args: | ||
| secret (bytes): The secret key used for HMAC signing and verification. | ||
| max_age (int): Session expiration in seconds. Defaults to 1 week (604800s). | ||
| same_site (str): SameSite cookie attribute. Defaults to ``"Strict"``. | ||
| samesite (str): SameSite cookie attribute. Defaults to ``"Lax"``. | ||
|
|
||
| Returns: | ||
| A middleware function to be registered via ``router.middleware()``. | ||
|
|
@@ -247,10 +247,10 @@ def main(): | |
| ``` | ||
| """ | ||
|
|
||
| def __init__(self, secret: bytes, max_age: int = 3600 * 24 * 7, same_site="Lax"): | ||
| def __init__(self, secret: bytes, max_age: int = 3600 * 24 * 7, samesite="Lax"): | ||
| self.secret = secret | ||
| self.max_age = max_age | ||
| self.same_site = same_site | ||
| self.samesite = samesite | ||
|
|
||
| def __call__(self, request, next, **kwargs) -> Response: | ||
| cookie = request.get_cookie("session") | ||
|
|
@@ -271,16 +271,13 @@ def __call__(self, request, next, **kwargs) -> Response: | |
| if current_state != initial_state: | ||
| signed_cookie = _sign_session(self.secret, self.max_age, request.session) | ||
|
|
||
| response.insert_header( | ||
| "set-cookie", | ||
| ( | ||
| f"session={signed_cookie}; " | ||
| f"Path=/; " | ||
| f"HttpOnly; " | ||
| f"Secure; " | ||
| f"SameSite={self.same_site}; " | ||
| f"Max-Age={self.max_age}" | ||
| ), | ||
| response.set_cookie( | ||
| name="session", | ||
| value=signed_cookie, | ||
| httponly=True, | ||
| secure=True, | ||
| samesite=self.samesite, | ||
| max_age=self.max_age, | ||
|
Comment on lines
+274
to
+280
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Use the runtime keyword The session code passes 📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| ) | ||
|
|
||
| return response | ||
|
|
@@ -430,15 +427,11 @@ def __call__(self, request, next, **kwargs) -> Response: | |
| response = convert_to_response(next(request, **kwargs)) | ||
|
|
||
| signed = _sign_csrf_token(self.secret, token) | ||
| response.insert_header( | ||
| "set-cookie", | ||
| ( | ||
| f"{self.cookie_name}={signed}; " | ||
| f"Path=/; " | ||
| f"Secure; " | ||
| f"SameSite=Lax; " | ||
| f"Max-Age={self.cookie_max_age}" | ||
| ), | ||
| response.set_cookie( | ||
| name=self.cookie_name, | ||
| value=signed, | ||
| max_age=self.cookie_max_age, | ||
| httponly=False, | ||
|
Comment on lines
+430
to
+434
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Make CSRF cookie security configurable. This call omits Add a 🤖 Prompt for AI Agents |
||
| ) | ||
|
|
||
| return response | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Renumber the remaining Important priorities.
Line [79] changes the heading to priority 13, but the Important section currently jumps from priority 9 to priorities 13 and 14. Rename the remaining entries to maintain contiguous priorities, or document that the gaps are intentional.
🤖 Prompt for AI Agents