HTTPBearer security scheme is returning 403 instead or 401 #9130
-
|
HTTPBearer security scheme enabled as a dependency is returning a |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments 1 reply
-
|
Could you provide an example of how you are getting that? |
Beta Was this translation helpful? Give feedback.
-
|
For API_KEY when the authorization is missing then it's a but for bearer it's a |
Beta Was this translation helpful? Give feedback.
-
|
@raphaelauv @ArcLightSlavik My setup is the following: Then if you don't provide a bearer token via the |
Beta Was this translation helpful? Give feedback.
-
|
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 |
Beta Was this translation helpful? Give feedback.
-
|
@aaaaahaaaaa You could open a PR to correct this |
Beta Was this translation helpful? Give feedback.
-
I'd like to know why for API_KEY, the error should be |
Beta Was this translation helpful? Give feedback.
-
|
It makes total sense to have 401 returned, I'm sure tiangolo did not mean 403 and it was just a small mishap |
Beta Was this translation helpful? Give feedback.
-
|
This is still not fixed |
Beta Was this translation helpful? Give feedback.
-
|
@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. |
Beta Was this translation helpful? Give feedback.
-
|
There is a workaround solution:
|
Beta Was this translation helpful? Give feedback.
-
|
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_keyAnd 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. |
Beta Was this translation helpful? Give feedback.
-
|
I've created #10177. |
Beta Was this translation helpful? Give feedback.
I've created #10177.