Skip to content

Commit

Permalink
Pass PasswordHashingAlgorithm to StorageAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Jan 2, 2024
1 parent 621265b commit aed6f6b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
16 changes: 7 additions & 9 deletions site/app/Pulse/Passwords/Storage/StorageAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MichalSpacekCz\Pulse\Passwords\Storage;

use DateTime;
use MichalSpacekCz\Pulse\Passwords\Algorithms\PasswordHashingAlgorithm;

class StorageAlgorithm
{
Expand All @@ -21,18 +22,15 @@ class StorageAlgorithm

public function __construct(
private readonly string $id,
private readonly string $name,
private readonly string $alias,
private readonly bool $salted,
private readonly bool $stretched,
private readonly PasswordHashingAlgorithm $hashingAlgorithm,
private readonly ?DateTime $from,
private readonly bool $fromConfirmed,
private readonly StorageAlgorithmAttributes $attributes,
private readonly ?string $note,
StorageDisclosure $disclosure,
) {
$this->addDisclosure($disclosure);
$this->fullAlgo = $this->formatFullAlgo($this->name, $this->attributes->getInner(), $this->attributes->getOuter());
$this->fullAlgo = $this->formatFullAlgo($this->hashingAlgorithm->getName(), $this->attributes->getInner(), $this->attributes->getOuter());
}


Expand Down Expand Up @@ -78,25 +76,25 @@ public function getId(): string

public function getName(): string
{
return $this->name;
return $this->hashingAlgorithm->getName();
}


public function getAlias(): string
{
return $this->alias;
return $this->hashingAlgorithm->getAlias();
}


public function isSalted(): bool
{
return $this->salted;
return $this->hashingAlgorithm->isSalted();
}


public function isStretched(): bool
{
return $this->stretched;
return $this->hashingAlgorithm->isStretched();
}


Expand Down
6 changes: 4 additions & 2 deletions site/app/Pulse/Passwords/Storage/StorageRegistryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace MichalSpacekCz\Pulse\Passwords\Storage;

use MichalSpacekCz\Pulse\Company;
use MichalSpacekCz\Pulse\Passwords\Algorithms\PasswordHashingAlgorithm;
use MichalSpacekCz\Pulse\Passwords\PasswordsSorting;
use MichalSpacekCz\Pulse\Passwords\Rating;
use MichalSpacekCz\Pulse\Sites;
Expand Down Expand Up @@ -39,13 +40,14 @@ public function get(array $data, string $sort): StorageRegistry
foreach ($data as $row) {
$siteId = $this->sites->generateId($row->siteId, $row->companyId);
$storageKey = $this->sorting->isAnyCompanyAlphabetically($sort) ? (string)$row->companyId : $siteId;
$algoKey = $row->algoId . '-' . ($row->from !== null ? $row->from->getTimestamp() : 'null');
$hashingAlgorithm = new PasswordHashingAlgorithm($row->algoId, $row->algoName, $row->algoAlias, (bool)$row->algoSalted, (bool)$row->algoStretched);
$algoKey = $hashingAlgorithm->getId() . '-' . ($row->from !== null ? $row->from->getTimestamp() : 'null');

if (!$registry->hasCompany($row->companyId)) {
$registry->addCompany(new Company($row->companyId, $row->companyName, $row->tradeName, $row->companyAlias, $row->sortName));
}
$disclosure = new StorageDisclosure($row->disclosureId, $row->disclosureUrl, $row->disclosureArchive, $row->disclosureNote, $row->disclosurePublished, $row->disclosureAdded, $row->disclosureType, $row->disclosureTypeAlias);
$algorithm = new StorageAlgorithm($algoKey, $row->algoName, $row->algoAlias, (bool)$row->algoSalted, (bool)$row->algoStretched, $row->from, (bool)$row->fromConfirmed, $this->algorithmAttributesFactory->get($row->attributes), $row->note, $disclosure);
$algorithm = new StorageAlgorithm($algoKey, $hashingAlgorithm, $row->from, (bool)$row->fromConfirmed, $this->algorithmAttributesFactory->get($row->attributes), $row->note, $disclosure);
$addSite = !$registry->hasSite($siteId);
if ($addSite) {
if ($row->siteId === null) {
Expand Down
3 changes: 2 additions & 1 deletion site/tests/Pulse/Passwords/RatingTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace MichalSpacekCz\Pulse\Passwords;

use DateTime;
use MichalSpacekCz\Pulse\Passwords\Algorithms\PasswordHashingAlgorithm;
use MichalSpacekCz\Pulse\Passwords\Storage\StorageAlgorithm;
use MichalSpacekCz\Pulse\Passwords\Storage\StorageAlgorithmAttributes;
use MichalSpacekCz\Pulse\Passwords\Storage\StorageDisclosure;
Expand Down Expand Up @@ -118,7 +119,7 @@ class RatingTest extends TestCase
private function getAlgo(string $alias, bool $salted, bool $stretched, array $disclosureTypes): StorageAlgorithm
{
$disclosure = new StorageDisclosure(123, 'https://example.com/', 'https://archive.example.com', null, new DateTime('yesterday'), new DateTime(), 'type', array_shift($disclosureTypes));
$algorithm = new StorageAlgorithm('1', 'foo', $alias, $salted, $stretched, new DateTime(), true, new StorageAlgorithmAttributes(null, null, null), null, $disclosure);
$algorithm = new StorageAlgorithm('1', new PasswordHashingAlgorithm(9, 'foo', $alias, $salted, $stretched), new DateTime(), true, new StorageAlgorithmAttributes(null, null, null), null, $disclosure);
foreach ($disclosureTypes as $typeAlias) {
$algorithm->addDisclosure(new StorageDisclosure(123, 'https://example.com/', 'https://archive.example.com', null, new DateTime('yesterday'), new DateTime(), 'type', $typeAlias));
}
Expand Down
3 changes: 2 additions & 1 deletion site/tests/Pulse/Passwords/Storage/StorageAlgorithmTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types = 1);
namespace MichalSpacekCz\Pulse\Passwords\Storage;

use DateTime;
use MichalSpacekCz\Pulse\Passwords\Algorithms\PasswordHashingAlgorithm;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\TestCase;
Expand Down Expand Up @@ -67,7 +68,7 @@ class StorageAlgorithmTest extends TestCase
{
$disclosure = new StorageDisclosure(123, 'https://example.com/', 'https://archive.example.com', null, new DateTime('yesterday'), new DateTime(), 'type', 'docs');
$attributes = new StorageAlgorithmAttributes($inner, $outer, null);
$algorithm = new StorageAlgorithm('1', self::ALGO, self::ALGO, true, true, new DateTime(), true, $attributes, null, $disclosure);
$algorithm = new StorageAlgorithm('1', new PasswordHashingAlgorithm(21, self::ALGO, self::ALGO, true, true), new DateTime(), true, $attributes, null, $disclosure);
Assert::same($expected, $algorithm->getFullAlgo());
}

Expand Down

0 comments on commit aed6f6b

Please sign in to comment.