Skip to content

Commit ebd3ef3

Browse files
committed
fixes bshaffer#331 - when you set one of client or client_credentials storage explicitly, you set the other (by default and if possible)
1 parent 6513372 commit ebd3ef3

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/OAuth2/Server.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,17 @@ public function addStorage($storage, $key = null)
322322
throw new \InvalidArgumentException(sprintf('storage of type "%s" must implement interface "%s"', $key, $this->storageMap[$key]));
323323
}
324324
$this->storages[$key] = $storage;
325+
326+
// special logic to handle "client" and "client_credentials" strangeness
327+
if ($key === 'client' && !isset($this->storages['client_credentials'])) {
328+
if ($storage instanceof \OAuth2\Storage\ClientCredentialsInterface) {
329+
$this->storages['client_credentials'] = $storage;
330+
}
331+
} elseif ($key === 'client_credentials' && !isset($this->storages['client'])) {
332+
if ($storage instanceof \OAuth2\Storage\ClientInterface) {
333+
$this->storages['client'] = $storage;
334+
}
335+
}
325336
} elseif (!is_null($key) && !is_numeric($key)) {
326337
throw new \InvalidArgumentException(sprintf('unknown storage key "%s", must be one of [%s]', $key, implode(', ', array_keys($this->storageMap))));
327338
} else {

test/OAuth2/ServerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,48 @@ public function testAddingStorageWithValidKeyOnlySetsThatKey()
187187
$this->assertFalse(isset($storages['authorization_code']));
188188
}
189189

190+
public function testAddingClientStorageSetsClientCredentialsStorageByDefault()
191+
{
192+
$server = new Server();
193+
$memory = $this->getMock('OAuth2\Storage\Memory');
194+
$server->addStorage($memory, 'client');
195+
196+
$client_credentials = $server->getStorage('client_credentials');
197+
198+
$this->assertNotNull($client_credentials);
199+
$this->assertEquals($client_credentials, $memory);
200+
}
201+
202+
public function testAddingClientCredentialsStorageSetsClientStorageByDefault()
203+
{
204+
$server = new Server();
205+
$memory = $this->getMock('OAuth2\Storage\Memory');
206+
$server->addStorage($memory, 'client_credentials');
207+
208+
$client = $server->getStorage('client');
209+
210+
$this->assertNotNull($client);
211+
$this->assertEquals($client, $memory);
212+
}
213+
214+
public function testSettingClientStorageByDefaultDoesNotOverrideSetStorage()
215+
{
216+
$server = new Server();
217+
$pdo = $this->getMockBuilder('OAuth2\Storage\Pdo')
218+
->disableOriginalConstructor()->getMock();
219+
220+
$memory = $this->getMock('OAuth2\Storage\Memory');
221+
222+
$server->addStorage($pdo, 'client');
223+
$server->addStorage($memory, 'client_credentials');
224+
225+
$client = $server->getStorage('client');
226+
$client_credentials = $server->getStorage('client_credentials');
227+
228+
$this->assertEquals($client, $pdo);
229+
$this->assertEquals($client_credentials, $memory);
230+
}
231+
190232
public function testAddingResponseType()
191233
{
192234
$storage = $this->getMock('OAuth2\Storage\Memory');

0 commit comments

Comments
 (0)