commercetools-php-sdk icon indicating copy to clipboard operation
commercetools-php-sdk copied to clipboard

PHP: function setScope in src/Core/Config.php does not strip leading and trailing quotes/double quotes

Open richardwolterink opened this issue 4 years ago • 1 comments

Describe the bug If a scope has a leading and trailing double quote, because you have more than one scope entry, the setScope function does not strip the leading and trailing double quote.

To Reproduce I use Symfony 4.4.27 If you have setup your env (.env.dist) files the following way:

COMMERCETOOLS_CLIENT_ID=adkjhadkjhakfds COMMERCETOOLS_CLIENT_SECRET=jdkfjlakdsjfladkjfaldskj COMMERCETOOLS_PROJECT=someproject COMMERCETOOLS_SCOPE="manage_orders:someproject view_orders:someproject manage_payments:someproject manage_order_edits:someproject view_customers:someproject"

Notice the double quotes in the COMMERCETOOLS_SCOPE

If now in your services.yaml, you instantiate the client as follows:

commercetools: api: clients: default_client: client_id: "%env(COMMERCETOOLS_CLIENT_ID)%" client_secret: "%env(COMMERCETOOLS_CLIENT_SECRET)%" project: "%env(COMMERCETOOLS_PROJECT)%" scope: "%env(COMMERCETOOLS_SCOPE)%"

If you now do a getScope on the config of the client, you will see:

"manage_orders:someproject view_orders:someproject manage_payments:someproject manage_order_edits:someproject view_customers:someproject"

Expected behavior A getScope on the config of the client, should result in: manage_orders:someproject view_orders:someproject manage_payments:someproject manage_order_edits:someproject view_customers:someproject

Screenshots/Code snippet In the constructor of the service which uses the CommerceTools client, I do the following to overcome this:

$config = $this->client->getConfig(); $config->setScope(str_replace('"', '', $config->getScope())); $this->client->setConfig($config);

In my opinion this should be done in the setScope function because if you declare an environment variable which contains spaces, in an environment file, it should be encapsulated in double or single quotes.

Stack information (please complete the following information):

  • PHP: [e.g. 7.2]
  • SDK: [e.g. v2.16]

Additional context Add any other context about the problem here.

richardwolterink avatar Aug 25 '21 15:08 richardwolterink

I'm actually unsure if it should be solved inside the SDK. The Config object already tries to deal with a lot of different combinations for the scope string. See https://github.com/commercetools/commercetools-php-sdk/blob/a1f12b67811808bf73b7ec9d405fdaabb50670e3/tests/unit/ConfigTest.php#L167-L224

Also the double quotes seems to be coming from symfony itself. And there are so many different ways to define a string in YAML. Did you btw tried to surround the %env with single quotes? And did you build your own client factory or are you using the symfony bundle?

Btw you can also leave out the scope at all as the auth endpoint will create a token with the default scopes of the OAuth client.

jenschude avatar Aug 27 '21 09:08 jenschude