corebase 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- corebase/__init__.py +108 -0
- corebase/_async/__init__.py +1 -0
- corebase/_async/auth_client.py +55 -0
- corebase/_async/client.py +398 -0
- corebase/_sync/__init__.py +1 -0
- corebase/_sync/auth_client.py +55 -0
- corebase/_sync/client.py +393 -0
- corebase/auth/__init__.py +20 -0
- corebase/auth/_async/__init__.py +1 -0
- corebase/auth/_async/gotrue_admin_api.py +356 -0
- corebase/auth/_async/gotrue_admin_mfa_api.py +32 -0
- corebase/auth/_async/gotrue_admin_oauth_api.py +93 -0
- corebase/auth/_async/gotrue_base_api.py +78 -0
- corebase/auth/_async/gotrue_client.py +1404 -0
- corebase/auth/_async/gotrue_mfa_api.py +94 -0
- corebase/auth/_async/storage.py +31 -0
- corebase/auth/_sync/__init__.py +1 -0
- corebase/auth/_sync/gotrue_admin_api.py +356 -0
- corebase/auth/_sync/gotrue_admin_mfa_api.py +32 -0
- corebase/auth/_sync/gotrue_admin_oauth_api.py +93 -0
- corebase/auth/_sync/gotrue_base_api.py +78 -0
- corebase/auth/_sync/gotrue_client.py +1394 -0
- corebase/auth/_sync/gotrue_mfa_api.py +94 -0
- corebase/auth/_sync/storage.py +31 -0
- corebase/auth/constants.py +15 -0
- corebase/auth/errors.py +259 -0
- corebase/auth/helpers.py +288 -0
- corebase/auth/py.typed +0 -0
- corebase/auth/timer.py +53 -0
- corebase/auth/types.py +1021 -0
- corebase/auth/version.py +1 -0
- corebase/client.py +12 -0
- corebase/functions/__init__.py +47 -0
- corebase/functions/_async/__init__.py +1 -0
- corebase/functions/_async/functions_client.py +218 -0
- corebase/functions/_sync/__init__.py +1 -0
- corebase/functions/_sync/functions_client.py +218 -0
- corebase/functions/errors.py +62 -0
- corebase/functions/py.typed +0 -0
- corebase/functions/utils.py +33 -0
- corebase/functions/version.py +1 -0
- corebase/lib/__init__.py +3 -0
- corebase/lib/client_options.py +189 -0
- corebase/migrate/__init__.py +374 -0
- corebase/migrate/__main__.py +7 -0
- corebase/postgrest/__init__.py +62 -0
- corebase/postgrest/_async/__init__.py +1 -0
- corebase/postgrest/_async/client.py +206 -0
- corebase/postgrest/_async/request_builder.py +503 -0
- corebase/postgrest/_sync/__init__.py +1 -0
- corebase/postgrest/_sync/client.py +206 -0
- corebase/postgrest/_sync/request_builder.py +503 -0
- corebase/postgrest/base_client.py +61 -0
- corebase/postgrest/base_request_builder.py +787 -0
- corebase/postgrest/constants.py +6 -0
- corebase/postgrest/exceptions.py +115 -0
- corebase/postgrest/py.typed +0 -0
- corebase/postgrest/types.py +72 -0
- corebase/postgrest/utils.py +91 -0
- corebase/postgrest/version.py +1 -0
- corebase/py.typed +0 -0
- corebase/realtime/__init__.py +17 -0
- corebase/realtime/_async/__init__.py +0 -0
- corebase/realtime/_async/channel.py +766 -0
- corebase/realtime/_async/client.py +498 -0
- corebase/realtime/_async/presence.py +185 -0
- corebase/realtime/_async/push.py +162 -0
- corebase/realtime/_async/timer.py +41 -0
- corebase/realtime/_sync/__init__.py +0 -0
- corebase/realtime/_sync/channel.py +30 -0
- corebase/realtime/_sync/client.py +71 -0
- corebase/realtime/_sync/presence.py +10 -0
- corebase/realtime/exceptions.py +38 -0
- corebase/realtime/message.py +176 -0
- corebase/realtime/py.typed +0 -0
- corebase/realtime/transformers.py +9 -0
- corebase/realtime/types.py +246 -0
- corebase/realtime/utils.py +5 -0
- corebase/realtime/version.py +1 -0
- corebase/storage/__init__.py +56 -0
- corebase/storage/_async/__init__.py +1 -0
- corebase/storage/_async/analytics.py +82 -0
- corebase/storage/_async/bucket.py +142 -0
- corebase/storage/_async/client.py +106 -0
- corebase/storage/_async/file_api.py +630 -0
- corebase/storage/_async/request.py +32 -0
- corebase/storage/_async/vectors.py +213 -0
- corebase/storage/_sync/__init__.py +1 -0
- corebase/storage/_sync/analytics.py +80 -0
- corebase/storage/_sync/bucket.py +142 -0
- corebase/storage/_sync/client.py +106 -0
- corebase/storage/_sync/file_api.py +630 -0
- corebase/storage/_sync/request.py +32 -0
- corebase/storage/_sync/vectors.py +199 -0
- corebase/storage/constants.py +16 -0
- corebase/storage/exceptions.py +84 -0
- corebase/storage/py.typed +0 -0
- corebase/storage/types.py +313 -0
- corebase/storage/utils.py +26 -0
- corebase/storage/version.py +1 -0
- corebase/types.py +8 -0
- corebase/version.py +1 -0
- corebase-1.0.0.dist-info/METADATA +78 -0
- corebase-1.0.0.dist-info/RECORD +106 -0
- corebase-1.0.0.dist-info/WHEEL +4 -0
- corebase-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from ..types import (
|
|
2
|
+
AuthMFAChallengeResponse,
|
|
3
|
+
AuthMFAEnrollResponse,
|
|
4
|
+
AuthMFAGetAuthenticatorAssuranceLevelResponse,
|
|
5
|
+
AuthMFAListFactorsResponse,
|
|
6
|
+
AuthMFAUnenrollResponse,
|
|
7
|
+
AuthMFAVerifyResponse,
|
|
8
|
+
MFAChallengeAndVerifyParams,
|
|
9
|
+
MFAChallengeParams,
|
|
10
|
+
MFAEnrollParams,
|
|
11
|
+
MFAUnenrollParams,
|
|
12
|
+
MFAVerifyParams,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class AsyncGoTrueMFAAPI:
|
|
17
|
+
"""
|
|
18
|
+
Contains the full multi-factor authentication API.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
async def enroll(self, params: MFAEnrollParams) -> AuthMFAEnrollResponse:
|
|
22
|
+
"""
|
|
23
|
+
Starts the enrollment process for a new Multi-Factor Authentication
|
|
24
|
+
factor. This method creates a new factor in the 'unverified' state.
|
|
25
|
+
Present the QR code or secret to the user and ask them to add it to their
|
|
26
|
+
authenticator app. Ask the user to provide you with an authenticator code
|
|
27
|
+
from their app and verify it by calling challenge and then verify.
|
|
28
|
+
|
|
29
|
+
The first successful verification of an unverified factor activates the
|
|
30
|
+
factor. All other sessions are logged out and the current one gets an
|
|
31
|
+
`aal2` authenticator level.
|
|
32
|
+
"""
|
|
33
|
+
raise NotImplementedError() # pragma: no cover
|
|
34
|
+
|
|
35
|
+
async def challenge(self, params: MFAChallengeParams) -> AuthMFAChallengeResponse:
|
|
36
|
+
"""
|
|
37
|
+
Prepares a challenge used to verify that a user has access to a MFA
|
|
38
|
+
factor. Provide the challenge ID and verification code by calling `verify`.
|
|
39
|
+
"""
|
|
40
|
+
raise NotImplementedError() # pragma: no cover
|
|
41
|
+
|
|
42
|
+
async def challenge_and_verify(
|
|
43
|
+
self,
|
|
44
|
+
params: MFAChallengeAndVerifyParams,
|
|
45
|
+
) -> AuthMFAVerifyResponse:
|
|
46
|
+
"""
|
|
47
|
+
Helper method which creates a challenge and immediately uses the given code
|
|
48
|
+
to verify against it thereafter. The verification code is provided by the
|
|
49
|
+
user by entering a code seen in their authenticator app.
|
|
50
|
+
"""
|
|
51
|
+
raise NotImplementedError() # pragma: no cover
|
|
52
|
+
|
|
53
|
+
async def verify(self, params: MFAVerifyParams) -> AuthMFAVerifyResponse:
|
|
54
|
+
"""
|
|
55
|
+
Verifies a verification code against a challenge. The verification code is
|
|
56
|
+
provided by the user by entering a code seen in their authenticator app.
|
|
57
|
+
"""
|
|
58
|
+
raise NotImplementedError() # pragma: no cover
|
|
59
|
+
|
|
60
|
+
async def unenroll(self, params: MFAUnenrollParams) -> AuthMFAUnenrollResponse:
|
|
61
|
+
"""
|
|
62
|
+
Unenroll removes a MFA factor. Unverified factors can safely be ignored
|
|
63
|
+
and it's not necessary to unenroll them. Unenrolling a verified MFA factor
|
|
64
|
+
cannot be done from a session with an `aal1` authenticator level.
|
|
65
|
+
"""
|
|
66
|
+
raise NotImplementedError() # pragma: no cover
|
|
67
|
+
|
|
68
|
+
async def list_factors(self) -> AuthMFAListFactorsResponse:
|
|
69
|
+
"""
|
|
70
|
+
Returns the list of MFA factors enabled for this user. For most use cases
|
|
71
|
+
you should consider using `get_authenticator_assurance_level`.
|
|
72
|
+
|
|
73
|
+
This uses a cached version of the factors and avoids incurring a network call.
|
|
74
|
+
If you need to update this list, call `get_user` first.
|
|
75
|
+
"""
|
|
76
|
+
raise NotImplementedError() # pragma: no cover
|
|
77
|
+
|
|
78
|
+
async def get_authenticator_assurance_level(
|
|
79
|
+
self,
|
|
80
|
+
) -> AuthMFAGetAuthenticatorAssuranceLevelResponse:
|
|
81
|
+
"""
|
|
82
|
+
Returns the Authenticator Assurance Level (AAL) for the active session.
|
|
83
|
+
|
|
84
|
+
- `aal1` (or `null`) means that the user's identity has been verified only
|
|
85
|
+
with a conventional login (email+password, OTP, magic link, social login,
|
|
86
|
+
etc.).
|
|
87
|
+
- `aal2` means that the user's identity has been verified both with a
|
|
88
|
+
conventional login and at least one MFA factor.
|
|
89
|
+
|
|
90
|
+
Although this method returns a promise, it's fairly quick (microseconds)
|
|
91
|
+
and rarely uses the network. You can use this to check whether the current
|
|
92
|
+
user needs to be shown a screen to verify their MFA factors.
|
|
93
|
+
"""
|
|
94
|
+
raise NotImplementedError() # pragma: no cover
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class AsyncSupportedStorage(ABC):
|
|
7
|
+
@abstractmethod
|
|
8
|
+
async def get_item(self, key: str) -> str | None: ... # pragma: no cover
|
|
9
|
+
|
|
10
|
+
@abstractmethod
|
|
11
|
+
async def set_item(self, key: str, value: str) -> None: ... # pragma: no cover
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
async def remove_item(self, key: str) -> None: ... # pragma: no cover
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AsyncMemoryStorage(AsyncSupportedStorage):
|
|
18
|
+
def __init__(self) -> None:
|
|
19
|
+
self.storage: dict[str, str] = {}
|
|
20
|
+
|
|
21
|
+
async def get_item(self, key: str) -> str | None:
|
|
22
|
+
if key in self.storage:
|
|
23
|
+
return self.storage[key]
|
|
24
|
+
return None
|
|
25
|
+
|
|
26
|
+
async def set_item(self, key: str, value: str) -> None:
|
|
27
|
+
self.storage[key] = value
|
|
28
|
+
|
|
29
|
+
async def remove_item(self, key: str) -> None:
|
|
30
|
+
if key in self.storage:
|
|
31
|
+
del self.storage[key]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from httpx import Client, QueryParams
|
|
4
|
+
|
|
5
|
+
from ..helpers import (
|
|
6
|
+
parse_link_response,
|
|
7
|
+
parse_user_response,
|
|
8
|
+
validate_uuid,
|
|
9
|
+
)
|
|
10
|
+
from ..types import (
|
|
11
|
+
AdminUserAttributes,
|
|
12
|
+
AuthMFAAdminDeleteFactorParams,
|
|
13
|
+
AuthMFAAdminDeleteFactorResponse,
|
|
14
|
+
AuthMFAAdminListFactorsParams,
|
|
15
|
+
AuthMFAAdminListFactorsResponse,
|
|
16
|
+
AuthMFAAdminListFactorsResponseParser,
|
|
17
|
+
CreateOAuthClientParams,
|
|
18
|
+
GenerateLinkParams,
|
|
19
|
+
GenerateLinkResponse,
|
|
20
|
+
InviteUserByEmailOptions,
|
|
21
|
+
OAuthClient,
|
|
22
|
+
OAuthClientListResponse,
|
|
23
|
+
OAuthClientResponse,
|
|
24
|
+
PageParams,
|
|
25
|
+
SignOutScope,
|
|
26
|
+
UpdateOAuthClientParams,
|
|
27
|
+
User,
|
|
28
|
+
UserList,
|
|
29
|
+
UserResponse,
|
|
30
|
+
)
|
|
31
|
+
from .gotrue_admin_mfa_api import SyncGoTrueAdminMFAAPI
|
|
32
|
+
from .gotrue_admin_oauth_api import SyncGoTrueAdminOAuthAPI
|
|
33
|
+
from .gotrue_base_api import SyncGoTrueBaseAPI
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SyncGoTrueAdminAPI(SyncGoTrueBaseAPI):
|
|
37
|
+
def __init__(
|
|
38
|
+
self,
|
|
39
|
+
*,
|
|
40
|
+
url: str = "",
|
|
41
|
+
headers: dict[str, str] | None = None,
|
|
42
|
+
http_client: Client | None = None,
|
|
43
|
+
verify: bool = True,
|
|
44
|
+
proxy: str | None = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
http_headers = headers or {}
|
|
47
|
+
SyncGoTrueBaseAPI.__init__(
|
|
48
|
+
self,
|
|
49
|
+
url=url,
|
|
50
|
+
headers=http_headers,
|
|
51
|
+
http_client=http_client,
|
|
52
|
+
verify=verify,
|
|
53
|
+
proxy=proxy,
|
|
54
|
+
)
|
|
55
|
+
# TODO(@o-santi): why is is this done this way?
|
|
56
|
+
self.mfa = SyncGoTrueAdminMFAAPI()
|
|
57
|
+
self.mfa.list_factors = self._list_factors # type: ignore[method-assign]
|
|
58
|
+
self.mfa.delete_factor = self._delete_factor # type: ignore[method-assign]
|
|
59
|
+
self.oauth = SyncGoTrueAdminOAuthAPI()
|
|
60
|
+
self.oauth.list_clients = self._list_oauth_clients # type: ignore[method-assign]
|
|
61
|
+
self.oauth.create_client = self._create_oauth_client # type: ignore[method-assign]
|
|
62
|
+
self.oauth.get_client = self._get_oauth_client # type: ignore[method-assign]
|
|
63
|
+
self.oauth.update_client = self._update_oauth_client # type: ignore[method-assign]
|
|
64
|
+
self.oauth.delete_client = self._delete_oauth_client # type: ignore[assignment, method-assign]
|
|
65
|
+
self.oauth.regenerate_client_secret = self._regenerate_oauth_client_secret # type: ignore[method-assign]
|
|
66
|
+
|
|
67
|
+
def sign_out(self, jwt: str, scope: SignOutScope = "global") -> None:
|
|
68
|
+
"""
|
|
69
|
+
Removes a logged-in session.
|
|
70
|
+
"""
|
|
71
|
+
self._request(
|
|
72
|
+
"POST",
|
|
73
|
+
"logout",
|
|
74
|
+
query=QueryParams(scope=scope),
|
|
75
|
+
jwt=jwt,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def invite_user_by_email(
|
|
79
|
+
self,
|
|
80
|
+
email: str,
|
|
81
|
+
options: InviteUserByEmailOptions | None = None,
|
|
82
|
+
) -> UserResponse:
|
|
83
|
+
"""
|
|
84
|
+
Sends an invite link to an email address.
|
|
85
|
+
"""
|
|
86
|
+
email_options = options or {}
|
|
87
|
+
response = self._request(
|
|
88
|
+
"POST",
|
|
89
|
+
"invite",
|
|
90
|
+
body={"email": email, "data": email_options.get("data")},
|
|
91
|
+
redirect_to=email_options.get("redirect_to"),
|
|
92
|
+
)
|
|
93
|
+
return parse_user_response(response)
|
|
94
|
+
|
|
95
|
+
def generate_link(self, params: GenerateLinkParams) -> GenerateLinkResponse:
|
|
96
|
+
"""
|
|
97
|
+
Generates email links and OTPs to be sent via a custom email provider.
|
|
98
|
+
"""
|
|
99
|
+
response = self._request(
|
|
100
|
+
"POST",
|
|
101
|
+
"admin/generate_link",
|
|
102
|
+
body={
|
|
103
|
+
"type": params.get("type"),
|
|
104
|
+
"email": params.get("email"),
|
|
105
|
+
"password": params.get("password"),
|
|
106
|
+
"new_email": params.get("new_email"),
|
|
107
|
+
"data": params.get("options", {}).get("data"),
|
|
108
|
+
},
|
|
109
|
+
redirect_to=params.get("options", {}).get("redirect_to"),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
return parse_link_response(response)
|
|
113
|
+
|
|
114
|
+
# User Admin API
|
|
115
|
+
|
|
116
|
+
def create_user(self, attributes: AdminUserAttributes) -> UserResponse:
|
|
117
|
+
"""
|
|
118
|
+
Creates a new user.
|
|
119
|
+
|
|
120
|
+
This function should only be called on a server.
|
|
121
|
+
Never expose your `secret` key in the browser.
|
|
122
|
+
"""
|
|
123
|
+
response = self._request(
|
|
124
|
+
"POST",
|
|
125
|
+
"admin/users",
|
|
126
|
+
body=attributes,
|
|
127
|
+
)
|
|
128
|
+
return parse_user_response(response)
|
|
129
|
+
|
|
130
|
+
def list_users(
|
|
131
|
+
self, page: int | None = None, per_page: int | None = None
|
|
132
|
+
) -> list[User]:
|
|
133
|
+
"""
|
|
134
|
+
Get a list of users.
|
|
135
|
+
|
|
136
|
+
This function should only be called on a server.
|
|
137
|
+
Never expose your `secret` key in the browser.
|
|
138
|
+
"""
|
|
139
|
+
response = self._request(
|
|
140
|
+
"GET",
|
|
141
|
+
"admin/users",
|
|
142
|
+
query=QueryParams(page=page, per_page=per_page),
|
|
143
|
+
)
|
|
144
|
+
return UserList.model_validate_json(response.content).users
|
|
145
|
+
|
|
146
|
+
def get_user_by_id(self, uid: str) -> UserResponse:
|
|
147
|
+
"""
|
|
148
|
+
Get user by id.
|
|
149
|
+
|
|
150
|
+
This function should only be called on a server.
|
|
151
|
+
Never expose your `secret` key in the browser.
|
|
152
|
+
"""
|
|
153
|
+
validate_uuid(uid)
|
|
154
|
+
|
|
155
|
+
response = self._request(
|
|
156
|
+
"GET",
|
|
157
|
+
f"admin/users/{uid}",
|
|
158
|
+
)
|
|
159
|
+
return parse_user_response(response)
|
|
160
|
+
|
|
161
|
+
def update_user_by_id(
|
|
162
|
+
self,
|
|
163
|
+
uid: str,
|
|
164
|
+
attributes: AdminUserAttributes,
|
|
165
|
+
) -> UserResponse:
|
|
166
|
+
"""
|
|
167
|
+
Updates the user data.
|
|
168
|
+
|
|
169
|
+
This function should only be called on a server.
|
|
170
|
+
Never expose your `secret` key in the browser.
|
|
171
|
+
"""
|
|
172
|
+
validate_uuid(uid)
|
|
173
|
+
response = self._request(
|
|
174
|
+
"PUT",
|
|
175
|
+
f"admin/users/{uid}",
|
|
176
|
+
body=attributes,
|
|
177
|
+
)
|
|
178
|
+
return parse_user_response(response)
|
|
179
|
+
|
|
180
|
+
def delete_user(self, id: str, should_soft_delete: bool = False) -> None:
|
|
181
|
+
"""
|
|
182
|
+
Delete a user. Requires a `secret` key.
|
|
183
|
+
|
|
184
|
+
This function should only be called on a server.
|
|
185
|
+
Never expose your `secret` key in the browser.
|
|
186
|
+
"""
|
|
187
|
+
validate_uuid(id)
|
|
188
|
+
body = {"should_soft_delete": should_soft_delete}
|
|
189
|
+
self._request("DELETE", f"admin/users/{id}", body=body)
|
|
190
|
+
|
|
191
|
+
def _list_factors(
|
|
192
|
+
self,
|
|
193
|
+
params: AuthMFAAdminListFactorsParams,
|
|
194
|
+
) -> AuthMFAAdminListFactorsResponse:
|
|
195
|
+
validate_uuid(params.get("user_id"))
|
|
196
|
+
response = self._request(
|
|
197
|
+
"GET",
|
|
198
|
+
f"admin/users/{params.get('user_id')}/factors",
|
|
199
|
+
)
|
|
200
|
+
return AuthMFAAdminListFactorsResponseParser.validate_json(response.content)
|
|
201
|
+
|
|
202
|
+
def _delete_factor(
|
|
203
|
+
self,
|
|
204
|
+
params: AuthMFAAdminDeleteFactorParams,
|
|
205
|
+
) -> AuthMFAAdminDeleteFactorResponse:
|
|
206
|
+
validate_uuid(params.get("user_id"))
|
|
207
|
+
validate_uuid(params.get("id"))
|
|
208
|
+
response = self._request(
|
|
209
|
+
"DELETE",
|
|
210
|
+
f"admin/users/{params.get('user_id')}/factors/{params.get('id')}",
|
|
211
|
+
)
|
|
212
|
+
return AuthMFAAdminDeleteFactorResponse.model_validate_json(response.content)
|
|
213
|
+
|
|
214
|
+
def _list_oauth_clients(
|
|
215
|
+
self,
|
|
216
|
+
params: PageParams | None = None,
|
|
217
|
+
) -> OAuthClientListResponse:
|
|
218
|
+
"""
|
|
219
|
+
Lists all OAuth clients with optional pagination.
|
|
220
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
221
|
+
|
|
222
|
+
This function should only be called on a server.
|
|
223
|
+
Never expose your `secret` key in the browser.
|
|
224
|
+
"""
|
|
225
|
+
if params:
|
|
226
|
+
query = QueryParams(page=params.page, per_page=params.per_page)
|
|
227
|
+
else:
|
|
228
|
+
query = None
|
|
229
|
+
response = self._request(
|
|
230
|
+
"GET",
|
|
231
|
+
"admin/oauth/clients",
|
|
232
|
+
query=query,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
result = OAuthClientListResponse.model_validate_json(response.content)
|
|
236
|
+
|
|
237
|
+
# Parse pagination headers
|
|
238
|
+
total = response.headers.get("x-total-count")
|
|
239
|
+
if total:
|
|
240
|
+
result.total = int(total)
|
|
241
|
+
|
|
242
|
+
links = response.headers.get("link")
|
|
243
|
+
if links:
|
|
244
|
+
for link in links.split(","):
|
|
245
|
+
parts = link.split(";")
|
|
246
|
+
if len(parts) >= 2:
|
|
247
|
+
page_match = parts[0].split("page=")
|
|
248
|
+
if len(page_match) >= 2:
|
|
249
|
+
page_num = int(page_match[1].split("&")[0].rstrip(">"))
|
|
250
|
+
rel = parts[1].split("=")[1].strip('"')
|
|
251
|
+
if rel == "next":
|
|
252
|
+
result.next_page = page_num
|
|
253
|
+
elif rel == "last":
|
|
254
|
+
result.last_page = page_num
|
|
255
|
+
|
|
256
|
+
return result
|
|
257
|
+
|
|
258
|
+
def _create_oauth_client(
|
|
259
|
+
self,
|
|
260
|
+
params: CreateOAuthClientParams,
|
|
261
|
+
) -> OAuthClientResponse:
|
|
262
|
+
"""
|
|
263
|
+
Creates a new OAuth client.
|
|
264
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
265
|
+
|
|
266
|
+
This function should only be called on a server.
|
|
267
|
+
Never expose your `secret` key in the browser.
|
|
268
|
+
"""
|
|
269
|
+
response = self._request(
|
|
270
|
+
"POST",
|
|
271
|
+
"admin/oauth/clients",
|
|
272
|
+
body=params,
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
return OAuthClientResponse(
|
|
276
|
+
client=OAuthClient.model_validate_json(response.content)
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
def _get_oauth_client(
|
|
280
|
+
self,
|
|
281
|
+
client_id: str,
|
|
282
|
+
) -> OAuthClientResponse:
|
|
283
|
+
"""
|
|
284
|
+
Gets details of a specific OAuth client.
|
|
285
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
286
|
+
|
|
287
|
+
This function should only be called on a server.
|
|
288
|
+
Never expose your `secret` key in the browser.
|
|
289
|
+
"""
|
|
290
|
+
validate_uuid(client_id)
|
|
291
|
+
response = self._request(
|
|
292
|
+
"GET",
|
|
293
|
+
f"admin/oauth/clients/{client_id}",
|
|
294
|
+
)
|
|
295
|
+
return OAuthClientResponse(
|
|
296
|
+
client=OAuthClient.model_validate_json(response.content)
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
def _update_oauth_client(
|
|
300
|
+
self,
|
|
301
|
+
client_id: str,
|
|
302
|
+
params: UpdateOAuthClientParams,
|
|
303
|
+
) -> OAuthClientResponse:
|
|
304
|
+
"""
|
|
305
|
+
Updates an OAuth client.
|
|
306
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
307
|
+
|
|
308
|
+
This function should only be called on a server.
|
|
309
|
+
Never expose your `secret` key in the browser.
|
|
310
|
+
"""
|
|
311
|
+
validate_uuid(client_id)
|
|
312
|
+
response = self._request(
|
|
313
|
+
"PUT",
|
|
314
|
+
f"admin/oauth/clients/{client_id}",
|
|
315
|
+
body=params,
|
|
316
|
+
)
|
|
317
|
+
return OAuthClientResponse(
|
|
318
|
+
client=OAuthClient.model_validate_json(response.content)
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
def _delete_oauth_client(
|
|
322
|
+
self,
|
|
323
|
+
client_id: str,
|
|
324
|
+
) -> None:
|
|
325
|
+
"""
|
|
326
|
+
Deletes an OAuth client.
|
|
327
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
328
|
+
|
|
329
|
+
This function should only be called on a server.
|
|
330
|
+
Never expose your `secret` key in the browser.
|
|
331
|
+
"""
|
|
332
|
+
validate_uuid(client_id)
|
|
333
|
+
self._request(
|
|
334
|
+
"DELETE",
|
|
335
|
+
f"admin/oauth/clients/{client_id}",
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
def _regenerate_oauth_client_secret(
|
|
339
|
+
self,
|
|
340
|
+
client_id: str,
|
|
341
|
+
) -> OAuthClientResponse:
|
|
342
|
+
"""
|
|
343
|
+
Regenerates the secret for an OAuth client.
|
|
344
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
345
|
+
|
|
346
|
+
This function should only be called on a server.
|
|
347
|
+
Never expose your `secret` key in the browser.
|
|
348
|
+
"""
|
|
349
|
+
validate_uuid(client_id)
|
|
350
|
+
response = self._request(
|
|
351
|
+
"POST",
|
|
352
|
+
f"admin/oauth/clients/{client_id}/regenerate_secret",
|
|
353
|
+
)
|
|
354
|
+
return OAuthClientResponse(
|
|
355
|
+
client=OAuthClient.model_validate_json(response.content)
|
|
356
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from ..types import (
|
|
2
|
+
AuthMFAAdminDeleteFactorParams,
|
|
3
|
+
AuthMFAAdminDeleteFactorResponse,
|
|
4
|
+
AuthMFAAdminListFactorsParams,
|
|
5
|
+
AuthMFAAdminListFactorsResponse,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SyncGoTrueAdminMFAAPI:
|
|
10
|
+
"""
|
|
11
|
+
Contains the full multi-factor authentication administration API.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def list_factors(
|
|
15
|
+
self,
|
|
16
|
+
params: AuthMFAAdminListFactorsParams,
|
|
17
|
+
) -> AuthMFAAdminListFactorsResponse:
|
|
18
|
+
"""
|
|
19
|
+
Lists all factors attached to a user.
|
|
20
|
+
"""
|
|
21
|
+
raise NotImplementedError() # pragma: no cover
|
|
22
|
+
|
|
23
|
+
def delete_factor(
|
|
24
|
+
self,
|
|
25
|
+
params: AuthMFAAdminDeleteFactorParams,
|
|
26
|
+
) -> AuthMFAAdminDeleteFactorResponse:
|
|
27
|
+
"""
|
|
28
|
+
Deletes a factor on a user. This will log the user out of all active
|
|
29
|
+
sessions (if the deleted factor was verified). There's no need to delete
|
|
30
|
+
unverified factors.
|
|
31
|
+
"""
|
|
32
|
+
raise NotImplementedError() # pragma: no cover
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from ..types import (
|
|
2
|
+
CreateOAuthClientParams,
|
|
3
|
+
OAuthClientListResponse,
|
|
4
|
+
OAuthClientResponse,
|
|
5
|
+
PageParams,
|
|
6
|
+
UpdateOAuthClientParams,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SyncGoTrueAdminOAuthAPI:
|
|
11
|
+
"""
|
|
12
|
+
Contains all OAuth client administration methods.
|
|
13
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def list_clients(
|
|
17
|
+
self,
|
|
18
|
+
params: PageParams | None = None,
|
|
19
|
+
) -> OAuthClientListResponse:
|
|
20
|
+
"""
|
|
21
|
+
Lists all OAuth clients with optional pagination.
|
|
22
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
23
|
+
|
|
24
|
+
This function should only be called on a server.
|
|
25
|
+
Never expose your `secret` key in the browser.
|
|
26
|
+
"""
|
|
27
|
+
raise NotImplementedError() # pragma: no cover
|
|
28
|
+
|
|
29
|
+
def create_client(
|
|
30
|
+
self,
|
|
31
|
+
params: CreateOAuthClientParams,
|
|
32
|
+
) -> OAuthClientResponse:
|
|
33
|
+
"""
|
|
34
|
+
Creates a new OAuth client.
|
|
35
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
36
|
+
|
|
37
|
+
This function should only be called on a server.
|
|
38
|
+
Never expose your `secret` key in the browser.
|
|
39
|
+
"""
|
|
40
|
+
raise NotImplementedError() # pragma: no cover
|
|
41
|
+
|
|
42
|
+
def get_client(
|
|
43
|
+
self,
|
|
44
|
+
client_id: str,
|
|
45
|
+
) -> OAuthClientResponse:
|
|
46
|
+
"""
|
|
47
|
+
Gets details of a specific OAuth client.
|
|
48
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
49
|
+
|
|
50
|
+
This function should only be called on a server.
|
|
51
|
+
Never expose your `secret` key in the browser.
|
|
52
|
+
"""
|
|
53
|
+
raise NotImplementedError() # pragma: no cover
|
|
54
|
+
|
|
55
|
+
def update_client(
|
|
56
|
+
self,
|
|
57
|
+
client_id: str,
|
|
58
|
+
params: UpdateOAuthClientParams,
|
|
59
|
+
) -> OAuthClientResponse:
|
|
60
|
+
"""
|
|
61
|
+
Updates an OAuth client.
|
|
62
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
63
|
+
|
|
64
|
+
This function should only be called on a server.
|
|
65
|
+
Never expose your `secret` key in the browser.
|
|
66
|
+
"""
|
|
67
|
+
raise NotImplementedError() # pragma: no cover
|
|
68
|
+
|
|
69
|
+
def delete_client(
|
|
70
|
+
self,
|
|
71
|
+
client_id: str,
|
|
72
|
+
) -> OAuthClientResponse:
|
|
73
|
+
"""
|
|
74
|
+
Deletes an OAuth client.
|
|
75
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
76
|
+
|
|
77
|
+
This function should only be called on a server.
|
|
78
|
+
Never expose your `secret` key in the browser.
|
|
79
|
+
"""
|
|
80
|
+
raise NotImplementedError() # pragma: no cover
|
|
81
|
+
|
|
82
|
+
def regenerate_client_secret(
|
|
83
|
+
self,
|
|
84
|
+
client_id: str,
|
|
85
|
+
) -> OAuthClientResponse:
|
|
86
|
+
"""
|
|
87
|
+
Regenerates the secret for an OAuth client.
|
|
88
|
+
Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
|
|
89
|
+
|
|
90
|
+
This function should only be called on a server.
|
|
91
|
+
Never expose your `secret` key in the browser.
|
|
92
|
+
"""
|
|
93
|
+
raise NotImplementedError() # pragma: no cover
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Literal, Self
|
|
4
|
+
|
|
5
|
+
from httpx import Client, HTTPStatusError, QueryParams, RequestError, Response
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
from ..constants import API_VERSION_HEADER_NAME, API_VERSIONS_2024_01_01_NAME
|
|
9
|
+
from ..helpers import handle_exception
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SyncGoTrueBaseAPI:
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
*,
|
|
16
|
+
url: str,
|
|
17
|
+
headers: dict[str, str],
|
|
18
|
+
http_client: Client | None,
|
|
19
|
+
verify: bool = True,
|
|
20
|
+
proxy: str | None = None,
|
|
21
|
+
) -> None:
|
|
22
|
+
self._url = url.rstrip("/")
|
|
23
|
+
# Copy: callers pass a dict they keep mutating, and GoTrueClient hands
|
|
24
|
+
# its own `_headers` to the admin API. Sharing the object means a later
|
|
25
|
+
# `_headers["Authorization"] = <user token>` silently downgrades every
|
|
26
|
+
# other holder of that dict.
|
|
27
|
+
self._headers = dict(headers)
|
|
28
|
+
self._http_client = http_client or Client(
|
|
29
|
+
verify=bool(verify),
|
|
30
|
+
proxy=proxy,
|
|
31
|
+
follow_redirects=True,
|
|
32
|
+
http2=True,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def __enter__(self) -> Self:
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
def __exit__(self, exc_t, exc_v, exc_tb) -> None:
|
|
39
|
+
self.close()
|
|
40
|
+
|
|
41
|
+
def close(self) -> None:
|
|
42
|
+
self._http_client.close()
|
|
43
|
+
|
|
44
|
+
def _request(
|
|
45
|
+
self,
|
|
46
|
+
method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"],
|
|
47
|
+
path: str,
|
|
48
|
+
*,
|
|
49
|
+
jwt: str | None = None,
|
|
50
|
+
redirect_to: str | None = None,
|
|
51
|
+
headers: dict[str, str] | None = None,
|
|
52
|
+
query: QueryParams | None = None,
|
|
53
|
+
body: Any | None = None,
|
|
54
|
+
) -> Response:
|
|
55
|
+
url = f"{self._url}/{path}"
|
|
56
|
+
headers = {**self._headers, **(headers or {})}
|
|
57
|
+
if API_VERSION_HEADER_NAME not in headers:
|
|
58
|
+
headers[API_VERSION_HEADER_NAME] = API_VERSIONS_2024_01_01_NAME
|
|
59
|
+
if "Content-Type" not in headers:
|
|
60
|
+
headers["Content-Type"] = "application/json;charset=UTF-8"
|
|
61
|
+
if jwt:
|
|
62
|
+
headers["Authorization"] = f"Bearer {jwt}"
|
|
63
|
+
query = query or QueryParams()
|
|
64
|
+
if redirect_to:
|
|
65
|
+
query = query.set("redirect_to", redirect_to)
|
|
66
|
+
try:
|
|
67
|
+
response = self._http_client.request(
|
|
68
|
+
method,
|
|
69
|
+
url,
|
|
70
|
+
headers=headers,
|
|
71
|
+
params=query,
|
|
72
|
+
json=body.model_dump() if isinstance(body, BaseModel) else body,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
response.raise_for_status()
|
|
76
|
+
return response
|
|
77
|
+
except (HTTPStatusError, RequestError, RuntimeError) as e:
|
|
78
|
+
raise handle_exception(e) from e
|