-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTPBearer security scheme is returning 403 instead or 401 #2026
Comments
Could you provide an example of how you are getting that?
|
For API_KEY when the authorization is missing then it's a but for bearer it's a |
@raphaelauv @ArcLightSlavik My setup is the following:
Then if you don't provide a bearer token via the |
To be clear here: I only want to support Authorization header to do the JWT verification. I don't want to support the full OAuth2 flows which is why I'm not using the |
@aaaaahaaaaa You could open a PR to correct this |
I'd like to know why for API_KEY, the error should be |
It makes total sense to have 401 returned, I'm sure tiangolo did not mean 403 and it was just a small mishap |
This is still not fixed |
@tiangolo is this something that was intentionally implemented this way? Tests are checking for this as well, but my understanding is that a 401 is the appropriate response. |
There is a workaround solution:
|
I think this should be qualified as bug, not question, since, from MDN:
and
In this case, could (re-)authenticating permit access to the resource ? Definitively yes (As I understand, 403 is: try as much as you want, you will never have access), I tend to think 401 is authentication, 403 is permissions. Why it does matter ? Normally, when client receive 401, either the credentials are wrong, either the app is not logged anymore (i.e session timeout), so the normal process is to go to login page again and retry. On 403, there is no need to go there again since it is already authenticated (or, login again but with different credentials). My solution (that I find hideous) is to do this: from typing import Optional
from fastapi.security import APIKeyHeader as ApiKeyHeader403
from starlette.exceptions import HTTPException
from starlette.status import HTTP_401_UNAUTHORIZED
from starlette.requests import Request
class APIKeyHeader(ApiKeyHeader403):
async def __call__(self, request: Request) -> Optional[str]:
try:
api_key = await super().__call__(request)
except HTTPException as exception:
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED, detail=exception.detail
)
return api_key And then use the PS: This should not be necessary, or I miss the point, but since the only check done is on the header presence, this should be corrected upstream with 401. It works because right now, the only exception on APIKeyHeader is when the header is missing, but if someday fastapi implement permissions, I'm not sure it will still be valid. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
HTTPBearer security scheme enabled as a dependency is returning a
403
when a request is unauthenticated because of a missing or a malformedauthorization
header. In those scenarios, a401
should be returned instead.The text was updated successfully, but these errors were encountered: