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.
Add integration and docs for Django Ninja authentication #1646
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: master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Add integration and docs for Django Ninja authentication #1646
Changes from all commits
29144e57b6b50e14e5d4dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
This is an interesting consideration, but Copilot's interpretation is incorrect.
To restate my code comment, as Copilot doesn't seem to understand... At this point in the code
valid is True. We should assume that ourHttpOAuth2handler is responsible for authenticating the entirety of this request (unfortunately, there's no specification to follow, but this seems like the only behavior that makes sense). It's possible that a session cookie was also passed with this request, in addition to OAuth2Authorization: Bearer ...headers; in that case, anAuthenticationMiddlewarewould have pre-setrequest.userto whatever the session cookie resolves, but we want to always overwrite that to whatever the OAuth2 token resolves. We have to do this unconditionally, or else we'd have conflicting sources of truth for the request user.However,
ris anAbstractAccessToken, andr.useris nullable. What should we do ifr.user is None? The answer should not depend on whether anAuthenticationMiddlewarehas already setrequest.user, because we now "own" the request's authentication and should unconditionally set the user.I don't think we should set
request.user = AnonymousUser(), because this isn't an anonymous request. It's an authenticated request, but the token simply doesn't have an associated user (which OAuth-Toolkit apparently allows).However, while
Request.userisn't a part of pure Django (it's not defined on theRequestmodel), practically it's always patched on byAuthenticationMiddleware(which is typically enabled). So, most sensible projects (anddjango-stubs) assume thatrequest.useris always set to something: either an actual user orAnonymousUser(it is not expected to beNone, so people don't guard against calling something likerequest.user.is_authenticated).So, what do we do? If you force me to resolve the compromise, I'd say perhaps we should set it to
AnonymousUser(), so at least we won't break people's type expectations, plus I'm hoping that havingAbstractAccessToken.userbeNoneis pretty rare. However, I'd really appreciate some additional feedback @dopry.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.
Thanks for taking the time to look into this. I'll try to find the time to consider it in more depth in the next few days.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.