mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-01 12:48:08 +09:00
Refactored the generatePassword function (#2131)
This commit is contained in:
1 parent
27ddcacd6e
commit
e1044636d4
4 files changed
+61
-51
No files matched your search
@@ -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)
|
||||
|
||||
+2
-2
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user