Completed
Pull Request — master (#37)
by Thierry
05:33
created

SignResponseSubscriber::signPsrResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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\Exception\SignResponseException;
7
use lepiaf\SapientBundle\Exception\WrongKeyException;
8
use lepiaf\SapientBundle\Service\PublicKeyGetter;
9
use ParagonIE\ConstantTime\Base64UrlSafe;
10
use ParagonIE\Sapient\CryptographyKeys\SigningSecretKey;
11
use ParagonIE\Sapient\Sapient;
12
use Psr\Http\Message\ResponseInterface;
13
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
14
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
18
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
19
use Symfony\Component\HttpKernel\KernelEvents;
20
21
class SignResponseSubscriber implements EventSubscriberInterface
22
{
23
    /**
24
     * @var HttpFoundationFactory
25
     */
26
    private $httpFoundationFactory;
27
28
    /**
29
     * @var string
30
     */
31
    private $signPrivateKey;
32
33
    /**
34
     * @var Sapient
35
     */
36
    private $sapient;
37
38
    /**
39
     * @var DiactorosFactory
40
     */
41
    private $diactorosFactory;
42
43
    /**
44
     * @var string
45
     */
46
    private $signerHost;
47
48 3
    public function __construct(HttpFoundationFactory $httpFoundationFactory, DiactorosFactory $diactorosFactory, Sapient $sapient, string $signPrivateKey, string $signerHost)
49
    {
50 3
        $this->httpFoundationFactory = $httpFoundationFactory;
51 3
        $this->diactorosFactory = $diactorosFactory;
52 3
        $this->sapient = $sapient;
53 3
        $this->signPrivateKey = $signPrivateKey;
54 3
        $this->signerHost = $signerHost;
55 3
    }
56
57 1
    public static function getSubscribedEvents()
58
    {
59
        return [
60 1
            KernelEvents::VIEW => ['signPsrResponse', -100],
61 1
            KernelEvents::RESPONSE => ['signHttpFoundationResponse', -100],
62
        ];
63
    }
64
65 1
    public function signHttpFoundationResponse(FilterResponseEvent $event): void
66
    {
67 1
        $event->setResponse(
68 1
            $this->signResponse($this->diactorosFactory->createResponse($event->getResponse()))
69
        );
70 1
    }
71
72 2
    public function signPsrResponse(GetResponseForControllerResultEvent $event): void
73
    {
74 2
        $response = $event->getControllerResult();
75 2
        if (!$response instanceof ResponseInterface) {
76 1
            return;
77
        }
78
79 1
        $event->setResponse($this->signResponse($response));
80 1
    }
81
82 2
    private function signResponse(ResponseInterface $response): Response
83
    {
84
        try {
85 2
            $psrResponse = $this->sapient->signResponse($response, new SigningSecretKey(Base64UrlSafe::decode($this->signPrivateKey)));
86
        } catch (\RangeException $rangeException) {
87
            throw new WrongKeyException($rangeException->getMessage());
88
        } catch (\SodiumException $sodiumException) {
89
            throw new SignResponseException('Cannot sign response.');
90
        }
91 2
        $psrResponse = $psrResponse->withHeader(PublicKeyGetter::HEADER_SIGNER, $this->signerHost);
92
93 2
        return $this->httpFoundationFactory->createResponse($psrResponse);
94
    }
95
}
96