Skip to content

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

Modify order of auth middleware calls (#7572)

Tomasz Szymańskic8883cd0b6f8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 30bf131515..62a24efcf4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -124,6 +124,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix doubled checkout total price for one line and zero shipping price - #7532 by @IKarbowiak
- Deprecate nested objects in TranslatableContent types - #7522 by @IKarbowiak
- Fix failing account mutations for app - #7569 by @IKarbowiak
+- Modify order of auth middleware calls - #7572 by @tomaszszymanski129
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/middleware.py b/saleor/graphql/middleware.py
index 56b9241135..a971f3b496 100644
--- a/saleor/graphql/middleware.py
+++ b/saleor/graphql/middleware.py
@@ -3,9 +3,10 @@ from typing import Optional
from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth.models import AnonymousUser
+from django.db.models import Exists, OuterRef
from django.utils.functional import SimpleLazyObject
-from ..app.models import App
+from ..app.models import App, AppToken
from ..core.exceptions import ReadOnlyException
from .views import API_PATH, GraphQLView
@@ -28,8 +29,10 @@ class JWTMiddleware:
def get_app(auth_token) -> Optional[App]:
- qs = App.objects.filter(tokens__auth_token=auth_token, is_active=True)
- return qs.first()
+ tokens = AppToken.objects.filter(auth_token=auth_token).values("pk")
+ return App.objects.filter(
+ Exists(tokens.filter(app_id=OuterRef("pk"))), is_active=True
+ ).first()
def app_middleware(next, root, info, **kwargs):
@@ -44,7 +47,7 @@ def app_middleware(next, root, info, **kwargs):
auth = request.META.get(app_auth_header, "").split()
if len(auth) == 2:
auth_prefix, auth_token = auth
- if auth_prefix.lower() == prefix:
+ if len(auth_token) == 30 and auth_prefix.lower() == prefix:
request.app = SimpleLazyObject(lambda: get_app(auth_token))
return next(root, info, **kwargs)
diff --git a/saleor/settings.py b/saleor/settings.py
index 472cc7d8d3..6d68dd4ce1 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -553,8 +553,8 @@ GRAPHENE = {
"RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST": True,
"RELAY_CONNECTION_MAX_LIMIT": 100,
"MIDDLEWARE": [
- "saleor.graphql.middleware.JWTMiddleware",
"saleor.graphql.middleware.app_middleware",
+ "saleor.graphql.middleware.JWTMiddleware",
],
}