Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 18693e0

Browse files
Add localizations/translations (#15)
* added translations Co-authored-by: Niek de Melker <[email protected]> Co-authored-by: Claudio Dekker <[email protected]>
1 parent 7be0935 commit 18693e0

7 files changed

+45
-21
lines changed

resources/lang/en/validation.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'pwned' => ':attribute was found in at least :num prior security incident(s). Please choose a more secure password.',
5+
];

src/Api/FakeApiGateway.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ubient\PwnedPasswords\Api;
44

5+
use RuntimeException;
56
use Ubient\PwnedPasswords\Contracts\ApiGateway;
67

78
class FakeApiGateway implements ApiGateway
@@ -15,7 +16,7 @@ class FakeApiGateway implements ApiGateway
1516
public function search(string $password): int
1617
{
1718
if ($password === 'password1') {
18-
throw new \RuntimeException('Simulated network connectivity issue.');
19+
throw new RuntimeException('Simulated network connectivity issue.');
1920
}
2021

2122
return collect([

src/Api/PwnedPasswordsGateway.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ubient\PwnedPasswords\Api;
44

55
use GuzzleHttp\Client as GuzzleClient;
6+
use GuzzleHttp\Exception\GuzzleException;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Cache;
89
use RuntimeException;
@@ -37,8 +38,8 @@ public function search(string $password): int
3738
* and the value is the amount of times the password was pwned.
3839
*
3940
* @param string $hashPrefix
40-
* @throws RuntimeException
4141
* @return Collection
42+
* @throws RuntimeException|GuzzleException
4243
*/
4344
protected function fetchHashes(string $hashPrefix): Collection
4445
{

src/PwnedPasswordsServiceProvider.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ class PwnedPasswordsServiceProvider extends ServiceProvider
1414
{
1515
public function boot()
1616
{
17-
Validator::extend('pwned', Pwned::class, Pwned::VALIDATION_ERROR_MESSAGE);
17+
$this->publishes([__DIR__.'/../resources/lang' => resource_path('lang/vendor/pwned-passwords')], 'lang');
18+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'PwnedPasswords');
19+
20+
Validator::extend('pwned', Pwned::class);
1821
Validator::replacer('pwned', function ($message, $attribute, $rule, $parameters) {
19-
return str_replace(':num', array_shift($parameters) ?? 1, $message);
22+
return trans('PwnedPasswords::validation.pwned', [
23+
'attribute' => $attribute,
24+
'num' => array_shift($parameters) ?? 1,
25+
]);
2026
});
2127
}
2228

src/Rules/Pwned.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
class Pwned implements Rule
1111
{
12-
/**
13-
* The validation error message.
14-
*/
15-
const VALIDATION_ERROR_MESSAGE = ':attribute was found in at least :num prior security incident(s). Please choose a more secure password.';
16-
1712
/**
1813
* @var ApiGateway
1914
*/
@@ -43,7 +38,7 @@ public function __construct(int $threshold = 1)
4338
* @param mixed $value
4439
* @return bool
4540
*/
46-
public function passes($attribute, $value)
41+
public function passes($attribute, $value): bool
4742
{
4843
try {
4944
return $this->gateway->search($value) < $this->threshold;
@@ -61,7 +56,7 @@ public function passes($attribute, $value)
6156
* @param $parameters
6257
* @return bool
6358
*/
64-
public function validate($attribute, $value, $parameters)
59+
public function validate($attribute, $value, $parameters): bool
6560
{
6661
$this->threshold = (int) (array_shift($parameters) ?? 1);
6762

@@ -73,8 +68,8 @@ public function validate($attribute, $value, $parameters)
7368
*
7469
* @return string
7570
*/
76-
public function message()
71+
public function message(): string
7772
{
78-
return str_replace(':num', $this->threshold, self::VALIDATION_ERROR_MESSAGE);
73+
return trans('PwnedPasswords::validation.pwned', ['num' => $this->threshold]);
7974
}
8075
}

tests/Feature/PwnedPasswordsServiceProviderTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ class PwnedPasswordsServiceProviderTest extends TestCase
1212
/** @test */
1313
public function it_should_register_the_validation_error_message_for_our_pwned_rule_with_the_validator(): void
1414
{
15-
$errorMessage = Validator::make(['attribute' => 'P@ssw0rd'], ['attribute' => 'pwned:23'])->errors()->first();
15+
$errorMessage = Validator::make(
16+
['password' => 'P@ssw0rd'],
17+
['password' => 'pwned:23']
18+
)->errors()->first();
1619

1720
$this->assertEquals(
18-
'attribute was found in at least 23 prior security incident(s). Please choose a more secure password.',
21+
'password was found in at least 23 prior security incident(s). Please choose a more secure password.',
1922
$errorMessage
2023
);
2124
}

tests/Feature/PwnedTest.php

+19-6
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ public function passwords_that_are_pwned_and_are_above_the_threshold_should_be_r
8787
/** @test */
8888
public function it_should_show_the_validation_error_message_when_used_as_a_rule_object(): void
8989
{
90-
$validator = Validator::make(['my-password' => 'P@ssw0rd'], [
91-
'my-password' => new Pwned(75),
92-
]);
93-
94-
$errorMessage = $validator->errors()->first();
90+
$errorMessage = Validator::make(
91+
['my-password' => 'P@ssw0rd'],
92+
['my-password' => new Pwned(75)]
93+
)->errors()->first();
9594

9695
$this->assertEquals(
9796
'my-password was found in at least 75 prior security incident(s). Please choose a more secure password.',
@@ -100,7 +99,21 @@ public function it_should_show_the_validation_error_message_when_used_as_a_rule_
10099
}
101100

102101
/** @test */
103-
public function it_should_pass_the_validation_when_a_network_error_occurs_during_lookup()
102+
public function it_should_show_the_validation_error_message_when_used_as_a_string(): void
103+
{
104+
$errorMessage = Validator::make(
105+
['attr' => 'P@ssw0rd'],
106+
['attr' => 'pwned:75']
107+
)->errors()->first();
108+
109+
$this->assertEquals(
110+
'attr was found in at least 75 prior security incident(s). Please choose a more secure password.',
111+
$errorMessage
112+
);
113+
}
114+
115+
/** @test */
116+
public function it_should_pass_the_validation_when_a_network_error_occurs_during_lookup(): void
104117
{
105118
config([
106119
'logging.default' => 'test',

0 commit comments

Comments
 (0)