-
Notifications
You must be signed in to change notification settings - Fork 16
/
FingerprintHelper.php
50 lines (36 loc) 路 1.1 KB
/
FingerprintHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Castor\Fingerprint;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\Cache\CacheInterface;
/** @internal */
class FingerprintHelper
{
private const SUFFIX = '.fingerprint';
public function __construct(
private readonly CacheItemPoolInterface&CacheInterface $cache,
) {
}
public function verifyFingerprintFromHash(string $fingerprint): bool
{
$itemKey = $fingerprint . self::SUFFIX;
if (false === $this->cache->hasItem($itemKey)) {
return false;
}
$cacheItem = $this->cache->getItem($itemKey);
if (false === $cacheItem->isHit()) {
return false;
}
if ($cacheItem->get() === $fingerprint) {
return true;
}
return false;
}
public function postProcessFingerprintForHash(string $hash): void
{
$itemKey = $hash . self::SUFFIX;
$cacheItem = $this->cache->getItem($itemKey);
$cacheItem->set($hash);
$cacheItem->expiresAt(new \DateTimeImmutable('+1 month'));
$this->cache->save($cacheItem);
}
}