🐛 Use 401
status code in security classes when credentials are missing
#13786
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR is an attempt to finally solve the issue with security tools returning error responses with status code
403
instead of401
when credentials are not provided.Breaking changes and workaround
These changes can break projects that rely on old behavior.
In order to mitigate this, the
not_authenticated_status_code
is introduced. If set to403
, it will make it work the same way as it was before changes (return403
status code).This option should be treated as a temporary workaround to give developers more time to update Clients to follow the new behavior.
Changes and reasoning
APIKeyQuery, APIKeyHeader, APIKeyCookie
not_authenticated_status_code
parameter can be used to revert this behavior back to returning 403 error code without sendingWWW-Authenticate
.WWW-Authenticate
information needed to understand how the key is supposed to be passed. I implemented default format (WWW-Authenticate: ApiKey in="...", name="..."
), but it’s possible to override the template forWWW-Authenticate
by subclassing and defining theformat_www_authenticate_header_value
methodHTTP Basic
WWW-Authenticate
header on a lack of credentialsrealm
is required according to the RFC, but optional in the current implementation. Fixing this would introduce breaking changes. Considering this is not a problem for people who want to follow the standard, I suggest we leave it as it is.HTTP Digest
WWW-Authenticate
is just a stub for now (justWWW-Authenticate: Digest
) (see notes)not_authenticated_status_code
parameter can be used to revert this behavior back to returning 403 error code without sendingWWW-Authenticate
.HTTPDigest
implementation is just a stub, we can’t follow standards (we don’t generatenonce
's, don’t haverealm
, …). I suggest we just change the error status code and add a stub forWWW-Authenticate
(justWWW-Authenticate: Digest
). For nowHTTPDigest
can’t be used as it is, so, this is not a problem.Digest
scheme. There have been made several attempts to implement it (Update HTTPDigest to match RFC 7616 #9825, 🔒 Match HTTP Digest specs from RFC 7616 #3071)HTTPDigest
is just a stub?HTTP Bearer, OAuth2 schemes, OIDC
OAuth2PasswordBearer
andOAuth2AuthorizationCodeBearer
: not needed.HTTPBearer
andOpenIdConnect
:OAuth2PasswordBearer
andOAuth2AuthorizationCodeBearer
.not_authenticated_status_code
parameter added toHTTPBearer
can be used to revert this behavior back to returning 403 error code without sendingWWW-Authenticate
.WWW-Authenticate
is not clearly described: It’s said that the value "Bearer" MUST be followed by one or more auth-param values. At the same time, all auth-param attributes are optional. In examples they always addrealm
. Since we don’t haverealm
, I suggest we just skip it and send justWWW-Authenticate: Bearer
WWW-Authenticate
format by addingrealm
andscope
Links