-
Notifications
You must be signed in to change notification settings - Fork 513
[chore] Use port-aware cookies #3779
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b05675
initial commit
jp-agenta 9240e0d
Update web/tests/playwright/global-teardown.ts
junaway c0d8f58
align around port from WEB_URL
jp-agenta 2c4109f
Update api/oss/src/core/auth/supertokens/cookie_names.py
junaway 77c2855
Merge branch 'main' into fix/port-aware-cookies
jp-agenta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,107 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import ipaddress | ||
| from functools import lru_cache | ||
| from typing import Mapping, NamedTuple | ||
| from urllib.parse import urlparse | ||
|
|
||
| from supertokens_python.recipe.session import constants as st_session_constants | ||
| from supertokens_python.recipe.session import cookie_and_header as st_cookie_and_header | ||
|
|
||
| from oss.src.utils.env import env | ||
| from oss.src.utils.logging import get_module_logger | ||
|
|
||
| log = get_module_logger(__name__) | ||
|
|
||
| DEFAULT_ACCESS_TOKEN_COOKIE_NAME = "sAccessToken" | ||
| DEFAULT_REFRESH_TOKEN_COOKIE_NAME = "sRefreshToken" | ||
|
|
||
|
|
||
| class SupertokensCookieNames(NamedTuple): | ||
| access_token: str | ||
| refresh_token: str | ||
|
|
||
|
|
||
| def _get_local_port_from_url(url: str | None) -> int | None: | ||
| if not url: | ||
| return None | ||
|
|
||
| parsed = urlparse(url) | ||
| if not _is_localhost_or_ip(parsed.hostname): | ||
| return None | ||
|
|
||
| try: | ||
| return parsed.port | ||
| except ValueError: | ||
| return None | ||
|
|
||
|
|
||
| def _is_localhost_or_ip(hostname: str | None) -> bool: | ||
| if not hostname: | ||
| return False | ||
|
|
||
| normalized = hostname.strip().lower() | ||
| if normalized == "localhost": | ||
| return True | ||
|
|
||
| # Handle IPv6 literals that may be enclosed in brackets, e.g. "[::1]" | ||
| if normalized.startswith("[") and normalized.endswith("]"): | ||
| normalized = normalized[1:-1] | ||
| try: | ||
| ipaddress.ip_address(normalized) | ||
| return True | ||
| except ValueError: | ||
| return False | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_local_cookie_port_suffix() -> str: | ||
| port = _get_local_port_from_url(env.agenta.web_url) | ||
| if port is None: | ||
| return "" | ||
| return f"_{port}" | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def get_supertokens_cookie_names() -> SupertokensCookieNames: | ||
| suffix = get_local_cookie_port_suffix() | ||
| return SupertokensCookieNames( | ||
| access_token=f"{DEFAULT_ACCESS_TOKEN_COOKIE_NAME}{suffix}", | ||
| refresh_token=f"{DEFAULT_REFRESH_TOKEN_COOKIE_NAME}{suffix}", | ||
| ) | ||
|
|
||
|
|
||
| def get_supertokens_access_token_cookie_name() -> str: | ||
| return get_supertokens_cookie_names().access_token | ||
|
|
||
|
|
||
| def get_supertokens_access_token_from_cookies( | ||
| cookies: Mapping[str, str], | ||
| ) -> str | None: | ||
| expected = get_supertokens_access_token_cookie_name() | ||
| token = cookies.get(expected) | ||
| if token: | ||
| return token | ||
|
|
||
| # Backward compatibility for setups without suffixing. | ||
| if get_local_cookie_port_suffix() == "": | ||
| return cookies.get(DEFAULT_ACCESS_TOKEN_COOKIE_NAME) | ||
|
junaway marked this conversation as resolved.
junaway marked this conversation as resolved.
|
||
|
|
||
| return None | ||
|
|
||
|
|
||
|
junaway marked this conversation as resolved.
|
||
| def apply_supertokens_cookie_name_overrides() -> None: | ||
| cookie_names = get_supertokens_cookie_names() | ||
| suffix = get_local_cookie_port_suffix() | ||
|
|
||
| st_session_constants.ACCESS_TOKEN_COOKIE_KEY = cookie_names.access_token | ||
| st_session_constants.REFRESH_TOKEN_COOKIE_KEY = cookie_names.refresh_token | ||
| st_cookie_and_header.ACCESS_TOKEN_COOKIE_KEY = cookie_names.access_token | ||
| st_cookie_and_header.REFRESH_TOKEN_COOKIE_KEY = cookie_names.refresh_token | ||
|
|
||
| if suffix: | ||
| log.info( | ||
| "Using SuperTokens cookie suffix '%s' for local host '%s'", | ||
| suffix, | ||
| urlparse(env.agenta.web_url).hostname, | ||
| ) | ||
|
junaway marked this conversation as resolved.
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.