Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning from user input #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class Feedback
/**
* @param int $score
* @param MatchInterface[] $sequence
* @return array
* @return array{
* warning: string,
* suggestions: string[]
* }
*/
public function getFeedback(int $score, array $sequence): array
{
Expand Down
6 changes: 4 additions & 2 deletions src/Matchers/BaseMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public function __construct(string $password, int $begin, int $end, string $toke
*
* @param bool $isSoleMatch
* Whether this is the only match in the password
* @return array
* Associative array with warning (string) and suggestions (array of strings)
* @return array{
* warning: string,
* suggestions: string[]
* } Associative array with warning (string) and suggestions (array of strings)
*/
#[ArrayShape(['warning' => 'string', 'suggestions' => 'string[]'])]
abstract public function getFeedback(bool $isSoleMatch): array;
Expand Down
2 changes: 2 additions & 0 deletions src/Matchers/DictionaryMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public function getFeedback(bool $isSoleMatch): array
public function getFeedbackWarning(bool $isSoleMatch): string
{
switch ($this->dictionaryName) {
case 'user_inputs':
return 'This is similar to, or incorporates parts of, other input';
case 'passwords':
if ($isSoleMatch && !$this->l33t && !$this->reversed) {
if ($this->rank <= 10) {
Expand Down
14 changes: 14 additions & 0 deletions test/FeedbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ZxcvbnPhp\Feedback;
use ZxcvbnPhp\Matchers\Bruteforce;
use ZxcvbnPhp\Matchers\DateMatch;
use ZxcvbnPhp\Matchers\DictionaryMatch;
use ZxcvbnPhp\Matchers\SequenceMatch;

class FeedbackTest extends TestCase
Expand Down Expand Up @@ -104,4 +105,17 @@ public function testBruteforceFeedback()
"bruteforce match only has the default suggestion"
);
}

public function testFeedbackFromUserInput()
{
$match = new DictionaryMatch('user_input_password', 0, 19, 'user_input_password', [
'dictionary_name' => 'user_inputs',
'matched_word' => 'user_input_password',
'rank' => '1'
]);
$feedback = $this->feedback->getFeedback(0, [$match]);

$this->assertEquals('This is similar to, or incorporates parts of, other input', $feedback['warning'], 'no warning for user input');
$this->assertNotEmpty($feedback['suggestions'], 'no suggestions for user input');
}
}