-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgotrue_admin_api.py
186 lines (166 loc) · 5.48 KB
/
gotrue_admin_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations
from functools import partial
from typing import Dict, List, Optional
from ..helpers import model_validate, parse_link_response, parse_user_response
from ..http_clients import SyncClient
from ..types import (
AdminUserAttributes,
AuthMFAAdminDeleteFactorParams,
AuthMFAAdminDeleteFactorResponse,
AuthMFAAdminListFactorsParams,
AuthMFAAdminListFactorsResponse,
GenerateLinkParams,
GenerateLinkResponse,
InviteUserByEmailOptions,
SignOutScope,
User,
UserResponse,
)
from .gotrue_admin_mfa_api import SyncGoTrueAdminMFAAPI
from .gotrue_base_api import SyncGoTrueBaseAPI
class SyncGoTrueAdminAPI(SyncGoTrueBaseAPI):
def __init__(
self,
*,
url: str = "",
headers: Dict[str, str] = {},
http_client: Optional[SyncClient] = None,
verify: bool = True,
proxy: Optional[str] = None,
) -> None:
SyncGoTrueBaseAPI.__init__(
self,
url=url,
headers=headers,
http_client=http_client,
verify=verify,
proxy=proxy,
)
self.mfa = SyncGoTrueAdminMFAAPI()
self.mfa.list_factors = self._list_factors
self.mfa.delete_factor = self._delete_factor
def sign_out(self, jwt: str, scope: SignOutScope = "global") -> None:
"""
Removes a logged-in session.
"""
return self._request(
"POST",
"logout",
query={"scope": scope},
jwt=jwt,
no_resolve_json=True,
)
def invite_user_by_email(
self,
email: str,
options: InviteUserByEmailOptions = {},
) -> UserResponse:
"""
Sends an invite link to an email address.
"""
return self._request(
"POST",
"invite",
body={"email": email, "data": options.get("data")},
redirect_to=options.get("redirect_to"),
xform=parse_user_response,
)
def generate_link(self, params: GenerateLinkParams) -> GenerateLinkResponse:
"""
Generates email links and OTPs to be sent via a custom email provider.
"""
return self._request(
"POST",
"admin/generate_link",
body={
"type": params.get("type"),
"email": params.get("email"),
"password": params.get("password"),
"new_email": params.get("new_email"),
"data": params.get("options", {}).get("data"),
},
redirect_to=params.get("options", {}).get("redirect_to"),
xform=parse_link_response,
)
# User Admin API
def create_user(self, attributes: AdminUserAttributes) -> UserResponse:
"""
Creates a new user.
This function should only be called on a server.
Never expose your `service_role` key in the browser.
"""
return self._request(
"POST",
"admin/users",
body=attributes,
xform=parse_user_response,
)
def list_users(self, page: int = None, per_page: int = None) -> List[User]:
"""
Get a list of users.
This function should only be called on a server.
Never expose your `service_role` key in the browser.
"""
return self._request(
"GET",
"admin/users",
query={"page": page, "per_page": per_page},
xform=lambda data: (
[model_validate(User, user) for user in data["users"]]
if "users" in data
else []
),
)
def get_user_by_id(self, uid: str) -> UserResponse:
"""
Get user by id.
This function should only be called on a server.
Never expose your `service_role` key in the browser.
"""
return self._request(
"GET",
f"admin/users/{uid}",
xform=parse_user_response,
)
def update_user_by_id(
self,
uid: str,
attributes: AdminUserAttributes,
) -> UserResponse:
"""
Updates the user data.
This function should only be called on a server.
Never expose your `service_role` key in the browser.
"""
return self._request(
"PUT",
f"admin/users/{uid}",
body=attributes,
xform=parse_user_response,
)
def delete_user(self, id: str, should_soft_delete: bool = False) -> None:
"""
Delete a user. Requires a `service_role` key.
This function should only be called on a server.
Never expose your `service_role` key in the browser.
"""
body = {"should_soft_delete": should_soft_delete}
return self._request("DELETE", f"admin/users/{id}", body=body)
def _list_factors(
self,
params: AuthMFAAdminListFactorsParams,
) -> AuthMFAAdminListFactorsResponse:
return self._request(
"GET",
f"admin/users/{params.get('user_id')}/factors",
xform=partial(model_validate, AuthMFAAdminListFactorsResponse),
)
def _delete_factor(
self,
params: AuthMFAAdminDeleteFactorParams,
) -> AuthMFAAdminDeleteFactorResponse:
return self._request(
"DELETE",
f"admin/users/{params.get('user_id')}/factors/{params.get('factor_id')}",
xform=partial(model_validate, AuthMFAAdminDeleteFactorResponse),
)