|
| 1 | +# Pwned Passwords |
| 2 | + |
| 3 | +[](https://github.com/ubient/laravel-pwned-passwords/releases) |
| 4 | +[](https://travis-ci.org/ubient/laravel-pwned-passwords) |
| 5 | +[](https://scrutinizer-ci.com/g/ubient/laravel-pwned-passwords) |
| 6 | +[](https://styleci.io/repos/151966705) |
| 7 | +[](https://packagist.org/packages/ubient/laravel-pwned-passwords) |
| 8 | + |
| 9 | +This package provides a Laravel validation rule that can be used to check a password |
| 10 | +against TroyHunt's [Pwned Passwords (haveibeenpwned.com)](https://haveibeenpwned.com/Passwords), |
| 11 | +a database containing 517,238,891 real world passwords previously exposed in data breaches. |
| 12 | + |
| 13 | +By using this validation rule, you can prevent re-use of passwords that are unsuitable for ongoing usage, |
| 14 | +resulting in a more secure application, as your users will have a much lower risk of having their accounts taken over. |
| 15 | + |
| 16 | +##### How it works |
| 17 | + |
| 18 | +In order to protect the value of the source password being searched for, Pwned Passwords implements a [k-Anonymity model](https://en.wikipedia.org/wiki/K-anonymity) that allows a password to be searched for by partial hash. |
| 19 | +This works by hashing the source password with SHA-1, and only sending the first 5 characters of that hash to the API. |
| 20 | +By checking whether the rest of the SHA-1 hash occurs within the output, we can verify both whether the password was pwned previously, and how frequently. |
| 21 | + |
| 22 | +### Usage |
| 23 | + |
| 24 | +Here's a few short examples of what you can do: |
| 25 | + |
| 26 | +```php |
| 27 | +/** |
| 28 | + * Get a validator for an incoming registration request. |
| 29 | + * |
| 30 | + * @param array $data |
| 31 | + * @return \Illuminate\Contracts\Validation\Validator |
| 32 | + */ |
| 33 | +protected function validator(array $data) |
| 34 | +{ |
| 35 | + return Validator::make($data, [ |
| 36 | + 'name' => 'required|string|max:255', |
| 37 | + 'email' => 'required|string|email|max:255|unique:users', |
| 38 | + 'password' => 'required|string|min:6|confirmed|pwned', |
| 39 | + ]); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +You can also relax the rule, allowing passwords that have been pwned multiple times. |
| 44 | +In the example below, passwords that have been pwned between 0 and 4 times are allowed: |
| 45 | + |
| 46 | +```php |
| 47 | +$request->validate([ |
| 48 | + 'password' => 'required|string|min:6|confirmed|pwned:5', |
| 49 | +]); |
| 50 | +``` |
| 51 | + |
| 52 | +Of course, you can also use a Rule object instead: |
| 53 | + |
| 54 | +```php |
| 55 | +use Ubient\PwnedPasswords\Rules\Pwned; |
| 56 | + |
| 57 | +$request->validate([ |
| 58 | + 'password' => ['required', 'string', 'min:6', 'confirmed', new Pwned(5)], |
| 59 | +]); |
| 60 | +``` |
| 61 | + |
| 62 | +## Installation |
| 63 | + |
| 64 | +You can install the package via composer: |
| 65 | + |
| 66 | +```bash |
| 67 | +composer require ubient/laravel-pwned-passwords |
| 68 | +``` |
| 69 | + |
| 70 | +### Testing |
| 71 | + |
| 72 | +``` bash |
| 73 | +composer test |
| 74 | +``` |
| 75 | + |
| 76 | +### Changelog |
| 77 | + |
| 78 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 79 | + |
| 80 | +## Contributing |
| 81 | + |
| 82 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 83 | + |
| 84 | +### Security |
| 85 | + |
| 86 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments