mailpoet
/
vendor-prefixed
/
symfony
/
validator
/
Constraints
/
NotCompromisedPasswordValidator.php
AbstractComparison.php
1 year ago
AbstractComparisonValidator.php
1 year ago
All.php
1 year ago
AllValidator.php
4 years ago
AtLeastOneOf.php
1 year ago
AtLeastOneOfValidator.php
1 year ago
Bic.php
1 year ago
BicValidator.php
1 year ago
Blank.php
1 year ago
BlankValidator.php
4 years ago
Callback.php
1 year ago
CallbackValidator.php
4 years ago
CardScheme.php
1 year ago
CardSchemeValidator.php
1 year ago
Cascade.php
1 year ago
Choice.php
1 year ago
ChoiceValidator.php
1 year ago
Cidr.php
1 year ago
CidrValidator.php
4 years ago
Collection.php
1 year ago
CollectionValidator.php
4 years ago
Composite.php
1 year ago
Compound.php
4 years ago
CompoundValidator.php
4 years ago
Count.php
1 year ago
CountValidator.php
4 years ago
Country.php
1 year ago
CountryValidator.php
4 years ago
CssColor.php
1 year ago
CssColorValidator.php
1 year ago
Currency.php
1 year ago
CurrencyValidator.php
4 years ago
Date.php
1 year ago
DateTime.php
1 year ago
DateTimeValidator.php
2 years ago
DateValidator.php
1 year ago
DisableAutoMapping.php
1 year ago
DivisibleBy.php
4 years ago
DivisibleByValidator.php
4 years ago
Email.php
1 year ago
EmailValidator.php
1 year ago
EnableAutoMapping.php
1 year ago
EqualTo.php
4 years ago
EqualToValidator.php
4 years ago
Existence.php
4 years ago
Expression.php
1 year ago
ExpressionLanguageSyntax.php
1 year ago
ExpressionLanguageSyntaxValidator.php
1 year ago
ExpressionValidator.php
1 year ago
File.php
1 year ago
FileValidator.php
4 years ago
GreaterThan.php
4 years ago
GreaterThanOrEqual.php
4 years ago
GreaterThanOrEqualValidator.php
4 years ago
GreaterThanValidator.php
4 years ago
GroupSequence.php
4 years ago
GroupSequenceProvider.php
4 years ago
Hostname.php
1 year ago
HostnameValidator.php
4 years ago
Iban.php
1 year ago
IbanValidator.php
1 year ago
IdenticalTo.php
4 years ago
IdenticalToValidator.php
4 years ago
Image.php
1 year ago
ImageValidator.php
4 years ago
Ip.php
1 year ago
IpValidator.php
4 years ago
IsFalse.php
1 year ago
IsFalseValidator.php
4 years ago
IsNull.php
1 year ago
IsNullValidator.php
4 years ago
IsTrue.php
1 year ago
IsTrueValidator.php
4 years ago
Isbn.php
1 year ago
IsbnValidator.php
1 year ago
Isin.php
1 year ago
IsinValidator.php
4 years ago
Issn.php
1 year ago
IssnValidator.php
4 years ago
Json.php
1 year ago
JsonValidator.php
1 year ago
Language.php
1 year ago
LanguageValidator.php
4 years ago
Length.php
1 year ago
LengthValidator.php
4 years ago
LessThan.php
4 years ago
LessThanOrEqual.php
4 years ago
LessThanOrEqualValidator.php
4 years ago
LessThanValidator.php
4 years ago
Locale.php
1 year ago
LocaleValidator.php
4 years ago
Luhn.php
1 year ago
LuhnValidator.php
4 years ago
Negative.php
4 years ago
NegativeOrZero.php
4 years ago
NotBlank.php
1 year ago
NotBlankValidator.php
4 years ago
NotCompromisedPassword.php
1 year ago
NotCompromisedPasswordValidator.php
1 year ago
NotEqualTo.php
4 years ago
NotEqualToValidator.php
4 years ago
NotIdenticalTo.php
4 years ago
NotIdenticalToValidator.php
4 years ago
NotNull.php
1 year ago
NotNullValidator.php
4 years ago
NumberConstraintTrait.php
4 years ago
Optional.php
4 years ago
Positive.php
4 years ago
PositiveOrZero.php
4 years ago
Range.php
1 year ago
RangeValidator.php
1 year ago
Regex.php
1 year ago
RegexValidator.php
4 years ago
Required.php
4 years ago
Sequentially.php
1 year ago
SequentiallyValidator.php
4 years ago
Time.php
1 year ago
TimeValidator.php
1 year ago
Timezone.php
1 year ago
TimezoneValidator.php
1 year ago
Traverse.php
4 years ago
Type.php
1 year ago
TypeValidator.php
4 years ago
Ulid.php
1 year ago
UlidValidator.php
2 years ago
Unique.php
1 year ago
UniqueValidator.php
1 year ago
Url.php
1 year ago
UrlValidator.php
1 year ago
Uuid.php
1 year ago
UuidValidator.php
4 years ago
Valid.php
1 year ago
ValidValidator.php
4 years ago
ZeroComparisonConstraintTrait.php
1 year ago
index.php
3 years ago
NotCompromisedPasswordValidator.php
69 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Constraints; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\HttpClient\HttpClient; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\ConstraintValidator; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 8 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedValueException; |
| 9 | use MailPoetVendor\Symfony\Contracts\HttpClient\Exception\ExceptionInterface; |
| 10 | use MailPoetVendor\Symfony\Contracts\HttpClient\HttpClientInterface; |
| 11 | class NotCompromisedPasswordValidator extends ConstraintValidator |
| 12 | { |
| 13 | private const DEFAULT_API_ENDPOINT = 'https://api.pwnedpasswords.com/range/%s'; |
| 14 | private $httpClient; |
| 15 | private $charset; |
| 16 | private $enabled; |
| 17 | private $endpoint; |
| 18 | public function __construct(?HttpClientInterface $httpClient = null, string $charset = 'UTF-8', bool $enabled = \true, ?string $endpoint = null) |
| 19 | { |
| 20 | if (null === $httpClient && !\class_exists(HttpClient::class)) { |
| 21 | throw new \LogicException(\sprintf('The "%s" class requires the "HttpClient" component. Try running "composer require symfony/http-client".', self::class)); |
| 22 | } |
| 23 | $this->httpClient = $httpClient ?? HttpClient::create(); |
| 24 | $this->charset = $charset; |
| 25 | $this->enabled = $enabled; |
| 26 | $this->endpoint = $endpoint ?? self::DEFAULT_API_ENDPOINT; |
| 27 | } |
| 28 | public function validate($value, Constraint $constraint) |
| 29 | { |
| 30 | if (!$constraint instanceof NotCompromisedPassword) { |
| 31 | throw new UnexpectedTypeException($constraint, NotCompromisedPassword::class); |
| 32 | } |
| 33 | if (!$this->enabled) { |
| 34 | return; |
| 35 | } |
| 36 | if (null !== $value && !\is_scalar($value) && !(\is_object($value) && \method_exists($value, '__toString'))) { |
| 37 | throw new UnexpectedValueException($value, 'string'); |
| 38 | } |
| 39 | $value = (string) $value; |
| 40 | if ('' === $value) { |
| 41 | return; |
| 42 | } |
| 43 | if ('UTF-8' !== $this->charset) { |
| 44 | $value = \mb_convert_encoding($value, 'UTF-8', $this->charset); |
| 45 | } |
| 46 | $hash = \strtoupper(\sha1($value)); |
| 47 | $hashPrefix = \substr($hash, 0, 5); |
| 48 | $url = \sprintf($this->endpoint, $hashPrefix); |
| 49 | try { |
| 50 | $result = $this->httpClient->request('GET', $url)->getContent(); |
| 51 | } catch (ExceptionInterface $e) { |
| 52 | if ($constraint->skipOnError) { |
| 53 | return; |
| 54 | } |
| 55 | throw $e; |
| 56 | } |
| 57 | foreach (\explode("\r\n", $result) as $line) { |
| 58 | if (!\str_contains($line, ':')) { |
| 59 | continue; |
| 60 | } |
| 61 | [$hashSuffix, $count] = \explode(':', $line); |
| 62 | if ($hashPrefix . $hashSuffix === $hash && $constraint->threshold <= (int) $count) { |
| 63 | $this->context->buildViolation($constraint->message)->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)->addViolation(); |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 |