File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 8
8
from utils .db import User
9
9
from utils .auth import (
10
10
get_session ,
11
+ validate_password_strength ,
11
12
get_user_from_reset_token ,
12
13
oauth2_scheme_cookie ,
13
14
get_password_hash ,
@@ -60,6 +61,10 @@ async def register(
60
61
if password != confirm_password :
61
62
raise HTTPException (status_code = 400 , detail = "Passwords do not match" )
62
63
64
+ if not validate_password_strength (password ):
65
+ raise HTTPException (
66
+ status_code = 400 , detail = "Password does not satisfy the security policy" )
67
+
63
68
user = UserCreate (name = name , email = email , password = password )
64
69
db_user = session .exec (select (User ).where (
65
70
User .email == user .email )).first ()
@@ -196,6 +201,10 @@ def reset_password(
196
201
if new_password != confirm_new_password :
197
202
raise HTTPException (status_code = 400 , detail = "Passwords do not match" )
198
203
204
+ if not validate_password_strength (new_password ):
205
+ raise HTTPException (
206
+ status_code = 400 , detail = "Password does not satisfy the security policy" )
207
+
199
208
authorized_user , reset_token = get_user_from_reset_token (
200
209
email , token , session )
201
210
Original file line number Diff line number Diff line change 1
1
# utils.py
2
2
import os
3
+ import re
3
4
import jwt
4
5
import uuid
5
6
import logging
@@ -33,6 +34,19 @@ def oauth2_scheme_cookie(
33
34
return access_token , refresh_token
34
35
35
36
37
+ def validate_password_strength (password : str ) -> bool :
38
+ """
39
+ Validate the password to ensure it meets the required criteria:
40
+ - At least one number
41
+ - At least one uppercase and one lowercase letter
42
+ - At least one special character
43
+ - At least 8 characters long
44
+ """
45
+ pattern = re .compile (
46
+ r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}" )
47
+ return bool (pattern .match (password ))
48
+
49
+
36
50
def get_password_hash (password : str ) -> str :
37
51
return pwd_context .hash (password )
38
52
You can’t perform that action at this time.
0 commit comments