|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UMA\Psr\Http\Message\HMAC; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\MessageInterface; |
|
6
|
|
|
use UMA\Psr\Http\Message\Internal\HashCalculator; |
|
7
|
|
|
use UMA\Psr\Http\Message\Internal\HeaderValidator; |
|
8
|
|
|
use UMA\Psr\Http\Message\Monitor\BlindMonitor; |
|
9
|
|
|
use UMA\Psr\Http\Message\Monitor\MonitorInterface; |
|
10
|
|
|
use UMA\Psr\Http\Message\Serializer\MessageSerializer; |
|
11
|
|
|
|
|
12
|
|
|
class Verifier |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var HashCalculator |
|
16
|
|
|
*/ |
|
17
|
|
|
private $calculator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var null|int |
|
21
|
|
|
*/ |
|
22
|
|
|
private $maxDelay = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var MonitorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $monitor; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var HeaderValidator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $validator; |
|
33
|
|
|
|
|
34
|
105 |
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
105 |
|
$this->calculator = new HashCalculator(); |
|
37
|
105 |
|
$this->monitor = new BlindMonitor(); |
|
38
|
105 |
|
$this->validator = (new HeaderValidator()) |
|
39
|
105 |
|
->addRule(Specification::AUTH_HEADER, Specification::AUTH_REGEXP) |
|
40
|
105 |
|
->addRule(Specification::SIGN_HEADER, Specification::SIGN_REGEXP); |
|
41
|
105 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $maxDelay |
|
45
|
|
|
* |
|
46
|
|
|
* @return Verifier |
|
47
|
|
|
*/ |
|
48
|
7 |
|
public function setMaximumDelay($maxDelay) |
|
49
|
|
|
{ |
|
50
|
7 |
|
$this->maxDelay = $maxDelay; |
|
51
|
|
|
|
|
52
|
7 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param MonitorInterface $monitor |
|
57
|
|
|
* |
|
58
|
|
|
* @return Verifier |
|
59
|
|
|
*/ |
|
60
|
7 |
|
public function setMonitor(MonitorInterface $monitor) |
|
61
|
|
|
{ |
|
62
|
7 |
|
$this->monitor = $monitor; |
|
63
|
|
|
|
|
64
|
7 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param MessageInterface $message |
|
69
|
|
|
* @param string $secret |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool Signature verification outcome. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \InvalidArgumentException When $message is an implementation of |
|
74
|
|
|
* MessageInterface that cannot be |
|
75
|
|
|
* serialized and thus neither verified. |
|
76
|
|
|
*/ |
|
77
|
105 |
|
public function verify(MessageInterface $message, $secret) |
|
78
|
|
|
{ |
|
79
|
105 |
|
if (false === $matches = $this->validator->conforms($message)) { |
|
80
|
91 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
84 |
|
$clientSideSignature = $matches[Specification::AUTH_HEADER][1]; |
|
84
|
|
|
|
|
85
|
84 |
|
$serverSideSignature = $this->calculator |
|
86
|
84 |
|
->hmac(MessageSerializer::serialize($this->withoutUnsignedHeaders($message)), $secret); |
|
87
|
|
|
|
|
88
|
|
|
return |
|
89
|
84 |
|
hash_equals($serverSideSignature, $clientSideSignature) && |
|
90
|
84 |
|
!$this->delayed($message) && |
|
91
|
84 |
|
!$this->monitor->seen($message); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param MessageInterface $message |
|
96
|
|
|
* |
|
97
|
|
|
* @return MessageInterface |
|
98
|
|
|
*/ |
|
99
|
84 |
|
private function withoutUnsignedHeaders(MessageInterface $message) |
|
100
|
|
|
{ |
|
101
|
84 |
|
$signedHeaders = array_filter(explode(',', $message->getHeaderLine(Specification::SIGN_HEADER))); |
|
102
|
|
|
|
|
103
|
84 |
|
foreach ($message->getHeaders() as $name => $value) { |
|
104
|
84 |
|
if (!in_array(mb_strtolower($name), $signedHeaders)) { |
|
105
|
84 |
|
$message = $message->withoutHeader($name); |
|
106
|
84 |
|
} |
|
107
|
84 |
|
} |
|
108
|
|
|
|
|
109
|
84 |
|
return $message; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param MessageInterface $message |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
84 |
|
private function delayed(MessageInterface $message) |
|
118
|
|
|
{ |
|
119
|
84 |
|
$currentTimestamp = (new \DateTime('now', new \DateTimeZone('GMT')))->getTimestamp(); |
|
120
|
84 |
|
$messageTimestamp = (new \DateTime($message->getHeaderLine(Specification::DATE_HEADER)))->getTimestamp(); |
|
121
|
|
|
|
|
122
|
84 |
|
return is_int($this->maxDelay) && $currentTimestamp - $messageTimestamp > $this->maxDelay; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|