-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSensitiveDataPayloadCryptographer.php
More file actions
190 lines (156 loc) · 6.19 KB
/
Copy pathSensitiveDataPayloadCryptographer.php
File metadata and controls
190 lines (156 loc) · 6.19 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
<?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 function array_key_exists;
use function is_int;
use function is_string;
final class SensitiveDataPayloadCryptographer 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,
) {
}
/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function encrypt(ClassMetadata $metadata, array $data): array
{
foreach ($metadata->properties() as $propertyMetadata) {
if (!$propertyMetadata->isSensitiveData()) {
continue;
}
$subjectId = $this->subjectId($propertyMetadata, $metadata, $data);
try {
$cipherKey = $this->cipherKeyStore->get($subjectId);
} catch (CipherKeyNotExists) {
$cipherKey = ($this->cipherKeyFactory)();
$this->cipherKeyStore->store($subjectId, $cipherKey);
}
$targetFieldName = $this->useEncryptedFieldName
? $propertyMetadata->encryptedFieldName()
: $propertyMetadata->fieldName();
$data[$targetFieldName] = $this->cipher->encrypt(
$cipherKey,
$data[$propertyMetadata->fieldName()],
);
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
{
foreach ($metadata->properties() as $propertyMetadata) {
if (!$propertyMetadata->isSensitiveData()) {
continue;
}
$subjectId = $this->subjectId($propertyMetadata, $metadata, $data);
try {
$cipherKey = $this->cipherKeyStore->get($subjectId);
} catch (CipherKeyNotExists) {
$cipherKey = null;
}
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 (!$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(PropertyMetadata $propertyMetadata, ClassMetadata $metadata, array $data): string
{
if (!$propertyMetadata->isSensitiveData()) {
throw new NotSensitiveData($metadata->className(), $propertyMetadata->propertyName());
}
$sensitiveDataSubjectIdName = $propertyMetadata->sensitiveDataSubjectIdName();
if (!$metadata->hasSubjectIdIdentifier($sensitiveDataSubjectIdName)) {
throw new MissingSubjectId($metadata->className(), $propertyMetadata->propertyName());
}
$fieldName = $metadata->getSubjectIdFieldName($sensitiveDataSubjectIdName);
if (!array_key_exists($fieldName, $data)) {
throw new MissingSubjectId($metadata->className(), $fieldName);
}
$subjectId = $data[$fieldName];
if (is_int($subjectId)) {
$subjectId = (string)$subjectId;
}
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->sensitiveDataFallbackCallable();
if (!$callback) {
return $propertyMetadata->sensitiveDataFallback();
}
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,
);
}
}