-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSecureCookieProvider.php
53 lines (45 loc) · 1.31 KB
/
SecureCookieProvider.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
51
52
53
<?php
namespace NPR\One\Providers;
use NPR\One\Interfaces\EncryptionInterface;
/**
* An extension of CookieProvider that encrypts cookies before setting them and decrypts them when retrieving them
*
* @package NPR\One\Providers
* @codeCoverageIgnore
*/
class SecureCookieProvider extends CookieProvider
{
/** @var EncryptionInterface - the encryption provider to use when encrypting and decrypting the cookies
* @internal */
private $encryptionProvider;
/**
* Sets the encryption provider that will be used to encrypt cookies before setting them and decrypt cookies when
* retrieving them.
*
* @param EncryptionInterface $encryptionProvider
*/
public function setEncryptionProvider(EncryptionInterface $encryptionProvider)
{
$this->encryptionProvider = $encryptionProvider;
}
/**
* {@inheritdoc}
*/
public function set($key, $value, $expiresIn = null)
{
$encryptedValue = $this->encryptionProvider->encrypt($value);
parent::set($key, $encryptedValue, $expiresIn);
}
/**
* {@inheritdoc}
*/
public function get($key): ?string
{
$value = parent::get($key);
if (!empty($value))
{
return $this->encryptionProvider->decrypt($value);
}
return $value;
}
}