Skip to content

Commit add4edd

Browse files
committed
feat: add third-party auth support
1 parent 37f9962 commit add4edd

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

supabase/_async/client.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616
from supafunc import AsyncFunctionsClient
1717

1818
from ..lib.client_options import ClientOptions
19+
from ..utils import AuthProxy, SupabaseException
1920
from .auth_client import AsyncSupabaseAuthClient
2021

2122

22-
# Create an exception class when user does not provide a valid url or key.
23-
class SupabaseException(Exception):
24-
def __init__(self, message: str):
25-
self.message = message
26-
super().__init__(self.message)
27-
28-
2923
class AsyncClient:
3024
"""Supabase client class."""
3125

@@ -77,10 +71,15 @@ def __init__(
7771
self.functions_url = f"{supabase_url}/functions/v1"
7872

7973
# Instantiate clients.
80-
self.auth = self._init_supabase_auth_client(
81-
auth_url=self.auth_url,
82-
client_options=options,
83-
)
74+
if not options.access_token:
75+
self.auth = self._init_supabase_auth_client(
76+
auth_url=self.auth_url,
77+
client_options=options,
78+
)
79+
else:
80+
self.access_token = options.access_token
81+
self.auth = AuthProxy()
82+
8483
self.realtime = self._init_realtime_client(
8584
realtime_url=self.realtime_url,
8685
supabase_key=self.supabase_key,
@@ -89,7 +88,9 @@ def __init__(
8988
self._postgrest = None
9089
self._storage = None
9190
self._functions = None
92-
self.auth.on_auth_state_change(self._listen_to_auth_events)
91+
92+
if not options.access_token:
93+
self.auth.on_auth_state_change(self._listen_to_auth_events)
9394

9495
@classmethod
9596
async def create(
@@ -103,8 +104,13 @@ async def create(
103104

104105
if auth_header is None:
105106
try:
106-
session = await client.auth.get_session()
107-
session_access_token = client._create_auth_header(session.access_token)
107+
if not options.access_token:
108+
session = await client.auth.get_session()
109+
session_access_token = client._create_auth_header(
110+
session.access_token
111+
)
112+
else:
113+
session_access_token = options.access_token
108114
except Exception as err:
109115
session_access_token = None
110116

supabase/_sync/client.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616
from supafunc import SyncFunctionsClient
1717

1818
from ..lib.client_options import ClientOptions
19+
from ..utils import AuthProxy, SupabaseException
1920
from .auth_client import SyncSupabaseAuthClient
2021

2122

22-
# Create an exception class when user does not provide a valid url or key.
23-
class SupabaseException(Exception):
24-
def __init__(self, message: str):
25-
self.message = message
26-
super().__init__(self.message)
27-
28-
2923
class SyncClient:
3024
"""Supabase client class."""
3125

@@ -77,10 +71,15 @@ def __init__(
7771
self.functions_url = f"{supabase_url}/functions/v1"
7872

7973
# Instantiate clients.
80-
self.auth = self._init_supabase_auth_client(
81-
auth_url=self.auth_url,
82-
client_options=options,
83-
)
74+
if not options.access_token:
75+
self.auth = self._init_supabase_auth_client(
76+
auth_url=self.auth_url,
77+
client_options=options,
78+
)
79+
else:
80+
self.access_token = options.access_token
81+
self.auth = AuthProxy()
82+
8483
self.realtime = self._init_realtime_client(
8584
realtime_url=self.realtime_url,
8685
supabase_key=self.supabase_key,
@@ -89,7 +88,9 @@ def __init__(
8988
self._postgrest = None
9089
self._storage = None
9190
self._functions = None
92-
self.auth.on_auth_state_change(self._listen_to_auth_events)
91+
92+
if not options.access_token:
93+
self.auth.on_auth_state_change(self._listen_to_auth_events)
9394

9495
@classmethod
9596
def create(
@@ -103,8 +104,13 @@ def create(
103104

104105
if auth_header is None:
105106
try:
106-
session = client.auth.get_session()
107-
session_access_token = client._create_auth_header(session.access_token)
107+
if not options.access_token:
108+
session = client.auth.get_session()
109+
session_access_token = client._create_auth_header(
110+
session.access_token
111+
)
112+
else:
113+
session_access_token = options.access_token
108114
except Exception as err:
109115
session_access_token = None
110116

supabase/lib/client_options.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class ClientOptions:
5151
flow_type: AuthFlowType = "implicit"
5252
"""flow type to use for authentication"""
5353

54+
access_token: Union[str, None] = None
55+
5456
def replace(
5557
self,
5658
schema: Optional[str] = None,
@@ -66,6 +68,7 @@ def replace(
6668
int, float, Timeout
6769
] = DEFAULT_STORAGE_CLIENT_TIMEOUT,
6870
flow_type: Optional[AuthFlowType] = None,
71+
access_token: Union[str, None] = None,
6972
) -> "ClientOptions":
7073
"""Create a new SupabaseClientOptions with changes"""
7174
client_options = ClientOptions()
@@ -84,4 +87,5 @@ def replace(
8487
storage_client_timeout or self.storage_client_timeout
8588
)
8689
client_options.flow_type = flow_type or self.flow_type
90+
client_options.access_token = access_token or self.access_token
8791
return client_options

supabase/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from gotrue.errors import AuthError
2+
3+
4+
# Create an exception class when user does not provide a valid url or key.
5+
class SupabaseException(Exception):
6+
def __init__(self, message: str):
7+
self.message = message
8+
super().__init__(self.message)
9+
10+
11+
class AuthProxy:
12+
def __getattr__(self, attr):
13+
raise AuthError(
14+
f"Supabase Client is configured with the access_token option, accessing supabase.auth.{attr} is not possible.",
15+
None,
16+
)

0 commit comments

Comments
 (0)