From addaede0df2c67c9d7a2828a0d12bcd11302cc44 Mon Sep 17 00:00:00 2001 From: djmaze Date: Fri, 5 Mar 2021 09:43:46 +0100 Subject: [PATCH] Improved password strength meter --- plugins/change-password/index.php | 16 ++++++---------- .../js/ChangePasswordUserSettings.js | 12 ++++-------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/plugins/change-password/index.php b/plugins/change-password/index.php index 83da65d66..4b79aa32e 100644 --- a/plugins/change-password/index.php +++ b/plugins/change-password/index.php @@ -89,7 +89,7 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin ->SetLabel('Password minimum strength') ->SetType(\RainLoop\Enumerations\PluginPropertyType::INT) ->SetDescription('Minimum strength of the password in %') - ->SetDefaultValue(60), + ->SetDefaultValue(70), ]; foreach ($this->getSupportedDrivers(true) as $name => $class) { $result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_enabled") @@ -125,7 +125,7 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin throw new ClientException(static::NewPasswordShort, null, $oActions->StaticI18N('NOTIFICATIONS/NEW_PASSWORD_SHORT')); } - if ($this->Config()->Get('plugin', 'pass_min_strength', 60) > static::PasswordStrength($sNewPassword)) { + if ($this->Config()->Get('plugin', 'pass_min_strength', 70) > static::PasswordStrength($sNewPassword)) { throw new ClientException(static::NewPasswordWeak, null, $oActions->StaticI18N('NOTIFICATIONS/NEW_PASSWORD_WEAK')); } @@ -201,13 +201,10 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin private static function PasswordStrength(string $sPassword) : int { $i = \strlen($sPassword); - $s = $i ? 1 : 0; + $max = min(100, $i * 8); + $s = 0; while (--$i) { - if ($sPassword[$i] != $sPassword[$i-1]) { - ++$s; - } else { - $s -= 0.5; - } + $s += ($sPassword[$i] != $sPassword[$i-1] ? 1 : -0.5); } $c = 0; $re = [ '/[^0-9A-Za-z]+/', '/[0-9]+/', '/[A-Z]+/', '/[a-z]+/' ]; @@ -221,9 +218,8 @@ class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin } } } - $s = ($s / 3 * $c); - return \max(0, \min(100, $s * 5)); + return \intval(\max(0, \min($max, $s * $c * 1.5))); } } diff --git a/plugins/change-password/js/ChangePasswordUserSettings.js b/plugins/change-password/js/ChangePasswordUserSettings.js index 831645beb..2450542fb 100644 --- a/plugins/change-password/js/ChangePasswordUserSettings.js +++ b/plugins/change-password/js/ChangePasswordUserSettings.js @@ -10,15 +10,12 @@ getPassStrength = v => { let m, i = v.length, - s = i?1:0, + max = Math.min(100, i * 8), + s = 0, c = 0, ii; while (i--) { - if (v[i] != v[i+1]) { - ++s; - } else { - s -= 0.5; - } + s += (v[i] != v[i+1] ? 1 : -0.5); } for (i = 0; i < 4; ++i) { m = v.match(pw_re[i]); @@ -31,8 +28,7 @@ } } } - s = (s / 3 * c); - return Math.max(0, Math.min(100, s * 5)); + return Math.max(0, Math.min(max, s * c * 1.5)); }; class ChangePasswordUserSettings