How to auth using JWTTokenUserAuthentication without Database

Viewed 1242

Henlo!

App deps:

djangorestframework-simplejwt==4.3.0, djangorestframework==3.10.3

I'm trying to auth User with JWTTokenUserAuthentication without using DB using shared SECRET_KEY.

In my settings I have:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTTokenUserAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
}

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': datetime.timedelta(minutes=120),
    'REFRESH_TOKEN_LIFETIME': datetime.timedelta(hours=12),
    'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': True,
    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': SECRET_KEY,
    'AUTH_HEADER_TYPES': ('Bearer',),
    'USER_ID_FIELD': 'id',
    'USER_ID_CLAIM': 'user_id',

    'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
    'TOKEN_TYPE_CLAIM': 'token_type',

    'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
    'SLIDING_TOKEN_LIFETIME': datetime.timedelta(minutes=5),
    'SLIDING_TOKEN_REFRESH_LIFETIME': datetime.timedelta(days=1),
}

For the views I use rest_framework_simplejwt.views.TokenObtainPairView. And in my app's urls:

urlpatterns = [
    path('test-token/', TokenObtainPairView.as_view(),
         name='token_obtain_pair')
]

So, I'm making post request providing it with Bearer token and credentials as username and password. But I get message:

{
    "detail": "No active account found with the given credentials"
}

What should I do to auth correctly?

1 Answers
  1. Create a super user and then some dummy user, if not done
  2. Make migrations.
  3. Create a view and its corresponding URL.
  4. From Postman, use your simplejwt token api, along with username and password to get the token.
  5. Use the access token you obtain to call the api you creat3d in step 3.

I can guide through entire process but that way you will not learn anything worthwhile. I am also a struggling newbie and veteran VB.net/Visual Basic developer but I am pois3d to learn new things like django...Roll your sleeves, man

Related