1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acquia\Hmac\Symfony; |
4
|
|
|
|
5
|
|
|
use Acquia\Hmac\ResponseSigner; |
6
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
7
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
8
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Signs the response from an HTTP HMAC-authenticated request. |
15
|
|
|
*/ |
16
|
|
|
class HmacResponseListener implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event |
20
|
3 |
|
*/ |
21
|
|
|
public function onKernelResponse(ResponseEvent $event) |
22
|
3 |
|
{ |
23
|
1 |
|
$mainRequest = method_exists($event, 'isMainRequest') ? $event->isMainRequest() : $event->isMasterRequest(); |
|
|
|
|
24
|
|
|
if (!$mainRequest) { |
25
|
|
|
return; |
26
|
2 |
|
} |
27
|
2 |
|
|
28
|
|
|
$request = $event->getRequest(); |
29
|
2 |
|
$response = $event->getResponse(); |
30
|
1 |
|
|
31
|
1 |
|
if ($request->attributes->has('hmac.key')) { |
32
|
|
|
$psr17Factory = new Psr17Factory(); |
33
|
1 |
|
$httpMessageFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory); |
34
|
1 |
|
$foundationFactory = new HttpFoundationFactory(); |
35
|
|
|
|
36
|
1 |
|
$psr7Request = $httpMessageFactory->createRequest($request); |
37
|
1 |
|
$psr7Response = $httpMessageFactory->createResponse($response); |
38
|
|
|
|
39
|
1 |
|
$signer = new ResponseSigner($request->attributes->get('hmac.key'), $psr7Request); |
40
|
|
|
$signedResponse = $signer->signResponse($psr7Response); |
41
|
2 |
|
|
42
|
|
|
$event->setResponse($foundationFactory->createResponse($signedResponse)); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
1 |
|
*/ |
49
|
|
|
public static function getSubscribedEvents() |
50
|
|
|
{ |
51
|
|
|
return [KernelEvents::RESPONSE => 'onKernelResponse']; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.