Skip to content

Commit

Permalink
Merge pull request #59 from in2code-de/feature/ip-enrich-fingerprint
Browse files Browse the repository at this point in the history
Enrich fingerprint hash with IP address
  • Loading branch information
einpraegsam authored Oct 23, 2024
2 parents ed47293 + 50e33ab commit a6212e8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Classes/Domain/Model/Fingerprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use In2code\Lux\Exception\FingerprintMustNotBeEmptyException;
use In2code\Lux\Utility\BackendUtility;
use In2code\Lux\Utility\EnvironmentUtility;
use In2code\Lux\Utility\IpUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use WhichBrowser\Parser;

Expand Down Expand Up @@ -51,8 +52,10 @@ public function setValue(string $value): self
}
if (strlen($value) === 33) {
$this->setType(self::TYPE_STORAGE);
$this->value = $value;
} else {
$this->value = hash('sha256', $value . IpUtility::getIpAddress());
}
$this->value = $value;
return $this;
}

Expand Down
92 changes: 92 additions & 0 deletions Tests/Unit/Domain/Model/FingerprintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace In2code\Lux\Tests\Unit\Domain\Model;

use In2code\Lux\Domain\Model\Fingerprint;
use In2code\Lux\Tests\Helper\TestingHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
* @coversDefaultClass \In2code\Lux\Domain\Model\Fingerprint
*/
class FingerprintTest extends UnitTestCase
{
protected bool $resetSingletonInstances = true;

public function setUp(): void
{
parent::setUp();
TestingHelper::setDefaultConstants();
}

/**
* @return void
* @covers ::setValue
*/
public function testFingerprintType(): void
{
$fingerprint = new Fingerprint();

$fingerprint->setValue(random_bytes(32));
self::assertEquals($fingerprint->getType(), Fingerprint::TYPE_FINGERPRINT);

$fingerprint->setValue(random_bytes(33));
self::assertEquals($fingerprint->getType(), Fingerprint::TYPE_STORAGE);
}

/**
* @return void
* @covers ::setValue
*/
public function testAssertSameHashesWithSameIps(): void
{
$fingerprint = new Fingerprint();
$identifier = bin2hex(random_bytes(16));

$fingerprint->setValue($identifier);
$value1 = $fingerprint->getValue();

$fingerprint->setValue($identifier);
$value2 = $fingerprint->getValue();

self::assertEquals($value1, $value2);
}

/**
* @return void
* @covers ::setValue
*/
public function testAssertDifferentHashesWithDifferentIps(): void
{
$fingerprint = new Fingerprint();
$identifier = random_bytes(32);

GeneralUtility::setIndpEnv('REMOTE_ADDR', '192.168.178.1');
$fingerprint->setValue($identifier);

$value1 = $fingerprint->getValue();

GeneralUtility::setIndpEnv('REMOTE_ADDR', '192.168.178.16');
$fingerprint->setValue($identifier);

$value2 = $fingerprint->getValue();

self::assertNotEquals($value1, $value2);
}

/**
* @return void
* @covers ::setValue
*/
public function testAssertNoHashingForStorageType(): void
{
$fingerprint = new Fingerprint();
$identifier = random_bytes(33);

$fingerprint->setValue($identifier);
$value = $fingerprint->getValue();

self::assertEquals($identifier, $value);
}
}

0 comments on commit a6212e8

Please sign in to comment.