-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathOutboundDecryptionChannelInterceptor.php
More file actions
56 lines (45 loc) · 1.63 KB
/
OutboundDecryptionChannelInterceptor.php
File metadata and controls
56 lines (45 loc) · 1.63 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
<?php
/**
* licence Enterprise
*/
namespace Ecotone\DataProtection;
use Ecotone\DataProtection\MessageEncryption\ChannelEncryptor;
use Ecotone\Messaging\Channel\AbstractChannelInterceptor;
use Ecotone\Messaging\Conversion\MediaType;
use Ecotone\Messaging\Message;
use Ecotone\Messaging\MessageChannel;
use Ecotone\Messaging\MessageHeaders;
use Ecotone\Messaging\Support\Assert;
class OutboundDecryptionChannelInterceptor extends AbstractChannelInterceptor
{
/**
* @param array<ChannelEncryptor> $messageEncryptors
*/
public function __construct(
private readonly ?ChannelEncryptor $channelEncryptor,
private readonly array $messageEncryptors,
) {
Assert::allInstanceOfType($this->messageEncryptors, ChannelEncryptor::class);
}
public function postReceive(Message $message, MessageChannel $messageChannel): ?Message
{
if (! $message->getHeaders()->getContentType()?->isCompatibleWith(MediaType::createApplicationJson())) {
return $message;
}
if ($messageEncryptor = $this->findMessageEncryptor($message)) {
return $messageEncryptor->decrypt($message);
}
if ($this->channelEncryptor) {
return $this->channelEncryptor->decrypt($message);
}
return $message;
}
private function findMessageEncryptor(Message $message): ?ChannelEncryptor
{
if (! $message->getHeaders()->containsKey(MessageHeaders::TYPE_ID)) {
return null;
}
$type = $message->getHeaders()->get(MessageHeaders::TYPE_ID);
return $this->messageEncryptors[$type] ?? null;
}
}