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\SigningPublicKey; |
9
|
|
|
use ParagonIE\Sapient\Sapient; |
10
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
14
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
15
|
|
|
|
16
|
|
|
class VerifyRequestSubscriber implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PublicKeyGetter |
20
|
|
|
*/ |
21
|
|
|
private $publicKeyGetter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Sapient |
25
|
|
|
*/ |
26
|
|
|
private $sapient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DiactorosFactory |
30
|
|
|
*/ |
31
|
|
|
private $diactorosFactory; |
32
|
|
|
|
33
|
2 |
|
public function __construct(DiactorosFactory $diactorosFactory, Sapient $sapient, PublicKeyGetter $publicKeyGetter) |
34
|
|
|
{ |
35
|
2 |
|
$this->diactorosFactory = $diactorosFactory; |
36
|
2 |
|
$this->sapient = $sapient; |
37
|
2 |
|
$this->publicKeyGetter = $publicKeyGetter; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
1 |
|
public static function getSubscribedEvents() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
1 |
|
KernelEvents::REQUEST => ['verifyRequest', -110], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public function verifyRequest(GetResponseEvent $event): void |
48
|
|
|
{ |
49
|
2 |
|
$publicKey = $this->getVerifyingKey($event->getRequest()); |
50
|
2 |
|
$psrRequest = $this->diactorosFactory->createRequest($event->getRequest()); |
51
|
2 |
|
$this->sapient->verifySignedRequest( |
52
|
2 |
|
$psrRequest, |
53
|
2 |
|
new SigningPublicKey(Base64UrlSafe::decode($publicKey)) |
54
|
|
|
); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
2 |
|
private function getVerifyingKey(Request $request): string |
58
|
|
|
{ |
59
|
2 |
|
$psrRequest = $this->diactorosFactory->createRequest($request); |
60
|
|
|
|
61
|
2 |
|
return $this->publicKeyGetter->getVerifyingKeyFromRequest($psrRequest); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|