fastapi.security.APIKeyHeader auth class doesn't validate authentication scheme_name argument #8676
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI, Security, Depends
from fastapi.security.api_key import APIKeyHeader, APIKey
"""
We want the APIKeyHeader class to return the value of the provided header.
Along with that, we will also need the class to validate if the header's auth scheme matches the provided scheme.
"""
scheme="Token"
auth_header = APIKeyHeader(name="Authorization", scheme_name=scheme, auto_error=False)
async def get_auth(auth_header: str = Security(auth_header)):
print(f'Original scheme is {scheme}')
print(f'User provided scheme was {auth_header.split(" ")[0]}')
if auth_header:
return auth_header
else:
return None
app = FastAPI()
@app.get("/")
async def hello(api_key: APIKey = Depends(get_auth)):
return "Hello"
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)Description
Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.70.0 Python Version3.9.4 Additional ContextI see that in line 53 of |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
I also have another suggestion for usage of scheme_name in APIKeyQuery and in APIKeyCookie. This argument won't be necessary as auth schemes are usually not part of query params or cookies according to my knowledge. |
Beta Was this translation helpful? Give feedback.
-
|
I'd be happy to send a PR if necessary. Let me know ;) |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
scheme_nameparameter has different meaning, it's just the name that will be used in openapi for this security scheme:APIKey***classes assume that token is sent as a value of parameter without any prefixes.If you need to accept it as
Authorization: Schema asdthen you need to use another security class