Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/supabase/src/supabase/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,12 @@ def _get_auth_headers(self, authorization: Optional[str] = None) -> Dict[str, st
def _listen_to_auth_events(
self, event: AuthChangeEvent, session: Optional[Session]
) -> None:
access_token = self.supabase_key
if event in ["SIGNED_IN", "TOKEN_REFRESHED", "SIGNED_OUT"]:
# reset postgrest and storage instance on event change
self._postgrest = None
self._storage = None
self._functions = None
access_token = session.access_token if session else self.supabase_key
# Every event except SIGNED_OUT carries the current session, including
# USER_UPDATED and MFA_CHALLENGE_VERIFIED, whose token can carry new claims (aal2).
self._postgrest = None
self._storage = None
self._functions = None
access_token = session.access_token if session else self.supabase_key
auth_header = self._create_auth_header(access_token)
self.options.headers["Authorization"] = auth_header
self.auth._headers["Authorization"] = auth_header
Expand Down
13 changes: 6 additions & 7 deletions src/supabase/src/supabase/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,12 @@ def _get_auth_headers(self, authorization: Optional[str] = None) -> Dict[str, st
def _listen_to_auth_events(
self, event: AuthChangeEvent, session: Optional[Session]
) -> None:
access_token = self.supabase_key
if event in ["SIGNED_IN", "TOKEN_REFRESHED", "SIGNED_OUT"]:
# reset postgrest and storage instance on event change
self._postgrest = None
self._storage = None
self._functions = None
access_token = session.access_token if session else self.supabase_key
# Every event except SIGNED_OUT carries the current session, including
# USER_UPDATED and MFA_CHALLENGE_VERIFIED, whose token can carry new claims (aal2).
self._postgrest = None
self._storage = None
self._functions = None
access_token = session.access_token if session else self.supabase_key
auth_header = self._create_auth_header(access_token)
self.options.headers["Authorization"] = auth_header
self.auth._headers["Authorization"] = auth_header
Expand Down
39 changes: 37 additions & 2 deletions src/supabase/tests/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from httpx import AsyncClient as AsyncHttpxClient
from httpx import AsyncHTTPTransport, Limits, Timeout
from supabase_auth import AsyncMemoryStorage
from supabase_auth.types import AuthChangeEvent

from supabase import (
AsyncClient,
Expand Down Expand Up @@ -88,20 +89,33 @@ async def test_schema_update() -> None:
assert client.schema("new_schema")


async def test_updates_the_authorization_header_on_auth_events() -> None:
@pytest.mark.parametrize(
"event",
[
"SIGNED_IN",
"TOKEN_REFRESHED",
"USER_UPDATED",
"MFA_CHALLENGE_VERIFIED",
"PASSWORD_RECOVERY",
],
)
async def test_updates_the_authorization_header_on_auth_events(
event: AuthChangeEvent,
) -> None:
url = os.environ["SUPABASE_TEST_URL"]
key = os.environ["SUPABASE_TEST_KEY"]

client = await create_async_client(url, key)

assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == f"Bearer {key}"
assert client.postgrest

mock_session = MagicMock(access_token="secretuserjwt")
realtime_mock = AsyncMock()
client.realtime = realtime_mock

client._listen_to_auth_events("SIGNED_IN", mock_session)
client._listen_to_auth_events(event, mock_session)

updated_authorization = f"Bearer {mock_session.access_token}"

Expand All @@ -119,6 +133,27 @@ async def test_updates_the_authorization_header_on_auth_events() -> None:
assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == updated_authorization

assert client.functions.headers.get("Authorization") == updated_authorization


async def test_falls_back_to_the_anon_key_on_sign_out() -> None:
url = os.environ["SUPABASE_TEST_URL"]
key = os.environ["SUPABASE_TEST_KEY"]

client = await create_async_client(url, key)
client.realtime = AsyncMock()

client._listen_to_auth_events("SIGNED_IN", MagicMock(access_token="secretuserjwt"))
client._listen_to_auth_events("SIGNED_OUT", None)

anon_authorization = f"Bearer {key}"

assert client.options.headers.get("Authorization") == anon_authorization
assert client.auth._headers.get("Authorization") == anon_authorization
assert client.postgrest.session.headers.get("Authorization") == anon_authorization
assert client.storage.session.headers.get("Authorization") == anon_authorization
assert client.functions.headers.get("Authorization") == anon_authorization


async def test_supports_setting_a_global_authorization_header() -> None:
url = os.environ["SUPABASE_TEST_URL"]
Expand Down
39 changes: 37 additions & 2 deletions src/supabase/tests/_sync/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from httpx import Client as SyncHttpxClient
from httpx import HTTPTransport, Limits, Timeout
from supabase_auth import SyncMemoryStorage
from supabase_auth.types import AuthChangeEvent

from supabase import (
Client,
Expand Down Expand Up @@ -88,20 +89,33 @@ def test_schema_update() -> None:
assert client.schema("new_schema")


def test_updates_the_authorization_header_on_auth_events() -> None:
@pytest.mark.parametrize(
"event",
[
"SIGNED_IN",
"TOKEN_REFRESHED",
"USER_UPDATED",
"MFA_CHALLENGE_VERIFIED",
"PASSWORD_RECOVERY",
],
)
def test_updates_the_authorization_header_on_auth_events(
event: AuthChangeEvent,
) -> None:
url = os.environ["SUPABASE_TEST_URL"]
key = os.environ["SUPABASE_TEST_KEY"]

client = create_client(url, key)

assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == f"Bearer {key}"
assert client.postgrest

mock_session = MagicMock(access_token="secretuserjwt")
realtime_mock = Mock()
client.realtime = realtime_mock

client._listen_to_auth_events("SIGNED_IN", mock_session)
client._listen_to_auth_events(event, mock_session)

updated_authorization = f"Bearer {mock_session.access_token}"

Expand All @@ -119,6 +133,27 @@ def test_updates_the_authorization_header_on_auth_events() -> None:
assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == updated_authorization

assert client.functions.headers.get("Authorization") == updated_authorization


def test_falls_back_to_the_anon_key_on_sign_out() -> None:
url = os.environ["SUPABASE_TEST_URL"]
key = os.environ["SUPABASE_TEST_KEY"]

client = create_client(url, key)
client.realtime = Mock()

client._listen_to_auth_events("SIGNED_IN", MagicMock(access_token="secretuserjwt"))
client._listen_to_auth_events("SIGNED_OUT", None)

anon_authorization = f"Bearer {key}"

assert client.options.headers.get("Authorization") == anon_authorization
assert client.auth._headers.get("Authorization") == anon_authorization
assert client.postgrest.session.headers.get("Authorization") == anon_authorization
assert client.storage.session.headers.get("Authorization") == anon_authorization
assert client.functions.headers.get("Authorization") == anon_authorization


def test_supports_setting_a_global_authorization_header() -> None:
url = os.environ["SUPABASE_TEST_URL"]
Expand Down
Loading