mirror of
https://github.com/kevin-DL/fast_api_api_template.git
synced 2026-01-11 09:44:34 +00:00
57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from jose import jwt, JWTError
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from api.config import settings
|
|
from crud.users import get_user
|
|
from api.database import SessionLocal
|
|
from schemas.users import User
|
|
|
|
|
|
class Token(BaseModel):
|
|
access_token: str
|
|
token_type: str
|
|
|
|
|
|
class TokenData(BaseModel):
|
|
sub: str | None = None
|
|
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
async def get_current_user(db: Session = Depends(get_db), token: str = Depends(oauth2_scheme)):
|
|
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])
|
|
sub: str = payload.get("sub")
|
|
if sub is None:
|
|
raise credentials_exception
|
|
token_data = TokenData(sub=sub)
|
|
except JWTError:
|
|
raise credentials_exception
|
|
user = get_user(db, user_id=token_data.sub)
|
|
if user is None:
|
|
raise credentials_exception
|
|
return user
|
|
|
|
|
|
async def get_current_active_user(current_user: User = Depends(get_current_user)):
|
|
if not current_user.is_active:
|
|
raise HTTPException(status_code=400, detail="Inactive user")
|
|
return current_user
|