Spaces:
Sleeping
Sleeping
Download app/api/deps.py from sandy898/resonate-api: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/sandy898/resonate-api/resolve/main/app/api/deps.py
- Command line
-
hf download hf://spaces/sandy898/resonate-api/app/api/deps.py
-
curl -L -o deps.py https://huggingface.co/spaces/sandy898/resonate-api/resolve/main/app/api/deps.py
1.16 kB
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import OAuth2PasswordBearer | |
| from jose import jwt, JWTError | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from sqlalchemy.future import select | |
| from app.core.config import settings | |
| from app.db.database import get_db | |
| from app.db.models import User | |
| oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/auth/login") | |
| async def get_current_user(token: str = Depends(oauth2_scheme), db: AsyncSession = Depends(get_db)): | |
| credentials_exception = HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Could not validate credentials", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| try: | |
| payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) | |
| user_id: str = payload.get("sub") | |
| if user_id is None: | |
| raise credentials_exception | |
| except JWTError: | |
| raise credentials_exception | |
| result = await db.execute(select(User).where(User.id == user_id)) | |
| user = result.scalars().first() | |
| if user is None: | |
| raise credentials_exception | |
| return user | |