Comments (4)
Hello, to get the payload you do not need to decode and use key. Key is needed just to sign the payload (that it is not altered).
Just write the util method for yourself to get the payload from token (helper):
import json
import jwt
def create(self, request, *args, **kwargs):
data = request.data
token_payload = get_token_payload(token=request.META.get('HTTP_AUTHORIZATION'))
user_id = token_payload['user_id']
... do more stuff ...
def get_token_payload(token):
return json.loads(jwt.utils.base64url_decode(token.split('.')[1]).decode())
from djangorestframework-simplejwt.
@chgad Assuming you're using the JWTAuthentication
backend properly by placing it in the list of DEFAULT_AUTHENTICATION_CLASSES
in the REST_FRAMEWORK
config object, Django should provide a user object to you on the view's request
object:
def create(self, request, *args, **kwargs):
user_id = request.user.id
... do more stuff ...
If you're not able to resolve the difficulty, feel free to re-open the issue.
from djangorestframework-simplejwt.
Just try to be more specific, what you should get in the view. If you need user_id
then you do not need to get it from the token. Just get it from request
object as @davesque mentioned.
from djangorestframework-simplejwt.
Hi,
please excuse me for taking so long to respond to the first/second answer.
@davesque suggestion is what i finally went with, which didn't work in the first run due to a miss-configuration of our settings.py
.
This miss-configuration resulted in self.request.user
always becoming an instance of AnnonymusUser
.
In response to @sshishov :
We are looking forward to implement interactions with services which do only provide datatransfer encrypted within the JWT. This being said, we would like to have a generic solution to access all encrypted claims from the JWT.
If this behavior/feature is of interest/relevance I'd be happy, with a little bit of guidance, to dig into this and present a PR in the future.
from djangorestframework-simplejwt.
Related Issues (20)
- Incorrect type hints for several classes/methods HOT 4
- Import "rest_framework_simplejwt.authentication" could not be resolved Pylance (reportMissingImports) HOT 2
- Import Error on Django 5 HOT 1
- ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)) with djangorestframework-jwt HOT 1
- How setup AWS Cognito with djangorestframework-simplejwt HOT 2
- CVE-2024-22513 HOT 1
- Security Issue HOT 3
- `Token` subclass' `for_user` method is typed to return a `Token` HOT 1
- encrypt username instead of user_id HOT 1
- The translation of the message "No active account found with the given credentials" does not work in various locales HOT 1
- TokenRefreshView doesn't register token_blacklist_outstandingtoken HOT 2
- How can we return custom JWT from dj-rest-auth instead of the session key HOT 1
- django5.0 is currently not supported HOT 1
- Why Outstanding token exist and why it stores raw refresh token
- Reported vulnerability in 5.3.1 HOT 3
- request.user.is_superuser and request.user.is_staff are False HOT 3
- Allow adding more field to payload of encoded `token` HOT 2
- Add JSON_ENCODER to the doc
- Can django 5.0 be used? Just use basic verification methods HOT 7
- TokenRefreshView should prevent users from refreshing token if the user is inactive
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from djangorestframework-simplejwt.