-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathaccounts.py
More file actions
615 lines (550 loc) · 21.2 KB
/
accounts.py
File metadata and controls
615 lines (550 loc) · 21.2 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
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
from datetime import datetime, timezone
from typing import cast
from fastapi import APIRouter, Depends, status, HTTPException, BackgroundTasks
from sqlalchemy import select, delete
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload
from config import get_jwt_auth_manager, get_settings, BaseAppSettings, get_accounts_email_notificator
from database import (
get_db,
UserModel,
UserGroupModel,
UserGroupEnum,
ActivationTokenModel,
PasswordResetTokenModel,
RefreshTokenModel
)
from exceptions import BaseSecurityError
from notifications import EmailSenderInterface, EmailSender
from schemas import (
UserRegistrationRequestSchema,
UserRegistrationResponseSchema,
MessageResponseSchema,
UserActivationRequestSchema,
PasswordResetRequestSchema,
PasswordResetCompleteRequestSchema,
UserLoginResponseSchema,
UserLoginRequestSchema,
TokenRefreshRequestSchema,
TokenRefreshResponseSchema
)
from security.interfaces import JWTAuthManagerInterface
router = APIRouter()
@router.post(
"/register/",
response_model=UserRegistrationResponseSchema,
summary="User Registration",
description="Register a new user with an email and password.",
status_code=status.HTTP_201_CREATED,
responses={
409: {
"description": "Conflict - User with this email already exists.",
"content": {
"application/json": {
"example": {
"detail": "A user with this email test@example.com already exists."
}
}
},
},
500: {
"description": "Internal Server Error - An error occurred during user creation.",
"content": {
"application/json": {
"example": {
"detail": "An error occurred during user creation."
}
}
},
},
}
)
async def register_user(
user_data: UserRegistrationRequestSchema,
background_tasks: BackgroundTasks,
email_sender: EmailSenderInterface = Depends(get_accounts_email_notificator),
db: AsyncSession = Depends(get_db),
) -> UserRegistrationResponseSchema:
"""
Endpoint for user registration.
Registers a new user, hashes their password, and assigns them to the default user group.
If a user with the same email already exists, an HTTP 409 error is raised.
In case of any unexpected issues during the creation process, an HTTP 500 error is returned.
Args:
user_data (UserRegistrationRequestSchema): The registration details including email and password.
db (AsyncSession): The asynchronous database session.
Returns:
UserRegistrationResponseSchema: The newly created user's details.
Raises:
HTTPException:
- 409 Conflict if a user with the same email exists.
- 500 Internal Server Error if an error occurs during user creation.
"""
stmt = select(UserModel).where(UserModel.email == user_data.email)
result = await db.execute(stmt)
existing_user = result.scalars().first()
if existing_user:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"A user with this email {user_data.email} already exists."
)
stmt = select(UserGroupModel).where(UserGroupModel.name == UserGroupEnum.USER)
result = await db.execute(stmt)
user_group = result.scalars().first()
if not user_group:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Default user group not found."
)
try:
new_user = UserModel.create(
email=str(user_data.email),
raw_password=user_data.password,
group_id=user_group.id,
)
db.add(new_user)
await db.flush()
activation_token = ActivationTokenModel(user_id=new_user.id)
db.add(activation_token)
await db.commit()
await db.refresh(new_user)
activation_link = f"http://127.0.0.1/accounts/activate/?token={activation_token.token}"
background_tasks.add_task(
email_sender.send_activation_email,
str(new_user.email),
activation_link
)
except SQLAlchemyError as e:
await db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred during user creation."
) from e
else:
return UserRegistrationResponseSchema.model_validate(new_user)
@router.post(
"/activate/",
response_model=MessageResponseSchema,
summary="Activate User Account",
description="Activate a user's account using their email and activation token.",
status_code=status.HTTP_200_OK,
responses={
400: {
"description": "Bad Request - The activation token is invalid or expired, "
"or the user account is already active.",
"content": {
"application/json": {
"examples": {
"invalid_token": {
"summary": "Invalid Token",
"value": {
"detail": "Invalid or expired activation token."
}
},
"already_active": {
"summary": "Account Already Active",
"value": {
"detail": "User account is already active."
}
},
}
}
},
},
},
)
async def activate_account(
activation_data: UserActivationRequestSchema,
background_tasks: BackgroundTasks,
db: AsyncSession = Depends(get_db),
email_sender: EmailSenderInterface = Depends(get_accounts_email_notificator),
) -> MessageResponseSchema:
"""
Endpoint to activate a user's account.
This endpoint verifies the activation token for a user by checking that the token record exists
and that it has not expired. If the token is valid and the user's account is not already active,
the user's account is activated and the activation token is deleted. If the token is invalid, expired,
or if the account is already active, an HTTP 400 error is raised.
Args:
activation_data (UserActivationRequestSchema): Contains the user's email and activation token.
db (AsyncSession): The asynchronous database session.
Returns:
MessageResponseSchema: A response message confirming successful activation.
Raises:
HTTPException:
- 400 Bad Request if the activation token is invalid or expired.
- 400 Bad Request if the user account is already active.
"""
stmt = (
select(ActivationTokenModel)
.options(joinedload(ActivationTokenModel.user))
.join(UserModel)
.where(
UserModel.email == activation_data.email,
ActivationTokenModel.token == activation_data.token
)
)
result = await db.execute(stmt)
token_record = result.scalars().first()
now_utc = datetime.now(timezone.utc)
if not token_record or cast(datetime, token_record.expires_at).replace(tzinfo=timezone.utc) < now_utc:
if token_record:
await db.delete(token_record)
await db.commit()
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid or expired activation token."
)
user = token_record.user
if user.is_active:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="User account is already active."
)
user.is_active = True
await db.delete(token_record)
await db.commit()
login_link = "http://127.0.0.1/accounts/login/"
background_tasks.add_task(
email_sender.send_activation_complete_email,
str(user.email),
login_link
)
return MessageResponseSchema(message="User account activated successfully.")
@router.post(
"/password-reset/request/",
response_model=MessageResponseSchema,
summary="Request Password Reset Token",
description=(
"Allows a user to request a password reset token. If the user exists and is active, "
"a new token will be generated and any existing tokens will be invalidated."
),
status_code=status.HTTP_200_OK,
)
async def request_password_reset_token(
data: PasswordResetRequestSchema,
background_tasks: BackgroundTasks,
db: AsyncSession = Depends(get_db),
email_sender: EmailSenderInterface = Depends(get_accounts_email_notificator),
) -> MessageResponseSchema:
"""
Endpoint to request a password reset token.
If the user exists and is active, invalidates any existing password reset tokens and generates a new one.
Always responds with a success message to avoid leaking user information.
Args:
data (PasswordResetRequestSchema): The request data containing the user's email.
db (AsyncSession): The asynchronous database session.
Returns:
MessageResponseSchema: A success message indicating that instructions will be sent.
"""
stmt = select(UserModel).filter_by(email=data.email)
result = await db.execute(stmt)
user = result.scalars().first()
if not user or not user.is_active:
return MessageResponseSchema(
message="If you are registered, you will receive an email with instructions."
)
await db.execute(delete(PasswordResetTokenModel).where(PasswordResetTokenModel.user_id == user.id))
reset_token = PasswordResetTokenModel(user_id=cast(int, user.id))
db.add(reset_token)
await db.commit()
reset_link = f"http://127.0.0.1/accounts/password-reset/?token={reset_token.token}"
background_tasks.add_task(
email_sender.send_password_reset_email,
str(user.email),
reset_link
)
return MessageResponseSchema(
message="If you are registered, you will receive an email with instructions."
)
@router.post(
"/reset-password/complete/",
response_model=MessageResponseSchema,
summary="Reset User Password",
description="Reset a user's password if a valid token is provided.",
status_code=status.HTTP_200_OK,
responses={
400: {
"description": (
"Bad Request - The provided email or token is invalid, "
"the token has expired, or the user account is not active."
),
"content": {
"application/json": {
"examples": {
"invalid_email_or_token": {
"summary": "Invalid Email or Token",
"value": {
"detail": "Invalid email or token."
}
},
"expired_token": {
"summary": "Expired Token",
"value": {
"detail": "Invalid email or token."
}
}
}
}
},
},
500: {
"description": "Internal Server Error - An error occurred while resetting the password.",
"content": {
"application/json": {
"example": {
"detail": "An error occurred while resetting the password."
}
}
},
},
},
)
async def reset_password(
data: PasswordResetCompleteRequestSchema,
background_tasks: BackgroundTasks,
db: AsyncSession = Depends(get_db),
email_sender: EmailSenderInterface = Depends(get_accounts_email_notificator),
) -> MessageResponseSchema:
"""
Endpoint for resetting a user's password.
Validates the token and updates the user's password if the token is valid and not expired.
Deletes the token after a successful password reset.
Args:
data (PasswordResetCompleteRequestSchema): The request data containing the user's email,
token, and new password.
db (AsyncSession): The asynchronous database session.
Returns:
MessageResponseSchema: A response message indicating successful password reset.
Raises:
HTTPException:
- 400 Bad Request if the email or token is invalid, or the token has expired.
- 500 Internal Server Error if an error occurs during the password reset process.
"""
stmt = select(UserModel).filter_by(email=data.email)
result = await db.execute(stmt)
user = result.scalars().first()
if not user or not user.is_active:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid email or token."
)
stmt = select(PasswordResetTokenModel).filter_by(user_id=user.id)
result = await db.execute(stmt)
token_record = result.scalars().first()
if not token_record or token_record.token != data.token:
if token_record:
await db.run_sync(lambda s: s.delete(token_record))
await db.commit()
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid email or token."
)
expires_at = cast(datetime, token_record.expires_at).replace(tzinfo=timezone.utc)
if expires_at < datetime.now(timezone.utc):
await db.run_sync(lambda s: s.delete(token_record))
await db.commit()
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid email or token."
)
try:
user.password = data.password
await db.run_sync(lambda s: s.delete(token_record))
await db.commit()
except SQLAlchemyError:
await db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while resetting the password."
)
login_link = "http://127.0.0.1/accounts/login/"
background_tasks.add_task(
email_sender.send_password_reset_complete_email,
str(user.email),
login_link
)
return MessageResponseSchema(message="Password reset successfully.")
@router.post(
"/login/",
response_model=UserLoginResponseSchema,
summary="User Login",
description="Authenticate a user and return access and refresh tokens.",
status_code=status.HTTP_201_CREATED,
responses={
401: {
"description": "Unauthorized - Invalid email or password.",
"content": {
"application/json": {
"example": {
"detail": "Invalid email or password."
}
}
},
},
403: {
"description": "Forbidden - User account is not activated.",
"content": {
"application/json": {
"example": {
"detail": "User account is not activated."
}
}
},
},
500: {
"description": "Internal Server Error - An error occurred while processing the request.",
"content": {
"application/json": {
"example": {
"detail": "An error occurred while processing the request."
}
}
},
},
},
)
async def login_user(
login_data: UserLoginRequestSchema,
db: AsyncSession = Depends(get_db),
settings: BaseAppSettings = Depends(get_settings),
jwt_manager: JWTAuthManagerInterface = Depends(get_jwt_auth_manager),
) -> UserLoginResponseSchema:
"""
Endpoint for user login.
Authenticates a user using their email and password.
If authentication is successful, creates a new refresh token and returns both access and refresh tokens.
Args:
login_data (UserLoginRequestSchema): The login credentials.
db (AsyncSession): The asynchronous database session.
settings (BaseAppSettings): The application settings.
jwt_manager (JWTAuthManagerInterface): The JWT authentication manager.
Returns:
UserLoginResponseSchema: A response containing the access and refresh tokens.
Raises:
HTTPException:
- 401 Unauthorized if the email or password is invalid.
- 403 Forbidden if the user account is not activated.
- 500 Internal Server Error if an error occurs during token creation.
"""
stmt = select(UserModel).filter_by(email=login_data.email)
result = await db.execute(stmt)
user = result.scalars().first()
if not user or not user.verify_password(login_data.password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid email or password.",
)
if not user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User account is not activated.",
)
jwt_refresh_token = jwt_manager.create_refresh_token({"user_id": user.id})
try:
refresh_token = RefreshTokenModel.create(
user_id=user.id,
days_valid=settings.LOGIN_TIME_DAYS,
token=jwt_refresh_token
)
db.add(refresh_token)
await db.flush()
await db.commit()
except SQLAlchemyError:
await db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while processing the request.",
)
jwt_access_token = jwt_manager.create_access_token({"user_id": user.id})
return UserLoginResponseSchema(
access_token=jwt_access_token,
refresh_token=jwt_refresh_token,
)
@router.post(
"/refresh/",
response_model=TokenRefreshResponseSchema,
summary="Refresh Access Token",
description="Refresh the access token using a valid refresh token.",
status_code=status.HTTP_200_OK,
responses={
400: {
"description": "Bad Request - The provided refresh token is invalid or expired.",
"content": {
"application/json": {
"example": {
"detail": "Token has expired."
}
}
},
},
401: {
"description": "Unauthorized - Refresh token not found.",
"content": {
"application/json": {
"example": {
"detail": "Refresh token not found."
}
}
},
},
404: {
"description": "Not Found - The user associated with the token does not exist.",
"content": {
"application/json": {
"example": {
"detail": "User not found."
}
}
},
},
},
)
async def refresh_access_token(
token_data: TokenRefreshRequestSchema,
db: AsyncSession = Depends(get_db),
jwt_manager: JWTAuthManagerInterface = Depends(get_jwt_auth_manager),
) -> TokenRefreshResponseSchema:
"""
Endpoint to refresh an access token.
Validates the provided refresh token, extracts the user ID from it, and issues
a new access token. If the token is invalid or expired, an error is returned.
Args:
token_data (TokenRefreshRequestSchema): Contains the refresh token.
db (AsyncSession): The asynchronous database session.
jwt_manager (JWTAuthManagerInterface): JWT authentication manager.
Returns:
TokenRefreshResponseSchema: A new access token.
Raises:
HTTPException:
- 400 Bad Request if the token is invalid or expired.
- 401 Unauthorized if the refresh token is not found.
- 404 Not Found if the user associated with the token does not exist.
"""
try:
decoded_token = jwt_manager.decode_refresh_token(token_data.refresh_token)
user_id = decoded_token.get("user_id")
except BaseSecurityError as error:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(error),
)
stmt = select(RefreshTokenModel).filter_by(token=token_data.refresh_token)
result = await db.execute(stmt)
refresh_token_record = result.scalars().first()
if not refresh_token_record:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Refresh token not found.",
)
stmt = select(UserModel).filter_by(id=user_id)
result = await db.execute(stmt)
user = result.scalars().first()
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found.",
)
new_access_token = jwt_manager.create_access_token({"user_id": user_id})
return TokenRefreshResponseSchema(access_token=new_access_token)