andrey-helldar / strong-password
This package provides a validator for ensuring strong passwords in Laravel applications
Fund package maintenance!
Patreon
Open Collective
paypal.me/helldar
yoomoney.ru/to/410012608840929
Requires
- php: ^7.1.3|^8.0
- illuminate/contracts: ^5.5|^6.0|^7.0|^8.0
- illuminate/support: ^5.5|^6.0|^7.0|^8.0
- illuminate/validation: ^5.5|^6.0|^7.0|^8.0
- kwn/number-to-words: ^1.12
Requires (Dev)
- orchestra/testbench: ^3.5|^4.0|^5.0|^6.0
- phpunit/phpunit: ^6.0|^7.0|^8.0|^9.0
Suggests
- symfony/thanks: Give thanks (in the form of a GitHub) to your fellow PHP package maintainers
README
This package provides a validator for ensuring strong passwords in Laravel applications.
In Laravel, since version 8.39.0, you can use the standard password functionality (#36960).
Installation
To get the latest version of Laravel Strong Password, simply require the project using Composer:
$ composer require andrey-helldar/strong-password
Or manually update require
block of composer.json
and run composer update
.
{ "require-dev": { "andrey-helldar/strong-password": "^1.0" } }
If you don't use auto-discovery, add the ServiceProvider to the providers array in app/Providers/AppServiceProvider.php
:
public function register() { $this->app->register(\Helldar\StrongPassword\ServiceProvider::class); }
You can also publish the config file to change implementations (ie. interface to specific class):
php artisan vendor:publish --provider="Helldar\StrongPassword\ServiceProvider"
Usage
Rules
Now, a Validator
facade is extended by few rules:
psw_letters
- The field must include at least one letter.psw_case_diff
- The field must include both upper and lower case letters.psw_numbers
- The field must include at least one number.psw_symbols
- The field must include at least one symbol.psw_min_length
- The field must be at least ten characters.psw_strong
- The field must contain at least two characters in the lower and upper registers, at least one digit and a special character, and at least ten characters (include all rules:psw_letters
,psw_case_diff
,psw_numbers
,psw_symbols
andpsw_min_length
).
// 1 $validator = \Validator::make(['foo' => 'qwerty'], ['foo' => 'psw_letters']); $validator->passes(); // return `true` // 2 $validator = \Validator::make(['bar' => 'qwerty'], ['bar' => 'psw_case_diff']); $validator->passes(); // return `false` // 3 $validator = \Validator::make(['baz' => 'qweRTY123!#'], ['baz' => 'psw_strong']); $validator->passes(); // return `true` // 4 $validator = \Validator::make(['baz' => 'qweRTY123!#'], ['baz' => 'psw_letters|psw_min_length']); $validator->passes(); // return `true`
Validation in context
You can also perform condition checking inside your code by accessing the Password
facade:
use Helldar\StrongPassword\Facades\Password; return Password::validate('qwerty'); return Password::errors('qwerty'); return Password::isAllow('qwerty');
For example, we will define the following rules in the config/strong-password.php file:
return [ 'min_length' => 27, 'rules' => [ 'psw_letters', 'psw_numbers', 'psw_min_length', ], ];
Thus, we will get the following results:
$password = 'qwerty'; return Password::validate($password); // throw ValidationException return Password::errors($password); // return array: // [ // 'password' => [ // 'The password must include at least one number.', // 'The password must be at least twenty-seven characters.', // ] // ] return Password::isAllow($password); // return false
and
$password = 'qWeRtYuIoP[]#!123qWeRtYuIqwd'; return Password::validate($password); // [ // 'password' => 'qWeRtYuIoP[]#!123qWeRtYuIqwd' // ] return Password::errors($password); // return null return Password::isAllow($password); // return true
License
This package is licensed under the MIT License.