andrey-helldar/strong-password

This package provides a validator for ensuring strong passwords in Laravel applications


Keywords
security, validation, password, laravel, strength, passwords, strong
License
MIT

Documentation

Laravel Strong Password

This package provides a validator for ensuring strong passwords in Laravel applications.

StyleCI Status Github Workflow Status For Laravel

Stable Version Unstable Version Total Downloads License

Contents

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 and psw_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