-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDoctrineCipherKeyStore.php
More file actions
108 lines (90 loc) · 3.04 KB
/
Copy pathDoctrineCipherKeyStore.php
File metadata and controls
108 lines (90 loc) · 3.04 KB
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
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Cryptography;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Patchlevel\EventSourcing\Schema\DoctrineHelper;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator;
use Patchlevel\Hydrator\Cryptography\Cipher\CipherKey;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyNotExists;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyStore;
use function array_key_exists;
use function base64_decode;
use function base64_encode;
/**
* @psalm-type Row = array{
* subject_id: non-empty-string,
* crypto_key: non-empty-string,
* crypto_method: non-empty-string,
* crypto_iv: non-empty-string
* }
*/
final class DoctrineCipherKeyStore implements CipherKeyStore, DoctrineSchemaConfigurator
{
/** @var array<string, CipherKey> */
private array $keyCache = [];
public function __construct(
private readonly Connection $connection,
private readonly string $tableName = 'crypto_keys',
) {
}
public function get(string $id): CipherKey
{
if (array_key_exists($id, $this->keyCache)) {
return $this->keyCache[$id];
}
/** @var Row|false $result */
$result = $this->connection->fetchAssociative(
"SELECT * FROM {$this->tableName} WHERE subject_id = :subject_id",
['subject_id' => $id],
);
if ($result === false) {
throw new CipherKeyNotExists($id);
}
$this->keyCache[$id] = new CipherKey(
base64_decode($result['crypto_key']),
$result['crypto_method'],
base64_decode($result['crypto_iv']),
);
return $this->keyCache[$id];
}
public function store(string $id, CipherKey $key): void
{
$this->connection->insert($this->tableName, [
'subject_id' => $id,
'crypto_key' => base64_encode($key->key),
'crypto_method' => $key->method,
'crypto_iv' => base64_encode($key->iv),
]);
$this->keyCache[$id] = $key;
}
public function remove(string $id): void
{
$this->connection->delete($this->tableName, ['subject_id' => $id]);
unset($this->keyCache[$id]);
}
public function configureSchema(Schema $schema, Connection $connection): void
{
if (!DoctrineHelper::sameDatabase($this->connection, $connection)) {
return;
}
$table = $schema->createTable($this->tableName);
$table->addColumn('subject_id', 'string')
->setNotnull(true)
->setLength(255);
$table->addColumn('crypto_key', 'string')
->setNotnull(true)
->setLength(255);
$table->addColumn('crypto_method', 'string')
->setNotnull(true)
->setLength(255);
$table->addColumn('crypto_iv', 'string')
->setNotnull(true)
->setLength(255);
$table->setPrimaryKey(['subject_id']);
}
public function clear(): void
{
$this->keyCache = [];
}
}