Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How can I use another deps? #419

Closed
Closed
@yusuke2930

Description

I want to use get_current_client_user like this.
but, this is not working....
only reusable_oauth2 is working

from typing import Generator
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import jwt
from pydantic import ValidationError
from sqlalchemy.orm import Session
from app import crud, models, schemas
from app.core import security
from app.core.config import settings
from app.db.session import SessionLocal
reusable_oauth2 = OAuth2PasswordBearer(
 tokenUrl=f"{settings.API_V1_STR}/login/access-token"
)
client_reusable_oauth2 = OAuth2PasswordBearer(
 tokenUrl=f"{settings.API_V1_STR}/client/login/access-token"
)
def get_db() -> Generator:
 try:
 db = SessionLocal()
 yield db
 finally:
 db.close()
def get_current_user(
 db: Session = Depends(get_db), token: str = Depends(reusable_oauth2)
) -> models.User:
 try:
 payload = jwt.decode(
 token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
 )
 token_data = schemas.TokenPayload(**payload)
 except (jwt.JWTError, ValidationError):
 raise HTTPException(
 status_code=status.HTTP_403_FORBIDDEN,
 detail="Could not validate credentials",
 )
 user = crud.user.get(db, id=token_data.sub)
 if not user:
 raise HTTPException(status_code=404, detail="User not found")
 return user
def get_current_active_user(
 current_user: models.User = Depends(get_current_user),
) -> models.User:
 if not crud.user.is_active(current_user):
 raise HTTPException(status_code=400, detail="Inactive user")
 return current_user
def get_current_active_superuser(
 current_user: models.User = Depends(get_current_user),
) -> models.User:
 if not crud.user.is_superuser(current_user):
 raise HTTPException(
 status_code=400, detail="The user doesn't have enough privileges"
 )
 return current_user
def get_current_client_user(
 db: Session = Depends(get_db), token: str = Depends(client_reusable_oauth2)
) -> models.ClientUser:
 try:
 payload = jwt.decode(
 token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
 )
 token_data = schemas.TokenPayload(**payload)
 except (jwt.JWTError, ValidationError):
 raise HTTPException(
 status_code=status.HTTP_403_FORBIDDEN,
 detail="Could not validate credentials",
 )
 client_user = crud.client_user.get(db, id=token_data.sub)
 if not client_user:
 raise HTTPException(status_code=404, detail="User not found")
 return client_user
def get_current_active_client_user(
 current_user: models.ClientUser = Depends(get_current_client_user),
) -> models.ClientUser:
 if not crud.client_user.is_active(current_user):
 raise HTTPException(status_code=400, detail="Inactive user")
 return current_user

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /