jazzband / djangorestframework-simplejwt

A JSON Web Token authentication plugin for the Django REST Framework.
https://django-rest-framework-simplejwt.readthedocs.io/
MIT License
3.99k stars 661 forks source link

Conversion error when migrating token_blacklist app. #131

Open AutumnalDream opened 5 years ago

AutumnalDream commented 5 years ago

When running the initial migrations for the token_blacklist app I would get this message.

File "/venv/lib/python3.7/site-packages/rest_framework_simplejwt/token_blacklist/migrations/0003_auto_20171017_2007.py", line 10, in populate_jti_hex
    token.jti_hex = token.jti.hex
AttributeError: 'str' object has no attribute 'hex'

So in that specific migration there is this code.

for token in OutstandingToken.objects.all():
        token.jti_hex = token.jti.hex
        token.save()

I changed it to this and now it works.

for token in OutstandingToken.objects.all():
        token.jti_hex = binascii.hexlify(bytes(token.jti, "utf-8"))
        token.save()
JaniL commented 4 years ago

Hey, we started using this library in our project and stumbled on this same issue as well with @JoonatanPartanen.

If we understood this correctly based on few commits we found related to this, token.jti should be a UUID object instead of str. Because of this we're seeing the error mentioned earlier since UUID has a method called hex and str does not. We're not sure why this is happening, but we do agree that this needs a pull request. Not sure if @AutumnalDream's solution is the right one though if jti changing from UUID to str is not intentional.

jparta commented 4 years ago

There is likely some migration magic going on here. Looking at the migrations preceding this one (in the same path), OutstandingToken.jti should indeed still be a UUID object instead of a str. I am not sure what actions cause the migrations to be executed out of order (or whatever causes this).

Here is a link to the file: https://github.com/SimpleJWT/django-rest-framework-simplejwt/blob/03656ce0c8e1868d742704f72e3cf17b3d5b5b33/rest_framework_simplejwt/token_blacklist/migrations/0003_auto_20171017_2007.py

There is a rejected commit that seems to try to fix this: https://github.com/SimpleJWT/django-rest-framework-simplejwt/commit/de2729431323670b6def6008e3d03aed97d07c2d#diff-0163daaef412efb1451cadacef906469

codertron3000 commented 3 months ago

I tried the following and the migration worked:

for token in OutstandingToken.objects.all():
        token.jti_hex = UUID(token.jti).hex
        token.save()