-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhandle.py
More file actions
326 lines (267 loc) · 9.43 KB
/
handle.py
File metadata and controls
326 lines (267 loc) · 9.43 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from __future__ import annotations
import logging
import secrets
import typing as t
from datetime import datetime
import sqlalchemy
from dependency_injector.wiring import Provide
from quart import Quart
from quart_sqlalchemy.session import SessionProxy
from quart_sqlalchemy.sim import signals
from quart_sqlalchemy.sim.logic import LogicComponent
from quart_sqlalchemy.sim.model import AuthUser
from quart_sqlalchemy.sim.model import AuthWallet
from quart_sqlalchemy.sim.model import EntityType
from quart_sqlalchemy.sim.model import WalletType
from quart_sqlalchemy.sim.util import ObjectID
sa = sqlalchemy
logger = logging.getLogger(__name__)
CLIENTS_PER_API_USER_LIMIT = 50
def get_product_type_by_client_id(_):
return EntityType.MAGIC.value
class MaxClientsExceeded(Exception):
pass
class AuthUserBaseError(Exception):
pass
class InvalidSubstringError(AuthUserBaseError):
pass
class HandlerBase:
logic: LogicComponent = Provide["logic"]
session_factory = SessionProxy()
class MagicClientHandler(HandlerBase):
auth_user_handler: AuthUserHandler = Provide["AuthUserHandler"]
def add(
self,
app_name=None,
rate_limit_tier=None,
connect_interop=None,
is_signing_modal_enabled=False,
global_audience_enabled=False,
):
"""Registers a new client.
Args:
is_magic_connect_enabled (boolean): if True, it will create a Magic Connect app.
Returns:
A ``MagicClient``.
"""
return self.logic.MagicClient.add(
self.session_factory(),
app_name=app_name,
rate_limit_tier=rate_limit_tier,
connect_interop=connect_interop,
is_signing_modal_enabled=is_signing_modal_enabled,
global_audience_enabled=global_audience_enabled,
)
def get_by_public_api_key(self, public_api_key):
return self.logic.MagicClient.get_by_public_api_key(self.session_factory(), public_api_key)
def get_by_id(self, magic_client_id):
return self.logic.MagicClient.get_by_id(self.session_factory(), magic_client_id)
def update_app_name_by_id(self, magic_client_id, app_name):
"""
Args:
magic_client_id (ObjectID|int|str): self explanatory.
app_name (str): Desired application name.
Returns:
None if `magic_client_id` doesn't exist in the db
app_name if update was successful
"""
client = self.logic.MagicClient.update_by_id(
self.session_factory(), magic_client_id, app_name=app_name
)
return client.app_name if client else None
def update_by_id(self, magic_client_id, **kwargs):
return self.logic.MagicClient.update_by_id(
self.session_factory(), magic_client_id, **kwargs
)
def set_inactive_by_id(self, magic_client_id):
"""
Args:
magic_client_id (ObjectID|int|str): self explanatory.
Returns:
None
"""
self.logic.MagicClient.update_by_id(
self.session_factory(), magic_client_id, is_active=False
)
def get_users_for_client(
self,
magic_client_id,
offset=None,
limit=None,
):
"""
Returns emails and signup timestamps for all auth users belonging to a given client
"""
product_type = get_product_type_by_client_id(magic_client_id)
auth_users = self.auth_user_handler.get_by_client_id_and_user_type(
magic_client_id,
product_type,
offset=offset,
limit=limit,
)
return {
"users": [
dict(email=u.email or "none", signup_ts=int(datetime.timestamp(u.time_created)))
for u in auth_users
]
}
class AuthUserHandler(HandlerBase):
def get_by_session_token(self, session_token):
return self.logic.AuthUser.get_by_session_token(self.session_factory(), session_token)
def get_or_create_by_email_and_client_id(
self,
email,
client_id,
user_type=EntityType.MAGIC.value,
):
session = self.session_factory()
with session.begin_nested():
auth_user = self.logic.AuthUser.get_by_email_and_client_id(
session,
email,
client_id,
user_type=user_type,
for_update=True,
)
if not auth_user:
auth_user = self.logic.AuthUser.add_by_email_and_client_id(
session,
client_id,
email=email,
user_type=user_type,
)
return auth_user
def create_verified_user(
self,
client_id,
email,
user_type=EntityType.FORTMATIC.value,
**kwargs,
):
session = self.session_factory()
with session.begin_nested():
auid = self.logic.AuthUser.add_by_email_and_client_id(
session,
client_id,
email,
user_type=user_type,
**kwargs,
).id
session.flush()
auth_user = self.logic.AuthUser.update_by_id(
session,
auid,
date_verified=datetime.utcnow(),
current_session_token=secrets.token_hex(16),
)
return auth_user
def get_by_id(self, auth_user_id) -> AuthUser:
return self.logic.AuthUser.get_by_id(self.session_factory(), auth_user_id)
def get_by_client_id_and_user_type(
self,
client_id,
user_type,
offset=None,
limit=None,
):
return self.logic.AuthUser.get_by_client_id_and_user_type(
self.session_factory(),
client_id,
user_type,
offset=offset,
limit=limit,
)
def exist_by_email_client_id_and_user_type(self, email, client_id, user_type):
return self.logic.AuthUser.exist_by_email_and_client_id(
self.session_factory(),
email,
client_id,
user_type=user_type,
)
def update_email_by_id(self, model_id, email):
return self.logic.AuthUser.update_by_id(self.session_factory(), model_id, email=email)
def get_by_email_client_id_and_user_type(self, email, client_id, user_type):
return self.logic.AuthUser.get_by_email_and_client_id(
self.session_factory(),
email,
client_id,
user_type,
)
def mark_date_verified_by_id(self, model_id):
return self.logic.AuthUser.update_by_id(
self.session_factory(),
model_id,
date_verified=datetime.utcnow(),
)
def set_role_by_email_magic_client_id(self, email, magic_client_id, role):
session = self.session_factory()
auth_user = self.logic.AuthUser.get_by_email_and_client_id(
session,
email,
magic_client_id,
EntityType.MAGIC.value,
for_update=True,
)
if not auth_user:
auth_user = self.logic.AuthUser.add_by_email_and_client_id(
session,
magic_client_id,
email,
user_type=EntityType.MAGIC.value,
)
session.flush()
return self.logic.AuthUser.update_by_id(session, auth_user.id, **{role: True})
def mark_as_inactive(self, auth_user_id):
self.logic.AuthUser.update_by_id(self.session_factory(), auth_user_id, is_active=False)
@signals.auth_user_duplicate.connect
def handle_duplicate_auth_users(
app: Quart,
original_auth_user_id: ObjectID,
duplicate_auth_user_ids: t.Sequence[ObjectID],
) -> None:
for dupe_id in duplicate_auth_user_ids:
app.container.logic().AuthUser.update_by_id(dupe_id, is_active=False)
class AuthWalletHandler(HandlerBase):
def get_by_id(self, model_id):
return self.logic.AuthWallet.get_by_id(self.session_factory(), model_id)
def get_by_public_address(self, public_address):
return self.logic.AuthWallet().get_by_public_address(self.session_factory(), public_address)
def get_by_auth_user_id(
self,
auth_user_id: ObjectID,
network: t.Optional[str] = None,
wallet_type: t.Optional[WalletType] = None,
**kwargs,
) -> t.List[AuthWallet]:
return self.logic.AuthWallet.get_by_auth_user_id(
self.session_factory(),
auth_user_id,
network=network,
wallet_type=wallet_type,
**kwargs,
)
def sync_auth_wallet(
self,
auth_user_id,
public_address,
encrypted_private_address,
wallet_management_type,
network: t.Optional[str] = None,
wallet_type: t.Optional[WalletType] = None,
):
session = self.session_factory()
with session.begin_nested():
if existing_wallet := self.logic.AuthWallet.get_by_auth_user_id(
session,
auth_user_id,
):
raise RuntimeError("WalletExistsForNetworkAndWalletType")
return self.logic.AuthWallet.add(
session,
public_address,
encrypted_private_address,
wallet_type,
network,
management_type=wallet_management_type,
auth_user_id=auth_user_id,
)