Skip to content

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

Authenticate the request in the external auth mutations (#19747)

Lukasz Ostrowskida32dce9e048
diff --git a/saleor/graphql/account/mutations/authentication/external_obtain_access_tokens.py b/saleor/graphql/account/mutations/authentication/external_obtain_access_tokens.py
index 9b3fe25697..0e0b2b0549 100644
--- a/saleor/graphql/account/mutations/authentication/external_obtain_access_tokens.py
+++ b/saleor/graphql/account/mutations/authentication/external_obtain_access_tokens.py
@@ -50,6 +50,7 @@ class ExternalObtainAccessTokens(BaseMutation):
if access_tokens_response.user and access_tokens_response.user.id:
user = access_tokens_response.user
+ info.context.user = user
info.context._cached_user = user
update_user_last_login_if_required(user)
diff --git a/saleor/graphql/account/mutations/authentication/external_refresh.py b/saleor/graphql/account/mutations/authentication/external_refresh.py
index 85d2900cb4..97165dec5d 100644
--- a/saleor/graphql/account/mutations/authentication/external_refresh.py
+++ b/saleor/graphql/account/mutations/authentication/external_refresh.py
@@ -48,6 +48,7 @@ class ExternalRefresh(BaseMutation):
if access_tokens_response.user and access_tokens_response.user.id:
user = access_tokens_response.user
+ info.context.user = user
info.context._cached_user = user
update_user_last_login_if_required(user)
diff --git a/saleor/graphql/account/tests/mutations/authentication/test_external_obtain_access_tokens.py b/saleor/graphql/account/tests/mutations/authentication/test_external_obtain_access_tokens.py
index e590e5c3d0..de9e0c2e5f 100644
--- a/saleor/graphql/account/tests/mutations/authentication/test_external_obtain_access_tokens.py
+++ b/saleor/graphql/account/tests/mutations/authentication/test_external_obtain_access_tokens.py
@@ -165,3 +165,67 @@ def test_external_obtain_access_tokens_do_update_last_login_when_out_of_threshol
assert customer_user.updated_at == time_out_of_threshold
assert customer_user.last_login == time_out_of_threshold
assert mock_refresh_token_middleware.called
+
+
+MUTATION_EXTERNAL_OBTAIN_ACCESS_TOKENS_ACCESSIBLE_CHANNELS = """
+ mutation externalObtainAccessTokens($pluginId: String!, $input: JSONString!){
+ externalObtainAccessTokens(pluginId: $pluginId, input: $input){
+ user{
+ email
+ accessibleChannels{
+ slug
+ isActive
+ }
+ }
+ errors{
+ field
+ message
+ }
+ }
+}
+"""
+
+
+@patch("saleor.core.middleware.jwt_decode_with_exception_handler")
+def test_external_obtain_access_tokens_resolves_staff_only_user_fields(
+ mock_refresh_token_middleware,
+ api_client,
+ staff_users,
+ permission_group_all_perms_channel_USD_only,
+ channel_USD,
+ monkeypatch,
+):
+ """The returned user must authenticate the request for the rest of the response.
+
+ `Channel.slug`/`isActive` are guarded by `AUTHENTICATED_STAFF_USER`, so without
+ `info.context.user` being set they raise `PermissionDenied` and fail the whole
+ mutation.
+ """
+ # given
+ user = staff_users[1]
+ assert user.is_staff is True
+ mocked_plugin_fun = Mock(
+ return_value=ExternalAccessTokens(token="token1", user=user)
+ )
+ monkeypatch.setattr(
+ "saleor.plugins.manager.PluginsManager.external_obtain_access_tokens",
+ mocked_plugin_fun,
+ )
+ variables = {
+ "pluginId": "pluginId1",
+ "input": json.dumps({"code": "ABCD", "state": "state-data"}),
+ }
+
+ # when
+ response = api_client.post_graphql(
+ MUTATION_EXTERNAL_OBTAIN_ACCESS_TOKENS_ACCESSIBLE_CHANNELS, variables
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["externalObtainAccessTokens"]
+ assert data["errors"] == []
+ assert data["user"]["email"] == user.email
+ assert data["user"]["accessibleChannels"] == [
+ {"slug": channel_USD.slug, "isActive": channel_USD.is_active}
+ ]
diff --git a/saleor/graphql/account/tests/mutations/authentication/test_external_refresh.py b/saleor/graphql/account/tests/mutations/authentication/test_external_refresh.py
index 1f0d4d5451..51c13fe5fb 100644
--- a/saleor/graphql/account/tests/mutations/authentication/test_external_refresh.py
+++ b/saleor/graphql/account/tests/mutations/authentication/test_external_refresh.py
@@ -156,3 +156,63 @@ def test_external_refresh_do_update_last_login_when_out_of_threshold(
assert customer_user.updated_at == time_out_of_threshold
assert customer_user.last_login == time_out_of_threshold
assert mock_refresh_token_middleware.called
+
+
+MUTATION_EXTERNAL_REFRESH_ACCESSIBLE_CHANNELS = """
+ mutation externalRefresh($pluginId: String!, $input: JSONString!){
+ externalRefresh(pluginId: $pluginId, input: $input){
+ user{
+ email
+ accessibleChannels{
+ slug
+ isActive
+ }
+ }
+ errors{
+ field
+ message
+ }
+ }
+}
+"""
+
+
+@patch("saleor.core.middleware.jwt_decode_with_exception_handler")
+def test_external_refresh_resolves_staff_only_user_fields(
+ mock_refresh_token_middleware,
+ api_client,
+ staff_users,
+ permission_group_all_perms_channel_USD_only,
+ channel_USD,
+ monkeypatch,
+):
+ """The returned user must authenticate the request for the rest of the response.
+
+ `Channel.slug`/`isActive` are guarded by `AUTHENTICATED_STAFF_USER`, so without
+ `info.context.user` being set they raise `PermissionDenied` and fail the whole
+ mutation.
+ """
+ # given
+ user = staff_users[1]
+ assert user.is_staff is True
+ mocked_plugin_fun = Mock(
+ return_value=ExternalAccessTokens(token="token1", user=user)
+ )
+ monkeypatch.setattr(
+ "saleor.plugins.manager.PluginsManager.external_refresh", mocked_plugin_fun
+ )
+ variables = {"pluginId": "pluginId1", "input": json.dumps({"refreshToken": "ABCD"})}
+
+ # when
+ response = api_client.post_graphql(
+ MUTATION_EXTERNAL_REFRESH_ACCESSIBLE_CHANNELS, variables
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["externalRefresh"]
+ assert data["errors"] == []
+ assert data["user"]["email"] == user.email
+ assert data["user"]["accessibleChannels"] == [
+ {"slug": channel_USD.slug, "isActive": channel_USD.is_active}
+ ]