From 9aa34725b41c13acb27c9470ca64ec13f932c9ba Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 9 Nov 2026 08:50:15 +0100 Subject: [PATCH 1/5] fix(auth): forward persist_session and storage_key to the session manager --- sdk-compliance.yaml | 8 ++- src/auth/src/supabase_auth/client.py | 9 ++- src/auth/tests/_async/test_gotrue.py | 83 +++++++++++++++++++++++++++- src/auth/tests/_sync/test_gotrue.py | 83 +++++++++++++++++++++++++++- 4 files changed, 175 insertions(+), 8 deletions(-) diff --git a/sdk-compliance.yaml b/sdk-compliance.yaml index 2eb9bdefc..67f02b6e5 100644 --- a/sdk-compliance.yaml +++ b/sdk-compliance.yaml @@ -431,8 +431,12 @@ features: - SyncMemoryStorage.remove_item - SyncMemoryStorage.set_item client.session_management.persist_session: - status: not_implemented - note: "The supabase client has no persist_session setting. The session is kept in session_storage of AuthClientOptions, which defaults to memory. The auth client constructors accept persist_session but do not forward it to the session manager, so sessions are always written to storage." + status: implemented + note: "The supabase client keeps the session in memory unless session_storage is set on AuthClientOptions. The auth client constructors take persist_session: when False, the session and the PKCE code verifier stay in memory and the storage passed in is not used. They also take a storage_key to keep clients that share a storage backend apart." + symbols: + - AuthClientOptions + - AsyncSupabaseAuthClient + - SyncSupabaseAuthClient client.request_configuration.custom_http_client: status: implemented diff --git a/src/auth/src/supabase_auth/client.py b/src/auth/src/supabase_auth/client.py index be6774545..c7e7bac5f 100644 --- a/src/auth/src/supabase_auth/client.py +++ b/src/auth/src/supabase_auth/client.py @@ -20,6 +20,7 @@ from supabase_utils.http.request import EmptyRequest, JSONRequest, Response from yarl import URL +from .constants import STORAGE_KEY from .errors import ( AuthImplicitGrantRedirectError, AuthInvalidJwtError, @@ -650,7 +651,7 @@ def __init__( http_session: AsyncHttpSession, *, headers: Mapping[str, str] | None = None, - storage_key: str | None = None, + storage_key: str = STORAGE_KEY, auto_refresh_token: bool = True, persist_session: bool = True, storage: AsyncSupportedStorage | None = None, @@ -668,6 +669,8 @@ def __init__( default_headers=default_headers, storage=storage or AsyncMemoryStorage(), state_change_emitters={}, + storage_key=storage_key, + persist_session=persist_session, auto_refresh_token=auto_refresh_token, ) super().__init__( @@ -1090,7 +1093,7 @@ def __init__( http_session: HttpSession, *, headers: Mapping[str, str] | None = None, - storage_key: str | None = None, + storage_key: str = STORAGE_KEY, auto_refresh_token: bool = True, persist_session: bool = True, storage: SyncSupportedStorage | None = None, @@ -1108,6 +1111,8 @@ def __init__( default_headers=default_headers, storage=storage or SyncMemoryStorage(), state_change_emitters={}, + storage_key=storage_key, + persist_session=persist_session, auto_refresh_token=auto_refresh_token, ) super().__init__( diff --git a/src/auth/tests/_async/test_gotrue.py b/src/auth/tests/_async/test_gotrue.py index 0cfd15e78..b05147d74 100644 --- a/src/auth/tests/_async/test_gotrue.py +++ b/src/auth/tests/_async/test_gotrue.py @@ -8,8 +8,8 @@ from pytest_mock import MockerFixture from supabase_utils.http.adapters.httpx import AsyncHttpxSession -from supabase_auth import AsyncSupabaseAuthClient -from supabase_auth.constants import EXPIRY_MARGIN +from supabase_auth import AsyncMemoryStorage, AsyncSupabaseAuthClient +from supabase_auth.constants import EXPIRY_MARGIN, STORAGE_KEY from supabase_auth.errors import ( AuthApiError, AuthInvalidJwtError, @@ -521,3 +521,82 @@ async def test_save_session_schedules_refresh_in_seconds() -> None: assert timer is not None timer.cancel() assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN + + +def mock_session(access_token: str) -> Session: + return Session( + access_token=access_token, + refresh_token="refresh-token", + token_type="bearer", + expires_in=3600, + expires_at=round(time.time()) + 3600, + user=User.model_validate( + { + "id": str(uuid4()), + "aud": "authenticated", + "app_metadata": {}, + "user_metadata": {}, + "created_at": "2024-01-01T00:00:00Z", + } + ), + ) + + +async def test_persist_session_false_keeps_session_out_of_storage() -> None: + storage = AsyncMemoryStorage() + client = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + auto_refresh_token=False, + persist_session=False, + storage=storage, + ) + session = mock_session("access-token") + + await client.session_manager.save_session(session) + + assert storage.storage == {} + assert await client.get_session() == session + + +async def test_persist_session_writes_session_to_default_storage_key() -> None: + storage = AsyncMemoryStorage() + client = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + auto_refresh_token=False, + storage=storage, + ) + session = mock_session("access-token") + + await client.session_manager.save_session(session) + + assert list(storage.storage) == [STORAGE_KEY] + assert await client.get_session() == session + + +async def test_storage_key_separates_clients_sharing_a_storage() -> None: + storage = AsyncMemoryStorage() + first = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + auto_refresh_token=False, + storage=storage, + storage_key="first", + ) + second = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + auto_refresh_token=False, + storage=storage, + storage_key="second", + ) + first_session = mock_session("first-access-token") + second_session = mock_session("second-access-token") + + await first.session_manager.save_session(first_session) + await second.session_manager.save_session(second_session) + + assert sorted(storage.storage) == ["first", "second"] + assert await first.get_session() == first_session + assert await second.get_session() == second_session diff --git a/src/auth/tests/_sync/test_gotrue.py b/src/auth/tests/_sync/test_gotrue.py index 0d6a69ef2..43194b8d7 100644 --- a/src/auth/tests/_sync/test_gotrue.py +++ b/src/auth/tests/_sync/test_gotrue.py @@ -8,8 +8,8 @@ from pytest_mock import MockerFixture from supabase_utils.http.adapters.httpx import HttpxSession -from supabase_auth import SyncSupabaseAuthClient -from supabase_auth.constants import EXPIRY_MARGIN +from supabase_auth import SyncMemoryStorage, SyncSupabaseAuthClient +from supabase_auth.constants import EXPIRY_MARGIN, STORAGE_KEY from supabase_auth.errors import ( AuthApiError, AuthInvalidJwtError, @@ -517,3 +517,82 @@ def test_save_session_schedules_refresh_in_seconds() -> None: assert timer is not None timer.cancel() assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN + + +def mock_session(access_token: str) -> Session: + return Session( + access_token=access_token, + refresh_token="refresh-token", + token_type="bearer", + expires_in=3600, + expires_at=round(time.time()) + 3600, + user=User.model_validate( + { + "id": str(uuid4()), + "aud": "authenticated", + "app_metadata": {}, + "user_metadata": {}, + "created_at": "2024-01-01T00:00:00Z", + } + ), + ) + + +def test_persist_session_false_keeps_session_out_of_storage() -> None: + storage = SyncMemoryStorage() + client = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + auto_refresh_token=False, + persist_session=False, + storage=storage, + ) + session = mock_session("access-token") + + client.session_manager.save_session(session) + + assert storage.storage == {} + assert client.get_session() == session + + +def test_persist_session_writes_session_to_default_storage_key() -> None: + storage = SyncMemoryStorage() + client = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + auto_refresh_token=False, + storage=storage, + ) + session = mock_session("access-token") + + client.session_manager.save_session(session) + + assert list(storage.storage) == [STORAGE_KEY] + assert client.get_session() == session + + +def test_storage_key_separates_clients_sharing_a_storage() -> None: + storage = SyncMemoryStorage() + first = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + auto_refresh_token=False, + storage=storage, + storage_key="first", + ) + second = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + auto_refresh_token=False, + storage=storage, + storage_key="second", + ) + first_session = mock_session("first-access-token") + second_session = mock_session("second-access-token") + + first.session_manager.save_session(first_session) + second.session_manager.save_session(second_session) + + assert sorted(storage.storage) == ["first", "second"] + assert first.get_session() == first_session + assert second.get_session() == second_session From 2a2c77b2c4c942b4e856eefc7472931968ec2e1b Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 9 Nov 2026 08:53:01 +0100 Subject: [PATCH 2/5] fix(auth): keep the session and code verifier in memory when persist_session is False --- MIGRATION.md | 8 +++ src/auth/src/supabase_auth/client.py | 4 +- src/auth/tests/_async/test_gotrue.py | 79 +++++++++++++++++----------- src/auth/tests/_sync/test_gotrue.py | 79 +++++++++++++++++----------- 4 files changed, 108 insertions(+), 62 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 7a31e8f21..39fa4d3b2 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -140,6 +140,14 @@ The client constructors of `supabase_auth`, `postgrest`, `storage3` and `supabas `heartbeat_interval` and `heartbeat_timeout` are typed `float` and are in seconds. `connect_once`, `RealtimeClient` and `RealtimeClientOptions` raise `ValueError` when either one is not positive. +## Auth `persist_session` and `storage_key` take effect + +`AsyncSupabaseAuthClient` and `SyncSupabaseAuthClient` accepted `persist_session` and `storage_key` but ignored them. Both are now used. + +- With `persist_session=False`, the session and the PKCE code verifier are kept in memory. The `storage` you pass is not read or written, so a session saved there by an earlier run is not restored. +- A custom `storage_key` is now the key the session is stored under, with the code verifier under `-code-verifier`. Sessions saved before the upgrade are under `supabase.auth.token`. Leave `storage_key` out to keep reading them. +- `storage_key` is typed `str` and defaults to `"supabase.auth.token"`. Passing `None` is no longer accepted. + ## Request timeouts `AsyncStorageClient` and `SyncStorageClient` no longer take a `timeout` parameter. It was ignored. Pass `request_timeout` as a keyword argument instead. diff --git a/src/auth/src/supabase_auth/client.py b/src/auth/src/supabase_auth/client.py index c7e7bac5f..4d91d1bb7 100644 --- a/src/auth/src/supabase_auth/client.py +++ b/src/auth/src/supabase_auth/client.py @@ -667,7 +667,7 @@ def __init__( base_url=self.base_url, executor=executor, default_headers=default_headers, - storage=storage or AsyncMemoryStorage(), + storage=storage if persist_session and storage else AsyncMemoryStorage(), state_change_emitters={}, storage_key=storage_key, persist_session=persist_session, @@ -1109,7 +1109,7 @@ def __init__( base_url=self.base_url, executor=executor, default_headers=default_headers, - storage=storage or SyncMemoryStorage(), + storage=storage if persist_session and storage else SyncMemoryStorage(), state_change_emitters={}, storage_key=storage_key, persist_session=persist_session, diff --git a/src/auth/tests/_async/test_gotrue.py b/src/auth/tests/_async/test_gotrue.py index b05147d74..c67b22db9 100644 --- a/src/auth/tests/_async/test_gotrue.py +++ b/src/auth/tests/_async/test_gotrue.py @@ -493,36 +493,6 @@ def handler(request: Request) -> Response: manager.refresh_token_timer.cancel() -async def test_save_session_schedules_refresh_in_seconds() -> None: - client = AsyncSupabaseAuthClient( - url="http://auth.test", - http_session=AsyncHttpxSession(client=AsyncClient()), - ) - manager = client.session_manager - user: dict[str, JSON] = { - "id": str(uuid4()), - "aud": "authenticated", - "app_metadata": {}, - "user_metadata": {}, - "created_at": "2024-01-01T00:00:00Z", - } - session = Session( - access_token="access-token", - refresh_token="refresh-token", - token_type="bearer", - expires_in=3600, - expires_at=round(time.time()) + 3600, - user=User.model_validate(user), - ) - - await manager.save_session(session) - - timer = manager.refresh_token_timer - assert timer is not None - timer.cancel() - assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN - - def mock_session(access_token: str) -> Session: return Session( access_token=access_token, @@ -542,6 +512,21 @@ def mock_session(access_token: str) -> Session: ) +async def test_save_session_schedules_refresh_in_seconds() -> None: + client = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + ) + manager = client.session_manager + + await manager.save_session(mock_session("access-token")) + + timer = manager.refresh_token_timer + assert timer is not None + timer.cancel() + assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN + + async def test_persist_session_false_keeps_session_out_of_storage() -> None: storage = AsyncMemoryStorage() client = AsyncSupabaseAuthClient( @@ -600,3 +585,37 @@ async def test_storage_key_separates_clients_sharing_a_storage() -> None: assert sorted(storage.storage) == ["first", "second"] assert await first.get_session() == first_session assert await second.get_session() == second_session + + +async def test_persist_session_false_does_not_restore_session_from_storage() -> None: + storage = AsyncMemoryStorage() + stored_session = mock_session("stored-access-token").model_dump_json() + await storage.set_item(STORAGE_KEY, stored_session) + client = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + auto_refresh_token=False, + persist_session=False, + storage=storage, + ) + + await client.initialize_from_storage() + + assert await client.get_session() is None + assert storage.storage == {STORAGE_KEY: stored_session} + + +async def test_persist_session_false_keeps_code_verifier_out_of_storage() -> None: + storage = AsyncMemoryStorage() + client = AsyncSupabaseAuthClient( + url="http://auth.test", + http_session=AsyncHttpxSession(client=AsyncClient()), + auto_refresh_token=False, + persist_session=False, + storage=storage, + flow_type="pkce", + ) + + await client.sign_in_with_oauth("github") + + assert storage.storage == {} diff --git a/src/auth/tests/_sync/test_gotrue.py b/src/auth/tests/_sync/test_gotrue.py index 43194b8d7..2d75fd25c 100644 --- a/src/auth/tests/_sync/test_gotrue.py +++ b/src/auth/tests/_sync/test_gotrue.py @@ -489,36 +489,6 @@ def handler(request: Request) -> Response: manager.refresh_token_timer.cancel() -def test_save_session_schedules_refresh_in_seconds() -> None: - client = SyncSupabaseAuthClient( - url="http://auth.test", - http_session=HttpxSession(client=Client()), - ) - manager = client.session_manager - user: dict[str, JSON] = { - "id": str(uuid4()), - "aud": "authenticated", - "app_metadata": {}, - "user_metadata": {}, - "created_at": "2024-01-01T00:00:00Z", - } - session = Session( - access_token="access-token", - refresh_token="refresh-token", - token_type="bearer", - expires_in=3600, - expires_at=round(time.time()) + 3600, - user=User.model_validate(user), - ) - - manager.save_session(session) - - timer = manager.refresh_token_timer - assert timer is not None - timer.cancel() - assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN - - def mock_session(access_token: str) -> Session: return Session( access_token=access_token, @@ -538,6 +508,21 @@ def mock_session(access_token: str) -> Session: ) +def test_save_session_schedules_refresh_in_seconds() -> None: + client = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + ) + manager = client.session_manager + + manager.save_session(mock_session("access-token")) + + timer = manager.refresh_token_timer + assert timer is not None + timer.cancel() + assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN + + def test_persist_session_false_keeps_session_out_of_storage() -> None: storage = SyncMemoryStorage() client = SyncSupabaseAuthClient( @@ -596,3 +581,37 @@ def test_storage_key_separates_clients_sharing_a_storage() -> None: assert sorted(storage.storage) == ["first", "second"] assert first.get_session() == first_session assert second.get_session() == second_session + + +def test_persist_session_false_does_not_restore_session_from_storage() -> None: + storage = SyncMemoryStorage() + stored_session = mock_session("stored-access-token").model_dump_json() + storage.set_item(STORAGE_KEY, stored_session) + client = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + auto_refresh_token=False, + persist_session=False, + storage=storage, + ) + + client.initialize_from_storage() + + assert client.get_session() is None + assert storage.storage == {STORAGE_KEY: stored_session} + + +def test_persist_session_false_keeps_code_verifier_out_of_storage() -> None: + storage = SyncMemoryStorage() + client = SyncSupabaseAuthClient( + url="http://auth.test", + http_session=HttpxSession(client=Client()), + auto_refresh_token=False, + persist_session=False, + storage=storage, + flow_type="pkce", + ) + + client.sign_in_with_oauth("github") + + assert storage.storage == {} From f9938d88f7cf38b95e6c709d14f93bbe99e66c4e Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 9 Nov 2026 09:02:48 +0100 Subject: [PATCH 3/5] fix(auth): keep a custom storage that is falsy when persisting sessions --- src/auth/src/supabase_auth/client.py | 12 +++++++-- .../tests/_async/test_pkce_code_challenge.py | 25 ++++++++++++++++++- .../tests/_sync/test_pkce_code_challenge.py | 25 ++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/auth/src/supabase_auth/client.py b/src/auth/src/supabase_auth/client.py index 4d91d1bb7..8d88831fe 100644 --- a/src/auth/src/supabase_auth/client.py +++ b/src/auth/src/supabase_auth/client.py @@ -667,7 +667,11 @@ def __init__( base_url=self.base_url, executor=executor, default_headers=default_headers, - storage=storage if persist_session and storage else AsyncMemoryStorage(), + storage=( + storage + if persist_session and storage is not None + else AsyncMemoryStorage() + ), state_change_emitters={}, storage_key=storage_key, persist_session=persist_session, @@ -1109,7 +1113,11 @@ def __init__( base_url=self.base_url, executor=executor, default_headers=default_headers, - storage=storage if persist_session and storage else SyncMemoryStorage(), + storage=( + storage + if persist_session and storage is not None + else SyncMemoryStorage() + ), state_change_emitters={}, storage_key=storage_key, persist_session=persist_session, diff --git a/src/auth/tests/_async/test_pkce_code_challenge.py b/src/auth/tests/_async/test_pkce_code_challenge.py index 51ca99662..e6a57273d 100644 --- a/src/auth/tests/_async/test_pkce_code_challenge.py +++ b/src/auth/tests/_async/test_pkce_code_challenge.py @@ -6,8 +6,10 @@ import pytest from httpx import AsyncClient, MockTransport, Request, Response from supabase_utils.http.adapters.httpx import AsyncHttpxSession +from yarl import URL -from supabase_auth import AsyncSupabaseAuthClient +from supabase_auth import AsyncMemoryStorage, AsyncSupabaseAuthClient +from supabase_auth.constants import STORAGE_KEY from supabase_auth.errors import AuthError from supabase_auth.helpers import generate_pkce_challenge from supabase_auth.types import ( @@ -40,6 +42,9 @@ def mock_client( payload: Any, requests: list[Request], flow_type: AuthFlowType = "pkce", + *, + persist_session: bool = True, + storage: AsyncMemoryStorage | None = None, ) -> AsyncSupabaseAuthClient: def handler(request: Request) -> Response: requests.append(request) @@ -51,6 +56,8 @@ def handler(request: Request) -> Response: client=AsyncClient(transport=MockTransport(handler)) ), auto_refresh_token=False, + persist_session=persist_session, + storage=storage, flow_type=flow_type, ) @@ -206,6 +213,22 @@ async def test_exchange_code_uses_and_removes_stored_verifier() -> None: assert await stored_verifier(client) is None +async def test_exchange_code_without_persistence_uses_verifier_kept_in_memory() -> None: + storage = AsyncMemoryStorage() + stale_verifier_key = f"{STORAGE_KEY}-code-verifier" + await storage.set_item(stale_verifier_key, "stale-verifier") + requests: list[Request] = [] + client = mock_client(SESSION, requests, persist_session=False, storage=storage) + + oauth_response = await client.sign_in_with_oauth("github") + await client.exchange_code_for_session("auth-code") + + code_challenge = URL(oauth_response.url).query["code_challenge"] + verifier = request_body(requests)["code_verifier"] + assert generate_pkce_challenge(verifier) == code_challenge + assert storage.storage == {stale_verifier_key: "stale-verifier"} + + async def test_exchange_code_saves_session_and_signs_in() -> None: requests: list[Request] = [] client = mock_client(SESSION, requests) diff --git a/src/auth/tests/_sync/test_pkce_code_challenge.py b/src/auth/tests/_sync/test_pkce_code_challenge.py index 5a24b92c1..4a4392b81 100644 --- a/src/auth/tests/_sync/test_pkce_code_challenge.py +++ b/src/auth/tests/_sync/test_pkce_code_challenge.py @@ -6,8 +6,10 @@ import pytest from httpx import Client, MockTransport, Request, Response from supabase_utils.http.adapters.httpx import HttpxSession +from yarl import URL -from supabase_auth import SyncSupabaseAuthClient +from supabase_auth import SyncMemoryStorage, SyncSupabaseAuthClient +from supabase_auth.constants import STORAGE_KEY from supabase_auth.errors import AuthError from supabase_auth.helpers import generate_pkce_challenge from supabase_auth.types import ( @@ -40,6 +42,9 @@ def mock_client( payload: Any, requests: list[Request], flow_type: AuthFlowType = "pkce", + *, + persist_session: bool = True, + storage: SyncMemoryStorage | None = None, ) -> SyncSupabaseAuthClient: def handler(request: Request) -> Response: requests.append(request) @@ -49,6 +54,8 @@ def handler(request: Request) -> Response: url="https://example.supabase.co/auth/v1", http_session=HttpxSession(client=Client(transport=MockTransport(handler))), auto_refresh_token=False, + persist_session=persist_session, + storage=storage, flow_type=flow_type, ) @@ -200,6 +207,22 @@ def test_exchange_code_uses_and_removes_stored_verifier() -> None: assert stored_verifier(client) is None +def test_exchange_code_without_persistence_uses_verifier_kept_in_memory() -> None: + storage = SyncMemoryStorage() + stale_verifier_key = f"{STORAGE_KEY}-code-verifier" + storage.set_item(stale_verifier_key, "stale-verifier") + requests: list[Request] = [] + client = mock_client(SESSION, requests, persist_session=False, storage=storage) + + oauth_response = client.sign_in_with_oauth("github") + client.exchange_code_for_session("auth-code") + + code_challenge = URL(oauth_response.url).query["code_challenge"] + verifier = request_body(requests)["code_verifier"] + assert generate_pkce_challenge(verifier) == code_challenge + assert storage.storage == {stale_verifier_key: "stale-verifier"} + + def test_exchange_code_saves_session_and_signs_in() -> None: requests: list[Request] = [] client = mock_client(SESSION, requests) From 3a3b415eccd07db56adfcf6a0a272c62f9d19b6b Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 9 Nov 2026 09:58:11 +0100 Subject: [PATCH 4/5] feat(supabase): add storage_key to AuthClientOptions --- sdk-compliance.yaml | 2 +- src/supabase/src/supabase/client.py | 2 ++ src/supabase/src/supabase/types.py | 4 ++++ src/supabase/tests/test_client.py | 6 ++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sdk-compliance.yaml b/sdk-compliance.yaml index 67f02b6e5..7ee989f4e 100644 --- a/sdk-compliance.yaml +++ b/sdk-compliance.yaml @@ -432,7 +432,7 @@ features: - SyncMemoryStorage.set_item client.session_management.persist_session: status: implemented - note: "The supabase client keeps the session in memory unless session_storage is set on AuthClientOptions. The auth client constructors take persist_session: when False, the session and the PKCE code verifier stay in memory and the storage passed in is not used. They also take a storage_key to keep clients that share a storage backend apart." + note: "The supabase client keeps the session in memory unless session_storage is set on AuthClientOptions. The auth client constructors take persist_session: when False, the session and the PKCE code verifier stay in memory and the storage passed in is not used. storage_key on AuthClientOptions and the auth client constructors keeps clients that share a storage backend apart." symbols: - AuthClientOptions - AsyncSupabaseAuthClient diff --git a/src/supabase/src/supabase/client.py b/src/supabase/src/supabase/client.py index a8cf1409e..0c6df9f7e 100644 --- a/src/supabase/src/supabase/client.py +++ b/src/supabase/src/supabase/client.py @@ -499,6 +499,7 @@ def auth(self) -> AsyncSupabaseAuthClient: headers=self._headers, auto_refresh_token=self._auth_options.auto_refresh_token, storage=self._auth_options.session_storage, + storage_key=self._auth_options.storage_key, flow_type=self._auth_options.flow_type, request_timeout=self._timeout_for(self._auth_options), ) @@ -647,6 +648,7 @@ def auth(self) -> SyncSupabaseAuthClient: headers=self._headers, auto_refresh_token=self._auth_options.auto_refresh_token, storage=self._auth_options.session_storage, + storage_key=self._auth_options.storage_key, flow_type=self._auth_options.flow_type, request_timeout=self._timeout_for(self._auth_options), ) diff --git a/src/supabase/src/supabase/types.py b/src/supabase/src/supabase/types.py index 88777a478..38af99db4 100644 --- a/src/supabase/src/supabase/types.py +++ b/src/supabase/src/supabase/types.py @@ -6,6 +6,7 @@ validate_heartbeat, ) from supabase_auth import AsyncSupportedStorage, AuthFlowType, SyncSupportedStorage +from supabase_auth.constants import STORAGE_KEY from supabase_utils.http.request import validate_request_timeout @@ -58,6 +59,9 @@ class AuthClientOptions[Storage: (AsyncSupportedStorage, SyncSupportedStorage)]( session_storage: Storage | None = None """Where the logged in session is stored. None (the default) keeps it in memory.""" + storage_key: str = STORAGE_KEY + """The key the session is stored under in `session_storage`. Give clients that share a `session_storage` different keys so they do not overwrite each other's session.""" + flow_type: AuthFlowType = "pkce" """The flow type to use for authentication.""" diff --git a/src/supabase/tests/test_client.py b/src/supabase/tests/test_client.py index 9c442834b..44e35e1a2 100644 --- a/src/supabase/tests/test_client.py +++ b/src/supabase/tests/test_client.py @@ -10,6 +10,7 @@ SyncMemoryStorage, SyncSupabaseAuthClient, ) +from supabase_auth.constants import STORAGE_KEY from supabase_utils.http.adapters.httpx import AsyncHttpxSession, HttpxSession import supabase.aiohttp @@ -344,11 +345,13 @@ def test_sync_client_passes_service_options_to_sub_clients() -> None: auth=AuthClientOptions( auto_refresh_token=False, session_storage=session_storage, + storage_key="custom-key", flow_type="implicit", ), ) assert client.postgrest.default_headers.get("Accept-Profile") == "private" assert client.auth.session_manager.storage is session_storage + assert client.auth.session_manager.storage_key == "custom-key" assert client.auth.session_manager.auto_refresh_token is False assert client.auth.flow_type == "implicit" @@ -364,11 +367,13 @@ async def test_async_client_passes_service_options_to_sub_clients() -> None: auth=AuthClientOptions( auto_refresh_token=False, session_storage=session_storage, + storage_key="custom-key", flow_type="implicit", ), ) assert client.postgrest.default_headers.get("Accept-Profile") == "private" assert client.auth.session_manager.storage is session_storage + assert client.auth.session_manager.storage_key == "custom-key" assert client.auth.session_manager.auto_refresh_token is False assert client.auth.flow_type == "implicit" @@ -378,6 +383,7 @@ def test_sync_client_uses_service_defaults() -> None: client = Client(URL, KEY, HttpxSession(client=http_client)) assert client.postgrest.default_headers.get("Accept-Profile") == "public" assert isinstance(client.auth.session_manager.storage, SyncMemoryStorage) + assert client.auth.session_manager.storage_key == STORAGE_KEY assert client.auth.session_manager.auto_refresh_token is True assert client.auth.flow_type == "pkce" From 1699eb0b974f5f17226e43386cf89a85e18ae5e7 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Mon, 9 Nov 2026 10:00:52 +0100 Subject: [PATCH 5/5] fix(auth)!: remove persist_session from the auth clients Leaving the storage out already keeps the session in memory, so a separate flag only added a contradictory combination. --- MIGRATION.md | 11 ++-- sdk-compliance.yaml | 2 +- src/auth/src/supabase_auth/client.py | 16 +----- src/auth/src/supabase_auth/session.py | 50 +++++-------------- src/auth/tests/_async/conftest.py | 7 --- src/auth/tests/_async/test_gotrue.py | 48 +++--------------- .../tests/_async/test_pkce_code_challenge.py | 25 +--------- src/auth/tests/_sync/conftest.py | 7 --- src/auth/tests/_sync/test_gotrue.py | 48 +++--------------- .../tests/_sync/test_pkce_code_challenge.py | 25 +--------- 10 files changed, 39 insertions(+), 200 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 39fa4d3b2..8825e9ba2 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -140,13 +140,14 @@ The client constructors of `supabase_auth`, `postgrest`, `storage3` and `supabas `heartbeat_interval` and `heartbeat_timeout` are typed `float` and are in seconds. `connect_once`, `RealtimeClient` and `RealtimeClientOptions` raise `ValueError` when either one is not positive. -## Auth `persist_session` and `storage_key` take effect +## Auth `persist_session` is removed and `storage_key` takes effect -`AsyncSupabaseAuthClient` and `SyncSupabaseAuthClient` accepted `persist_session` and `storage_key` but ignored them. Both are now used. +`AsyncSupabaseAuthClient` and `SyncSupabaseAuthClient` no longer take `persist_session`. It was never passed on to the session manager, so it had no effect. To keep the session only in memory, leave `storage` out. Each client then gets its own memory storage, so clients created per request do not share a session. -- With `persist_session=False`, the session and the PKCE code verifier are kept in memory. The `storage` you pass is not read or written, so a session saved there by an earlier run is not restored. -- A custom `storage_key` is now the key the session is stored under, with the code verifier under `-code-verifier`. Sessions saved before the upgrade are under `supabase.auth.token`. Leave `storage_key` out to keep reading them. -- `storage_key` is typed `str` and defaults to `"supabase.auth.token"`. Passing `None` is no longer accepted. +`storage_key` was accepted but ignored as well. It is now the key the session is stored under, with the PKCE code verifier under `-code-verifier`. Set it on `AuthClientOptions` or on the auth client constructors to keep clients that share a storage apart. + +- Sessions saved before the upgrade are under `supabase.auth.token`, the default. Leave `storage_key` out to keep reading them. +- `storage_key` is typed `str`. Passing `None` is no longer accepted. ## Request timeouts diff --git a/sdk-compliance.yaml b/sdk-compliance.yaml index 7ee989f4e..bbdcd3ad5 100644 --- a/sdk-compliance.yaml +++ b/sdk-compliance.yaml @@ -432,7 +432,7 @@ features: - SyncMemoryStorage.set_item client.session_management.persist_session: status: implemented - note: "The supabase client keeps the session in memory unless session_storage is set on AuthClientOptions. The auth client constructors take persist_session: when False, the session and the PKCE code verifier stay in memory and the storage passed in is not used. storage_key on AuthClientOptions and the auth client constructors keeps clients that share a storage backend apart." + note: "The session is kept in memory unless a storage is given, with session_storage on AuthClientOptions or storage on the auth client constructors. There is no persist_session flag, since leaving the storage out already keeps the session in memory. storage_key on AuthClientOptions and the auth client constructors keeps clients that share a storage apart." symbols: - AuthClientOptions - AsyncSupabaseAuthClient diff --git a/src/auth/src/supabase_auth/client.py b/src/auth/src/supabase_auth/client.py index 8d88831fe..e9890c60c 100644 --- a/src/auth/src/supabase_auth/client.py +++ b/src/auth/src/supabase_auth/client.py @@ -653,7 +653,6 @@ def __init__( headers: Mapping[str, str] | None = None, storage_key: str = STORAGE_KEY, auto_refresh_token: bool = True, - persist_session: bool = True, storage: AsyncSupportedStorage | None = None, flow_type: AuthFlowType = "implicit", request_timeout: float | None = None, @@ -667,14 +666,9 @@ def __init__( base_url=self.base_url, executor=executor, default_headers=default_headers, - storage=( - storage - if persist_session and storage is not None - else AsyncMemoryStorage() - ), + storage=storage if storage is not None else AsyncMemoryStorage(), state_change_emitters={}, storage_key=storage_key, - persist_session=persist_session, auto_refresh_token=auto_refresh_token, ) super().__init__( @@ -1099,7 +1093,6 @@ def __init__( headers: Mapping[str, str] | None = None, storage_key: str = STORAGE_KEY, auto_refresh_token: bool = True, - persist_session: bool = True, storage: SyncSupportedStorage | None = None, flow_type: AuthFlowType = "implicit", request_timeout: float | None = None, @@ -1113,14 +1106,9 @@ def __init__( base_url=self.base_url, executor=executor, default_headers=default_headers, - storage=( - storage - if persist_session and storage is not None - else SyncMemoryStorage() - ), + storage=storage if storage is not None else SyncMemoryStorage(), state_change_emitters={}, storage_key=storage_key, - persist_session=persist_session, auto_refresh_token=auto_refresh_token, ) super().__init__( diff --git a/src/auth/src/supabase_auth/session.py b/src/auth/src/supabase_auth/session.py index 09edef024..313fe2884 100644 --- a/src/auth/src/supabase_auth/session.py +++ b/src/auth/src/supabase_auth/session.py @@ -80,9 +80,7 @@ class _SessionManagerCommon[HttpIO: (SyncHttpIO, AsyncHttpIO)]: default_headers: Headers state_change_emitters: dict[str, Subscription] storage_key: str = field(default=STORAGE_KEY, kw_only=True) - persist_session: bool = field(default=True, kw_only=True) network_retries: int = field(default=0, kw_only=True) - in_memory_session: Session | None = field(default=None, kw_only=True) auto_refresh_token: bool = field(default=True, kw_only=True) def _refresh_access_token(self, refresh_token: str) -> HttpMethod[AuthResponse]: @@ -160,10 +158,7 @@ async def __aenter__(self) -> Self: return self async def remove_session(self) -> None: - if self.persist_session: - await self.storage.remove_item(self.storage_key) - else: - self.in_memory_session = None + await self.storage.remove_item(self.storage_key) if self.refresh_token_timer: self.refresh_token_timer.cancel() self.refresh_token_timer = None @@ -185,16 +180,10 @@ async def get_session(self) -> Session | None: The session returned can be null if the session is not detected which can happen in the event a user is not signed-in or has logged out. """ - current_session: Session | None = None - if self.persist_session: - maybe_session = await self.storage.get_item(self.storage_key) - current_session = self.parse_valid_session(maybe_session) - if not current_session: - await self.remove_session() - else: - current_session = self.in_memory_session - + maybe_session = await self.storage.get_item(self.storage_key) + current_session = self.parse_valid_session(maybe_session) if not current_session: + await self.remove_session() return None time_now = round(time.time()) has_expired = ( @@ -213,8 +202,6 @@ async def get_session_or_raise(self) -> Session: return session async def save_session(self, session: Session) -> None: - if not self.persist_session: - self.in_memory_session = session expire_at = session.expires_at if expire_at: time_now = round(time.time()) @@ -223,7 +210,7 @@ async def save_session(self, session: Session) -> None: EXPIRY_MARGIN if expire_in > EXPIRY_MARGIN else 0.5 ) self.start_auto_refresh_token(expire_in - refresh_duration_before_expires) - if self.persist_session and session.expires_at: + if session.expires_at: await self.storage.set_item(self.storage_key, session.model_dump_json()) async def recover_and_refresh(self) -> None: @@ -258,8 +245,7 @@ async def recover_and_refresh(self) -> None: return await self.remove_session() return - if self.persist_session: - await self.save_session(current_session) + await self.save_session(current_session) self.notify_all_subscribers("SIGNED_IN", current_session) async def refresh_token_function(self) -> None: @@ -330,10 +316,7 @@ def __enter__(self) -> Self: return self def remove_session(self) -> None: - if self.persist_session: - self.storage.remove_item(self.storage_key) - else: - self.in_memory_session = None + self.storage.remove_item(self.storage_key) if self.refresh_token_timer: self.refresh_token_timer.cancel() self.refresh_token_timer = None @@ -355,16 +338,10 @@ def get_session(self) -> Session | None: The session returned can be null if the session is not detected which can happen in the event a user is not signed-in or has logged out. """ - current_session: Session | None = None - if self.persist_session: - maybe_session = self.storage.get_item(self.storage_key) - current_session = self.parse_valid_session(maybe_session) - if not current_session: - self.remove_session() - else: - current_session = self.in_memory_session - + maybe_session = self.storage.get_item(self.storage_key) + current_session = self.parse_valid_session(maybe_session) if not current_session: + self.remove_session() return None time_now = round(time.time()) has_expired = ( @@ -383,8 +360,6 @@ def get_session_or_raise(self) -> Session: return session def save_session(self, session: Session) -> None: - if not self.persist_session: - self.in_memory_session = session expire_at = session.expires_at if expire_at: time_now = round(time.time()) @@ -393,7 +368,7 @@ def save_session(self, session: Session) -> None: EXPIRY_MARGIN if expire_in > EXPIRY_MARGIN else 0.5 ) self.start_auto_refresh_token(expire_in - refresh_duration_before_expires) - if self.persist_session and session.expires_at: + if session.expires_at: self.storage.set_item(self.storage_key, session.model_dump_json()) def refresh_token_function(self) -> None: @@ -439,8 +414,7 @@ def recover_and_refresh(self) -> None: return self.remove_session() return - if self.persist_session: - self.save_session(current_session) + self.save_session(current_session) self.notify_all_subscribers("SIGNED_IN", current_session) def start_auto_refresh_token(self, seconds: float) -> None: diff --git a/src/auth/tests/_async/conftest.py b/src/auth/tests/_async/conftest.py index c55f63818..dff80dd70 100644 --- a/src/auth/tests/_async/conftest.py +++ b/src/auth/tests/_async/conftest.py @@ -118,7 +118,6 @@ async def auth_client( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -131,7 +130,6 @@ async def auth_client_with_session( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=False, http_session=request.param(), ) as client: credentials = mock_user_credentials() @@ -156,7 +154,6 @@ async def auth_client_with_asymmetric_session( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=False, http_session=request.param(), ) as client: yield client @@ -169,7 +166,6 @@ async def auth_subscription_client( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -182,7 +178,6 @@ async def client_api_auto_confirm_enabled_client( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -195,7 +190,6 @@ async def client_api_auto_confirm_off_signups_enabled_client( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_OFF, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -208,7 +202,6 @@ async def client_api_auto_confirm_disabled_client( async with AsyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_DISABLED_AUTO_CONFIRM_OFF, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client diff --git a/src/auth/tests/_async/test_gotrue.py b/src/auth/tests/_async/test_gotrue.py index c67b22db9..a11f55e19 100644 --- a/src/auth/tests/_async/test_gotrue.py +++ b/src/auth/tests/_async/test_gotrue.py @@ -527,24 +527,7 @@ async def test_save_session_schedules_refresh_in_seconds() -> None: assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN -async def test_persist_session_false_keeps_session_out_of_storage() -> None: - storage = AsyncMemoryStorage() - client = AsyncSupabaseAuthClient( - url="http://auth.test", - http_session=AsyncHttpxSession(client=AsyncClient()), - auto_refresh_token=False, - persist_session=False, - storage=storage, - ) - session = mock_session("access-token") - - await client.session_manager.save_session(session) - - assert storage.storage == {} - assert await client.get_session() == session - - -async def test_persist_session_writes_session_to_default_storage_key() -> None: +async def test_save_session_writes_session_to_default_storage_key() -> None: storage = AsyncMemoryStorage() client = AsyncSupabaseAuthClient( url="http://auth.test", @@ -587,35 +570,20 @@ async def test_storage_key_separates_clients_sharing_a_storage() -> None: assert await second.get_session() == second_session -async def test_persist_session_false_does_not_restore_session_from_storage() -> None: - storage = AsyncMemoryStorage() - stored_session = mock_session("stored-access-token").model_dump_json() - await storage.set_item(STORAGE_KEY, stored_session) - client = AsyncSupabaseAuthClient( +async def test_clients_without_storage_do_not_share_sessions() -> None: + first = AsyncSupabaseAuthClient( url="http://auth.test", http_session=AsyncHttpxSession(client=AsyncClient()), auto_refresh_token=False, - persist_session=False, - storage=storage, ) - - await client.initialize_from_storage() - - assert await client.get_session() is None - assert storage.storage == {STORAGE_KEY: stored_session} - - -async def test_persist_session_false_keeps_code_verifier_out_of_storage() -> None: - storage = AsyncMemoryStorage() - client = AsyncSupabaseAuthClient( + second = AsyncSupabaseAuthClient( url="http://auth.test", http_session=AsyncHttpxSession(client=AsyncClient()), auto_refresh_token=False, - persist_session=False, - storage=storage, - flow_type="pkce", ) + session = mock_session("access-token") - await client.sign_in_with_oauth("github") + await first.session_manager.save_session(session) - assert storage.storage == {} + assert await first.get_session() == session + assert await second.get_session() is None diff --git a/src/auth/tests/_async/test_pkce_code_challenge.py b/src/auth/tests/_async/test_pkce_code_challenge.py index e6a57273d..51ca99662 100644 --- a/src/auth/tests/_async/test_pkce_code_challenge.py +++ b/src/auth/tests/_async/test_pkce_code_challenge.py @@ -6,10 +6,8 @@ import pytest from httpx import AsyncClient, MockTransport, Request, Response from supabase_utils.http.adapters.httpx import AsyncHttpxSession -from yarl import URL -from supabase_auth import AsyncMemoryStorage, AsyncSupabaseAuthClient -from supabase_auth.constants import STORAGE_KEY +from supabase_auth import AsyncSupabaseAuthClient from supabase_auth.errors import AuthError from supabase_auth.helpers import generate_pkce_challenge from supabase_auth.types import ( @@ -42,9 +40,6 @@ def mock_client( payload: Any, requests: list[Request], flow_type: AuthFlowType = "pkce", - *, - persist_session: bool = True, - storage: AsyncMemoryStorage | None = None, ) -> AsyncSupabaseAuthClient: def handler(request: Request) -> Response: requests.append(request) @@ -56,8 +51,6 @@ def handler(request: Request) -> Response: client=AsyncClient(transport=MockTransport(handler)) ), auto_refresh_token=False, - persist_session=persist_session, - storage=storage, flow_type=flow_type, ) @@ -213,22 +206,6 @@ async def test_exchange_code_uses_and_removes_stored_verifier() -> None: assert await stored_verifier(client) is None -async def test_exchange_code_without_persistence_uses_verifier_kept_in_memory() -> None: - storage = AsyncMemoryStorage() - stale_verifier_key = f"{STORAGE_KEY}-code-verifier" - await storage.set_item(stale_verifier_key, "stale-verifier") - requests: list[Request] = [] - client = mock_client(SESSION, requests, persist_session=False, storage=storage) - - oauth_response = await client.sign_in_with_oauth("github") - await client.exchange_code_for_session("auth-code") - - code_challenge = URL(oauth_response.url).query["code_challenge"] - verifier = request_body(requests)["code_verifier"] - assert generate_pkce_challenge(verifier) == code_challenge - assert storage.storage == {stale_verifier_key: "stale-verifier"} - - async def test_exchange_code_saves_session_and_signs_in() -> None: requests: list[Request] = [] client = mock_client(SESSION, requests) diff --git a/src/auth/tests/_sync/conftest.py b/src/auth/tests/_sync/conftest.py index e2bc80e13..36d935f37 100644 --- a/src/auth/tests/_sync/conftest.py +++ b/src/auth/tests/_sync/conftest.py @@ -118,7 +118,6 @@ def auth_client( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -131,7 +130,6 @@ def auth_client_with_session( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=False, http_session=request.param(), ) as client: credentials = mock_user_credentials() @@ -156,7 +154,6 @@ def auth_client_with_asymmetric_session( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=False, http_session=request.param(), ) as client: yield client @@ -169,7 +166,6 @@ def auth_subscription_client( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -182,7 +178,6 @@ def client_api_auto_confirm_enabled_client( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -195,7 +190,6 @@ def client_api_auto_confirm_off_signups_enabled_client( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_OFF, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client @@ -208,7 +202,6 @@ def client_api_auto_confirm_disabled_client( with SyncSupabaseAuthClient( url=GOTRUE_URL_SIGNUP_DISABLED_AUTO_CONFIRM_OFF, auto_refresh_token=False, - persist_session=True, http_session=request.param(), ) as client: yield client diff --git a/src/auth/tests/_sync/test_gotrue.py b/src/auth/tests/_sync/test_gotrue.py index 2d75fd25c..7d6e9c012 100644 --- a/src/auth/tests/_sync/test_gotrue.py +++ b/src/auth/tests/_sync/test_gotrue.py @@ -523,24 +523,7 @@ def test_save_session_schedules_refresh_in_seconds() -> None: assert 3600 - EXPIRY_MARGIN - 5 <= timer._seconds <= 3600 - EXPIRY_MARGIN -def test_persist_session_false_keeps_session_out_of_storage() -> None: - storage = SyncMemoryStorage() - client = SyncSupabaseAuthClient( - url="http://auth.test", - http_session=HttpxSession(client=Client()), - auto_refresh_token=False, - persist_session=False, - storage=storage, - ) - session = mock_session("access-token") - - client.session_manager.save_session(session) - - assert storage.storage == {} - assert client.get_session() == session - - -def test_persist_session_writes_session_to_default_storage_key() -> None: +def test_save_session_writes_session_to_default_storage_key() -> None: storage = SyncMemoryStorage() client = SyncSupabaseAuthClient( url="http://auth.test", @@ -583,35 +566,20 @@ def test_storage_key_separates_clients_sharing_a_storage() -> None: assert second.get_session() == second_session -def test_persist_session_false_does_not_restore_session_from_storage() -> None: - storage = SyncMemoryStorage() - stored_session = mock_session("stored-access-token").model_dump_json() - storage.set_item(STORAGE_KEY, stored_session) - client = SyncSupabaseAuthClient( +def test_clients_without_storage_do_not_share_sessions() -> None: + first = SyncSupabaseAuthClient( url="http://auth.test", http_session=HttpxSession(client=Client()), auto_refresh_token=False, - persist_session=False, - storage=storage, ) - - client.initialize_from_storage() - - assert client.get_session() is None - assert storage.storage == {STORAGE_KEY: stored_session} - - -def test_persist_session_false_keeps_code_verifier_out_of_storage() -> None: - storage = SyncMemoryStorage() - client = SyncSupabaseAuthClient( + second = SyncSupabaseAuthClient( url="http://auth.test", http_session=HttpxSession(client=Client()), auto_refresh_token=False, - persist_session=False, - storage=storage, - flow_type="pkce", ) + session = mock_session("access-token") - client.sign_in_with_oauth("github") + first.session_manager.save_session(session) - assert storage.storage == {} + assert first.get_session() == session + assert second.get_session() is None diff --git a/src/auth/tests/_sync/test_pkce_code_challenge.py b/src/auth/tests/_sync/test_pkce_code_challenge.py index 4a4392b81..5a24b92c1 100644 --- a/src/auth/tests/_sync/test_pkce_code_challenge.py +++ b/src/auth/tests/_sync/test_pkce_code_challenge.py @@ -6,10 +6,8 @@ import pytest from httpx import Client, MockTransport, Request, Response from supabase_utils.http.adapters.httpx import HttpxSession -from yarl import URL -from supabase_auth import SyncMemoryStorage, SyncSupabaseAuthClient -from supabase_auth.constants import STORAGE_KEY +from supabase_auth import SyncSupabaseAuthClient from supabase_auth.errors import AuthError from supabase_auth.helpers import generate_pkce_challenge from supabase_auth.types import ( @@ -42,9 +40,6 @@ def mock_client( payload: Any, requests: list[Request], flow_type: AuthFlowType = "pkce", - *, - persist_session: bool = True, - storage: SyncMemoryStorage | None = None, ) -> SyncSupabaseAuthClient: def handler(request: Request) -> Response: requests.append(request) @@ -54,8 +49,6 @@ def handler(request: Request) -> Response: url="https://example.supabase.co/auth/v1", http_session=HttpxSession(client=Client(transport=MockTransport(handler))), auto_refresh_token=False, - persist_session=persist_session, - storage=storage, flow_type=flow_type, ) @@ -207,22 +200,6 @@ def test_exchange_code_uses_and_removes_stored_verifier() -> None: assert stored_verifier(client) is None -def test_exchange_code_without_persistence_uses_verifier_kept_in_memory() -> None: - storage = SyncMemoryStorage() - stale_verifier_key = f"{STORAGE_KEY}-code-verifier" - storage.set_item(stale_verifier_key, "stale-verifier") - requests: list[Request] = [] - client = mock_client(SESSION, requests, persist_session=False, storage=storage) - - oauth_response = client.sign_in_with_oauth("github") - client.exchange_code_for_session("auth-code") - - code_challenge = URL(oauth_response.url).query["code_challenge"] - verifier = request_body(requests)["code_verifier"] - assert generate_pkce_challenge(verifier) == code_challenge - assert storage.storage == {stale_verifier_key: "stale-verifier"} - - def test_exchange_code_saves_session_and_signs_in() -> None: requests: list[Request] = [] client = mock_client(SESSION, requests)