1 | <?php |
||
5 | class SignedRequest |
||
6 | { |
||
7 | private $method; |
||
8 | |||
9 | private $host; |
||
10 | |||
11 | private $pathInfo; |
||
12 | |||
13 | private $content; |
||
14 | |||
15 | private $signatureTime; |
||
16 | |||
17 | public function __construct($method, $host, $pathInfo, $content, $signatureTime = null) |
||
18 | { |
||
19 | $this->setMethod(strtoupper($method)); |
||
20 | $this->signatureTime = $signatureTime; |
||
21 | $this->host = $host; |
||
22 | $this->pathInfo = $pathInfo; |
||
23 | $this->content = $content; |
||
24 | } |
||
25 | |||
26 | public function buildSignature(SignatureConfig $signatureConfig) |
||
27 | { |
||
28 | $payload = array( |
||
29 | $this->method, |
||
30 | $this->host, |
||
31 | $this->pathInfo, |
||
32 | $this->content |
||
33 | ); |
||
34 | |||
35 | if ($signatureConfig->isReplayProtectionEnabled()) { |
||
36 | $this->guardValidSignatureTime(); |
||
37 | // use unshift to keep BC on signature generation |
||
38 | array_unshift($payload, $this->signatureTime); |
||
39 | } |
||
40 | |||
41 | return hash_hmac( |
||
42 | $signatureConfig->getAlgorithm(), |
||
43 | implode("\n", $payload), |
||
44 | $signatureConfig->getSecret() |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | public function authenticateSignature($signature, SignatureConfig $signatureConfig, ReplayProtection $replayProtection) |
||
60 | |||
61 | private function guardValidSignatureTime() |
||
62 | { |
||
69 | |||
70 | private function setMethod($method) |
||
82 | } |
||
83 |