-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPersonalDataPayloadCryptographer.php
More file actions
208 lines (170 loc) · 6.2 KB
/
Copy pathPersonalDataPayloadCryptographer.php
File metadata and controls
208 lines (170 loc) · 6.2 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Cryptography;
use Patchlevel\Hydrator\Cryptography\Cipher\Cipher;
use Patchlevel\Hydrator\Cryptography\Cipher\CipherKeyFactory;
use Patchlevel\Hydrator\Cryptography\Cipher\DecryptionFailed;
use Patchlevel\Hydrator\Cryptography\Cipher\OpensslCipher;
use Patchlevel\Hydrator\Cryptography\Cipher\OpensslCipherKeyFactory;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyNotExists;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyStore;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Metadata\PropertyMetadata;
use Stringable;
use function array_key_exists;
use function is_int;
use function is_string;
final class PersonalDataPayloadCryptographer implements PayloadCryptographer
{
public function __construct(
private readonly CipherKeyStore $cipherKeyStore,
private readonly CipherKeyFactory $cipherKeyFactory,
private readonly Cipher $cipher,
private readonly bool $useEncryptedFieldName = false,
private readonly bool $fallbackToFieldName = false,
private readonly bool $encryptNull = true,
) {
}
/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function encrypt(ClassMetadata $metadata, array $data): array
{
$subjectId = $this->subjectId($metadata, $data);
if ($subjectId === null) {
return $data;
}
try {
$cipherKey = $this->cipherKeyStore->get($subjectId);
} catch (CipherKeyNotExists) {
$cipherKey = ($this->cipherKeyFactory)();
$this->cipherKeyStore->store($subjectId, $cipherKey);
}
foreach ($metadata->properties() as $propertyMetadata) {
if (!$propertyMetadata->isPersonalData()) {
continue;
}
$value = $data[$propertyMetadata->fieldName()] ?? null;
if (!$this->encryptNull && $value === null) {
continue;
}
$targetFieldName = $this->useEncryptedFieldName
? $propertyMetadata->encryptedFieldName()
: $propertyMetadata->fieldName();
$data[$targetFieldName] = $this->cipher->encrypt(
$cipherKey,
$value,
);
if (!$this->useEncryptedFieldName) {
continue;
}
unset($data[$propertyMetadata->fieldName()]);
}
return $data;
}
/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function decrypt(ClassMetadata $metadata, array $data): array
{
$subjectId = $this->subjectId($metadata, $data);
if ($subjectId === null) {
return $data;
}
try {
$cipherKey = $this->cipherKeyStore->get($subjectId);
} catch (CipherKeyNotExists) {
$cipherKey = null;
}
foreach ($metadata->properties() as $propertyMetadata) {
if (!$propertyMetadata->isPersonalData()) {
continue;
}
if ($this->useEncryptedFieldName && array_key_exists($propertyMetadata->encryptedFieldName(), $data)) {
$rawData = $data[$propertyMetadata->encryptedFieldName()];
unset($data[$propertyMetadata->encryptedFieldName()]);
} elseif (!$this->useEncryptedFieldName || $this->fallbackToFieldName) {
$rawData = $data[$propertyMetadata->fieldName()];
} else {
continue;
}
if (!is_string($rawData)) {
continue;
}
if (!$cipherKey) {
$data[$propertyMetadata->fieldName()] = $this->fallback($propertyMetadata, $subjectId, $rawData);
continue;
}
try {
$data[$propertyMetadata->fieldName()] = $this->cipher->decrypt(
$cipherKey,
$rawData,
);
} catch (DecryptionFailed) {
$data[$propertyMetadata->fieldName()] = $this->fallback($propertyMetadata, $subjectId, $rawData);
}
}
return $data;
}
/** @param array<string, mixed> $data */
private function subjectId(ClassMetadata $metadata, array $data): string|null
{
$fieldName = $metadata->dataSubjectIdField();
if ($fieldName === null) {
return null;
}
if (!array_key_exists($fieldName, $data)) {
throw new MissingSubjectId($metadata->className(), $fieldName);
}
$subjectId = $data[$fieldName];
if (is_int($subjectId)) {
$subjectId = (string)$subjectId;
}
if ($subjectId instanceof Stringable) {
$subjectId = $subjectId->__toString();
}
if (!is_string($subjectId)) {
throw new UnsupportedSubjectId($metadata->className(), $fieldName, $subjectId);
}
return $subjectId;
}
private function fallback(PropertyMetadata $propertyMetadata, string $subjectId, mixed $rawData): mixed
{
$callback = $propertyMetadata->personalDataFallbackCallback();
if (!$callback) {
return $propertyMetadata->personalDataFallback();
}
return $callback($subjectId, $rawData);
}
/** @param non-empty-string $method */
public static function createWithOpenssl(
CipherKeyStore $cryptoStore,
string $method = OpensslCipherKeyFactory::DEFAULT_METHOD,
bool $useEncryptedFieldName = false,
bool $fallbackToFieldName = false,
): static {
return new self(
$cryptoStore,
new OpensslCipherKeyFactory($method),
new OpensslCipher(),
$useEncryptedFieldName,
$fallbackToFieldName,
);
}
/** @param non-empty-string $method */
public static function createWithDefaultSettings(
CipherKeyStore $cryptoStore,
string $method = OpensslCipherKeyFactory::DEFAULT_METHOD,
): static {
return new self(
$cryptoStore,
new OpensslCipherKeyFactory($method),
new OpensslCipher(),
true,
);
}
}