Spaces:
Sleeping
Sleeping
File size: 5,766 Bytes
2dc6f03 | 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 | """
Input validation utilities for mobile app backend.
"""
import re
from typing import Tuple, Optional
from config import Config
class Validators:
"""Input validation utilities."""
EMAIL_PATTERN = re.compile(
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
)
PASSWORD_MIN_LENGTH = 8
PASSWORD_MAX_LENGTH = 128
@staticmethod
def validate_email(email: str) -> Tuple[bool, Optional[str]]:
"""Validate email format."""
if not email:
return False, "Email is required"
email = email.strip().lower()
if len(email) > 254:
return False, "Email is too long"
if not Validators.EMAIL_PATTERN.match(email):
return False, "Invalid email format"
return True, None
@staticmethod
def validate_password(password: str) -> Tuple[bool, Optional[str]]:
"""
Validate password strength.
Requirements: 8+ chars, 1 uppercase, 1 lowercase, 1 number.
"""
if not password:
return False, "Password is required"
if len(password) < Validators.PASSWORD_MIN_LENGTH:
return False, f"Password must be at least {Validators.PASSWORD_MIN_LENGTH} characters"
if len(password) > Validators.PASSWORD_MAX_LENGTH:
return False, "Password is too long"
if not re.search(r'[A-Z]', password):
return False, "Password must contain at least one uppercase letter"
if not re.search(r'[a-z]', password):
return False, "Password must contain at least one lowercase letter"
if not re.search(r'\d', password):
return False, "Password must contain at least one number"
return True, None
@staticmethod
def validate_name(name: str) -> Tuple[bool, Optional[str]]:
"""Validate user name."""
if not name:
return False, "Name is required"
name = name.strip()
if len(name) < 2:
return False, "Name must be at least 2 characters"
if len(name) > 100:
return False, "Name is too long"
if not re.match(r"^[a-zA-Z\s\-']+$", name):
return False, "Name contains invalid characters"
return True, None
@staticmethod
def validate_otp(otp: str) -> Tuple[bool, Optional[str]]:
"""Validate OTP format."""
if not otp:
return False, "Verification code is required"
otp = otp.strip()
if len(otp) != Config.OTP_LENGTH:
return False, f"Verification code must be {Config.OTP_LENGTH} digits"
if not otp.isdigit():
return False, "Verification code must contain only numbers"
return True, None
@staticmethod
def validate_document_type(doc_type: str) -> Tuple[bool, Optional[str]]:
"""Validate document type."""
valid_types = ['insurance', 'academic', 'id', 'financial', 'medical', 'general']
if not doc_type:
return False, "Document type is required"
if doc_type.lower() not in valid_types:
return False, f"Invalid document type. Must be one of: {', '.join(valid_types)}"
return True, None
@staticmethod
def validate_file(file, max_size: int = None) -> Tuple[bool, Optional[str]]:
"""Validate uploaded file."""
if not file or not file.filename:
return False, "File is required"
filename = file.filename.lower()
allowed = Config.ALLOWED_EXTENSIONS
if not any(filename.endswith(f'.{ext}') for ext in allowed):
return False, f"File type not allowed. Allowed types: {', '.join(allowed)}"
if max_size:
try:
file.seek(0, 2)
size = file.tell()
file.seek(0)
if size > max_size:
max_mb = max_size / (1024 * 1024)
return False, f"File size exceeds {max_mb:.1f}MB limit"
except Exception:
pass
return True, None
@staticmethod
def validate_device_id(device_id: str) -> Tuple[bool, Optional[str]]:
"""Validate device identifier."""
if not device_id:
return False, "Device ID is required"
device_id = device_id.strip()
if len(device_id) < 8:
return False, "Device ID is too short"
if len(device_id) > 256:
return False, "Device ID is too long"
return True, None
@staticmethod
def validate_fcm_token(token: str) -> Tuple[bool, Optional[str]]:
"""Validate Firebase Cloud Messaging token."""
if not token:
return False, "FCM token is required"
token = token.strip()
if len(token) < 32:
return False, "Invalid FCM token format"
if len(token) > 512:
return False, "FCM token is too long"
return True, None
@staticmethod
def sanitize_string(value: str, max_length: int = 255) -> str:
"""Sanitize a string input."""
if not value:
return ""
value = value.strip()
if len(value) > max_length:
value = value[:max_length]
return value
|