-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncryptedDriver.php
101 lines (80 loc) · 2.98 KB
/
EncryptedDriver.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace Snicco\Component\Session\Driver;
use BadMethodCallException;
use Snicco\Component\Session\SessionEncryptor;
use Snicco\Component\Session\ValueObject\SerializedSession;
final class EncryptedDriver implements UserSessionsDriver
{
private SessionDriver $driver;
private SessionEncryptor $encryptor;
public function __construct(SessionDriver $driver, SessionEncryptor $encryptor)
{
$this->driver = $driver;
$this->encryptor = $encryptor;
}
public function destroy(string $selector): void
{
$this->driver->destroy($selector);
}
public function gc(int $seconds_without_activity): void
{
$this->driver->gc($seconds_without_activity);
}
public function read(string $selector): SerializedSession
{
$encrypted_session = $this->driver->read($selector);
return SerializedSession::fromString(
$this->encryptor->decrypt($encrypted_session->data()),
$encrypted_session->hashedValidator(),
$encrypted_session->lastActivity(),
$encrypted_session->userId()
);
}
public function touch(string $selector, int $current_timestamp): void
{
$this->driver->touch($selector, $current_timestamp);
}
public function write(string $selector, SerializedSession $session): void
{
$data = $session->data();
$encrypted_data = $this->encryptor->encrypt($data);
$this->driver->write(
$selector,
SerializedSession::fromString(
$encrypted_data,
$session->hashedValidator(),
$session->lastActivity(),
$session->userId()
)
);
}
public function destroyAllForAllUsers(): void
{
if (! $this->driver instanceof UserSessionsDriver) {
throw new BadMethodCallException(__METHOD__ . ' needs an implementation of ' . UserSessionsDriver::class);
}
$this->driver->destroyAllForAllUsers();
}
public function destroyAllForUserId($user_id): void
{
if (! $this->driver instanceof UserSessionsDriver) {
throw new BadMethodCallException(__METHOD__ . ' needs an implementation of ' . UserSessionsDriver::class);
}
$this->driver->destroyAllForUserId($user_id);
}
public function destroyAllForUserIdExcept(string $selector, $user_id): void
{
if (! $this->driver instanceof UserSessionsDriver) {
throw new BadMethodCallException(__METHOD__ . ' needs an implementation of ' . UserSessionsDriver::class);
}
$this->driver->destroyAllForUserIdExcept($selector, $user_id);
}
public function getAllForUserId($user_id): iterable
{
if (! $this->driver instanceof UserSessionsDriver) {
throw new BadMethodCallException(__METHOD__ . ' needs an implementation of ' . UserSessionsDriver::class);
}
return $this->driver->getAllForUserId($user_id);
}
}