Skip to content

Commit 187d575

Browse files
Moved password regex to top of file as exportable constant
1 parent 7e315a6 commit 187d575

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

utils/auth.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
ALGORITHM = "HS256"
3535
ACCESS_TOKEN_EXPIRE_MINUTES = 30
3636
REFRESH_TOKEN_EXPIRE_DAYS = 30
37+
PASSWORD_PATTERN = re.compile(
38+
r"(?=.*\d)" # At least one digit
39+
r"(?=.*[a-z])" # At least one lowercase letter
40+
r"(?=.*[A-Z])" # At least one uppercase letter
41+
r"(?=.*[@$!%*?&{}<>.,\\'#\-_=+\(\)\[\]:;|~/])" # At least one special character
42+
r"[A-Za-z\d@$!%*?&{}<>.,\\'#\-_=+\(\)\[\]:;|~/]{8,}" # At least 8 characters long
43+
)
3744

3845

3946
# --- Custom Exceptions ---
@@ -105,11 +112,8 @@ def validate_password_strength(v: str) -> str:
105112
- At least 8 characters long
106113
"""
107114
logger.debug(f"Validating password for {field_name}")
108-
pattern = re.compile(
109-
r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&{}<>.,\\'#\-_=+\(\)\[\]:;|~/])[A-Za-z\d@$!%*?&{}<>.,\\'#\-_=+\(\)\[\]:;|~/]{8,}")
110-
if not pattern.match(v):
111-
logger.debug(f"Password for {
112-
field_name} does not satisfy the security policy")
115+
if not PASSWORD_PATTERN.match(v):
116+
logger.debug(f"Password for {field_name} does not satisfy the security policy")
113117
raise PasswordValidationError(
114118
field=field_name,
115119
message=f"{field_name} does not satisfy the security policy"

0 commit comments

Comments
 (0)