[Python] How to make Depends optional in FastAPI?

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have an API which gives images to the user and non-users. Images can be public or private.

    My code:


    @router.get("/{id}")
    def get_resource(id: str, current_user: User = Depends(get_current_user)):
    return return_resource(id, current_user)


    This code enforces authorization strictly. What I want is if the user is not logged in, it should then set the current_user parameter to None, so that I can allow access to public images and restrict private.

    Other codes:

    get_current_user


    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login")

    async def get_current_user(required: bool = True, token: str = Depends(oauth2_scheme)):
    credentials_exception = HTTPException(
    status_code=status.HTTP_401_UNAUTHORIZED,
    detail="Could not validate credentials",
    headers={"WWW-Authenticate": "Bearer"},
    )

    if not required and not token:
    return None

    return verify_token(token, credentials_exception)


    I want to send parameter like required to get_current_user

    verify_token


    def verify_token(token: str, credentials_exception):
    try:
    payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
    email: str = payload.get("email")
    pk: str = payload.get("pk")
    if email is None:
    raise credentials_exception
    token_data = TokenData(
    email=email,
    pk = pk
    )
    except JWTError:
    raise credentials_exception
    return token_data

    Continue reading...

Compartilhe esta Página