Spaces:
Runtime error
Runtime error
Download auth.py from vaishnav019/humres-api: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/spaces/vaishnav019/humres-api/resolve/main/auth.py
- Command line
-
hf download hf://spaces/vaishnav019/humres-api/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/vaishnav019/humres-api/resolve/main/auth.py
1.44 kB
| import os | |
| from datetime import datetime, timedelta, timezone | |
| from dotenv import load_dotenv | |
| from jose import jwt, JWTError | |
| from passlib.context import CryptContext | |
| from fastapi import FastAPI, Request, Depends, HTTPException | |
| from fastapi.security import OAuth2PasswordBearer | |
| load_dotenv() | |
| JWT_SECRET = os.getenv("JWT_SECRET") | |
| JWT_ALGORITHM = os.getenv("JWT_ALGORITHM") | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto", bcrypt__rounds=10) | |
| oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/login") | |
| def hash_password(password: str): | |
| return pwd_context.hash(password) | |
| def verify_password(plain_password: str, hashed_password: str): | |
| return pwd_context.verify(plain_password, hashed_password) | |
| def create_token(data: dict): | |
| payload = data.copy() | |
| payload["exp"] = datetime.now(timezone.utc) + timedelta(minutes=30) | |
| return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM) | |
| def get_current_user(token: str = Depends(oauth2_scheme)): | |
| try: | |
| payload = jwt.decode( | |
| token, | |
| JWT_SECRET, | |
| algorithms=[JWT_ALGORITHM] | |
| ) | |
| user_id = payload.get("user_id") | |
| if user_id is None: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid token" | |
| ) | |
| return user_id | |
| except JWTError: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid token" | |
| ) | |