-
Notifications
You must be signed in to change notification settings - Fork 4
Fix the order of authentication classes for DRF APIs #898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds a new unauthenticated payment confirmation test, updates the Swagger API docs test to expect HTTP 401 for a normal user, and changes REST_FRAMEWORK.DEFAULT_AUTHENTICATION_CLASSES to remove TokenAuthentication and place OAuth2Authentication before SessionAuthentication. Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Are we using token authentication for any process (i.e. the TokenAuthentication class)? Maybe a question for @calellowitz |
|
I'm fine with this PR as is and I think it might work, but @calellowitz made an interesting suggestion on slack which I'm thinking we could explore, ie. to remove the other two auth classes. To quote Cal:
|
Yes, this only matters for DRF views. |
I don't believe we are. We use it (or something similar) in connect-id, but I don't know of any place in this repo |
| REST_FRAMEWORK = { | ||
| "DEFAULT_AUTHENTICATION_CLASSES": ( | ||
| "rest_framework.authentication.SessionAuthentication", | ||
| "rest_framework.authentication.TokenAuthentication", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description mentions just re-ordering, however this is removing TokenAuthentication altogether?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the TokenAuthentication is not being used, it has been removed. See comment
Updated the description in that regards. Thanks for catching.
Product Description
Fixes the order of authentication classes for DRF APIs and removes the ununsed TokenAuthentication class
No UI facing change.
Technical Summary
Ticket
Issue
API requests made with an invalid or expired OAuth2 bearer token were returning 403 Forbidden instead of the expected 401 Unauthorized. This made it unclear to API clients that re-authentication was required.
Root Cause
Django REST Framework determines whether to return 401 or 403 based on the first authentication class in DEFAULT_AUTHENTICATION_CLASSES. Previously, SessionAuthentication was listed first. Since session auth does not return a WWW-Authenticate header, DRF downgraded authentication failures to 403, even when OAuth2 authentication was involved.
Fix
Reordered the authentication classes to make OAuth2Authentication the primary mechanism. Also since the TokenAuthentication class is not being used anywhere, it was removed.
This ensures invalid or expired OAuth2 tokens correctly return 401 Unauthorized, while preserving session-based access.
Safety Assurance
Safety story
The order in which authentication happens is modified, with the primary authentication mehtod OAuth2 so the change is quite safe.
Affects DRF APIs.
Automated test coverage
A test case added to ensure correct error is returned.
QA Plan
None
Labels & Review