-
Notifications
You must be signed in to change notification settings - Fork 678
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
User ID method handling #169
Comments
I’m planning on creating a signal handler (with lots of warnings for when to use them, but this scenario seems fine) for post authentication. I can add a pre_authenticate handler as well. @ethanjahn |
The other method is to add the username on token creation. So you'd include it in the refresh and access token payload. |
@Andrew-Chen-Wang To further detail this issue, I recently opened a new issue with the auth0 Django API tutorial. I've linked it back to this discussion. |
Hi @asoundlife You'll have to give me until next week, possible Monday to re-visit the tutorial to see why this is necessary. For now, I'm gonna ask some questions so I may better understand this issue (I've been running out of time lately to maintain some of my repositories with college comin' up).
Are we putting the username in the payload? If so, that can be done in the serializer in the "validate" method. Is the username the same as your database's username or Auth0's? Doe the payload look like "auth0|Andrew-Chen-Wang" or "auth0|BASE64_BLAHS_IN_AUTH0"? Storing "auth0.username" in the User model? I must've passed it in the tutorial. Mind sharing a blockquote? I do see the RemoteAuthentication middlewear. Again, adding a callback would help for additional authentication measures, I suppose. If anyone does make a callback, only make a PRE-authentication callback. If it were POST/after, then people will think they can use it for Thanks for helping me understand the issue! |
Thanks @Andrew-Chen-Wang , To illustrate the answers to your questions, consider the scenario of having a Django backend serving an API to a frontend like Vue, using auth0 to serve as the authentication provider. The auth0 team recommends setting up an authentication system in the following way: 1. Configure a new auth0 application & API with the auth0 dashboard (their website)This gives the basic structure for managing your users and the rules behind authentication. For our example scenario above, the dashboard serves as scaffolding until a user management tool is created for the Vue frontend, which interacts with the auth0 API via JWT. 2. User authenticates via a "universal" (customizable) login screenThis is hosted by auth0, which is enabled by default and mandatory if you're not on a paid subscription. The universal login screen processes the form with the auth0 backend and then redirects the user to our example scenario site, along with an (RS256) JWT authentication token. 3. Auth0 provides an SDK for SPAs:This SDK allows you to create a Vue component that manages the connection between the Vue frontend and provides all the necessary methods to connect and interact with the authentication pieces of the auth0 API. I hope this clarifies question 1 for this particular scenario. One key note before moving on:For all but the "Enterprise" version of auth0, Auth0 stores your user accounts and profiles. In the Enterprise version, you can configure your own database to manage your user profiles. However, they still recommend migrating your user database to auth0. I hope this adds some clarity to your question 2. Auth0 actually recommends that you shouldn't manage your own authentication, generally speaking. This also means that in many use cases you won't store user account information on the backend Django database, though, auth0 does provide methods to do this. NextIn keeping with our example, for Vue to consume the backend Django API it sends an axios request with an authentication header that embeds the JWT token managed by Vue/Auth0 SDK. The token includes basic user information and user permissions. In the Django API tutorials posted by auth0, the auth0 team rely's on the unmaintained drf-jwt package to manage the communication between the Vue frontend and the Django API backend. This solution still works, but users such as myself are looking to migrate away from it, given the status of that package. SimpleJWT appears to be the best solution :). See my linked issue for details regarding the auth0 implementation with drf-jwt and where I've taken things with SimpleJWT. In auth0's recommended implementation, drf-jwt decodes the token and automagically authenticates the user to the backend server (using In this regard, I'm not sure how to answer your question 3. Is the username part of the payload? Technically yes, but it could literally be anything. The user is being authenticated via
I hope this provides more detail. Let me know if I can provide anything further! I've attached my backend.py and tokens.py files that I forked from SimpleJWT. (auth0simple.gz) With these edits, the token decode method recommended by auth0 works with simpleJWT and you can |
I see. I'm quickly going through my GitHub mail atm, so you'll still have to give me a little bit of time before big reviewing. There isn't a PR for this yet, but #227 may be a solution. Take a look at it for now. Thank you for all the info @asoundlife! Looking forward to getting y'all a solution hopefully within this week. |
Awesome! After a quick glance of #227, it looks like some of the work done there could be applicable to implementing a solution here. If that's the case, it might make sense to merge these issues once things become a bit more clear. Thanks again @Andrew-Chen-Wang . I'll work through #227 and see if I can get the authentication side working for auth0. |
Hi, thanks for the great work on this project!
I am using Auth0 and have been using the now deprecated django-rest-framework-jwt library but would like to switch to simplejwt. Part of the implementation I was using used a function to alter a username from the token and authenticate the user in Django's User model.
The relevant function is:
and the setting in django-rest-framework-jwt is:
This is necessary because Auth0 uses usernames that look like "auth0|username" and they recommend storing as "auth0.username" in the User model (not fully sure why) and also because I need to authenticate the remote user before the token user_id claim means anything.
Do you know if there is a way to use a function upon receiving the JWT token in simplejwt? Alternatively, if there is a workaround happy to implement it!
The text was updated successfully, but these errors were encountered: