1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace lepiaf\SapientBundle\EventSubscriber; |
5
|
|
|
|
6
|
|
|
use lepiaf\SapientBundle\Service\PublicKeyGetter; |
7
|
|
|
use ParagonIE\ConstantTime\Base64UrlSafe; |
8
|
|
|
use ParagonIE\Sapient\CryptographyKeys\SealingPublicKey; |
9
|
|
|
use ParagonIE\Sapient\Sapient; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; |
12
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
17
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
18
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
19
|
|
|
|
20
|
|
|
class SealResponseSubscriber implements EventSubscriberInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var HttpFoundationFactory |
24
|
|
|
*/ |
25
|
|
|
private $httpFoundationFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PublicKeyGetter |
29
|
|
|
*/ |
30
|
|
|
private $publicKeyGetter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Sapient |
34
|
|
|
*/ |
35
|
|
|
private $sapient; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var DiactorosFactory |
39
|
|
|
*/ |
40
|
|
|
private $diactorosFactory; |
41
|
|
|
|
42
|
3 |
|
public function __construct(HttpFoundationFactory $httpFoundationFactory, DiactorosFactory $diactorosFactory, Sapient $sapient, PublicKeyGetter $publicKeyGetter) |
43
|
|
|
{ |
44
|
3 |
|
$this->httpFoundationFactory = $httpFoundationFactory; |
45
|
3 |
|
$this->diactorosFactory = $diactorosFactory; |
46
|
3 |
|
$this->sapient = $sapient; |
47
|
3 |
|
$this->publicKeyGetter = $publicKeyGetter; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
1 |
|
public static function getSubscribedEvents() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
1 |
|
KernelEvents::VIEW => ['sealPsrResponse', -110], |
54
|
1 |
|
KernelEvents::RESPONSE => ['sealHttpFoundationResponse', -110], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function sealHttpFoundationResponse(FilterResponseEvent $event): void |
59
|
|
|
{ |
60
|
1 |
|
$publicKey = $this->getSealingKey($event->getRequest()); |
61
|
1 |
|
$psrResponse = $this->diactorosFactory->createResponse($event->getResponse()); |
62
|
1 |
|
$event->setResponse($this->sealResponse($psrResponse, $publicKey)); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
2 |
|
public function sealPsrResponse(GetResponseForControllerResultEvent $event): void |
66
|
|
|
{ |
67
|
2 |
|
$response = $event->getControllerResult(); |
68
|
2 |
|
if (!$response instanceof ResponseInterface) { |
69
|
1 |
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$publicKey = $this->getSealingKey($event->getRequest()); |
73
|
1 |
|
$event->setResponse($this->sealResponse($response, $publicKey)); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
2 |
|
private function sealResponse(ResponseInterface $response, string $publicKey): Response |
77
|
|
|
{ |
78
|
2 |
|
$psrResponse = $this->sapient->sealResponse($response, new SealingPublicKey(Base64UrlSafe::decode($publicKey))); |
79
|
|
|
|
80
|
2 |
|
return $this->httpFoundationFactory->createResponse($psrResponse); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
private function getSealingKey(Request $request) |
84
|
|
|
{ |
85
|
2 |
|
$psrRequest = $this->diactorosFactory->createRequest($request); |
86
|
|
|
|
87
|
2 |
|
return $this->publicKeyGetter->getSealingKey($psrRequest); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|