I have a custom permission class to refuse any requests that do not have the correct API key provided in the request headers, and I'm not sure how to add it to the Django Rest Framework JWT views, including obtain_jwt_token
, refresh_jwt_token
, and verify_jwt_token
. How can I accomplish this?
It's possible by extending the jwt-view and using the newly created view. See the example below,
# permissions.py
from rest_framework.permissions import BasePermission
class MyPermissionClass(BasePermission):
# do something with your permission class
pass
# jwt_clone_views.py
from rest_framework_jwt.views import ObtainJSONWebToken, RefreshJSONWebToken, VerifyJSONWebToken
permission_cls = {"permission_classes": (MyPermissionClass,)}
obtain_jwt_token = ObtainJSONWebToken.as_view(**permission_cls)
refresh_jwt_token = RefreshJSONWebToken.as_view(**permission_cls)
verify_jwt_token = VerifyJSONWebToken.as_view(**permission_cls)
# urls.py
from .jwt_clone_views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token
urlpatterns = [
'',
# ...
url(r'^api-token-auth/', obtain_jwt_token),
# and so on
]
Here I created a new module named jwt_clone_views
which takes the permission classes as a parameter.
In urls.py
, I used the newly created views, which includes permission_classes
Note : I didn't test the solution.