From 60799ae9ab9f9b0a8f5288c91d4466d596054833 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 29 Jan 2013 10:38:57 -0600 Subject: [PATCH] [#3585] CS/logic cleanup - Re-work conditionals to reduce nesting - Minor whitespace changes --- .../Adapter/Http/ApacheResolver.php | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/library/Zend/Authentication/Adapter/Http/ApacheResolver.php b/library/Zend/Authentication/Adapter/Http/ApacheResolver.php index 5e68db5a690..22c945caac4 100644 --- a/library/Zend/Authentication/Adapter/Http/ApacheResolver.php +++ b/library/Zend/Authentication/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.')); } }