When I implemented Auth0 with simple JWT, I got the error 401, indicating an invalid token. Oddly, when decoding the token on jwt.io, it appeared to be valid. It would be very helpful whenever simple JWT is used to know how to keep user info in the database. DRF-JWT uses Django’s RemoteUserMiddleware to create it. Any help would be appreciated. Thank you
Here’s a snippet of the relevant settings from my settings.py file:
INSTALLED_APPS = [
...
"rest_framework",
"rest_framework_simplejwt",
"rest_framework_simplejwt.token_blacklist",
...
]
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
],
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication"
],
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}
AUTH0_DOMAIN = "my-auth0-domain"
JWT_ISSUER = f"https://{AUTH0_DOMAIN}/"
JWT_AUDIENCE = auth0_credentials["AUTH0_API_AUDIENCE"]
JWKS_URL = f"https://{AUTH0_DOMAIN}/.well-known/jwks.json"
SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=5),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
"ROTATE_REFRESH_TOKENS": True,
"BLACKLIST_AFTER_ROTATION": True,
"UPDATE_LAST_LOGIN": True,
'ALGORITHM': 'RS256',
'AUDIENCE': JWT_AUDIENCE,
'ISSUER': JWT_ISSUER,
'JWK_URL': JWK_URL,
'AUTH_HEADER_TYPES': 'Bearer',
'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
'USER_ID_FIELD': 'auth0_sub',
'USER_ID_CLAIM': 'sub',
"JTI_CLAIM": None,
"TOKEN_TYPE_CLAIM": None,
}
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
]
Note: Regarding ‘USER_ID_FIELD’: ‘auth0_sub’, I stored the Auth0 user ID values in an ‘auth0_sub’ column in the user table of Django, ‘USER_ID_CLAIM’: ‘sub’, the payload value of the Auth0 access token (Auth0 user ID).