|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rezzza\SecurityBundle\Request; |
|
4
|
|
|
|
|
5
|
|
|
use Rezzza\SecurityBundle\Security\Firewall\SignatureConfig; |
|
6
|
|
|
use Rezzza\SecurityBundle\Security\Firewall\SignedRequest; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Psr7RequestSigner |
|
11
|
|
|
* |
|
12
|
|
|
* @author Stephane PY <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Psr7RequestSigner |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var SignatureConfig */ |
|
17
|
|
|
private $signatureConfig; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param SignatureConfig $signatureConfig signatureConfig |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(SignatureConfig $signatureConfig) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->signatureConfig = $signatureConfig; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param RequestInterface $request request |
|
29
|
|
|
* |
|
30
|
|
|
* @return Request |
|
31
|
|
|
*/ |
|
32
|
|
|
public function sign(RequestInterface $request) |
|
33
|
|
|
{ |
|
34
|
|
|
$replayProtectionEnabled = $this->signatureConfig->isReplayProtectionEnabled(); |
|
35
|
|
|
$time = $replayProtectionEnabled ? time() : null; |
|
36
|
|
|
|
|
37
|
|
|
$signedRequest = new SignedRequest( |
|
38
|
|
|
$request->getMethod(), |
|
39
|
|
|
$request->getUri()->getHost(), |
|
40
|
|
|
$request->getUri()->getPath(), |
|
41
|
|
|
(string) $request->getBody(), |
|
42
|
|
|
$time |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$signature = $signedRequest->buildSignature($this->signatureConfig); |
|
46
|
|
|
$uri = $request->getUri(); |
|
47
|
|
|
|
|
48
|
|
|
$query = $uri->getQuery(); |
|
49
|
|
|
$query .= (empty($query)) ? '?' : '&'; |
|
50
|
|
|
$query .= '_signature='.$signature; |
|
51
|
|
|
|
|
52
|
|
|
if ($replayProtectionEnabled) { |
|
53
|
|
|
$query .= '&_signature_ttl='.$time; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$uri = $uri->withQuery($query); |
|
57
|
|
|
|
|
58
|
|
|
return $request->withUri($uri); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|