|
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\SigningSecretKey; |
|
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\Response; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
18
|
|
|
|
|
19
|
|
|
class SignResponseSubscriber implements EventSubscriberInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var HttpFoundationFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
private $httpFoundationFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $signPrivateKey; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Sapient |
|
33
|
|
|
*/ |
|
34
|
|
|
private $sapient; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var DiactorosFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
private $diactorosFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $signerHost; |
|
45
|
|
|
|
|
46
|
3 |
|
public function __construct(HttpFoundationFactory $httpFoundationFactory, DiactorosFactory $diactorosFactory, Sapient $sapient, string $signPrivateKey, string $signerHost) |
|
47
|
|
|
{ |
|
48
|
3 |
|
$this->httpFoundationFactory = $httpFoundationFactory; |
|
49
|
3 |
|
$this->diactorosFactory = $diactorosFactory; |
|
50
|
3 |
|
$this->sapient = $sapient; |
|
51
|
3 |
|
$this->signPrivateKey = $signPrivateKey; |
|
52
|
3 |
|
$this->signerHost = $signerHost; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public static function getSubscribedEvents() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
1 |
|
KernelEvents::VIEW => ['signPsrResponse', -100], |
|
59
|
1 |
|
KernelEvents::RESPONSE => ['signHttpFoundationResponse', -100], |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function signHttpFoundationResponse(FilterResponseEvent $event): void |
|
64
|
|
|
{ |
|
65
|
1 |
|
$event->setResponse( |
|
66
|
1 |
|
$this->signResponse($this->diactorosFactory->createResponse($event->getResponse())) |
|
67
|
|
|
); |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
public function signPsrResponse(GetResponseForControllerResultEvent $event): void |
|
71
|
|
|
{ |
|
72
|
2 |
|
$response = $event->getControllerResult(); |
|
73
|
2 |
|
if (!$response instanceof ResponseInterface) { |
|
74
|
1 |
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$event->setResponse($this->signResponse($response)); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
private function signResponse(ResponseInterface $response): Response |
|
81
|
|
|
{ |
|
82
|
2 |
|
$psrResponse = $this->sapient->signResponse($response, new SigningSecretKey(Base64UrlSafe::decode($this->signPrivateKey))); |
|
83
|
2 |
|
$psrResponse = $psrResponse->withHeader(PublicKeyGetter::HEADER_SIGNER, $this->signerHost); |
|
84
|
|
|
|
|
85
|
2 |
|
return $this->httpFoundationFactory->createResponse($psrResponse); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|