1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PasswordHelper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Facade class that provides backward compatibility with the old API. |
9
|
|
|
* |
10
|
|
|
* This class wraps the new implementation while maintaining the same |
11
|
|
|
* public interface as the original Password Helper. |
12
|
|
|
* |
13
|
|
|
* @package PasswordHelper |
14
|
|
|
*/ |
15
|
|
|
class Password |
16
|
|
|
{ |
17
|
|
|
private Policy $policy; |
18
|
|
|
private Generator $generator; |
19
|
|
|
private Validator $validator; |
20
|
|
|
private StrengthChecker $strengthChecker; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Creates a new password helper with the specified configuration. |
24
|
|
|
* |
25
|
|
|
* @param array<string, int> $config Optional configuration to override defaults |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $config = []) |
28
|
|
|
{ |
29
|
|
|
$this->policy = new Policy( |
30
|
|
|
$config['minimumLength'] ?? 10, |
31
|
|
|
20, // maximumLength |
32
|
|
|
$this->calculateMinimumCharacterTypes($config), |
33
|
|
|
false, // allowRepeatedCharacters |
34
|
|
|
false, // allowSequentialCharacters |
35
|
|
|
false // allowCommonPatterns |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->generator = new Generator( |
39
|
|
|
$this->policy->getMinimumLength(), |
40
|
|
|
$this->policy->getMaximumLength() |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$this->validator = new Validator($this->policy); |
44
|
|
|
$this->strengthChecker = new StrengthChecker(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Generates a new password that meets the policy requirements. |
49
|
|
|
* |
50
|
|
|
* @return string The generated password |
51
|
|
|
*/ |
52
|
|
|
public function generate(): string |
53
|
|
|
{ |
54
|
|
|
return $this->generator->generate(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Validates that a password meets the complexity requirements. |
59
|
|
|
* |
60
|
|
|
* @param string $password The password to validate |
61
|
|
|
* @return bool True if the password meets all requirements |
62
|
|
|
*/ |
63
|
|
|
public function validateComplexity(string $password): bool |
64
|
|
|
{ |
65
|
|
|
return $this->validator->isValidPassword($password); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks the strength of a password and returns a descriptive rating. |
70
|
|
|
* |
71
|
|
|
* @param string $password The password to check |
72
|
|
|
* @return string The strength rating: "Very Weak", "Weak", "Fair", "Good", "Strong", or "Very Strong" |
73
|
|
|
*/ |
74
|
|
|
public function checkStrength(string $password): string |
75
|
|
|
{ |
76
|
|
|
$score = $this->strengthChecker->checkStrength($password); |
77
|
|
|
|
78
|
|
|
return match (true) { |
79
|
|
|
$score < 20 => 'Very Weak', |
80
|
|
|
$score < 40 => 'Weak', |
81
|
|
|
$score < 60 => 'Fair', |
82
|
|
|
$score < 80 => 'Good', |
83
|
|
|
$score < 90 => 'Strong', |
84
|
|
|
default => 'Very Strong' |
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Hashes a password using PHP's password_hash function. |
90
|
|
|
* |
91
|
|
|
* @param string $password The password to hash |
92
|
|
|
* @return string The hashed password |
93
|
|
|
*/ |
94
|
|
|
public function hash(string $password): string |
95
|
|
|
{ |
96
|
|
|
return password_hash($password, PASSWORD_DEFAULT); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Verifies a password against a hash. |
101
|
|
|
* |
102
|
|
|
* @param string $password The password to verify |
103
|
|
|
* @param string $hash The hash to verify against |
104
|
|
|
* @return bool True if the password matches the hash |
105
|
|
|
*/ |
106
|
|
|
public function verify(string $password, string $hash): bool |
107
|
|
|
{ |
108
|
|
|
return password_verify($password, $hash); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Checks if a hash needs to be rehashed. |
113
|
|
|
* |
114
|
|
|
* @param string $hash The hash to check |
115
|
|
|
* @return bool True if the hash should be rehashed |
116
|
|
|
*/ |
117
|
|
|
public function checkForRehash(string $hash): bool |
118
|
|
|
{ |
119
|
|
|
return password_needs_rehash($hash, PASSWORD_DEFAULT); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Calculates the minimum character types based on the old configuration. |
124
|
|
|
* |
125
|
|
|
* @param array<string, int> $config The configuration array |
126
|
|
|
* @return int The minimum number of character types required |
127
|
|
|
*/ |
128
|
|
|
private function calculateMinimumCharacterTypes(array $config): int |
129
|
|
|
{ |
130
|
|
|
$types = 0; |
131
|
|
|
|
132
|
|
|
if (($config['minimumDigits'] ?? 1) > 0) { |
133
|
|
|
$types++; |
134
|
|
|
} |
135
|
|
|
if (($config['minimumSpecialChars'] ?? 1) > 0) { |
136
|
|
|
$types++; |
137
|
|
|
} |
138
|
|
|
if (($config['minimumUppercase'] ?? 1) > 0 || ($config['minimumLowercase'] ?? 1) > 0) { |
139
|
|
|
$types++; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $types; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|