From e1044636d4dbd7adda91ab64ee44f2e002691c2e Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Sun, 11 Feb 2024 09:02:47 -0800 Subject: [PATCH] Refactored the generatePassword function (#2131) --- src/library/FOSSBilling/Tools.php | 102 ++++++++++++++----------- src/load.php | 4 +- src/modules/Servicedomain/Service.php | 2 +- src/modules/Servicehosting/Service.php | 4 +- 4 files changed, 61 insertions(+), 51 deletions(-) diff --git a/src/library/FOSSBilling/Tools.php b/src/library/FOSSBilling/Tools.php index ff10eac2c..fec1a9a8a 100644 --- a/src/library/FOSSBilling/Tools.php +++ b/src/library/FOSSBilling/Tools.php @@ -110,62 +110,72 @@ class Tools } /** - * Generates random password. + * Generates a password of a set length and complexity. * - * @param int $length - * @param int $strength + * @param int $length the length of the password to generate + * @param bool|int $includeSpecial If special characters should be included. If 4 is passed, that's considered to be true (added for backwards compatibility). * - * @return string + * @throws InformationException if it failed to generate a password meeting the requirements within 50 iterations */ - public function generatePassword($length = 8, $strength = 3) + public function generatePassword(int $length = 8, bool|int $includeSpecial = false): string { - $upper = 0; - $lower = 0; - $numeric = 0; - $other = 0; + $characters = 'abcdefghijklmnopqrstuvwxyz'; + $numbers = '0123456789'; + $specialCharacters = '!@#$%&?()+-_'; - $upper_letters = 'QWERTYUIOPASDFGHJKLZXCVBNM'; - $lower_letters = 'qwertyuiopasdfghjklzxccvbnm'; - $numbers = '1234567890'; - $symbols = '!@#$%&?()+-_'; - - switch ($strength) { - // lowercase + uppercase + numeric - case 3: - $lower = random_int(1, $length - 2); - $upper = random_int(1, $length - $lower - 1); - $numeric = $length - $lower - $upper; - - break; - // lowercase + uppercase + numeric + symbols - case 4: - default: - $lower = random_int(1, $length - 3); - $upper = random_int(1, $length - $lower - 2); - $numeric = random_int(1, $length - $lower - $upper - 1); - $other = $length - $lower - $upper - $numeric; - - break; + // Backwards compatibility with previous behavior + if (is_int($includeSpecial)) { + $includeSpecial = $includeSpecial === 4; } - $passOrder = []; - - for ($i = 0; $i < $upper; ++$i) { - $passOrder[] = $upper_letters[random_int(0, mt_getrandmax()) % strlen($upper_letters)]; - } - for ($i = 0; $i < $lower; ++$i) { - $passOrder[] = $lower_letters[random_int(0, mt_getrandmax()) % strlen($lower_letters)]; - } - for ($i = 0; $i < $numeric; ++$i) { - $passOrder[] = $numbers[random_int(0, mt_getrandmax()) % strlen($numbers)]; - } - for ($i = 0; $i < $other; ++$i) { - $passOrder[] = $symbols[random_int(0, mt_getrandmax()) % strlen($symbols)]; + $charSet = $characters . strtoupper($characters) . $numbers; + if ($includeSpecial) { + $charSet .= $specialCharacters; } - shuffle($passOrder); + $charSetLength = strlen($charSet); - return implode('', $passOrder); + // Loop flow-control + $valid = false; + $iterations = 0; + + // Password requirements validation + $hasLowercase = false; + $hasUppercase = false; + $hasNumber = false; + $hasSpecial = false; + + $password = ''; + while (!$valid && $iterations < 100) { + // Add a random character to the password from the provided list of acceptable. + $character = substr($charSet, random_int(0, $charSetLength - 1), 1); + $password .= $character; + + // Handle validations + $hasLowercase = $hasLowercase || str_contains($characters, $character); + $hasUppercase = $hasUppercase || str_contains(strtoupper($characters), $character); + $hasNumber = $hasNumber || str_contains($numbers, $character); + $hasSpecial = !$includeSpecial || $hasSpecial || str_contains($specialCharacters, $character); + + // Once we reach the required length, check if the password is valid + if (strlen($password) === $length) { + $valid = $hasLowercase && $hasUppercase && $hasNumber && $hasSpecial; + if (!$valid) { + ++$iterations; + $password = ''; + $hasLowercase = false; + $hasUppercase = false; + $hasNumber = false; + $hasSpecial = false; + } + } + } + + if ($valid) { + return $password; + } else { + throw new InformationException('We were unable to generate a password with the required parameters'); + } } public function autoLinkText($text) diff --git a/src/load.php b/src/load.php index 83e9806c2..0eaa4eac6 100644 --- a/src/load.php +++ b/src/load.php @@ -153,7 +153,7 @@ function errorHandler(int $number, string $message, string $file, int $line) require_once PATH_LIBRARY . DIRECTORY_SEPARATOR . 'FOSSBilling' . DIRECTORY_SEPARATOR . 'ErrorPage.php'; } - if (!class_exists('\\' . FOSSBilling\SentryHelper::class)) { + if (!class_exists('\\' . SentryHelper::class)) { require_once PATH_LIBRARY . DIRECTORY_SEPARATOR . 'FOSSBilling' . DIRECTORY_SEPARATOR . 'SentryHelper.php'; } @@ -261,7 +261,7 @@ $loader->register(); define('BIND_TO', FOSSBilling\Tools::getDefaultInterface()); // Now that the config file is loaded, we can enable Sentry -FOSSBilling\SentryHelper::registerSentry(); +SentryHelper::registerSentry(); // Verify the installer was removed. checkInstaller(); diff --git a/src/modules/Servicedomain/Service.php b/src/modules/Servicedomain/Service.php index 68049caee..9f0f807b0 100644 --- a/src/modules/Servicedomain/Service.php +++ b/src/modules/Servicedomain/Service.php @@ -680,7 +680,7 @@ class Service implements \FOSSBilling\InjectionAwareInterface $contact ->setEmail($email) ->setUsername($email) - ->setPassword($this->di['tools']->generatePassword(10, 3)) + ->setPassword($this->di['tools']->generatePassword(10)) ->setFirstname($first_name) ->setLastname($last_name) ->setCity($city) diff --git a/src/modules/Servicehosting/Service.php b/src/modules/Servicehosting/Service.php index f3d61f9bb..8d8aa75e5 100644 --- a/src/modules/Servicehosting/Service.php +++ b/src/modules/Servicehosting/Service.php @@ -110,7 +110,7 @@ class Service implements InjectionAwareInterface $serverManager = $this->_getServerMangerForOrder($model); // Generate a password for the service - $pass = $this->di['tools']->generatePassword($serverManager->getPasswordLength(), 4); + $pass = $this->di['tools']->generatePassword($serverManager->getPasswordLength(), true); // If a password is already specified in the order's configuration, use that instead if (isset($config['password']) && !empty($config['password'])) { @@ -238,7 +238,7 @@ class Service implements InjectionAwareInterface $serverManager = $this->_getServerMangerForOrder($model); // As we replace the password internally with asterisks, generate a new password - $pass = $this->di['tools']->generatePassword($serverManager->getPasswordLength(), 4); + $pass = $this->di['tools']->generatePassword($serverManager->getPasswordLength(), true); $model->pass = $pass; // Retrieve the adapter and account, then create the account on the server