Completed
Pull Request — master (#19)
by Thierry
04:18
created

VerifyRequestSubscriber::verifyRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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
    public function __construct(DiactorosFactory $diactorosFactory, Sapient $sapient, PublicKeyGetter $publicKeyGetter)
34
    {
35
        $this->diactorosFactory = $diactorosFactory;
36
        $this->sapient = $sapient;
37
        $this->publicKeyGetter = $publicKeyGetter;
38
    }
39
40
    public static function getSubscribedEvents()
41
    {
42
        return [
43
            KernelEvents::REQUEST => 'verifyRequest',
44
        ];
45
    }
46
47
    public function verifyRequest(GetResponseEvent $event): void
48
    {
49
        $publicKey = $this->getVerifyingKey($event->getRequest());
50
        $psrResponse = $this->diactorosFactory->createRequest($event->getRequest());
51
        $this->sapient->verifySignedRequest(
52
            $psrResponse,
53
            new SigningPublicKey(Base64UrlSafe::decode($publicKey))
54
        );
55
    }
56
57
    private function getVerifyingKey(Request $request): string
58
    {
59
        $psrRequest = $this->diactorosFactory->createRequest($request);
60
61
        return $this->publicKeyGetter->getSealingKey($psrRequest);
62
    }
63
}
64