1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace lepiaf\SapientBundle\EventSubscriber; |
5
|
|
|
|
6
|
|
|
use ParagonIE\ConstantTime\Base64UrlSafe; |
7
|
|
|
use ParagonIE\Sapient\CryptographyKeys\SealingSecretKey; |
8
|
|
|
use ParagonIE\Sapient\Sapient; |
9
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
12
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
13
|
|
|
|
14
|
|
|
class UnsealRequestSubscriber implements EventSubscriberInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Sapient |
18
|
|
|
*/ |
19
|
|
|
private $sapient; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DiactorosFactory |
23
|
|
|
*/ |
24
|
|
|
private $diactorosFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $privateKey; |
30
|
|
|
|
31
|
2 |
|
public function __construct(Sapient $sapient, DiactorosFactory $diactorosFactory, string $privateKey) |
32
|
|
|
{ |
33
|
2 |
|
$this->sapient = $sapient; |
34
|
2 |
|
$this->diactorosFactory = $diactorosFactory; |
35
|
2 |
|
$this->privateKey = $privateKey; |
36
|
2 |
|
} |
37
|
|
|
|
38
|
1 |
|
public static function getSubscribedEvents() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
1 |
|
KernelEvents::REQUEST => ['unsealRequest', -100], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function unsealRequest(GetResponseEvent $event): void |
46
|
|
|
{ |
47
|
2 |
|
$request = $event->getRequest(); |
48
|
2 |
|
$psrRequest = $this->diactorosFactory->createRequest($request); |
49
|
2 |
|
$unsealedPsrRequest = $this->sapient->unsealRequest( |
50
|
2 |
|
$psrRequest, |
51
|
2 |
|
new SealingSecretKey(Base64UrlSafe::decode($this->privateKey)) |
52
|
|
|
); |
53
|
|
|
|
54
|
1 |
|
$request->initialize( |
55
|
1 |
|
$request->query->all(), |
56
|
1 |
|
$request->request->all(), |
57
|
1 |
|
$request->attributes->all(), |
58
|
1 |
|
$request->cookies->all(), |
59
|
1 |
|
$request->files->all(), |
60
|
1 |
|
$request->server->all(), |
61
|
1 |
|
(string) $unsealedPsrRequest->getBody() |
62
|
|
|
); |
63
|
1 |
|
} |
64
|
|
|
} |
65
|
|
|
|