From dd9793a55bb2d687395299ab0f348dd84c914829 Mon Sep 17 00:00:00 2001 From: Jurgen Van de Moere Date: Thu, 3 Jan 2013 21:28:28 +0100 Subject: [PATCH 01/11] Revert "Added protected contentTypes array and requestHasContentType function" This reverts commit 9dfdefbf4c9d695bc0d2125c30571ae37ff95a89. --- test/Adapter/Http/TestAsset/htbasic.1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Adapter/Http/TestAsset/htbasic.1 b/test/Adapter/Http/TestAsset/htbasic.1 index 37ab404..7f18f96 100644 --- a/test/Adapter/Http/TestAsset/htbasic.1 +++ b/test/Adapter/Http/TestAsset/htbasic.1 @@ -1,3 +1,3 @@ -Bryce:Test Realm:ThisIsNotMyPassword -Mufasa:Test Realm:Circle Of Life -Bad Chars:In:Creds +Bryce:Test Realm:ThisIsNotMyPassword +Mufasa:Test Realm:Circle Of Life +Bad Chars:In:Creds From ea8035b8b9384df4e215f6436043d20674cba5f2 Mon Sep 17 00:00:00 2001 From: Mike Willbanks Date: Sat, 12 Jan 2013 15:20:08 -0800 Subject: [PATCH 02/11] Resolved coding standard violations --- src/Validator/Authentication.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Validator/Authentication.php b/src/Validator/Authentication.php index 5db6db3..fdc29fd 100644 --- a/src/Validator/Authentication.php +++ b/src/Validator/Authentication.php @@ -116,12 +116,13 @@ public function getAdapter() /** * Set Adapter * - * @param Zend\Authentication\Adapter\AdapterInterface $adapter + * @param Zend\Authentication\Adapter\AdapterInterface $adapter * @return Authentication */ public function setAdapter(AdapterInterface $adapter) { $this->adapter = $adapter; + return $this; } @@ -138,12 +139,13 @@ public function getIdentity() /** * Set Identity * - * @param mixed $identity + * @param mixed $identity * @return Authentication */ public function setIdentity($identity) { $this->identity = $identity; + return $this; } @@ -160,12 +162,13 @@ public function getCredential() /** * Set Credential * - * @param mixed $credential + * @param mixed $credential * @return Authentication */ public function setCredential($credential) { $this->credential = $credential; + return $this; } @@ -182,20 +185,21 @@ public function getService() /** * Set Service * - * @param Zend\Authentication\AuthenticationService $service + * @param Zend\Authentication\AuthenticationService $service * @return Authentication */ public function setService(AuthenticationService $service) { $this->service = $service; + return $this; } /** * Is Valid * - * @param mixed $value - * @param array $context + * @param mixed $value + * @param array $context * @return bool */ public function isValid($value = null, $context = null) @@ -224,12 +228,12 @@ public function isValid($value = null, $context = null) } $this->adapter->setIdentity($identity); $this->adapter->setCredential($credential); - + if (!$this->service) { throw new Exception\RuntimeException('AuthenticationService must be set prior to validation'); } $result = $this->service->authenticate($this->adapter); - + if ($result->getCode() != Result::SUCCESS) { switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: @@ -247,8 +251,10 @@ public function isValid($value = null, $context = null) default: $this->error(self::GENERAL); } + return false; } + return true; } } From ed55991549791d84b6467212bd52a77341a450fc Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Tue, 9 Oct 2012 22:13:30 +0200 Subject: [PATCH 03/11] Replaced tabs by spaces. --- src/Storage/Chain.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Storage/Chain.php b/src/Storage/Chain.php index dd5027d..4abc832 100644 --- a/src/Storage/Chain.php +++ b/src/Storage/Chain.php @@ -46,14 +46,14 @@ public function add( StorageInterface $storage, $priority = 1 ) $this->storages->insert($storage, $priority); } - /** - * Loop over the queue of storages until a storage is found that is non-empty. If such - * storage is not found, then this chain storage itself is empty. - * - * In case a non-empty storage is found then this chain storage is also non-empty. Report - * that, but also make sure that all the storages with a higher priorty that are empty - * are filled. - * + /** + * Loop over the queue of storages until a storage is found that is non-empty. If such + * storage is not found, then this chain storage itself is empty. + * + * In case a non-empty storage is found then this chain storage is also non-empty. Report + * that, but also make sure that all the storages with a higher priorty that are empty + * are filled. + * * @see StorageInterface::isEmpty() */ public function isEmpty() @@ -81,10 +81,10 @@ public function isEmpty() return true; } - /** - * If the chain is non-empty then the storage with the top priority is guaranteed to be - * filled. Return its value. - * + /** + * If the chain is non-empty then the storage with the top priority is guaranteed to be + * filled. Return its value. + * * @see StorageInterface::read() */ public function read() @@ -92,9 +92,9 @@ public function read() return $this->storages->top()->read(); } - /** - * Write the new $contents to all storages in the chain. - * + /** + * Write the new $contents to all storages in the chain. + * * @see StorageInterface::write() */ public function write( $contents ) @@ -103,10 +103,10 @@ public function write( $contents ) $storage->write($contents); } - /** - * Clear all storages in the chain. - * - * @see StorageInterface::clear() + /** + * Clear all storages in the chain. + * + * @see StorageInterface::clear() */ public function clear() { From ba1b9f7fbcab0c3356450ac53f0cc1444a516cc1 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Sun, 13 Jan 2013 17:53:35 +0100 Subject: [PATCH 04/11] Add tests for Chain storage engine. --- test/Storage/ChainTest.php | 149 +++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 test/Storage/ChainTest.php diff --git a/test/Storage/ChainTest.php b/test/Storage/ChainTest.php new file mode 100644 index 0000000..ba68831 --- /dev/null +++ b/test/Storage/ChainTest.php @@ -0,0 +1,149 @@ +assertTrue($chain->isEmpty()); + } + + /** + * Ensure chain with single empty storage behavious as expected. + */ + public function testSingularChainEmpty() + { + $chain = new Chain; + $chain->add($this->storageFactory()); + + $this->assertTrue($chain->isEmpty()); + } + + /** + * Ensure chain with single non-empty storage behavious as expected. + */ + public function testSingularChainNonEmpty() + { + $chain = new Chain; + $chain->add($this->storageFactory(self::ID)); + + $this->assertFalse($chain->isEmpty()); + $this->assertEquals(self::ID, $chain->read()); + } + + /** + * Ensure the priority of storage engines is correctly used. + */ + public function testChainPriority() + { + $storageA = $this->storageFactory(); + $storageB = $this->storageFactory(self::ID); + + $chain = new Chain; + $chain->add($storageA); // Defaults to 1 + $chain->add($storageB, 10); + $chain->isEmpty(); + + // Storage B has higher priority AND is non-empty. Thus + // storage A should been used at all and remain empty. + $this->assertTrue($storageA->isEmpty()); + } + + /** + * Ensure that a chain with empty storages is considered empty and + * won't populated any of its underlying storages. + */ + public function testEmptyChainIsEmpty() + { + $emptyStorageA = $this->storageFactory(); + $emptyStorageB = $this->storageFactory(); + + $chain = new Chain; + $chain->add($emptyStorageA); + $chain->add($emptyStorageB); + + $this->assertTrue($chain->isEmpty()); + + // Storage A and B remain empty + $this->assertTrue($emptyStorageA->isEmpty()); + $this->assertTrue($emptyStorageB->isEmpty()); + } + + /** + * Ensure that chain will yield non-empty if one of its underlying storage + * engines is non-empty. + * + * Make sure that storage engines with higher priority then the first non-empty + * storage engine get populated with that same content. + */ + public function testSuccessfullReadWillPopulateStoragesWithHigherPriority() + { + $emptyStorageA = $this->storageFactory(); + $emptyStorageB = $this->storageFactory(); + $storageC = $this->storageFactory(self::ID); + $emptyStorageD = $this->storageFactory(); + + $chain = new Chain; + $chain->add($emptyStorageA); + $chain->add($emptyStorageB); + $chain->add($storageC); + $chain->add($emptyStorageD); + + // Chain is non empty + $this->assertFalse($chain->isEmpty()); + $this->assertEquals(self::ID, $chain->read()); + + // Storage A and B are filled + $this->assertFalse($emptyStorageA->isEmpty()); + $this->assertEquals(self::ID, $emptyStorageA->read()); + $this->assertFalse($emptyStorageA->isEmpty()); + $this->assertEquals(self::ID, $emptyStorageB->read()); + + // Storage C and D remain identical + $this->assertFalse($storageC->isEmpty()); + $this->assertEquals(self::ID, $storageC->read()); + $this->assertTrue($emptyStorageD->isEmpty()); + } + + /** + * @param mixed $identity + * @return StorageInterface + */ + protected function storageFactory( $identity = null ) + { + $storage = new NonPersistent(); + + if( $identity !== null ) + $storage->write($identity); + + return $storage; + } +} \ No newline at end of file From 1dc04e76ca363c072a3f81da5062efd7b1374c39 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 14 Jan 2013 15:52:21 +0100 Subject: [PATCH 05/11] Use correct plural of `storage`. --- src/Storage/Chain.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Storage/Chain.php b/src/Storage/Chain.php index 4abc832..e7adc11 100644 --- a/src/Storage/Chain.php +++ b/src/Storage/Chain.php @@ -21,20 +21,20 @@ class Chain implements StorageInterface { /** - * Contains all storages that this authentication method uses. A storage + * Contains all storage that this authentication method uses. A storage * placed in the priority queue with a higher priority is always used * before using a storage with a lower priority. * * @var PriorityQueue */ - protected $storages; + protected $storageQueue; /** - * Initializes the priority queue that contains storages. + * Initializes the priority queue. */ public function __construct() { - $this->storages = new PriorityQueue(); + $this->storageQueue = new PriorityQueue(); } /** @@ -43,35 +43,35 @@ public function __construct() */ public function add( StorageInterface $storage, $priority = 1 ) { - $this->storages->insert($storage, $priority); + $this->storageQueue->insert($storage, $priority); } /** - * Loop over the queue of storages until a storage is found that is non-empty. If such + * Loop over the queue of storage until a storage is found that is non-empty. If such * storage is not found, then this chain storage itself is empty. * * In case a non-empty storage is found then this chain storage is also non-empty. Report - * that, but also make sure that all the storages with a higher priorty that are empty + * that, but also make sure that all storage with higher priorty that are empty * are filled. * * @see StorageInterface::isEmpty() */ public function isEmpty() { - $storagesWithHigherPriority = array(); + $storageWithHigherPriority = array(); - // Loop invariant: $storagesWithHigherPriority contains all storages with a higher priorty + // Loop invariant: $storageWithHigherPriority contains all storage with higher priorty // than the current one. - foreach( $this->storages as $storage ) + foreach( $this->storageQueue as $storage ) { if( $storage->isEmpty() ) { - $storagesWithHigherPriority[] = $storage; + $storageWithHigherPriority[] = $storage; } else { $storageValue = $storage->read(); - foreach( $storagesWithHigherPriority as $higherPriorityStorage ) + foreach( $storageWithHigherPriority as $higherPriorityStorage ) $higherPriorityStorage->write($storageValue); return false; @@ -89,28 +89,28 @@ public function isEmpty() */ public function read() { - return $this->storages->top()->read(); + return $this->storageQueue->top()->read(); } /** - * Write the new $contents to all storages in the chain. + * Write the new $contents to all storage in the chain. * * @see StorageInterface::write() */ public function write( $contents ) { - foreach( $this->storages as $storage ) + foreach( $this->storageQueue as $storage ) $storage->write($contents); } /** - * Clear all storages in the chain. + * Clear all storage in the chain. * * @see StorageInterface::clear() */ public function clear() { - foreach( $this->storages as $storage ) + foreach( $this->storageQueue as $storage ) $storage->clear(); } } \ No newline at end of file From 8406d8793e9bb6fe4d535c142944c036766b72c9 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 14 Jan 2013 15:53:36 +0100 Subject: [PATCH 06/11] Refactored isEmpty loop to be more readible. --- src/Storage/Chain.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Storage/Chain.php b/src/Storage/Chain.php index e7adc11..e72504d 100644 --- a/src/Storage/Chain.php +++ b/src/Storage/Chain.php @@ -67,15 +67,14 @@ public function isEmpty() if( $storage->isEmpty() ) { $storageWithHigherPriority[] = $storage; + continue; } - else - { - $storageValue = $storage->read(); - foreach( $storageWithHigherPriority as $higherPriorityStorage ) - $higherPriorityStorage->write($storageValue); - - return false; - } + + $storageValue = $storage->read(); + foreach( $storageWithHigherPriority as $higherPriorityStorage ) + $higherPriorityStorage->write($storageValue); + + return false; } return true; From c13793e5097f0ed8d65a99050ac0a2a84abd8ad4 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 14 Jan 2013 15:54:20 +0100 Subject: [PATCH 07/11] Renamed storageQueue to storageChain. --- src/Storage/Chain.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Storage/Chain.php b/src/Storage/Chain.php index e72504d..a35f8b7 100644 --- a/src/Storage/Chain.php +++ b/src/Storage/Chain.php @@ -27,14 +27,14 @@ class Chain implements StorageInterface * * @var PriorityQueue */ - protected $storageQueue; + protected $storageChain; /** * Initializes the priority queue. */ public function __construct() { - $this->storageQueue = new PriorityQueue(); + $this->storageChain = new PriorityQueue(); } /** @@ -43,7 +43,7 @@ public function __construct() */ public function add( StorageInterface $storage, $priority = 1 ) { - $this->storageQueue->insert($storage, $priority); + $this->storageChain->insert($storage, $priority); } /** @@ -62,7 +62,7 @@ public function isEmpty() // Loop invariant: $storageWithHigherPriority contains all storage with higher priorty // than the current one. - foreach( $this->storageQueue as $storage ) + foreach( $this->storageChain as $storage ) { if( $storage->isEmpty() ) { @@ -88,7 +88,7 @@ public function isEmpty() */ public function read() { - return $this->storageQueue->top()->read(); + return $this->storageChain->top()->read(); } /** @@ -98,7 +98,7 @@ public function read() */ public function write( $contents ) { - foreach( $this->storageQueue as $storage ) + foreach( $this->storageChain as $storage ) $storage->write($contents); } @@ -109,7 +109,7 @@ public function write( $contents ) */ public function clear() { - foreach( $this->storageQueue as $storage ) + foreach( $this->storageChain as $storage ) $storage->clear(); } } \ No newline at end of file From 72483733344ada438152e1ac5b2045ceb96b9220 Mon Sep 17 00:00:00 2001 From: Roel van Duijnhoven Date: Mon, 14 Jan 2013 15:57:55 +0100 Subject: [PATCH 08/11] Use PSR-2 as coding style. --- src/Storage/Chain.php | 59 +++++++++++++++++++------------------- test/Storage/ChainTest.php | 55 ++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/Storage/Chain.php b/src/Storage/Chain.php index a35f8b7..2f62101 100644 --- a/src/Storage/Chain.php +++ b/src/Storage/Chain.php @@ -24,11 +24,11 @@ class Chain implements StorageInterface * Contains all storage that this authentication method uses. A storage * placed in the priority queue with a higher priority is always used * before using a storage with a lower priority. - * + * * @var PriorityQueue */ protected $storageChain; - + /** * Initializes the priority queue. */ @@ -36,54 +36,53 @@ public function __construct() { $this->storageChain = new PriorityQueue(); } - + /** * @param StorageInterface $storage - * @param integer $priority + * @param integer $priority */ public function add( StorageInterface $storage, $priority = 1 ) { $this->storageChain->insert($storage, $priority); } - + /** - * Loop over the queue of storage until a storage is found that is non-empty. If such - * storage is not found, then this chain storage itself is empty. - * - * In case a non-empty storage is found then this chain storage is also non-empty. Report - * that, but also make sure that all storage with higher priorty that are empty + * Loop over the queue of storage until a storage is found that is non-empty. If such + * storage is not found, then this chain storage itself is empty. + * + * In case a non-empty storage is found then this chain storage is also non-empty. Report + * that, but also make sure that all storage with higher priorty that are empty * are filled. - * + * * @see StorageInterface::isEmpty() */ public function isEmpty() - { + { $storageWithHigherPriority = array(); - // Loop invariant: $storageWithHigherPriority contains all storage with higher priorty + // Loop invariant: $storageWithHigherPriority contains all storage with higher priorty // than the current one. - foreach( $this->storageChain as $storage ) - { - if( $storage->isEmpty() ) - { + foreach ($this->storageChain as $storage) { + if ( $storage->isEmpty() ) { $storageWithHigherPriority[] = $storage; continue; } - + $storageValue = $storage->read(); - foreach( $storageWithHigherPriority as $higherPriorityStorage ) + foreach ($storageWithHigherPriority as $higherPriorityStorage) { $higherPriorityStorage->write($storageValue); - + } + return false; } - + return true; } /** - * If the chain is non-empty then the storage with the top priority is guaranteed to be - * filled. Return its value. - * + * If the chain is non-empty then the storage with the top priority is guaranteed to be + * filled. Return its value. + * * @see StorageInterface::read() */ public function read() @@ -93,23 +92,25 @@ public function read() /** * Write the new $contents to all storage in the chain. - * + * * @see StorageInterface::write() */ public function write( $contents ) { - foreach( $this->storageChain as $storage ) + foreach ($this->storageChain as $storage) { $storage->write($contents); + } } /** * Clear all storage in the chain. - * + * * @see StorageInterface::clear() */ public function clear() { - foreach( $this->storageChain as $storage ) + foreach ($this->storageChain as $storage) { $storage->clear(); + } } -} \ No newline at end of file +} diff --git a/test/Storage/ChainTest.php b/test/Storage/ChainTest.php index ba68831..d820573 100644 --- a/test/Storage/ChainTest.php +++ b/test/Storage/ChainTest.php @@ -25,28 +25,28 @@ class ChainTest extends TestCase { const ID = 1337; - + /** * Ensure chain without storage behavious as empty storage. */ public function testEmptyChain() { $chain = new Chain; - + $this->assertTrue($chain->isEmpty()); } - + /** - * Ensure chain with single empty storage behavious as expected. + * Ensure chain with single empty storage behavious as expected. */ public function testSingularChainEmpty() { $chain = new Chain; $chain->add($this->storageFactory()); - + $this->assertTrue($chain->isEmpty()); } - + /** * Ensure chain with single non-empty storage behavious as expected. */ @@ -54,11 +54,11 @@ public function testSingularChainNonEmpty() { $chain = new Chain; $chain->add($this->storageFactory(self::ID)); - + $this->assertFalse($chain->isEmpty()); $this->assertEquals(self::ID, $chain->read()); } - + /** * Ensure the priority of storage engines is correctly used. */ @@ -66,17 +66,17 @@ public function testChainPriority() { $storageA = $this->storageFactory(); $storageB = $this->storageFactory(self::ID); - + $chain = new Chain; $chain->add($storageA); // Defaults to 1 $chain->add($storageB, 10); $chain->isEmpty(); - + // Storage B has higher priority AND is non-empty. Thus // storage A should been used at all and remain empty. $this->assertTrue($storageA->isEmpty()); } - + /** * Ensure that a chain with empty storages is considered empty and * won't populated any of its underlying storages. @@ -85,23 +85,23 @@ public function testEmptyChainIsEmpty() { $emptyStorageA = $this->storageFactory(); $emptyStorageB = $this->storageFactory(); - + $chain = new Chain; $chain->add($emptyStorageA); $chain->add($emptyStorageB); - + $this->assertTrue($chain->isEmpty()); - + // Storage A and B remain empty $this->assertTrue($emptyStorageA->isEmpty()); $this->assertTrue($emptyStorageB->isEmpty()); } - + /** * Ensure that chain will yield non-empty if one of its underlying storage * engines is non-empty. - * - * Make sure that storage engines with higher priority then the first non-empty + * + * Make sure that storage engines with higher priority then the first non-empty * storage engine get populated with that same content. */ public function testSuccessfullReadWillPopulateStoragesWithHigherPriority() @@ -110,40 +110,41 @@ public function testSuccessfullReadWillPopulateStoragesWithHigherPriority() $emptyStorageB = $this->storageFactory(); $storageC = $this->storageFactory(self::ID); $emptyStorageD = $this->storageFactory(); - + $chain = new Chain; $chain->add($emptyStorageA); $chain->add($emptyStorageB); $chain->add($storageC); $chain->add($emptyStorageD); - + // Chain is non empty $this->assertFalse($chain->isEmpty()); $this->assertEquals(self::ID, $chain->read()); - + // Storage A and B are filled $this->assertFalse($emptyStorageA->isEmpty()); $this->assertEquals(self::ID, $emptyStorageA->read()); $this->assertFalse($emptyStorageA->isEmpty()); $this->assertEquals(self::ID, $emptyStorageB->read()); - + // Storage C and D remain identical $this->assertFalse($storageC->isEmpty()); $this->assertEquals(self::ID, $storageC->read()); $this->assertTrue($emptyStorageD->isEmpty()); } - + /** - * @param mixed $identity + * @param mixed $identity * @return StorageInterface */ protected function storageFactory( $identity = null ) { $storage = new NonPersistent(); - - if( $identity !== null ) + + if ($identity !== null) { $storage->write($identity); - + } + return $storage; } -} \ No newline at end of file +} From 0ea589a08abb1bf32dcb298b9ce4b8b1d823d2bb Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 14 Jan 2013 10:08:13 -0600 Subject: [PATCH 09/11] [zendframework/zf2#2718] CS fixes - Remove leading/trailing space inside method argument lists. --- src/Storage/Chain.php | 6 +++--- test/Storage/ChainTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Storage/Chain.php b/src/Storage/Chain.php index 2f62101..4d14589 100644 --- a/src/Storage/Chain.php +++ b/src/Storage/Chain.php @@ -41,7 +41,7 @@ public function __construct() * @param StorageInterface $storage * @param integer $priority */ - public function add( StorageInterface $storage, $priority = 1 ) + public function add(StorageInterface $storage, $priority = 1) { $this->storageChain->insert($storage, $priority); } @@ -63,7 +63,7 @@ public function isEmpty() // Loop invariant: $storageWithHigherPriority contains all storage with higher priorty // than the current one. foreach ($this->storageChain as $storage) { - if ( $storage->isEmpty() ) { + if ($storage->isEmpty()) { $storageWithHigherPriority[] = $storage; continue; } @@ -95,7 +95,7 @@ public function read() * * @see StorageInterface::write() */ - public function write( $contents ) + public function write($contents) { foreach ($this->storageChain as $storage) { $storage->write($contents); diff --git a/test/Storage/ChainTest.php b/test/Storage/ChainTest.php index d820573..b53f932 100644 --- a/test/Storage/ChainTest.php +++ b/test/Storage/ChainTest.php @@ -137,7 +137,7 @@ public function testSuccessfullReadWillPopulateStoragesWithHigherPriority() * @param mixed $identity * @return StorageInterface */ - protected function storageFactory( $identity = null ) + protected function storageFactory($identity = null) { $storage = new NonPersistent(); From 1c58acaae44b3e4d135dfe1ad890589f004dba04 Mon Sep 17 00:00:00 2001 From: Mike Willbanks Date: Mon, 14 Jan 2013 10:06:44 -0800 Subject: [PATCH 10/11] Updated AbstractAdapter based on feedback to provide a validatable adapter interface, changed contract in the Authentication validator to ensure that the adapter must be a ValidatableAdapterInterface --- src/Adapter/AbstractAdapter.php | 8 ++-- src/Adapter/Http.php | 2 +- src/Adapter/ValidatableAdapterInterface.php | 51 +++++++++++++++++++++ src/Validator/Authentication.php | 8 ++-- 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/Adapter/ValidatableAdapterInterface.php diff --git a/src/Adapter/AbstractAdapter.php b/src/Adapter/AbstractAdapter.php index 2856a5e..fce1c66 100644 --- a/src/Adapter/AbstractAdapter.php +++ b/src/Adapter/AbstractAdapter.php @@ -15,7 +15,7 @@ * @package Zend_Authentication * @subpackage Adapter */ -abstract class AbstractAdapter implements AdapterInterface +abstract class AbstractAdapter implements ValidatableAdapterInterface { /** @@ -42,12 +42,13 @@ public function getCredential() /** * Sets the credential for binding * - * @param mixed $credential + * @param mixed $credential * @return AbstractAdapter */ public function setCredential($credential) { $this->credential = $credential; + return $this; } @@ -65,12 +66,13 @@ public function getIdentity() /** * Sets the identity for binding * - * @param mixed $identity + * @param mixed $identity * @return AbstractAdpter */ public function setIdentity($identity) { $this->identity = $identity; + return $this; } } diff --git a/src/Adapter/Http.php b/src/Adapter/Http.php index b4d13e2..24b4432 100644 --- a/src/Adapter/Http.php +++ b/src/Adapter/Http.php @@ -27,7 +27,7 @@ * @todo Track nonces, nonce-count, opaque for replay protection and stale support * @todo Support Authentication-Info header */ -class Http extends AbstractAdapter +class Http implements AdapterInterface { /** * Reference to the HTTP Request object diff --git a/src/Adapter/ValidatableAdapterInterface.php b/src/Adapter/ValidatableAdapterInterface.php new file mode 100644 index 0000000..dc3f646 --- /dev/null +++ b/src/Adapter/ValidatableAdapterInterface.php @@ -0,0 +1,51 @@ +adapter = $adapter; From e8e0772d2804f0434ead1a3e75c9c70825efc513 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 29 Jan 2013 10:38:57 -0600 Subject: [PATCH 11/11] [zendframework/zf2#3585] CS/logic cleanup - Re-work conditionals to reduce nesting - Minor whitespace changes --- src/Adapter/Http/ApacheResolver.php | 42 ++++++++++++++++++----------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Adapter/Http/ApacheResolver.php b/src/Adapter/Http/ApacheResolver.php index 5e68db5..22c945c 100644 --- a/src/Adapter/Http/ApacheResolver.php +++ b/src/Adapter/Http/ApacheResolver.php @@ -101,20 +101,27 @@ public function resolve($username, $realm, $password = null) { if (empty($username)) { throw new Exception\InvalidArgumentException('Username is required'); - } elseif (!ctype_print($username) || strpos($username, ':') !== false) { - throw new Exception\InvalidArgumentException('Username must consist only of printable characters, ' - . 'excluding the colon'); } + + if (!ctype_print($username) || strpos($username, ':') !== false) { + throw new Exception\InvalidArgumentException( + 'Username must consist only of printable characters, excluding the colon' + ); + } + if (!empty($realm) && (!ctype_print($realm) || strpos($realm, ':') !== false)) { - throw new Exception\InvalidArgumentException('Realm must consist only of printable characters, ' - . 'excluding the colon.'); + throw new Exception\InvalidArgumentException( + 'Realm must consist only of printable characters, excluding the colon' + ); } + if (empty($password)) { throw new Exception\InvalidArgumentException('Password is required'); } + // Open file, read through looking for matching credentials ErrorHandler::start(E_WARNING); - $fp = fopen($this->file, 'r'); + $fp = fopen($this->file, 'r'); $error = ErrorHandler::stop(); if (!$fp) { throw new Exception\RuntimeException('Unable to open password file: ' . $this->file, 0, $error); @@ -123,17 +130,20 @@ public function resolve($username, $realm, $password = null) // No real validation is done on the contents of the password file. The // assumption is that we trust the administrators to keep it secure. while (($line = fgetcsv($fp, 512, ':')) !== false) { - if ($line[0] == $username) { - if (isset($line[2])) { - if ($line[1] == $realm) { - $matchedHash = $line[2]; - break; - } - } else { - $matchedHash = $line[1]; + if ($line[0] != $username) { + continue; + } + + if (isset($line[2])) { + if ($line[1] == $realm) { + $matchedHash = $line[2]; break; } + continue; } + + $matchedHash = $line[1]; + break; } fclose($fp); @@ -154,8 +164,8 @@ public function resolve($username, $realm, $password = null) if ($apache->verify($password, $matchedHash)) { return new AuthResult(AuthResult::SUCCESS, $username); - } else { - return new AuthResult(AuthResult::FAILURE_CREDENTIAL_INVALID, null, array('Passwords did not match.')); } + + return new AuthResult(AuthResult::FAILURE_CREDENTIAL_INVALID, null, array('Passwords did not match.')); } }