Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add mutations to authenticate API request with JWT; restrict dashboard API access
Marcin Gębalad319bde64385
diff --git a/requirements.in b/requirements.inindex 91c19a57ef..c96dea7f96 100644--- a/requirements.in+++ b/requirements.in@@ -11,6 +11,7 @@ django-cache-url>=1.0.0django-countries>=5.2django-filter==1.1django-fsm>=2.6.0+django-graphql-jwt==0.1.8django-impersonate==1.3django-mptt>=0.7.1django-payments>=0.12.1diff --git a/requirements.txt b/requirements.txtindex 1f70a97dd4..8e6ef14673 100644--- a/requirements.txt+++ b/requirements.txt@@ -30,8 +30,10 @@ django-cache-url==2.0.0django-celery-results==1.0.1django-countries==5.2django-elasticsearch-dsl==0.4.4+django-environ==0.4.4 # via django-graphql-jwtdjango-filter==1.1django-fsm==2.6.0+django-graphql-jwt==0.1.8django-impersonate==1.3django-js-asset==1.0.0 # via django-mpttdjango-mptt==0.9.0@@ -54,7 +56,7 @@ freezegun==0.3.9google-i18n-address==2.2.2google-measurement-protocol==1.0.0graphene-django==2.0.0-graphene==2.0.1 # via graphene-django+graphene==2.0.1 # via django-graphql-jwt, graphene-djangographql-core==2.0 # via graphene, graphql-relaygraphql-relay==0.4.5 # via graphenehtml5lib==1.0.1 # via bleach, weasyprint@@ -78,7 +80,7 @@ psycopg2==2.7.4purl==1.3.1py==1.5.2 # via pytestpycparser==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-corepyphen==0.9.4 # via weasyprintpytest-django==3.1.2pytest-mock==1.7.0@@ -94,14 +96,14 @@ requests==2.18.4rx==1.6.0 # via graphql-cores3transfer==0.1.12 # via boto3singledispatch==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, vcrpysocial-auth-app-django==2.1.0social-auth-core==1.6.0 # via social-auth-app-djangostripe==1.77.1 # via django-paymentssuds-jurko==0.6 # via django-paymentstext-unidecode==1.1tinycss2==0.6.1 # via cairosvg, cssselect2, weasyprint-typing==3.6.2 # via prices, promise+typing==3.6.2 # via google-measurement-protocol, prices, promiseurllib3==1.22 # via elasticsearch, requestsuwsgi==2.0.15 ; platform_system != "Windows"vcrpy==1.11.1diff --git a/saleor/dashboard/graphql/api.py b/saleor/dashboard/graphql/api.pyindex 30e9440820..6e75f9be5f 100644--- a/saleor/dashboard/graphql/api.py+++ b/saleor/dashboard/graphql/api.py@@ -1,6 +1,7 @@import graphenefrom graphene_django.debug import DjangoDebugfrom graphene_django.filter import DjangoFilterConnectionField+from graphql_extensions.auth.decorators import staff_member_requiredfrom ...graphql.core.filters import DistinctFilterSetfrom ...graphql.page.types import Page@@ -23,15 +24,19 @@ class Query(graphene.ObjectType):pages = DjangoFilterConnectionField(Page, filterset_class=DistinctFilterSet)+ @staff_member_requireddef resolve_category(self, info, id):return get_node(info, id, only_type=Category)+ @staff_member_requireddef resolve_categories(self, info, level=None, **kwargs):return resolve_categories(info, level)+ @staff_member_requireddef resolve_page(self, info, id):return get_node(info, id, only_type=Page)+ @staff_member_requireddef resolve_pages(self, info):return resolve_all_pages()diff --git a/saleor/dashboard/graphql/mutations.py b/saleor/dashboard/graphql/mutations.pyindex a8d9793a69..398fbd7c08 100644--- a/saleor/dashboard/graphql/mutations.py+++ b/saleor/dashboard/graphql/mutations.py@@ -5,6 +5,7 @@ from django.core.exceptions import ImproperlyConfiguredfrom graphene.types.mutation import MutationOptionsfrom graphene_django.form_converter import convert_form_fieldfrom graphene_django.registry import get_global_registry+from graphql_extensions.auth.decorators import staff_member_requiredfrom ...graphql.core.types import Errorfrom ...graphql.utils import get_node@@ -172,3 +173,11 @@ class ModelFormUpdateMutation(ModelFormMutation):kwargs['instance'] = instancereturn 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.pyindex b3ef3e80ed..40fce14695 100644--- a/saleor/dashboard/graphql/page/mutations.py+++ b/saleor/dashboard/graphql/page/mutations.py@@ -3,20 +3,20 @@ import graphenefrom ....page import modelsfrom ...page.forms import PageFormfrom ..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.Pagediff --git a/saleor/dashboard/graphql/product/mutations.py b/saleor/dashboard/graphql/product/mutations.pyindex 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_requiredfrom ....graphql.product.types import Categoryfrom ....graphql.utils import get_nodefrom ....product import modelsfrom ...category.forms import CategoryFormfrom ..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.Categorydiff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex 20a9342ec9..6a52521d30 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -1,4 +1,5 @@import graphene+import graphql_jwtfrom graphene_django.debug import DjangoDebugfrom 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.pyindex 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 = [