Coder Social home page Coder Social logo

Comments (4)

sshishov avatar sshishov commented on August 22, 2024

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.

davesque avatar davesque commented on August 22, 2024

@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.

sshishov avatar sshishov commented on August 22, 2024

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.

chgad avatar chgad commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.