Spaces:
Running on Zero
Running on Zero
Download auth_utils.py from wraithklu/toxiguard-api: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/wraithklu/toxiguard-api/resolve/main/auth_utils.py
- Command line
-
hf download hf://spaces/wraithklu/toxiguard-api/auth_utils.py
-
curl -L -o auth_utils.py https://huggingface.co/spaces/wraithklu/toxiguard-api/resolve/main/auth_utils.py
1.02 kB
| import os | |
| from datetime import datetime, timedelta | |
| from jose import JWTError, jwt | |
| from fastapi import HTTPException, status | |
| import secrets | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| SECRET_KEY = os.getenv("JWT_SECRET_KEY", secrets.token_hex(32)) | |
| ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256") | |
| ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "1440")) | |
| def create_access_token(data: dict): | |
| to_encode = data.copy() | |
| expire = datetime.utcnow() + timedelta( | |
| minutes=ACCESS_TOKEN_EXPIRE_MINUTES | |
| ) | |
| to_encode.update({"exp": expire}) | |
| return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
| def verify_token(token: str): | |
| try: | |
| payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| return payload | |
| except JWTError: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid token" | |
| ) | |
| # ✅ API KEY GENERATOR | |
| def generate_api_key(): | |
| return secrets.token_hex(32) |