CoolFace
Apppublic

Soham0708/forensicLogsChaiAi

sourceHugging Faceotherupdated 2y agoView on Hugging Face
0likes
auth.py40 linesDownload Raw Back to utils
1from fastapi import HTTPException, status2from fastapi.security import HTTPBearer3from datetime import datetime, timedelta4from jose import JWTError, jwt5import bcrypt6 7security = HTTPBearer()8 9# Secret key for JWT token10SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"11ALGORITHM = "HS256"12ACCESS_TOKEN_EXPIRE_MINUTES = 18013 14# Function to generate JWT token15def create_access_token(data: dict):16    to_encode = data.copy()17    expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)18    to_encode.update({"exp": expire})19    encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)20    return encoded_jwt21 22# Function to hash a password23def hash_password(password: str) -> str:24    hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())25    return hashed_password26 27# Function to verify a password28def verify_password(plain_password: str, hashed_password: bytes) -> bool:29    return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password)30 31 32def verify_token(token: str):33    try:34        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])35        return payload36    except JWTError:37        raise HTTPException(38            status_code=status.HTTP_401_UNAUTHORIZED,39            detail="Invalid or expired token"40        )