Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
graphql: Add HTTP401-wrapper @auth_required for gql actions
- Create @auth_required wrapper that throws custom GraphqlAuthenticationError if user is not authenticated - Create DRFAuthenticatedGraphQLView which wraps GraphQLView and re-raises DRF.AuthenticationFailed if GraphqlAuthenticationError is raised. DRF will then return 401 - Create AuthenticatedGraphQLView which wraps GraphQLView and returns Note: DRFAuthenticatedGraphQL view This is a custom workaround. It raises an HTTP 401 error, which is not a part of the GraphQL spec. It may fit some frontend apps for a transition period to GraphQL, but should not be seen as a permanent solution. AuthenticatedGraphQLView is more in line with GraphQL spec, and shoul be preferred if applicable for you.
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from graphene_django.views import GraphQLView | ||
from graphql import GraphQLError | ||
|
||
from rest_framework import ( | ||
permissions, | ||
request as drf_request, | ||
) | ||
from rest_framework.decorators import ( | ||
api_view, | ||
permission_classes, | ||
) | ||
from rest_framework.exceptions import AuthenticationFailed | ||
|
||
|
||
class GraphqlAuthenticationError(GraphQLError): | ||
pass | ||
|
||
|
||
def auth_required(fn): | ||
def wrapper(*args, **kwargs): | ||
*_, info = args | ||
|
||
if not info.context.user.is_authenticated: | ||
raise GraphqlAuthenticationError('Authentication required.') | ||
return fn(*args, **kwargs) | ||
return wrapper | ||
|
||
|
||
class DRFAuthenticatedGraphQLView(GraphQLView): | ||
""" | ||
Returns a HTTP 401. Outside GraphQL spec. | ||
""" | ||
def parse_body(self, request): | ||
if isinstance(request, drf_request.Request): | ||
return request.data | ||
return super().parse_body(request) | ||
|
||
@staticmethod | ||
def format_error(error): | ||
if ( | ||
hasattr(error, 'original_error') and | ||
isinstance(error.original_error, GraphqlAuthenticationError)): | ||
raise AuthenticationFailed() | ||
return GraphQLView.format_error(error) | ||
|
||
@classmethod | ||
def as_view(cls, *args, **kwargs): | ||
view = super(GraphQLView, cls).as_view(*args, **kwargs) | ||
view = permission_classes((permissions.AllowAny,))(view) | ||
view = api_view(['GET', 'POST'])(view) | ||
return view | ||
|
||
|
||
class AuthenticatedGraphQLView(GraphQLView): | ||
""" | ||
Returns a regular GraphQL error | ||
""" | ||
def parse_body(self, request): | ||
if isinstance(request, drf_request.Request): | ||
return request.data | ||
return super().parse_body(request) | ||
|
||
@classmethod | ||
def as_view(cls, *args, **kwargs): | ||
view = super(GraphQLView, cls).as_view(*args, **kwargs) | ||
view = permission_classes((permissions.AllowAny,))(view) | ||
view = api_view(['GET', 'POST'])(view) | ||
return view |