-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
manager.py
705 lines (581 loc) · 24.2 KB
/
manager.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import uuid
from typing import Any, Dict, Generic, Optional, Union
import jwt
from fastapi import Request, Response
from fastapi.security import OAuth2PasswordRequestForm
from fastapi_users import exceptions, models, schemas
from fastapi_users.db import BaseUserDatabase
from fastapi_users.jwt import SecretType, decode_jwt, generate_jwt
from fastapi_users.password import PasswordHelper, PasswordHelperProtocol
from fastapi_users.types import DependencyCallable
RESET_PASSWORD_TOKEN_AUDIENCE = "fastapi-users:reset"
VERIFY_USER_TOKEN_AUDIENCE = "fastapi-users:verify"
class BaseUserManager(Generic[models.UP, models.ID]):
"""
User management logic.
:attribute reset_password_token_secret: Secret to encode reset password token.
:attribute reset_password_token_lifetime_seconds: Lifetime of reset password token.
:attribute reset_password_token_audience: JWT audience of reset password token.
:attribute verification_token_secret: Secret to encode verification token.
:attribute verification_token_lifetime_seconds: Lifetime of verification token.
:attribute verification_token_audience: JWT audience of verification token.
:param user_db: Database adapter instance.
"""
reset_password_token_secret: SecretType
reset_password_token_lifetime_seconds: int = 3600
reset_password_token_audience: str = RESET_PASSWORD_TOKEN_AUDIENCE
verification_token_secret: SecretType
verification_token_lifetime_seconds: int = 3600
verification_token_audience: str = VERIFY_USER_TOKEN_AUDIENCE
user_db: BaseUserDatabase[models.UP, models.ID]
password_helper: PasswordHelperProtocol
def __init__(
self,
user_db: BaseUserDatabase[models.UP, models.ID],
password_helper: Optional[PasswordHelperProtocol] = None,
):
self.user_db = user_db
if password_helper is None:
self.password_helper = PasswordHelper()
else:
self.password_helper = password_helper # pragma: no cover
def parse_id(self, value: Any) -> models.ID:
"""
Parse a value into a correct models.ID instance.
:param value: The value to parse.
:raises InvalidID: The models.ID value is invalid.
:return: An models.ID object.
"""
raise NotImplementedError() # pragma: no cover
async def get(self, id: models.ID) -> models.UP:
"""
Get a user by id.
:param id: Id. of the user to retrieve.
:raises UserNotExists: The user does not exist.
:return: A user.
"""
user = await self.user_db.get(id)
if user is None:
raise exceptions.UserNotExists()
return user
async def get_by_email(self, user_email: str) -> models.UP:
"""
Get a user by e-mail.
:param user_email: E-mail of the user to retrieve.
:raises UserNotExists: The user does not exist.
:return: A user.
"""
user = await self.user_db.get_by_email(user_email)
if user is None:
raise exceptions.UserNotExists()
return user
async def get_by_oauth_account(self, oauth: str, account_id: str) -> models.UP:
"""
Get a user by OAuth account.
:param oauth: Name of the OAuth client.
:param account_id: Id. of the account on the external OAuth service.
:raises UserNotExists: The user does not exist.
:return: A user.
"""
user = await self.user_db.get_by_oauth_account(oauth, account_id)
if user is None:
raise exceptions.UserNotExists()
return user
async def create(
self,
user_create: schemas.UC,
safe: bool = False,
request: Optional[Request] = None,
) -> models.UP:
"""
Create a user in database.
Triggers the on_after_register handler on success.
:param user_create: The UserCreate model to create.
:param safe: If True, sensitive values like is_superuser or is_verified
will be ignored during the creation, defaults to False.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises UserAlreadyExists: A user already exists with the same e-mail.
:return: A new user.
"""
await self.validate_password(user_create.password, user_create)
existing_user = await self.user_db.get_by_email(user_create.email)
if existing_user is not None:
raise exceptions.UserAlreadyExists()
user_dict = (
user_create.create_update_dict()
if safe
else user_create.create_update_dict_superuser()
)
password = user_dict.pop("password")
user_dict["hashed_password"] = self.password_helper.hash(password)
created_user = await self.user_db.create(user_dict)
await self.on_after_register(created_user, request)
return created_user
async def oauth_callback(
self: "BaseUserManager[models.UOAP, models.ID]",
oauth_name: str,
access_token: str,
account_id: str,
account_email: str,
expires_at: Optional[int] = None,
refresh_token: Optional[str] = None,
request: Optional[Request] = None,
*,
associate_by_email: bool = False,
is_verified_by_default: bool = False,
) -> models.UOAP:
"""
Handle the callback after a successful OAuth authentication.
If the user already exists with this OAuth account, the token is updated.
If a user with the same e-mail already exists and `associate_by_email` is True,
the OAuth account is associated to this user.
Otherwise, the `UserNotExists` exception is raised.
If the user does not exist, it is created and the on_after_register handler
is triggered.
:param oauth_name: Name of the OAuth client.
:param access_token: Valid access token for the service provider.
:param account_id: models.ID of the user on the service provider.
:param account_email: E-mail of the user on the service provider.
:param expires_at: Optional timestamp at which the access token expires.
:param refresh_token: Optional refresh token to get a
fresh access token from the service provider.
:param request: Optional FastAPI request that
triggered the operation, defaults to None
:param associate_by_email: If True, any existing user with the same
e-mail address will be associated to this user. Defaults to False.
:param is_verified_by_default: If True, the `is_verified` flag will be
set to `True` on newly created user. Make sure the OAuth Provider you're
using does verify the email address before enabling this flag.
Defaults to False.
:return: A user.
"""
oauth_account_dict = {
"oauth_name": oauth_name,
"access_token": access_token,
"account_id": account_id,
"account_email": account_email,
"expires_at": expires_at,
"refresh_token": refresh_token,
}
try:
user = await self.get_by_oauth_account(oauth_name, account_id)
except exceptions.UserNotExists:
try:
# Associate account
user = await self.get_by_email(account_email)
if not associate_by_email:
raise exceptions.UserAlreadyExists()
user = await self.user_db.add_oauth_account(user, oauth_account_dict)
except exceptions.UserNotExists:
# Create account
password = self.password_helper.generate()
user_dict = {
"email": account_email,
"hashed_password": self.password_helper.hash(password),
"is_verified": is_verified_by_default,
}
user = await self.user_db.create(user_dict)
user = await self.user_db.add_oauth_account(user, oauth_account_dict)
await self.on_after_register(user, request)
else:
# Update oauth
for existing_oauth_account in user.oauth_accounts:
if (
existing_oauth_account.account_id == account_id
and existing_oauth_account.oauth_name == oauth_name
):
user = await self.user_db.update_oauth_account(
user, existing_oauth_account, oauth_account_dict
)
return user
async def oauth_associate_callback(
self: "BaseUserManager[models.UOAP, models.ID]",
user: models.UOAP,
oauth_name: str,
access_token: str,
account_id: str,
account_email: str,
expires_at: Optional[int] = None,
refresh_token: Optional[str] = None,
request: Optional[Request] = None,
) -> models.UOAP:
"""
Handle the callback after a successful OAuth association.
We add this new OAuth account to the given user.
:param oauth_name: Name of the OAuth client.
:param access_token: Valid access token for the service provider.
:param account_id: models.ID of the user on the service provider.
:param account_email: E-mail of the user on the service provider.
:param expires_at: Optional timestamp at which the access token expires.
:param refresh_token: Optional refresh token to get a
fresh access token from the service provider.
:param request: Optional FastAPI request that
triggered the operation, defaults to None
:return: A user.
"""
oauth_account_dict = {
"oauth_name": oauth_name,
"access_token": access_token,
"account_id": account_id,
"account_email": account_email,
"expires_at": expires_at,
"refresh_token": refresh_token,
}
user = await self.user_db.add_oauth_account(user, oauth_account_dict)
await self.on_after_update(user, {}, request)
return user
async def request_verify(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Start a verification request.
Triggers the on_after_request_verify handler on success.
:param user: The user to verify.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises UserInactive: The user is inactive.
:raises UserAlreadyVerified: The user is already verified.
"""
if not user.is_active:
raise exceptions.UserInactive()
if user.is_verified:
raise exceptions.UserAlreadyVerified()
token_data = {
"sub": str(user.id),
"email": user.email,
"aud": self.verification_token_audience,
}
token = generate_jwt(
token_data,
self.verification_token_secret,
self.verification_token_lifetime_seconds,
)
await self.on_after_request_verify(user, token, request)
async def verify(self, token: str, request: Optional[Request] = None) -> models.UP:
"""
Validate a verification request.
Changes the is_verified flag of the user to True.
Triggers the on_after_verify handler on success.
:param token: The verification token generated by request_verify.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises InvalidVerifyToken: The token is invalid or expired.
:raises UserAlreadyVerified: The user is already verified.
:return: The verified user.
"""
try:
data = decode_jwt(
token,
self.verification_token_secret,
[self.verification_token_audience],
)
except jwt.PyJWTError:
raise exceptions.InvalidVerifyToken()
try:
user_id = data["sub"]
email = data["email"]
except KeyError:
raise exceptions.InvalidVerifyToken()
try:
user = await self.get_by_email(email)
except exceptions.UserNotExists:
raise exceptions.InvalidVerifyToken()
try:
parsed_id = self.parse_id(user_id)
except exceptions.InvalidID:
raise exceptions.InvalidVerifyToken()
if parsed_id != user.id:
raise exceptions.InvalidVerifyToken()
if user.is_verified:
raise exceptions.UserAlreadyVerified()
verified_user = await self._update(user, {"is_verified": True})
await self.on_after_verify(verified_user, request)
return verified_user
async def forgot_password(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Start a forgot password request.
Triggers the on_after_forgot_password handler on success.
:param user: The user that forgot its password.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises UserInactive: The user is inactive.
"""
if not user.is_active:
raise exceptions.UserInactive()
token_data = {
"sub": str(user.id),
"password_fgpt": self.password_helper.hash(user.hashed_password),
"aud": self.reset_password_token_audience,
}
token = generate_jwt(
token_data,
self.reset_password_token_secret,
self.reset_password_token_lifetime_seconds,
)
await self.on_after_forgot_password(user, token, request)
async def reset_password(
self, token: str, password: str, request: Optional[Request] = None
) -> models.UP:
"""
Reset the password of a user.
Triggers the on_after_reset_password handler on success.
:param token: The token generated by forgot_password.
:param password: The new password to set.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises InvalidResetPasswordToken: The token is invalid or expired.
:raises UserInactive: The user is inactive.
:raises InvalidPasswordException: The password is invalid.
:return: The user with updated password.
"""
try:
data = decode_jwt(
token,
self.reset_password_token_secret,
[self.reset_password_token_audience],
)
except jwt.PyJWTError:
raise exceptions.InvalidResetPasswordToken()
try:
user_id = data["sub"]
password_fingerprint = data["password_fgpt"]
except KeyError:
raise exceptions.InvalidResetPasswordToken()
try:
parsed_id = self.parse_id(user_id)
except exceptions.InvalidID:
raise exceptions.InvalidResetPasswordToken()
user = await self.get(parsed_id)
valid_password_fingerprint, _ = self.password_helper.verify_and_update(
user.hashed_password, password_fingerprint
)
if not valid_password_fingerprint:
raise exceptions.InvalidResetPasswordToken()
if not user.is_active:
raise exceptions.UserInactive()
updated_user = await self._update(user, {"password": password})
await self.on_after_reset_password(user, request)
return updated_user
async def update(
self,
user_update: schemas.UU,
user: models.UP,
safe: bool = False,
request: Optional[Request] = None,
) -> models.UP:
"""
Update a user.
Triggers the on_after_update handler on success
:param user_update: The UserUpdate model containing
the changes to apply to the user.
:param user: The current user to update.
:param safe: If True, sensitive values like is_superuser or is_verified
will be ignored during the update, defaults to False
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:return: The updated user.
"""
if safe:
updated_user_data = user_update.create_update_dict()
else:
updated_user_data = user_update.create_update_dict_superuser()
updated_user = await self._update(user, updated_user_data)
await self.on_after_update(updated_user, updated_user_data, request)
return updated_user
async def delete(
self,
user: models.UP,
request: Optional[Request] = None,
) -> None:
"""
Delete a user.
:param user: The user to delete.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
await self.on_before_delete(user, request)
await self.user_db.delete(user)
await self.on_after_delete(user, request)
async def validate_password(
self, password: str, user: Union[schemas.UC, models.UP]
) -> None:
"""
Validate a password.
*You should overload this method to add your own validation logic.*
:param password: The password to validate.
:param user: The user associated to this password.
:raises InvalidPasswordException: The password is invalid.
:return: None if the password is valid.
"""
return # pragma: no cover
async def on_after_register(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Perform logic after successful user registration.
*You should overload this method to add your own logic.*
:param user: The registered user
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_update(
self,
user: models.UP,
update_dict: Dict[str, Any],
request: Optional[Request] = None,
) -> None:
"""
Perform logic after successful user update.
*You should overload this method to add your own logic.*
:param user: The updated user
:param update_dict: Dictionary with the updated user fields.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_request_verify(
self, user: models.UP, token: str, request: Optional[Request] = None
) -> None:
"""
Perform logic after successful verification request.
*You should overload this method to add your own logic.*
:param user: The user to verify.
:param token: The verification token.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_verify(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Perform logic after successful user verification.
*You should overload this method to add your own logic.*
:param user: The verified user.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_forgot_password(
self, user: models.UP, token: str, request: Optional[Request] = None
) -> None:
"""
Perform logic after successful forgot password request.
*You should overload this method to add your own logic.*
:param user: The user that forgot its password.
:param token: The forgot password token.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_reset_password(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Perform logic after successful password reset.
*You should overload this method to add your own logic.*
:param user: The user that reset its password.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_login(
self,
user: models.UP,
request: Optional[Request] = None,
response: Optional[Response] = None,
) -> None:
"""
Perform logic after user login.
*You should overload this method to add your own logic.*
:param user: The user that is logging in
:param request: Optional FastAPI request
:param response: Optional response built by the transport.
Defaults to None
"""
return # pragma: no cover
async def on_before_delete(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Perform logic before user delete.
*You should overload this method to add your own logic.*
:param user: The user to be deleted
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def on_after_delete(
self, user: models.UP, request: Optional[Request] = None
) -> None:
"""
Perform logic before user delete.
*You should overload this method to add your own logic.*
:param user: The user to be deleted
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
"""
return # pragma: no cover
async def authenticate(
self, credentials: OAuth2PasswordRequestForm
) -> Optional[models.UP]:
"""
Authenticate and return a user following an email and a password.
Will automatically upgrade password hash if necessary.
:param credentials: The user credentials.
"""
try:
user = await self.get_by_email(credentials.username)
except exceptions.UserNotExists:
# Run the hasher to mitigate timing attack
# Inspired from Django: https://code.djangoproject.com/ticket/20760
self.password_helper.hash(credentials.password)
return None
verified, updated_password_hash = self.password_helper.verify_and_update(
credentials.password, user.hashed_password
)
if not verified:
return None
# Update password hash to a more robust one if needed
if updated_password_hash is not None:
await self.user_db.update(user, {"hashed_password": updated_password_hash})
return user
async def _update(self, user: models.UP, update_dict: Dict[str, Any]) -> models.UP:
validated_update_dict = {}
for field, value in update_dict.items():
if field == "email" and value != user.email:
try:
await self.get_by_email(value)
raise exceptions.UserAlreadyExists()
except exceptions.UserNotExists:
validated_update_dict["email"] = value
validated_update_dict["is_verified"] = False
elif field == "password" and value is not None:
await self.validate_password(value, user)
validated_update_dict["hashed_password"] = self.password_helper.hash(
value
)
else:
validated_update_dict[field] = value
return await self.user_db.update(user, validated_update_dict)
class UUIDIDMixin:
def parse_id(self, value: Any) -> uuid.UUID:
if isinstance(value, uuid.UUID):
return value
try:
return uuid.UUID(value)
except ValueError as e:
raise exceptions.InvalidID() from e
class IntegerIDMixin:
def parse_id(self, value: Any) -> int:
if isinstance(value, float):
raise exceptions.InvalidID()
try:
return int(value)
except ValueError as e:
raise exceptions.InvalidID() from e
UserManagerDependency = DependencyCallable[BaseUserManager[models.UP, models.ID]]