Skip to content

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

[SALEOR-6404] Use JWS to sign webhooks with secretKey instead of obscure signature

Tomasz Magulskiac065cdce667
diff --git a/saleor/core/jwt_manager.py b/saleor/core/jwt_manager.py
index 4d7a7b7236..f79fd1e643 100644
--- a/saleor/core/jwt_manager.py
+++ b/saleor/core/jwt_manager.py
@@ -11,6 +11,8 @@ from django.core.exceptions import ImproperlyConfigured
from django.core.management.color import color_style
from django.utils.module_loading import import_string
from jwt.algorithms import RSAAlgorithm
+from jwt import api_jws
+
logger = logging.getLogger(__name__)
@@ -118,6 +120,16 @@ class JWTManager(JWTManagerBase):
payload, cls.get_private_key(), algorithm="RS256", headers={"kid": KID}
)
+ @classmethod
+ def jws_encode(cls, payload: bytes, is_payload_detached: bool = True) -> bytes:
+ return api_jws.encode(
+ payload,
+ key=cls.get_private_key(),
+ algorithm="RS256",
+ headers={"kid": KID},
+ is_payload_detached=is_payload_detached,
+ )
+
@classmethod
def decode(cls, token, verify_expiration: bool = True):
headers = jwt.get_unverified_header(token)
diff --git a/saleor/plugins/webhook/__init__.py b/saleor/plugins/webhook/__init__.py
index 051b9f4b22..835e99af8b 100644
--- a/saleor/plugins/webhook/__init__.py
+++ b/saleor/plugins/webhook/__init__.py
@@ -1,15 +1,11 @@
import hashlib
import hmac
-from jwt.algorithms import get_default_algorithms
-
from ...core.jwt_manager import get_jwt_manager
def signature_for_payload(body: bytes, secret_key):
if secret_key is None:
- algorithm = get_default_algorithms()["RS256"]
- private_key = get_jwt_manager().get_private_key()
- return algorithm.sign(body, private_key).hex()
+ return get_jwt_manager().jws_encode(body)
hash = hmac.new(bytes(secret_key, "utf-8"), body, hashlib.sha256)
return hash.hexdigest()