8000 Password Strength estimator extraction to dedicated service (no Backward Compatibility) by yalit · Pull Request #54882 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Password Strength estimator extraction to dedicated service (no Backward Compatibility) #54882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Password\PasswordStrengthEstimatorInterface;

final class PasswordStrengthValidator extends ConstraintValidator
{
/**
* @param (\Closure(string):PasswordStrength::STRENGTH_*)|null $passwordStrengthEstimator
*/
public function __construct(
private readonly ?\Closure $passwordStrengthEstimator = null,
private readonly PasswordStrengthEstimatorInterface $passwordStrengthEstimator
) {
}

Expand All @@ -39,8 +37,8 @@ public function validate(#[\SensitiveParameter] mixed $value, Constraint $constr
if (!\is_string($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$passwordStrengthEstimator = $this->passwordStrengthEstimator ?? self::estimateStrength(...);
$strength = $passwordStrengthEstimator((string) $value);

$strength = $this->passwordStrengthEstimator->estimateStrength((string) $value);

if ($strength < $constraint->minScore) {
$this->context->buildViolation($constraint->message)
Expand All @@ -49,43 +47,4 @@ public function validate(#[\SensitiveParameter] mixed $value, Constraint $constr
->addViolation();
}
}

/**
* Returns the estimated strength of a password.
*
* The higher the value, the stronger the password.
*
* @return PasswordStrength::STRENGTH_*
*/
private static function estimateStrength(#[\SensitiveParameter] string $password): int
{
if (!$length = \strlen($password)) {
return PasswordStrength::STRENGTH_VERY_WEAK;
}
$password = count_chars($password, 1);
$chars = \count($password);

$control = $digit = $upper = $lower = $symbol = $other = 0;
foreach ($password as $chr => $count) {
match (true) {
$chr < 32 || 127 === $chr => $control = 33,
48 <= $chr && $chr <= 57 => $digit = 10,
65 <= $chr && $chr <= 90 => $upper = 26,
97 <= $chr && $chr <= 122 => $lower = 26,
128 <= $chr => $other = 128,
default => $symbol = 33,
};
}

$pool = $lower + $upper + $digit + $symbol + $control + $other;
$entropy = $chars * log($pool, 2) + ($length - $chars) * log($chars, 2);

return match (true) {
$entropy >= 120 => PasswordStrength::STRENGTH_VERY_STRONG,
$entropy >= 100 => PasswordStrength::STRENGTH_STRONG,
$entropy >= 80 => PasswordStrength::STRENGTH_MEDIUM,
$entropy >= 60 => PasswordStrength::STRENGTH_WEAK,
default => PasswordStrength::STRENGTH_VERY_WEAK,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Symfony\Component\Validator\Password;

use Stringable;
use Symfony\Component\Validator\Constraints\PasswordStrength;

class PasswordStrengthEstimator implements PasswordStrengthEstimatorInterface
{
public function estimateStrength(#[\SensitiveParameter] string|Stringable $password): int
{
if (!$length = \strlen($password)) {
return PasswordStrength::STRENGTH_VERY_WEAK;
}
$password = count_chars($password, 1);
$chars = \count($password);

$control = $digit = $upper = $lower = $symbol = $other = 0;
f 8000 oreach ($password as $chr => $count) {
match (true) {
$chr < 32 || 127 === $chr => $control = 33,
48 <= $chr && $chr <= 57 => $digit = 10,
65 <= $chr && $chr <= 90 => $upper = 26,
97 <= $chr && $chr <= 122 => $lower = 26,
128 <= $chr => $other = 128,
default => $symbol = 33,
};
}

$pool = $lower + $upper + $digit + $symbol + $control + $other;
$entropy = $chars * log($pool, 2) + ($length - $chars) * log($chars, 2);

return match (true) {
$entropy >= 120 => PasswordStrength::STRENGTH_VERY_STRONG,
$entropy >= 100 => PasswordStrength::STRENGTH_STRONG,
$entropy >= 80 => PasswordStrength::STRENGTH_MEDIUM,
$entropy >= 60 => PasswordStrength::STRENGTH_WEAK,
default => PasswordStrength::STRENGTH_VERY_WEAK,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\Validator\Password;

use Stringable;
use Symfony\Component\Validator\Constraints\PasswordStrength;

interface PasswordStrengthEstimatorInterface
{
/**
* Returns the estimated strength of a password.
*
* The higher the value, the stronger the password.
*
* @return PasswordStrength::STRENGTH_*
*/
public function estimateStrength(#[\SensitiveParameter] string|Stringable $password): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

use Symfony\Component\Validator\Constraints\PasswordStrength;
use Symfony\Component\Validator\Constraints\PasswordStrengthValidator;
use Symfony\Component\Validator\Password\PasswordStrengthEstimator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;

class PasswordStrengthValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): PasswordStrengthValidator
{
return new PasswordStrengthValidator();
return new PasswordStrengthValidator(new PasswordStrengthEstimator());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Symfony\Component\Validator\Tests\Password;

use PHPUnit\Framework\TestCase;
use Stringable;
use Symfony\Component\Validator\Constraints\PasswordStrength;
use Symfony\Component\Validator\Password\PasswordStrengthEstimator;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;

class PasswordStrengthEstimatorTest extends TestCase
{
/** @dataProvider getPasswords */
public function testEstimateStrength(string|Stringable $password, int $expectedStrength): void
{
self::assertSame($expectedStrength, (new PasswordStrengthEstimator())->estimateStrength($password));
}

/** @return array<string, array<string, int>> */
public function getPasswords(): iterable
{
yield ['How-is-this', PasswordStrength::STRENGTH_WEAK];
yield ['Reasonable-pwd', PasswordStrength::STRENGTH_MEDIUM];
yield ['This 1s a very g00d Pa55word! ;-)', PasswordStrength::STRENGTH_VERY_STRONG];
yield ['pudding-smack-👌🏼-fox-😎', PasswordStrength::STRENGTH_VERY_STRONG];
yield [new StringableValue('How-is-this'), PasswordStrength::STRENGTH_WEAK];
}
}
Loading
0