-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UML-2725 implement check for pwned passwords
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
service-front/app/src/Common/src/Validator/CommonPasswordValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Common\Validator; | ||
|
||
use Laminas\Validator\AbstractValidator; | ||
use RuntimeException; | ||
|
||
class CommonPasswordValidator extends AbstractValidator | ||
{ | ||
public const COMMON_PASSWORD = 'common'; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected array $messageTemplates = [ | ||
self::COMMON_PASSWORD => 'Password is too common', | ||
]; | ||
|
||
|
||
const TMP_ROOT_PATH = '/tmp/'; | ||
const PWNED_PW_URL = 'https://www.ncsc.gov.uk/static-assets/documents/PwnedPasswordsTop100k.txt'; | ||
|
||
private string $filePathCommonPasswords; | ||
|
||
private string $pwnedPasswordsUrl; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->filePathCommonPasswords = self::TMP_ROOT_PATH.'commonpasswords.txt'; | ||
$this->pwnedPasswordsUrl = self::PWNED_PW_URL; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid($value): bool | ||
{ | ||
$isValid = true; | ||
$this->checkCommonPasswordsFileExists($this->filePathCommonPasswords); | ||
|
||
if ($this->passwordMatchesCommonPasswords($value, $this->filePathCommonPasswords)) { | ||
$this->error(self::COMMON_PASSWORD); | ||
$isValid = false; | ||
} | ||
|
||
return $isValid; | ||
} | ||
|
||
protected function passwordMatchesCommonPasswords(string $searchTerm, string $filePath): bool | ||
{ | ||
$matches = []; | ||
$handle = @fopen($filePath, 'r'); | ||
if ($handle && strlen($searchTerm) > 0) { | ||
while (!feof($handle)) { | ||
$buffer = fgets($handle); | ||
if (false !== strpos($buffer, $searchTerm)) { | ||
$matches[] = $buffer; | ||
} | ||
} | ||
fclose($handle); | ||
} | ||
//show results: | ||
if (count($matches) > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
protected function checkCommonPasswordsFileExists(string $filePath): void | ||
{ | ||
if (file_exists($filePath) & (time() - filemtime($filePath) < 24 * 3600)) { | ||
return; | ||
} else { | ||
$fp = fopen($this->pwnedPasswordsUrl, 'r'); | ||
if (false !== $fp) { | ||
$written = file_put_contents( | ||
"$filePath", | ||
$fp | ||
); | ||
if (false === $written) { | ||
throw new RuntimeException(sprintf('Unable to download or write common password file to disk')); | ||
} | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
service-front/app/test/CommonTest/Validator/CommonPasswordValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommonTest\Validator; | ||
|
||
use Common\Validator\CommonPasswordValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CommonPasswordValidatorTest extends TestCase | ||
{ | ||
const PW_FILE_PATH = '/tmp/commonpasswords.txt'; | ||
const PWNED_PW_URL = 'https://www.ncsc.gov.uk/static-assets/documents/PwnedPasswordsTop100k.txt'; | ||
|
||
public function setUp(): void | ||
{ | ||
file_put_contents( | ||
self::PW_FILE_PATH, | ||
fopen(self::PWNED_PW_URL, 'r') | ||
); | ||
|
||
$this->validator = new CommonPasswordValidator(); | ||
} | ||
|
||
/** | ||
* Verify a constraint message is triggered when value is invalid. | ||
*/ | ||
public function testValidateOnInvalid() | ||
{ | ||
$this->assertFalse($this->validator->isValid('Password123')); | ||
print(count($this->validator->getMessages())); | ||
$this->assertArrayHasKey(CommonPasswordValidator::COMMON_PASSWORD, $this->validator->getMessages()); | ||
} | ||
|
||
|
||
/** | ||
* Verify no constraint message is triggered when value is valid. | ||
*/ | ||
public function testValidateOnValid() | ||
{ | ||
$this->assertTrue($this->validator->isValid('Aformidablepw876!')); | ||
$this->assertCount(0, $this->validator->getMessages()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters