Andrei Neagoie Python -
def test_token_validation(self, auth_service): auth_service.register_user("test@example.com", "ValidPass123!") token, _ = auth_service.login("test@example.com", "ValidPass123!", "10.0.0.1") user = auth_service.verify_token(token) assert user.email == "test@example.com"
def test_rate_limiting(self, auth_service): auth_service.register_user("test@example.com", "ValidPass123!") ip = "192.168.1.100" # Try wrong password 5 times for _ in range(5): with pytest.raises(InvalidPasswordError): auth_service.login("test@example.com", "wrong", ip) # 6th attempt should trigger rate limit with pytest.raises(RateLimitExceededError): auth_service.login("test@example.com", "wrong", ip) andrei neagoie python
import pytest from datetime import datetime, timedelta def test_token_validation(self, auth_service): auth_service
@dataclass class User: """User entity representing authenticated users""" user_id: str email: str password_hash: str created_at: datetime last_login: Optional[datetime] = None is_active: bool = True failed_attempts: int = 0 locked_until: Optional[datetime] = None _ = auth_service.login("test@example.com"
@staticmethod def verify_password(password: str, stored_hash: str) -> bool: """ Verify password against stored hash Args: password: Plain text password to verify stored_hash: Stored hash string (salt:hash) Returns: True if password matches, False otherwise """ try: salt_hex, hash_hex = stored_hash.split(':') salt = bytes.fromhex(salt_hex) # Hash the provided password with the same salt test_hash = hashlib.pbkdf2_hmac( 'sha256', password.encode('utf-8'), salt, 100000 ) # Constant-time comparison to prevent timing attacks return test_hash.hex() == hash_hex except (ValueError, TypeError): return False
class RateLimitExceededError(AuthenticationError): """Raised when too many attempts""" pass