-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheNonceStorage.php
41 lines (33 loc) · 1.21 KB
/
CacheNonceStorage.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
<?php declare(strict_types=1);
namespace danielburger1337\OAuth2\DPoP\NonceStorage;
use Psr\Cache\CacheItemPoolInterface;
class CacheNonceStorage implements NonceStorageInterface
{
/**
* @param CacheItemPoolInterface $cache The PSR-6 cache used as the storage engine.
* @param \DateInterval $ttl How long the nonce must be cached.
* This value should ideally match the lifetime of the nonce.
* Consult the documentation of your upstream server.
*/
public function __construct(
private readonly CacheItemPoolInterface $cache,
private readonly \DateInterval $ttl = new \DateInterval('PT5M'),
) {
}
public function getCurrentNonce(string $key): ?string
{
$item = $this->cache->getItem($key);
if (!$item->isHit()) {
return null;
}
$nonce = $item->get();
return \is_string($nonce) ? $nonce : null;
}
public function storeNextNonce(string $key, string $nonce): void
{
$item = $this->cache->getItem($key);
$item->set($nonce);
$item->expiresAfter($this->ttl);
$this->cache->save($item);
}
}