Skip to content

Commit fd1f317

Browse files
committed
client_id is now passed to getDefaultScope().
1 parent 0cd195d commit fd1f317

File tree

7 files changed

+25
-7
lines changed

7 files changed

+25
-7
lines changed

src/OAuth2/Controller/AuthorizeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function validateAuthorizeRequest(RequestInterface $request, ResponseInte
145145
$response_type = $request->query('response_type');
146146
$state = $request->query('state');
147147
if (!$scope = $this->scopeUtil->getScopeFromRequest($request)) {
148-
$scope = $this->scopeUtil->getDefaultScope();
148+
$scope = $this->scopeUtil->getDefaultScope($client_id);
149149
}
150150

151151
// type and client_id are required

src/OAuth2/Controller/TokenController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function grantAccessToken(RequestInterface $request, ResponseInterface $r
125125
*/
126126
$availableScope = $grantType->getScope();
127127
if (!$requestedScope = $this->scopeUtil->getScopeFromRequest($request)) {
128-
$requestedScope = $availableScope ? $availableScope : $this->scopeUtil->getDefaultScope();
128+
$requestedScope = $availableScope ? $availableScope : $this->scopeUtil->getDefaultScope($clientId);
129129
}
130130

131131
if (($requestedScope && !$this->scopeUtil->scopeExists($requestedScope, $clientId))

src/OAuth2/Scope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function getScopeFromRequest(RequestInterface $request)
7272
return $request->request('scope', $request->query('scope'));
7373
}
7474

75-
public function getDefaultScope()
75+
public function getDefaultScope($client_id = null)
7676
{
77-
return $this->storage->getDefaultScope();
77+
return $this->storage->getDefaultScope($client_id);
7878
}
7979
}

src/OAuth2/Storage/Memory.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Memory implements AuthorizationCodeInterface,
2626
private $jwt;
2727
private $supportedScopes;
2828
private $clientSupportedScopes;
29+
private $clientDefaultScopes;
2930
private $defaultScope;
3031

3132
public function __construct($params = array())
@@ -39,6 +40,7 @@ public function __construct($params = array())
3940
'jwt' => array(),
4041
'default_scope' => null,
4142
'client_supported_scopes' => array(),
43+
'client_default_scopes' => array(),
4244
'supported_scopes' => array(),
4345
), $params);
4446

@@ -50,6 +52,7 @@ public function __construct($params = array())
5052
$this->jwt = $params['jwt'];
5153
$this->supportedScopes = $params['supported_scopes'];
5254
$this->clientSupportedScopes = $params['client_supported_scopes'];
55+
$this->clientDefaultScopes = $params['client_default_scopes'];
5356
$this->defaultScope = $params['default_scope'];
5457
}
5558

@@ -171,9 +174,13 @@ public function scopeExists($scope, $client_id = null)
171174
return (count(array_diff($scope, $allowedScopes)) == 0);
172175
}
173176

174-
public function getDefaultScope()
177+
public function getDefaultScope($client_id = null)
175178
{
176-
return $this->defaultScope;
179+
if ($client_id && array_key_exists($client_id, $this->clientDefaultScopes)) {
180+
return implode(' ', $this->clientDefaultScopes[$client_id]);
181+
}else{
182+
return $this->defaultScope;
183+
}
177184
}
178185

179186
/*JWTBearerInterface */

src/OAuth2/Storage/ScopeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ public function scopeExists($scope, $client_id = null);
4141
* ex:
4242
* null
4343
*/
44-
public function getDefaultScope();
44+
public function getDefaultScope($client_id = null);
4545
}

test/OAuth2/ScopeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace OAuth2;
44

55
use OAuth2\Storage\Memory;
6+
use OAuth2\Storage\Bootstrap;
67

78
class ScopeTest extends \PHPUnit_Framework_TestCase
89
{
@@ -38,5 +39,11 @@ public function testScopeStorage()
3839

3940
$this->assertEquals($scopeUtil->getDefaultScope(), 'base');
4041
$this->assertTrue($scopeUtil->scopeExists('only-this-one', 'client_id'));
42+
43+
//Test getting default scopes with a client_id
44+
$memoryStorage = Bootstrap::getInstance()->getMemoryStorage();
45+
$scopeUtil = new Scope($memoryStorage);
46+
$this->assertEquals($scopeUtil->getDefaultScope('Test Default Scope Client ID'), 'clientscope1 clientscope2');
47+
$this->assertEquals($scopeUtil->getDefaultScope('Test Default Scope Client ID 2'), 'clientscope3');
4148
}
4249
}

test/config/storage.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@
109109
"Test Client ID" : ["clientscope1", "clientscope2"],
110110
"Test Client ID 2" : ["clientscope3"]
111111
},
112+
"client_default_scopes" : {
113+
"Test Default Scope Client ID" : ["clientscope1", "clientscope2"],
114+
"Test Default Scope Client ID 2" : ["clientscope3"]
115+
},
112116
"supported_scopes" : [
113117
"scope1",
114118
"scope2",

0 commit comments

Comments
 (0)