app.middlewares.authentication module¶
- async app.middlewares.authentication.auth_api_key_or_oauth2(api_key=Depends(get_api_key_header), oauth_2_scheme=Depends(CustomOAuth2AuthorizationCodeBearer))[source]¶
Authenticate a request using an API key or, failing that, OAuth2.
The API key is tried first: if a valid key is present the request is authenticated immediately. Otherwise the request falls back to OAuth2 bearer-token validation.
- Parameters:
api_key (str) – The API key resolved from the
X-API-Keyheader viaApiKeyService.get_api_key_header.oauth_2_scheme (str) – The OAuth2 bearer token extracted from the
Authorizationheader.
- Returns:
bool –
Trueif either credential is valid.- Raises:
HTTPException –
401if both mechanisms fail.
- async app.middlewares.authentication.auth_oauth2(oauth_2_scheme=Depends(CustomOAuth2AuthorizationCodeBearer))[source]¶
Authenticate a request using an OAuth2 bearer token.
- Parameters:
oauth_2_scheme (str) – The OAuth2 bearer token extracted from the
Authorizationheader.- Returns:
bool –
Trueif the token is valid.- Raises:
HTTPException –
401if OAuth2 authentication fails.