1
votes

I'm using django rest framwork with jwt.

In setting I used RS256 ALGORITHM

I want send token after user authentication, this is my function I'm trying to send token with user_id and is_user data it will produce token but when I pass token in request to server server response:

detail
:
"Error decoding signature."

Why?

here is my http://localhost:8000/login/ server response:

{
    "username": "admin",
    "email": "",
    "token": "b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJpc191c2VyIjp0cnVlfQ.kIz9TBYqVJVgFV5siM3QfNWHxBN28BlZRY_t8TFADmg'",
    "is_staff": false
}

login func:

JWT_SECRET = 'secret'
JWT_ALGORITHM = 'HS256'
JWT_EXP_DELTA_SECONDS = 20

   def validate(self, data):
        user_obj = None
        email = data.get('email', None)
        username = data.get('username', None)
        password = data.get('password')
        if not email and not username:
            raise ValidationError("email or username is required!")
        if '@' in username:
            email = username
        user = User.objects.filter(
            Q(email=email) |
            Q(username=username)
        ).distinct()

        # user = user.exclude(email__isnull=True).exclude(email__iexact='')
        if user.exists() and user.count() == 1:
            user_obj = user.first()
        else:
            raise ValidationError("this username/email is not valid")
        if user_obj:
            if not user_obj.check_password(password):
                raise ValidationError("password is incorrect")

        # payload_handler(user)
        # payload = payload_handler(user_obj)

        payload = {
            'user_id': user_obj.id,
            'is_user': True,
        }
        jwt_token = jwt.encode(payload, JWT_SECRET, JWT_ALGORITHM)
        # code = json_response({'token': jwt_token.decode('utf-8')})

        data['token'] = jwt_token
        return data

jwt setting:

JWT_AUTH = {
    'JWT_SECRET_KEY': SECRET_KEY,
    'JWT_ALGORITHM': 'RS256',
    'JWT_AUTH_HEADER_PREFIX': 'Bearer',
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=6600),

}
1

1 Answers

1
votes

For your "logic func" you use HS256 algorithm and in your "jwt setting" you use RS256. I think it must be a problem.