GithubHelp home page GithubHelp logo

Comments (3)

mongkok avatar mongkok commented on May 27, 2024

Hi @virusdefender,

There is no easy way to invalidate the token when the user changes his password.

You could add a change password time field to the user model and check if the field is grater than the token issued time.

class JSONWebTokenBackend(object):

    def authenticate(self, request=None, **credentials):
        if request is None:
            return None

        token = get_authorization_header(request)

        if token is not None:
            payload = get_payload(token)
            user = get_user_by_payload(payload)

            if user.password_modified > payload['orig_iat']:
            	raise GraphQLJWTError(_('Invalid token'))

            return user

        return None

    def get_user(self, user_id):
        return get_user_by_natural_key(user_id)

from django-graphql-jwt.

virusdefender avatar virusdefender commented on May 27, 2024

thanks, my solution is to add user's password hash as a token field

class UserPwdSigHelper:
    def __init__(self, user):
        self.user = user

    def sha256(self, s):
        return hashlib.sha256(s.encode("utf-8")).hexdigest()

    def make_sig(self):
        return self.sha256(
            self.sha256(self.user.username) + "$" + self.user.password
        )

    def verify(self, sig):
        return constant_time_compare(sig, self.make_sig())

def get_user_by_payload(payload):
    username = payload.get(get_user_model().USERNAME_FIELD)

    if username:
        user = get_user_by_natural_key(username)
        if user and user.is_active and UserPwdSigHelper(user).verify(
            payload.get('pwd_sig', '')
        ):
            return user

from django-graphql-jwt.

mongkok avatar mongkok commented on May 27, 2024

Sorry I took so long to answer,
I think adding the datefield field could be a better solution,
jpadilla/django-rest-framework-jwt#105

from django-graphql-jwt.

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.