Skip to content

Conversation

@mrPaintMan
Copy link

@mrPaintMan mrPaintMan commented Oct 22, 2024

TL;DR This PR aims to change default error code for HTTPBearer and HTTPDigest to from 403_Forbidden to 401_Unauthorized.

Goal of this PR

This PR aims to address the issues of having status code 403_forbidden for the HTTPBearer and HTTPDigest classes, and change them to 401_Unauthorized. From what we understand, the reason for why this hasn't been fixed already is what @tiangolo states in his comment in PR #2120. This PR aims to comply to the error handling described in RFC standards 6750 (Bearer auth), 7616 (Digest auth) and 7617 (Basic auth).

This PR does not aim to provide a working implementation of the Bearer or the Digest authentication schemas, but rather to improve the existing error handling.

Changed behavior

In order to comply to these standards, we have introduced a default value (the value global) to the realm challenge of the WWW-Authenticate header, since it MUST be present for Basic auth, and MAY be present for Bearer and Digest auth. On top of this, Bearer auth MUST have at least one challenge in the WWW-Authenticate header, but no specific challenge is required, thus making the realm challenge suitable for this case as well.

Also, the challenge qop (quality of protection) MUST be present on an erroneous request to an endpoint protected by Digest auth. We solved this by defaulting this value to auth since we interpreted that this is the default value according to the standard.

Additional changes

  • Changed default error message to better correspond to default errors
  • Added the components qop and realm to the openapi docs according to how other components are handled
  • Added tests covering the usages of the field realm for Basic, Bearer and Digest auth
  • Added tests covering the usages of the field qop for Digest auth

Disclosure

We have very little experience with contributing to open source projects, and are very open to suggestions and feedback on this work!

filiperiksson4 and others added 8 commits October 9, 2024 12:32
Added a default value for realm, to satisfy the requirements of basic. Also changed some of the 403 codes to 401 since 403 was not fitting for that scenario.
Changed some of the tests to match the more correct formating of the HTTP-schemas. Then matched the test cases for the new error messages and new error codes.
@alejsdev alejsdev changed the title Updating HTTP auth error codes to 401 while complying to RFC standards ♻️ Updating HTTP auth error codes to 401 while complying to RFC standards Oct 22, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2025

This pull request has a merge conflict that needs to be resolved.

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 5, 2025
@svlandeg
Copy link
Member

Hi @mrPaintMan, thanks for the detailed and thoughtful PR! We've had quite a few open PRs for this issue, but have recently decided that we'll likely go with #13786, which provides an additional fall-back mechanism for users that need more time to update (as this is a breaking change). As such, I'll go ahead and close this one. If you feel like it, always welcome to help review #13786 !

@svlandeg svlandeg closed this Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants