60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
"""
|
|
Password validation utilities for enforcing password strength requirements.
|
|
"""
|
|
import re
|
|
from typing import Tuple, List
|
|
|
|
# Password strength requirements
|
|
MIN_PASSWORD_LENGTH = 8
|
|
REQUIRE_UPPERCASE = True
|
|
REQUIRE_LOWERCASE = True
|
|
REQUIRE_NUMBER = True
|
|
REQUIRE_SPECIAL = True
|
|
|
|
def validate_password_strength(password: str) -> Tuple[bool, List[str]]:
|
|
"""
|
|
Validate password meets strength requirements.
|
|
|
|
Args:
|
|
password: The password to validate
|
|
|
|
Returns:
|
|
Tuple of (is_valid, list_of_errors)
|
|
"""
|
|
errors = []
|
|
|
|
if not password:
|
|
return False, ['Password is required']
|
|
|
|
# Check minimum length
|
|
if len(password) < MIN_PASSWORD_LENGTH:
|
|
errors.append(f'Password must be at least {MIN_PASSWORD_LENGTH} characters long')
|
|
|
|
# Check for uppercase letter
|
|
if REQUIRE_UPPERCASE and not re.search(r'[A-Z]', password):
|
|
errors.append('Password must contain at least one uppercase letter')
|
|
|
|
# Check for lowercase letter
|
|
if REQUIRE_LOWERCASE and not re.search(r'[a-z]', password):
|
|
errors.append('Password must contain at least one lowercase letter')
|
|
|
|
# Check for number
|
|
if REQUIRE_NUMBER and not re.search(r'\d', password):
|
|
errors.append('Password must contain at least one number')
|
|
|
|
# Check for special character
|
|
if REQUIRE_SPECIAL and not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
|
|
errors.append('Password must contain at least one special character (!@#$%^&*(),.?":{}|<>)')
|
|
|
|
# Check for common weak passwords
|
|
common_passwords = [
|
|
'password', '12345678', 'qwerty', 'abc123', 'password123',
|
|
'admin', 'letmein', 'welcome', 'monkey', '1234567890'
|
|
]
|
|
if password.lower() in common_passwords:
|
|
errors.append('Password is too common. Please choose a stronger password')
|
|
|
|
is_valid = len(errors) == 0
|
|
return is_valid, errors
|
|
|