-
Notifications
You must be signed in to change notification settings - Fork 766
It does not support Django Rest Framework token authentication backend #476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
To support DRF Token Authentication Backend I wrote a class which derives from GraphQLVIew. from django.http import HttpResponse
from graphene_django.views import GraphQLView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.permissions import IsAuthenticated
from rest_framework import status
class AuthenticatedGraphQLView(GraphQLView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
def authenticate_request(self, request):
for auth_class in self.authentication_classes:
auth_tuple = auth_class().authenticate(request)
if auth_tuple:
request.user, request.token = auth_tuple
break
def check_permissions(self, request):
for permission_class in self.permission_classes:
if not permission_class().has_permission(request, self):
return False
return True
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
try:
self.authenticate_request(request)
has_permission = self.check_permissions(request)
if not has_permission:
return HttpResponse(
json.dumps({'errors': ['permission denied']}),
status=status.HTTP_403_FORBIDDEN,
content_type='application/json')
except AuthenticationFailed as auth_failed_error:
return HttpResponse(
json.dumps({
'errors': [str(auth_failed_error)]
}),
status=status.HTTP_401_UNAUTHORIZED,
content_type='application/json')
return super(AuthenticatedGraphQLView, self).dispatch(request, *args, **kwargs) (edit) added the imports |
Thanks for the code you shared. In my opinion, we should have a setting like :
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@vinayan3 Thanks for posting your code! Question...did you ever try adding SessionAuthentication to your Doing so is helpful if you want logged in users to be able to access the GraphiQL UI. However, when I tried adjusting the code you posted like so:
It fails when DRF tries looking for request._request as part of the sessions auth check.
Curious if anyone else has attempted to use DRF token authentication and session authentication such that the GraphiQL UI is available to logged in users. |
bump, I've resorted to a separate view for a DRF REST endpoint and another for graphiql |
Uh oh!
There was an error while loading. Please reload this page.
when I try to access info.context.user it all the time returns anonymous user, my same code works fine with django-graphql-jwt
Debugging gives me this :
Means that no auth middleware has been set to the graphql view
The text was updated successfully, but these errors were encountered: