Spaces:
Sleeping
Sleeping
Update src/utils/security.py
Browse files123456789101112131415161718192021222324253 unmodified lines29303132333421 unmodified lines56575859from passlib.context import CryptContextfrom datetime import datetime, timedeltafrom typing import Optionalimport uuidfrom jose import JWTError, jwtfrom ..config import settings# Password hashing contextpwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")BCRYPT_MAX_LENGTH = 72 # bcrypt limitdef hash_password(password: str) -> str:"""Hash a password using bcrypt (truncate to 72 bytes)."""truncated = password[:BCRYPT_MAX_LENGTH]return pwd_context.hash(truncated)def verify_password(plain_password: str, hashed_password: str) -> bool:"""Verify a plain password against its hash (truncate to 72 bytes)."""truncated = plain_password[:BCRYPT_MAX_LENGTH]return pwd_context.verify(truncated, hashed_password)def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:3 unmodified linesif expires_delta:expire = datetime.utcnow() + expires_deltaelse:expire = datetime.utcnow() + timedelta(days=settings.ACCESS_TOKEN_EXPIRE_DAYS)to_encode.update({"exp": expire, "iat": datetime.utcnow()})21 unmodified linesreturn uuid.UUID(user_id_str)except ValueError:return Nonereturn None123456789101112131415161718192021223 unmodified lines2627282930313221 unmodified lines54555657from passlib.context import CryptContextfrom datetime import datetime, timedeltafrom typing import Optional, Unionimport uuidfrom jose import JWTError, jwtfrom ..config import settings# Password hashing contextpwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")def hash_password(password: str) -> str:"""Hash a password using bcrypt."""return pwd_context.hash(password)def verify_password(plain_password: str, hashed_password: str) -> bool:"""Verify a plain password against its hash."""return pwd_context.verify(plain_password, hashed_password)def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:3 unmodified linesif expires_delta:expire = datetime.utcnow() + expires_deltaelse:# Default to 7 days if no expiration is providedexpire = datetime.utcnow() + timedelta(days=settings.ACCESS_TOKEN_EXPIRE_DAYS)to_encode.update({"exp": expire, "iat": datetime.utcnow()})21 unmodified linesreturn uuid.UUID(user_id_str)except ValueError:return Nonereturn None
src/utils/security.py+7-9