|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Olssonm\Zxcvbn; |
|
4
|
|
|
|
|
5
|
|
|
use ZxcvbnPhp\Zxcvbn as ZxcvbnPhp; |
|
6
|
|
|
use Illuminate\Support\ServiceProvider; |
|
7
|
|
|
use Validator; |
|
8
|
|
|
|
|
9
|
|
|
class ZxcvbnServiceProvider extends ServiceProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Perform post-registration booting of services. |
|
13
|
|
|
* |
|
14
|
|
|
* @return void |
|
15
|
|
|
*/ |
|
16
|
|
|
public function boot() |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Extend the Laravel Validator with the "zxcvbn_min" rule |
|
20
|
|
|
*/ |
|
21
|
|
|
Validator::extend('zxcvbn_min', function($attribute, $value, $parameters) { |
|
22
|
|
|
$zxcvbn = new ZxcvbnPhp(); |
|
23
|
|
|
$zxcvbn = $zxcvbn->passwordStrength($value); |
|
24
|
|
|
$target = 5; |
|
25
|
|
|
|
|
26
|
|
|
if (isset($parameters[0])) { |
|
27
|
|
|
$target = $parameters[0]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return ($zxcvbn['score'] >= $target); |
|
31
|
|
|
}, 'Your :attribute is not secure enough.'); |
|
32
|
|
|
|
|
33
|
|
|
Validator::replacer('zxcvbn_min', function($message, $attribute) { |
|
34
|
|
|
$message = str_replace(':attribute', $attribute, $message); |
|
35
|
|
|
return $message; |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Extend the Laravel Validator with the "zxcvbn_min" rule |
|
40
|
|
|
*/ |
|
41
|
|
|
Validator::extend('zxcvbn_dictionary', function($attribute, $value, $parameters) { |
|
42
|
|
|
$email = null; |
|
43
|
|
|
$username = null; |
|
44
|
|
|
|
|
45
|
|
|
if (isset($parameters[0])) { |
|
46
|
|
|
$email = $parameters[0]; |
|
47
|
|
|
$username = $parameters[1]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$zxcvbn = new ZxcvbnPhp(); |
|
51
|
|
|
$zxcvbn = $zxcvbn->passwordStrength($value, [$username, $email]); |
|
52
|
|
|
|
|
53
|
|
|
if (isset($zxcvbn['sequence'][0])) { |
|
54
|
|
|
$dictionary = $zxcvbn['sequence'][0]; |
|
55
|
|
|
if (isset($dictionary->dictionaryName)) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return true; |
|
61
|
|
|
|
|
62
|
|
|
}, 'Your :attribute is insecure. It either matches a commonly used password, or you have used a similar username/password combination.'); |
|
63
|
|
|
|
|
64
|
|
|
Validator::replacer('zxcvbn_dictionary', function($message, $attribute) { |
|
65
|
|
|
$message = str_replace(':attribute', $attribute, $message); |
|
66
|
|
|
return $message; |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Register any package services. |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public function register() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->app->bind('zxcvbn', function() { |
|
78
|
|
|
return new ZxcvbnPhp(); |
|
79
|
|
|
}); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|