Skip to content

Commit

Permalink
fixes #331 - when you set one of client or client_credentials storage…
Browse files Browse the repository at this point in the history
… explicitly, you set the other (by default and if possible)
  • Loading branch information
bshaffer committed Feb 5, 2014
1 parent 6513372 commit ebd3ef3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/OAuth2/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ public function addStorage($storage, $key = null)
throw new \InvalidArgumentException(sprintf('storage of type "%s" must implement interface "%s"', $key, $this->storageMap[$key]));
}
$this->storages[$key] = $storage;

// special logic to handle "client" and "client_credentials" strangeness
if ($key === 'client' && !isset($this->storages['client_credentials'])) {
if ($storage instanceof \OAuth2\Storage\ClientCredentialsInterface) {
$this->storages['client_credentials'] = $storage;
}
} elseif ($key === 'client_credentials' && !isset($this->storages['client'])) {
if ($storage instanceof \OAuth2\Storage\ClientInterface) {
$this->storages['client'] = $storage;
}
}
} elseif (!is_null($key) && !is_numeric($key)) {
throw new \InvalidArgumentException(sprintf('unknown storage key "%s", must be one of [%s]', $key, implode(', ', array_keys($this->storageMap))));
} else {
Expand Down
42 changes: 42 additions & 0 deletions test/OAuth2/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,48 @@ public function testAddingStorageWithValidKeyOnlySetsThatKey()
$this->assertFalse(isset($storages['authorization_code']));
}

public function testAddingClientStorageSetsClientCredentialsStorageByDefault()
{
$server = new Server();
$memory = $this->getMock('OAuth2\Storage\Memory');
$server->addStorage($memory, 'client');

$client_credentials = $server->getStorage('client_credentials');

$this->assertNotNull($client_credentials);
$this->assertEquals($client_credentials, $memory);
}

public function testAddingClientCredentialsStorageSetsClientStorageByDefault()
{
$server = new Server();
$memory = $this->getMock('OAuth2\Storage\Memory');
$server->addStorage($memory, 'client_credentials');

$client = $server->getStorage('client');

$this->assertNotNull($client);
$this->assertEquals($client, $memory);
}

public function testSettingClientStorageByDefaultDoesNotOverrideSetStorage()
{
$server = new Server();
$pdo = $this->getMockBuilder('OAuth2\Storage\Pdo')
->disableOriginalConstructor()->getMock();

$memory = $this->getMock('OAuth2\Storage\Memory');

$server->addStorage($pdo, 'client');
$server->addStorage($memory, 'client_credentials');

$client = $server->getStorage('client');
$client_credentials = $server->getStorage('client_credentials');

$this->assertEquals($client, $pdo);
$this->assertEquals($client_credentials, $memory);
}

public function testAddingResponseType()
{
$storage = $this->getMock('OAuth2\Storage\Memory');
Expand Down

0 comments on commit ebd3ef3

Please sign in to comment.