Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a7c18ac

Browse files
committed
Add a new attribute to skip when an error occurs. Review.
1 parent c864bfc commit a7c18ac

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/Symfony/Component/Validator/Constraints/NotPwned.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ class NotPwned extends Constraint
2929

3030
public $message = 'This password has been leaked in a data breach, it must not be used. Please use another password.';
3131
public $threshold = 1;
32+
public $skipOnError = false;
3233
}

src/Symfony/Component/Validator/Constraints/NotPwnedValidator.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class NotPwnedValidator extends ConstraintValidator
3030
{
3131
private const RANGE_API = 'https://api.pwnedpasswords.com/range/%s';
32+
3233
private $httpClient;
3334

3435
public function __construct(HttpClientInterface $httpClient = null)
@@ -60,13 +61,20 @@ public function validate($value, Constraint $constraint)
6061
return;
6162
}
6263

63-
$httpClient = $this->httpClient;
64-
6564
$hash = strtoupper(sha1($value));
6665
$hashPrefix = substr($hash, 0, 5);
6766
$url = sprintf(self::RANGE_API, $hashPrefix);
6867

69-
$result = $httpClient->request('GET', $url)->getContent();
68+
try {
69+
$result = $this->httpClient->request('GET', $url)->getContent();
70+
} catch (ExceptionInterface $e) {
71+
if ($constraint->skipOnError) {
72+
return;
73+
}
74+
75+
throw $e;
76+
}
77+
7078
foreach (explode("\r\n", $result) as $line) {
7179
list($hashSuffix, $count) = explode(':', $line);
7280

src/Symfony/Component/Validator/Tests/Constraints/NotPwnedTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ public function testDefaultValues()
2323
{
2424
$constraint = new NotPwned();
2525
$this->assertSame(1, $constraint->threshold);
26+
$this->assertFalse($constraint->skipOnError);
2627
}
2728
}

src/Symfony/Component/Validator/Tests/Constraints/NotPwnedValidatorTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ protected function createValidator()
4444
$this->returnCallback(function (string $method, string $url): ResponseInterface {
4545
if (self::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $url) {
4646
throw new class('Problem contacting the Have I been Pwned API.') extends \Exception implements ServerExceptionInterface {
47+
public function getResponse(): ResponseInterface
48+
{
49+
throw new \RuntimeException('Not implemented');
50+
}
4751
};
4852
}
4953

@@ -96,16 +100,14 @@ public function testThresholdReached()
96100

97101
public function testThresholdNotReached()
98102
{
99-
$constraint = new NotPwned(['threshold' => 10]);
100-
$this->validator->validate(self::PASSWORD_LEAKED, $constraint);
103+
$this->validator->validate(self::PASSWORD_LEAKED, new NotPwned(['threshold' => 10]));
101104

102105
$this->assertNoViolation();
103106
}
104107

105108
public function testValidPassword()
106109
{
107-
$constraint = new NotPwned();
108-
$this->validator->validate(self::PASSWORD_NOT_LEAKED, $constraint);
110+
$this->validator->validate(self::PASSWORD_NOT_LEAKED, new NotPwned());
109111

110112
$this->assertNoViolation();
111113
}
@@ -134,4 +136,10 @@ public function testApiError()
134136
{
135137
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotPwned());
136138
}
139+
140+
public function testApiErrorSkipped()
141+
{
142+
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotPwned(['skipOnError' => true]));
143+
$this->assertTrue(true); // No exception have been thrown
144+
}
137145
}

0 commit comments

Comments
 (0)