question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OAuth2: Header issue

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

async def get_current_user(
    token: str = Depends(oauth2_scheme), db: AsyncIOMotorClient = Depends(get_database)
):
    credentials_exception = HTTPException(
        status_code=HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"}
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        email: str = payload.get("sub")
        if email is None:
            raise credentials_exception
        token_data = TokenPlayload(email=email)
    except JWTError:
        raise credentials_exception

    dbuser = await get_user_by_email(db, token_data.email)
    if not dbuser:
        raise credentials_exception
    user = User(**dbuser.dict(), token=token)
    return user

Description

I follow the documentation for security part. The authorization works, however, when I want to use it as dependencies for other routes. I got this error.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.68.0

Python Version

3.8.3

Additional Context

Screenshot 2021-08-09 234947

Screenshot 2021-08-09 234638

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
CungVietDuycommented, Aug 9, 2021

@Kludex thanks for your recommendation. I changed the import statements and it worked.

0reactions
Kludexcommented, Aug 9, 2021

You’re welcome. Would you mind closing the issue? 😗

For reference, FastAPI creates its own HTTPException, inheriting from Starlette HTTPException: https://github.com/tiangolo/fastapi/blob/25a48287ed6043f7ee81417cef3c4d1f1dc56f72/fastapi/exceptions.py#L8-L16

Read more comments on GitHub >

github_iconTop Results From Across the Web

OAuth 2.0 Authorization Header - Stack Overflow
I have checked the differences between OAuth 1.0 & 2.0, and I have some confusion on Authorization Header (1.0 and 2.0), OAuth 1.0...
Read more >
Making Authenticated Requests - OAuth 2.0 Simplified
The access token is sent to the service in the HTTP Authorization header prefixed by the text Bearer . Historically, some services allowed...
Read more >
Authorization header is not passed · Issue #1343 - GitHub
As I said, my config works, only Chrome (also Safari) does not show the header. But that's another issue nothing to do with...
Read more >
Trying to get OAuth 2.0 token, receiving Missing valid ...
Incidentally, I'm using a Go library to help with OAuth2, and I also got the same error Missing valid authorization header error, then...
Read more >
CORS error when posting to /oauth2/token | FusionAuth Forum
When I use Postman, there is no CORS issue and this POST request to /oauth2/token works and I get valid Access and ID...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found