Skip to content

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

Handle error raised by 0Auth when fetching token (#12672)

Iga Karbowiakf92751595498
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3f9f95777..16f66d9a72 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Remove default `EMAIL_URL` value pointing to console output; from now on EMAIL_URL has to be set explicitly - #12580 by @maarcingebala
- Fix sending `product_created` event in `ProductBulkCreate` mutation - #12605 by @SzymJ
- Add `ORDER_REFUNDED`, `ORDER_FULLY_REFUNDED`, `ORDER_PAID` webhooks - #12533 by @korycins
+- Handle error raised by 0Auth when fetching token - #12672 by @IKarbowiakg
# 3.13.0
diff --git a/saleor/plugins/openid_connect/plugin.py b/saleor/plugins/openid_connect/plugin.py
index dbb60a53c5..91ff4858fc 100644
--- a/saleor/plugins/openid_connect/plugin.py
+++ b/saleor/plugins/openid_connect/plugin.py
@@ -278,9 +278,18 @@ class OpenIDConnectPlugin(BasePlugin):
{"code": ValidationError(msg, code=PluginErrorCode.INVALID.value)}
)
- token_data = self.oauth.fetch_token(
- self.config.token_url, code=code, redirect_uri=redirect_uri
- )
+ try:
+ token_data = self.oauth.fetch_token(
+ self.config.token_url, code=code, redirect_uri=redirect_uri
+ )
+ except AuthlibBaseError as error:
+ raise ValidationError(
+ {
+ "code": ValidationError(
+ error.description, code=PluginErrorCode.INVALID.value
+ )
+ }
+ )
parsed_id_token = get_parsed_id_token(
token_data, self.config.json_web_key_set_url
diff --git a/saleor/plugins/openid_connect/tests/test_plugin.py b/saleor/plugins/openid_connect/tests/test_plugin.py
index 88ea489a8e..37d8f5e522 100644
--- a/saleor/plugins/openid_connect/tests/test_plugin.py
+++ b/saleor/plugins/openid_connect/tests/test_plugin.py
@@ -2,6 +2,7 @@ from unittest.mock import MagicMock, Mock
from urllib.parse import parse_qs, urlparse
import pytest
+from authlib.integrations.base_client.errors import OAuthError
from authlib.jose.errors import JoseError
from django.core import signing
from django.core.exceptions import ValidationError
@@ -662,6 +663,36 @@ def test_external_obtain_access_tokens_missing_redirect_uri_in_state(openid_plug
)
+def test_external_obtain_access_tokens_fetch_token_raises_error(
+ openid_plugin, monkeypatch, rf, id_token, id_payload
+):
+ # given
+ mocked_jwt_validator = MagicMock()
+ mocked_jwt_validator.__getitem__.side_effect = id_payload.__getitem__
+ mocked_jwt_validator.get.side_effect = id_payload.get
+
+ monkeypatch.setattr(
+ "saleor.plugins.openid_connect.utils.get_decoded_token",
+ Mock(return_value=mocked_jwt_validator),
+ )
+ plugin = openid_plugin(use_oauth_scope_permissions=True)
+
+ monkeypatch.setattr(
+ "saleor.plugins.openid_connect.plugin.OAuth2Session.fetch_token",
+ Mock(side_effect=OAuthError()),
+ )
+
+ redirect_uri = "http://localhost:3000/used-logged-in"
+ state = signing.dumps({"redirectUri": redirect_uri})
+ code = "oauth-code"
+
+ # when & then
+ with pytest.raises(ValidationError):
+ plugin.external_obtain_access_tokens(
+ {"state": state, "code": code}, rf.request(), previous_value=None
+ )
+
+
test_url = "http://saleor.io/"