-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCryptographyMiddleware.php
More file actions
189 lines (151 loc) · 5.87 KB
/
Copy pathCryptographyMiddleware.php
File metadata and controls
189 lines (151 loc) · 5.87 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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Extension\Cryptography;
use Closure;
use Patchlevel\Hydrator\Extension\Cryptography\Cipher\DecryptionFailed;
use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyNotExists;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\Middleware\Middleware;
use Patchlevel\Hydrator\Middleware\Stack;
use Patchlevel\Hydrator\Normalizer\NormalizerWithContext;
use Stringable;
use function array_key_exists;
use function assert;
use function is_array;
use function is_int;
use function is_string;
/** @experimental */
final class CryptographyMiddleware implements Middleware
{
public function __construct(
private readonly Cryptographer $cryptographer,
) {
}
/**
* @param ClassMetadata<T> $metadata
* @param array<string, mixed> $data
* @param array<string, mixed> $context
*
* @return T
*
* @template T of object
*/
public function hydrate(ClassMetadata $metadata, array $data, array $context, Stack $stack): object
{
$context[SubjectIds::class] = $subjectIds = $this->resolveSubjectIds($metadata, $data, $context);
if ($context[LegacyCryptographyDecryptMiddleware::class] ?? false) {
unset($context[LegacyCryptographyDecryptMiddleware::class]);
return $stack->next()->hydrate($metadata, $data, $context, $stack);
}
foreach ($metadata->properties as $propertyMetadata) {
$info = $propertyMetadata->extras[SensitiveDataInfo::class] ?? null;
if (!$info instanceof SensitiveDataInfo) {
continue;
}
$value = $data[$propertyMetadata->fieldName] ?? null;
if ($value === null) {
continue;
}
if (!$this->cryptographer->supports($value)) {
continue;
}
$subjectId = $subjectIds->get($info->subjectIdName);
try {
$data[$propertyMetadata->fieldName] = $this->cryptographer->decrypt($subjectId, $value);
} catch (DecryptionFailed | CipherKeyNotExists) {
$fallback = $info->fallback instanceof Closure
? ($info->fallback)($subjectId)
: $info->fallback;
if ($propertyMetadata->normalizer) {
if ($propertyMetadata->normalizer instanceof NormalizerWithContext) {
$fallback = $propertyMetadata->normalizer->normalize($fallback, $context);
} else {
$fallback = $propertyMetadata->normalizer->normalize($fallback);
}
}
$data[$propertyMetadata->fieldName] = $fallback;
}
}
return $stack->next()->hydrate(
$metadata,
$data,
$context,
$stack,
);
}
/**
* @param ClassMetadata<T> $metadata
* @param T $object
* @param array<string, mixed> $context
*
* @return array<string, mixed>
*
* @template T of object
*/
public function extract(ClassMetadata $metadata, object $object, array $context, Stack $stack): array
{
$context[SubjectIds::class] = $subjectIds = $this->resolveSubjectIds($metadata, $object, $context);
$data = $stack->next()->extract($metadata, $object, $context, $stack);
foreach ($metadata->properties as $propertyMetadata) {
$info = $propertyMetadata->extras[SensitiveDataInfo::class] ?? null;
if (!$info instanceof SensitiveDataInfo) {
continue;
}
$value = $data[$propertyMetadata->fieldName] ?? null;
if ($value === null) {
continue;
}
$data[$propertyMetadata->fieldName] = $this->cryptographer->encrypt(
$subjectIds->get($info->subjectIdName),
$value,
);
}
return $data;
}
/**
* @param array<string, mixed>|object $data
* @param array<string, mixed> $context
*/
private function resolveSubjectIds(
ClassMetadata $metadata,
array|object $data,
array $context,
): SubjectIds {
$subjectIds = $context[SubjectIds::class] ?? new SubjectIds();
assert($subjectIds instanceof SubjectIds);
$mapping = $metadata->extras[SubjectIdFieldMapping::class] ?? null;
if (!$mapping instanceof SubjectIdFieldMapping) {
return $subjectIds;
}
$result = [];
foreach ($mapping->nameToField as $name => $fieldName) {
if (is_array($data)) {
if (!array_key_exists($fieldName, $data)) {
throw new MissingSubjectIdForField($metadata->className, $fieldName);
}
$subjectId = $data[$fieldName];
} else {
$property = $metadata->propertyForField($fieldName);
$subjectId = $property->getValue($data);
if ($property->normalizer) {
if ($property->normalizer instanceof NormalizerWithContext) {
$subjectId = $property->normalizer->normalize($subjectId, $context);
} else {
$subjectId = $property->normalizer->normalize($subjectId);
}
}
}
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);
}
$result[$name] = $subjectId;
}
return $subjectIds->merge(new SubjectIds($result));
}
}