Completed
Push — master ( 4cd2db...6c58cf )
by Thierry
20:42 queued 16:44
created

VerifyRequestSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 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',
44
        ];
45
    }
46
47 2
    public function verifyRequest(GetResponseEvent $event): void
48
    {
49 2
        $publicKey = $this->getVerifyingKey($event->getRequest());
50 2
        $psrResponse = $this->diactorosFactory->createRequest($event->getRequest());
51 2
        $this->sapient->verifySignedRequest(
52 2
            $psrResponse,
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->getRequestVerifyingKey($psrRequest);
62
    }
63
}
64