Skip to content

Conversation

@mnshai
Copy link

@mnshai mnshai commented Feb 6, 2025

Changes

  • Change HTTP status code from 403 to 401 for authentication failures across all security classes
  • Add appropriate WWW-Authenticate headers according to RFC standards
  • Update all related tests to expect 401 and verify headers

Requirements

  • Update security modules: api_key.py, http.py, oauth2.py, open_id_connect_url.py
  • Update corresponding tests
  • All tests pass

Standards Reference

Breaking Change

This is technically a breaking change as it modifies response status codes, but brings the implementation in line with HTTP standards where:

  • 401 Unauthorized: Client needs to authenticate first
  • 403 Forbidden: Client is authenticated but doesn't have permissions

Checklist

  • Commit message follows the guidelines
  • Tests for the changes have been added/updated

@mnshai mnshai changed the title feat(security): change authentication failure status from 403 to 401 feature(security): change authentication failure status from 403 to 401 Feb 6, 2025
@mnshai mnshai marked this pull request as draft February 6, 2025 07:47
@mnshai mnshai changed the title feature(security): change authentication failure status from 403 to 401 feat(security): change authentication failure status from 403 to 401 Feb 6, 2025
@mnshai mnshai marked this pull request as ready for review February 6, 2025 07:50
@mnshai
Copy link
Author

mnshai commented Feb 6, 2025

Hi! Could someone please add the appropriate labels to this PR? This change modifies security status codes to follow HTTP standards, so I believe it needs a feature or bug label.

@mnshai
Copy link
Author

mnshai commented Feb 6, 2025

Fixes #10177

@alejsdev alejsdev added the feature New feature or request label Feb 6, 2025
@alejsdev alejsdev changed the title feat(security): change authentication failure status from 403 to 401 ✨ Change authentication failure status from 403 to 401 Feb 6, 2025
@Olegt0rr
Copy link

Label feature looks weird for me. This PR fixes wrong behaviour, so it's definitely a bug fix.

@mnshai
Copy link
Author

mnshai commented Mar 3, 2025

Hi maintainers,

Just wanted to check if there's any feedback on this PR or if there's anything I can improve to help move it forward.

@alejsdev - would appreciate your thoughts when you have a moment.

Thanks!

@jbw-vtl
Copy link

jbw-vtl commented May 23, 2025

Hey All, is anything pending here still to get this merged?

We currently have to maintain a custom HTTPBearer class just to change this from 403 -> 401

@svlandeg
Copy link
Member

@mnshai: thanks for this contribution, it's a detailed and thoughtful PR. As you mentioned: while correct, this will be a breaking change. For that reason, we'll likely go with #13786 instead, which provides an additional fall-back mechanism for users that need more time to update. As such, I'll go ahead and close this one. Anyone who has time, is 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

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants