Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add mutations to authenticate API request with JWT; restrict dashboard API access

Marcin Gębalad319bde64385
diff --git a/requirements.in b/requirements.in
index 91c19a57ef..c96dea7f96 100644
--- a/requirements.in
+++ b/requirements.in
@@ -11,6 +11,7 @@ django-cache-url>=1.0.0
django-countries>=5.2
django-filter==1.1
django-fsm>=2.6.0
+django-graphql-jwt==0.1.8
django-impersonate==1.3
django-mptt>=0.7.1
django-payments>=0.12.1
diff --git a/requirements.txt b/requirements.txt
index 1f70a97dd4..8e6ef14673 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -30,8 +30,10 @@ django-cache-url==2.0.0
django-celery-results==1.0.1
django-countries==5.2
django-elasticsearch-dsl==0.4.4
+django-environ==0.4.4 # via django-graphql-jwt
django-filter==1.1
django-fsm==2.6.0
+django-graphql-jwt==0.1.8
django-impersonate==1.3
django-js-asset==1.0.0 # via django-mptt
django-mptt==0.9.0
@@ -54,7 +56,7 @@ freezegun==0.3.9
google-i18n-address==2.2.2
google-measurement-protocol==1.0.0
graphene-django==2.0.0
-graphene==2.0.1 # via graphene-django
+graphene==2.0.1 # via django-graphql-jwt, graphene-django
graphql-core==2.0 # via graphene, graphql-relay
graphql-relay==0.4.5 # via graphene
html5lib==1.0.1 # via bleach, weasyprint
@@ -78,7 +80,7 @@ psycopg2==2.7.4
purl==1.3.1
py==1.5.2 # via pytest
pycparser==2.18 # via cffi
-pyjwt==1.5.3 # via django-payments, social-auth-core
+pyjwt==1.5.3 # via django-graphql-jwt, django-payments, social-auth-core
pyphen==0.9.4 # via weasyprint
pytest-django==3.1.2
pytest-mock==1.7.0
@@ -94,14 +96,14 @@ requests==2.18.4
rx==1.6.0 # via graphql-core
s3transfer==0.1.12 # via boto3
singledispatch==3.4.0.3 # via graphene-django
-six==1.11.0 # via bleach, cryptography, django-templated-email, elasticsearch-dsl, faker, freezegun, graphene, graphene-django, graphql-core, graphql-relay, html5lib, promise, purl, pytest, python-dateutil, singledispatch, social-auth-app-django, social-auth-core, vcrpy
+six==1.11.0 # via bleach, cryptography, django-environ, django-templated-email, elasticsearch-dsl, faker, freezegun, graphene, graphene-django, graphql-core, graphql-relay, html5lib, promise, purl, pytest, python-dateutil, singledispatch, social-auth-app-django, social-auth-core, vcrpy
social-auth-app-django==2.1.0
social-auth-core==1.6.0 # via social-auth-app-django
stripe==1.77.1 # via django-payments
suds-jurko==0.6 # via django-payments
text-unidecode==1.1
tinycss2==0.6.1 # via cairosvg, cssselect2, weasyprint
-typing==3.6.2 # via prices, promise
+typing==3.6.2 # via google-measurement-protocol, prices, promise
urllib3==1.22 # via elasticsearch, requests
uwsgi==2.0.15 ; platform_system != "Windows"
vcrpy==1.11.1
diff --git a/saleor/dashboard/graphql/api.py b/saleor/dashboard/graphql/api.py
index 30e9440820..6e75f9be5f 100644
--- a/saleor/dashboard/graphql/api.py
+++ b/saleor/dashboard/graphql/api.py
@@ -1,6 +1,7 @@
import graphene
from graphene_django.debug import DjangoDebug
from graphene_django.filter import DjangoFilterConnectionField
+from graphql_extensions.auth.decorators import staff_member_required
from ...graphql.core.filters import DistinctFilterSet
from ...graphql.page.types import Page
@@ -23,15 +24,19 @@ class Query(graphene.ObjectType):
pages = DjangoFilterConnectionField(
Page, filterset_class=DistinctFilterSet)
+ @staff_member_required
def resolve_category(self, info, id):
return get_node(info, id, only_type=Category)
+ @staff_member_required
def resolve_categories(self, info, level=None, **kwargs):
return resolve_categories(info, level)
+ @staff_member_required
def resolve_page(self, info, id):
return get_node(info, id, only_type=Page)
+ @staff_member_required
def resolve_pages(self, info):
return resolve_all_pages()
diff --git a/saleor/dashboard/graphql/mutations.py b/saleor/dashboard/graphql/mutations.py
index a8d9793a69..398fbd7c08 100644
--- a/saleor/dashboard/graphql/mutations.py
+++ b/saleor/dashboard/graphql/mutations.py
@@ -5,6 +5,7 @@ from django.core.exceptions import ImproperlyConfigured
from graphene.types.mutation import MutationOptions
from graphene_django.form_converter import convert_form_field
from graphene_django.registry import get_global_registry
+from graphql_extensions.auth.decorators import staff_member_required
from ...graphql.core.types import Error
from ...graphql.utils import get_node
@@ -172,3 +173,11 @@ class ModelFormUpdateMutation(ModelFormMutation):
kwargs['instance'] = instance
return kwargs
+
+
+class StaffMemberRequiredMutation(graphene.Mutation):
+
+ @classmethod
+ @staff_member_required
+ def mutate(cls, root, info, *args, **kwargs):
+ return super().mutate(root, info, *args, **kwargs)
diff --git a/saleor/dashboard/graphql/page/mutations.py b/saleor/dashboard/graphql/page/mutations.py
index b3ef3e80ed..40fce14695 100644
--- a/saleor/dashboard/graphql/page/mutations.py
+++ b/saleor/dashboard/graphql/page/mutations.py
@@ -3,20 +3,20 @@ import graphene
from ....page import models
from ...page.forms import PageForm
from ..mutations import (
- ModelDeleteMutation, ModelFormMutation, ModelFormUpdateMutation)
+ ModelDeleteMutation, ModelFormMutation, ModelFormUpdateMutation,
+ StaffMemberRequiredMutation)
-class PageCreate(ModelFormMutation):
+class PageCreate(StaffMemberRequiredMutation, ModelFormMutation):
class Meta:
form_class = PageForm
-class PageUpdate(ModelFormUpdateMutation):
+class PageUpdate(StaffMemberRequiredMutation, ModelFormUpdateMutation):
class Meta:
form_class = PageForm
-class PageDelete(ModelDeleteMutation):
-
+class PageDelete(StaffMemberRequiredMutation, ModelDeleteMutation):
class Meta:
model = models.Page
diff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.py
index 98b277ebaa..8da625a15b 100644
--- a/saleor/dashboard/graphql/product/mutations.py
+++ b/saleor/dashboard/graphql/product/mutations.py
@@ -1,14 +1,16 @@
import graphene
+from graphql_extensions.auth.decorators import staff_member_required
from ....graphql.product.types import Category
from ....graphql.utils import get_node
from ....product import models
from ...category.forms import CategoryForm
from ..mutations import (
- ModelDeleteMutation, ModelFormMutation, ModelFormUpdateMutation)
+ BaseMutation, ModelDeleteMutation, ModelFormMutation,
+ ModelFormUpdateMutation, StaffMemberRequiredMutation)
-class CategoryCreateMutation(ModelFormMutation):
+class CategoryCreateMutation(StaffMemberRequiredMutation, ModelFormMutation):
class Arguments:
parent_id = graphene.ID()
@@ -27,7 +29,8 @@ class CategoryCreateMutation(ModelFormMutation):
return kwargs
-class CategoryUpdateMutation(ModelFormUpdateMutation):
+class CategoryUpdateMutation(
+ StaffMemberRequiredMutation, ModelFormUpdateMutation):
class Meta:
form_class = CategoryForm
@@ -38,7 +41,6 @@ class CategoryUpdateMutation(ModelFormUpdateMutation):
return kwargs
-class CategoryDelete(ModelDeleteMutation):
-
+class CategoryDelete(StaffMemberRequiredMutation, ModelDeleteMutation):
class Meta:
model = models.Category
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 20a9342ec9..6a52521d30 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -1,4 +1,5 @@
import graphene
+import graphql_jwt
from graphene_django.debug import DjangoDebug
from graphene_django.filter import DjangoFilterConnectionField
@@ -57,4 +58,10 @@ class Query(graphene.ObjectType):
return resolve_attributes(in_category, info)
-schema = graphene.Schema(Query)
+class Mutations(graphene.ObjectType):
+ token_auth = graphql_jwt.ObtainJSONWebToken.Field()
+ verify_token = graphql_jwt.Verify.Field()
+ refresh_token = graphql_jwt.Refresh.Field()
+
+
+schema = graphene.Schema(Query, Mutations)
diff --git a/saleor/settings.py b/saleor/settings.py
index 75ab9bca04..91bd4ec5d8 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -131,6 +131,7 @@ MIDDLEWARE = [
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django_babel.middleware.LocaleMiddleware',
+ 'graphql_jwt.middleware.JSONWebTokenMiddleware',
'saleor.core.middleware.discounts',
'saleor.core.middleware.google_analytics',
'saleor.core.middleware.country',
@@ -363,6 +364,7 @@ GRAPHENE = {'MIDDLEWARE': ['graphene_django.debug.DjangoDebugMiddleware']}
AUTHENTICATION_BACKENDS = [
'saleor.account.backends.facebook.CustomFacebookOAuth2',
'saleor.account.backends.google.CustomGoogleOAuth2',
+ 'graphql_jwt.backends.JSONWebTokenBackend',
'django.contrib.auth.backends.ModelBackend']
SOCIAL_AUTH_PIPELINE = [