Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 7f1ddec

Browse files
author
Jens Schulze
committed
feat(OAuth): Client scope can be left empty
Closes #291
1 parent 1c22189 commit 7f1ddec

File tree

5 files changed

+92
-8
lines changed

5 files changed

+92
-8
lines changed

src/Client/OAuth/Manager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ public function refreshToken()
106106
{
107107
$scope = $this->getConfig()->getScope();
108108
$grantType = $this->getConfig()->getGrantType();
109-
$data = [Config::SCOPE => $scope, Config::GRANT_TYPE => $grantType];
109+
$data = [Config::GRANT_TYPE => $grantType];
110+
if (!empty($scope)) {
111+
$data[Config::SCOPE] = $scope;
112+
}
110113

111114
switch ($grantType) {
112115
case Config::GRANT_TYPE_PASSWORD:

src/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ public function getScope()
268268
*/
269269
public function setScope($scope)
270270
{
271+
if (empty($scope)) {
272+
$scope = [];
273+
}
271274
if (!is_array($scope)) {
272275
$scope = explode(' ', $scope);
273276
}

tests/integration/ManagerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
7+
namespace Commercetools\Core;
8+
9+
10+
use Commercetools\Core\Client\OAuth\Manager;
11+
12+
class ManagerTest extends ApiTestCase
13+
{
14+
public function testEmptyScope()
15+
{
16+
$config = $this->getClientConfig('manage_project');
17+
$config->setScope('');
18+
$manager = new Manager($config);
19+
20+
$token = $manager->refreshToken();
21+
$this->assertEmpty($config->getScope());
22+
$this->assertNotEmpty($token->getScope());
23+
$this->assertNotEmpty($token->getToken());
24+
$this->assertContains('manage_project', $token->getScope());
25+
}
26+
}

tests/unit/Client/OAuth/ManagerTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testScope()
103103
Config::CLIENT_SECRET => 'secret',
104104
Config::SCOPE => 'test_scope',
105105
Config::OAUTH_URL => 'oauthUrl',
106-
Config::PROJECT => 'project',
106+
Config::PROJECT => 'testScope',
107107
Config::API_URL => 'apiUrl'
108108
]);
109109
$manager = $this->getManager(
@@ -112,13 +112,42 @@ public function testScope()
112112
"access_token" => "myToken",
113113
"token_type" => "Bearer",
114114
"expires_in" => 1000,
115-
"scope" => "test_scope:project"
115+
"scope" => "test_scope:testScope"
116116
],
117117
200,
118118
true
119119
);
120-
$this->assertInstanceOf('\Commercetools\Core\Client\OAuth\Token', $manager->getToken());
121-
$this->assertSame($manager->getConfig()->getScope(), $manager->getToken()->getScope());
120+
$token = $manager->getToken();
121+
$this->assertInstanceOf('\Commercetools\Core\Client\OAuth\Token', $token);
122+
$this->assertSame($manager->getConfig()->getScope(), $token->getScope());
123+
}
124+
125+
public function testEmptyScope()
126+
{
127+
$config = Config::fromArray([
128+
Config::CLIENT_ID => 'id',
129+
Config::CLIENT_SECRET => 'secret',
130+
Config::SCOPE => '',
131+
Config::OAUTH_URL => 'oauthUrl',
132+
Config::PROJECT => 'testEmptyScope',
133+
Config::API_URL => 'apiUrl'
134+
]);
135+
$manager = $this->getManager(
136+
$config,
137+
[
138+
"access_token" => "myToken",
139+
"token_type" => "Bearer",
140+
"expires_in" => 1000,
141+
"scope" => "test_scope:testEmptyScope"
142+
],
143+
200,
144+
true
145+
);
146+
$token = $manager->getToken();
147+
$this->assertEmpty($config->getScope());
148+
$this->assertInstanceOf('\Commercetools\Core\Client\OAuth\Token', $token);
149+
$this->assertSame("test_scope:testEmptyScope", $token->getScope());
150+
$this->assertNotSame($manager->getConfig()->getScope(), $token->getScope());
122151
}
123152

124153
public function testScopes()
@@ -128,7 +157,7 @@ public function testScopes()
128157
Config::CLIENT_SECRET => 'secret',
129158
Config::SCOPE => ['scope1', 'scope2'],
130159
Config::OAUTH_URL => 'oauthUrl',
131-
Config::PROJECT => 'project',
160+
Config::PROJECT => 'testScopes',
132161
Config::API_URL => 'apiUrl'
133162
]);
134163
$manager = $this->getManager(
@@ -137,7 +166,7 @@ public function testScopes()
137166
"access_token" => "myToken",
138167
"token_type" => "Bearer",
139168
"expires_in" => 1000,
140-
"scope" => "scope1:project scope2:project"
169+
"scope" => "scope1:testScopes scope2:testScopes"
141170
],
142171
200,
143172
true
@@ -154,7 +183,7 @@ public function testCache()
154183
"access_token" => "myToken",
155184
"token_type" => "Bearer",
156185
"expires_in" => 1000,
157-
"scope" => "manage_project:project"
186+
"scope" => "manage_project:testCache"
158187
]
159188
);
160189
$manager->getToken(); // first call ensures caching of token

tests/unit/ConfigTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ public function testDefaultScope()
123123
$this->assertSame('manage_project:project', $config->getScope());
124124
}
125125

126+
public function testEmptyStringScope()
127+
{
128+
$config = Config::fromArray($this->getConfig());
129+
$config->setScope('');
130+
$this->assertSame('', $config->getScope());
131+
}
132+
133+
public function testEmptyStringScopeFromArray()
134+
{
135+
$config = $this->getConfig();
136+
$config[Config::SCOPE] = '';
137+
$config = Config::fromArray($config);
138+
$this->assertSame('', $config->getScope());
139+
}
140+
141+
public function testEmptyArrayScope()
142+
{
143+
$config = Config::fromArray($this->getConfig());
144+
$config->setScope([]);
145+
$this->assertSame('', $config->getScope());
146+
}
147+
148+
126149
/**
127150
* @dataProvider getScopes
128151
*/

0 commit comments

Comments
 (0)