Spaces:
Paused
Paused
from fastapi.security import OAuth2PasswordBearer | |
from fastapi import HTTPException, Depends | |
from jose import JWTError, jwt | |
from datetime import datetime, timedelta | |
SECRET_KEY = "llmbenchmark_tr" # your secret key | |
ALGORITHM = "HS256" | |
ACCESS_TOKEN_EXPIRE_MINUTES = 30 | |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/token") | |
def create_access_token(data: dict): | |
to_encode = data.copy() | |
expire = datetime.now() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) | |
to_encode.update({"exp": expire}) | |
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
return encoded_jwt | |
def get_current_user(token: str = Depends(oauth2_scheme)): | |
credentials_exception = HTTPException( | |
status_code=401, | |
detail="Could not validate credentials", | |
headers={"WWW-Authenticate": "Bearer"}, | |
) | |
try: | |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
username: str = payload.get("sub") | |
if username is None: | |
raise credentials_exception | |
return username | |
except JWTError: | |
raise credentials_exception |