Spaces:
Running
Running
| # app/core/security.py | |
| from datetime import datetime, timedelta | |
| from jose import jwt | |
| from passlib.context import CryptContext | |
| from app.core.config import settings | |
| import random | |
| pwd_context = CryptContext( | |
| schemes=["bcrypt_sha256"], | |
| deprecated="auto" | |
| ) | |
| def generate_otp(): | |
| return str(random.randint(100000, 999999)) | |
| def hash_password(p: str) -> str: | |
| return pwd_context.hash(p) | |
| def verify_password(plain: str, hashed: str) -> bool: | |
| return pwd_context.verify(plain, hashed) | |
| def create_access_token(data: dict, expires_minutes: int | None = None) -> str: | |
| to_encode = data.copy() | |
| expire = datetime.utcnow() + timedelta( | |
| minutes=expires_minutes or settings.ACCESS_TOKEN_EXPIRE_MINUTES | |
| ) | |
| to_encode.update({"exp": expire}) | |
| return jwt.encode(to_encode, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM) |