grip / app /core /security.py
github-actions
Deploy to Hugging Face
ee2e133
from datetime import datetime, timedelta
from typing import Optional
from jose import jwt
from passlib.context import CryptContext
from app.core.config import get_settings
settings = get_settings()
pwd_context = CryptContext(schemes=["argon2", "bcrypt"], deprecated="auto")
def verify_password(plain_password: str, hashed_password: str) -> bool:
# Bcrypt has a max length limit of 72 bytes, truncate input to avoid crash
return pwd_context.verify(plain_password[:72], hashed_password)
def get_password_hash(password: str) -> str:
# Bcrypt has a max length limit of 72 bytes for passwords
# We truncate excessively long passwords to prevent crashing
return pwd_context.hash(password[:72])
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
return encoded_jwt