evotodi/password-meter-bundle

Password meter for symfony 6 that complements https://github.com/HamedFathi/PasswordMeter and passwordmeter.com

7.0.0 2024-01-10 16:21 UTC

This package is auto-updated.

Last update: 2024-04-10 16:49:05 UTC


README

Symfony PhpStorm

Password Meter Symfony Bundle

PasswordMeter is a php equivalent clone of HamedFathi/PasswordMeter for javascript.

Installation

Install the package with:

composer require evotodi/password-meter-bundle

Usage

This bundle provides a single service to generate a password score like passwordmeter.com and HamedFathi/PasswordMeter.

Configuration

# config/packages/evotodi_password_meter.yaml
evotodi_password_meter:
    # Custom password requirements provider class
    requirements_provider: null
    
    # Custom password score range provider class
    score_provider: null

Implementing password requirements

The default requirements are null and will only return a score and status.
Creating a custom requirements provider will give and array of errors that match your custom requirements.

First create a class that implements Evotodi\PasswordMeterBundle\RequirementsInterface and implement the getRequirements method. From the getRequirements method return a new Evotodi\PasswordMeterBundle\Models\Requirements with your desired password requirements.

// src/Service/PasswordMeterRequirementsProvider.php

namespace App\Service;

use Evotodi\PasswordMeterBundle\Interfaces\RequirementsInterface;
use Evotodi\PasswordMeterBundle\Models\Requirements;

class PasswordMeterRequirementsProvider implements RequirementsInterface
{

	public function getRequirements(): Requirements
	{
		return new Requirements(minLength: 10);
	}
}

Then set the following config. You may need to create the config file if it does not exist.

# config/packages/evotodi_password_meter.yaml
evotodi_password_meter:
    requirements_provider: App\Service\PasswordMeterRequirementsProvider

Implementing custom password score range

Create a class that implements Evotodi\PasswordMeterBundle\ScoreRangeInterface and implement the getScoreRange method. From the getScoreRange method return a new array of score ranges.

// src/Service/PasswordMeterScoreProvider.php

namespace App\Service;

use Evotodi\PasswordMeterBundle\Interfaces\ScoreRangeInterface;

class PasswordMeterScoreProvider implements ScoreRangeInterface
{

	public function getScoreRange(): array
	{
		return [
            '40' => 'veryWeak', // 001 <= x <  040
            '80' => 'weak', // 040 <= x <  080
            '120' => 'medium', // 080 <= x <  120
            '180' => 'strong', // 120 <= x <  180
            '200' => 'veryStrong', // 180 <= x <  200
            '_' => 'perfect', //  >= 200
        ];
	}
}

The array must contain at least 2 elements and the last element key must be '_'.

Then set the following config. You may need to create the config file if it does not exist.

# config/packages/evotodi_password_meter.yaml
evotodi_password_meter:
    score_provider: App\Service\PasswordMeterScoreProvider

Contributions

Contributions are very welcome!

Please create detailed issues and pull requests.

Licence

This package is free software distributed under the terms of the MIT license.

Updates

2024-01-10: Add Symfony 7 support 2023-01-30: Initial release