-
Notifications
You must be signed in to change notification settings - Fork 415
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
feat: Support cookie authentication #4662
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 3 Skipped Deployments
|
Docker builds report
|
Uffizzi Preview |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #4662 +/- ##
==========================================
+ Coverage 97.19% 97.20% +0.01%
==========================================
Files 1166 1171 +5
Lines 40436 40640 +204
==========================================
+ Hits 39301 39506 +205
+ Misses 1135 1134 -1 ☔ View full report in Codecov by Sentry. |
api/custom_auth/urls.py
Outdated
path("logout/", TokenDestroyView.as_view(), name="authtoken-logout"), | ||
path( | ||
"logout/", | ||
JWTSlidingBlacklistView.as_view(), | ||
name="authtoken-logout", | ||
), |
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.
So, I guess what you're saying in other conversations is that TokenDestroyView
does nothing, is that right? I guess I'm concerned about a couple of things here:
- Are we removing functionality here for regular tokens? I assume not, but just checking.
- Shouldn't we just have a
Logout
view class where we can internalise the conditional logic depending on the setting?
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.
TokenDestroyView
, in fact, does remove the token. However, the endpoint has never been used by the frontend; to log the user out, the application simply removed the t
cookie.
Now that I expect the endpoint to be actually used, I ultimately decided to remove function related to classic tokens and only implement the JWT logout.
I have renamed the view and added a docstring to hopefully be more clear.
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.
Maybe the FE never used this functionality, but that's not to say that it's never been used by someone integrating with the API. It's obviously very unlikely, but not totally out of the question since we only provided API access via these user tokens for a long time.
I don't see the harm in either:
- Reverting to what you had initially and subclassing
TokenDestroyView
and callingsuper()
- Having 2 separate views that we add into urlpatterns depending on the existence of the environment variable.
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.
but that's not to say that it's never been used by someone integrating with the API
They will still have the DELETE /api/v1/auth/token/
endpoint that serves this exact function, and arguably has better semantics as we do not delete classic tokens on logout.
I can add the notion to OpenAPI docs.
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.
They will still have the DELETE /api/v1/auth/token/ endpoint that serves this exact function, and arguably has better semantics as we do not delete classic tokens on logout.
But it's technically a breaking API change, right? We're removing functionality from an existing endpoint, that existed, and was documented for people to use.
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.
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.
We can't just remove it though. I don't mind adding a warning that it'll be deprecated in the next version (and let's add it to the V2 API issue).
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 problem is that it's being called in FE now. We need to either add conditional logic to FE or explain to users that their tokens get invalidated if they logout explicitly. The latter is fine by me as using an admin API key is the clear alternative.
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.
their tokens get invalidated if they login explicitly
I assume here you meant to say 'logout explicitly' ?
It doesn't seem like it'd be hard to add conditional logic to the frontend to only call the logout endpoint if the jwt cookie exists, and seems to be the right thing to do to maintain compatibility, but I can't speak for @kyle-ssg here.
Alternatively, we do what you suggested, and just show a warning when the user hits the logout button to say that any direct API integrations they have using the user token will stop functioning.
Another alternative (that doesn't involve any more FE work) is still to conditionally change the view class used by the logout
route depending on the existence of environment variable, but we may then need to think about whether we can actually run both in parallel.
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.
As discussed offline, the FE conditional logic was added and token deletion function restored in the BE.
350daf5
to
dfb0476
Compare
e7faf0a
to
1a0dc87
Compare
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.
Approving with one minor comment
if (!data.token) { | ||
return | ||
} | ||
;(Project.cookieAuthEnabled |
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.
... is the semi-colon here some horrible JS syntax that I'm not aware of, or a typo? @kyle-ssg
Thanks for submitting a PR! Please check the boxes below:
docs/
if required so people know about the feature!Changes
This adds a basic JWT cookie support. As long as
COOKIE_AUTH_ENABLED
setting is set toTrue
:The JWT lifetime is set to be 10 hours by default. No refresh endpoints have been implemented at this point; when we do implement refreshing, token lifetime should be shortened significantly.
It's possible to invalidate the cookie via a
POST /api/v1/auth/logout/
request. The token is added to blacklist and the cookie is unset.A signal handler is added to set
Access-Control-Allow-Origin
based onget_current_site_url
utility function. The function is modified to properly use the Django sites framework cache.How did you test this code?
Added an integration test.
BE (including SAML) + FE has been tested in staging.