1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Authenticate; |
4
|
|
|
|
5
|
|
|
use Ds\Authenticate\HashHmac; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class HashHmacTest |
9
|
|
|
* |
10
|
|
|
* @package Tests\Authenticate |
11
|
|
|
*/ |
12
|
|
|
class HashHmacTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \ReflectionClass |
16
|
|
|
*/ |
17
|
|
|
public $reflector; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public $algo; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public $data; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HashHmac |
31
|
|
|
*/ |
32
|
|
|
public $hmac; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->algo = 'sha512'; |
40
|
|
|
$this->privateKey = 'privateKey'; |
|
|
|
|
41
|
|
|
$this->reflector = new \ReflectionClass(HashHmac::class); |
42
|
|
|
$this->data = ['foo' => 'bar']; |
43
|
|
|
$this->hmac = new HashHmac($this->privateKey,$this->algo); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
|
|
public function testHash() |
50
|
|
|
{ |
51
|
|
|
$actual = $this->hmac->create($this->data); |
52
|
|
|
$token = base64_decode($actual); |
53
|
|
|
$tokenParts = explode('=',$token); |
54
|
|
|
$tokenHash = $tokenParts[1]; |
55
|
|
|
$expected = hash_hmac($this->algo, json_encode($this->data) , $this->privateKey); |
56
|
|
|
$this->assertEquals($tokenHash, $expected); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public function testCompareHash() |
63
|
|
|
{ |
64
|
|
|
$encodedHash = $this->hmac->create($this->data); |
65
|
|
|
$this->assertEquals(true, $this->hmac->verify($encodedHash,$this->data)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
|
|
public function testCompareHashInvalidAlgo() |
72
|
|
|
{ |
73
|
|
|
$this->setExpectedException('Exception'); |
|
|
|
|
74
|
|
|
$hash = 'shafoo2=647a6cbf9b30cb8cc3fd5147a8fd30a19775fe3290e3dd0e729ae2f1e8a485f833469df616374fa923fcc3533545a11e6deba25a63bcd1ec81c316c090890ddc=10f3e8ed9c5c8b33cadda267882fa6449f5af465bc1dc420=1457010889'; |
75
|
|
|
$encoded = base64_encode($hash); |
76
|
|
|
$this->hmac->verify($encoded, $this->data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
public function testCompareHashNonMatch() |
83
|
|
|
{ |
84
|
|
|
|
85
|
|
|
$this->setExpectedException('Exception'); |
|
|
|
|
86
|
|
|
$encodedHash = $this->hmac->create($this->data); |
87
|
|
|
$differentPayload = ['foos' => 'bat']; |
88
|
|
|
$this->hmac->verify($encodedHash, $differentPayload); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
*/ |
94
|
|
|
public function testVerify() |
95
|
|
|
{ |
96
|
|
|
$token = $this->hmac->create($this->data); |
97
|
|
|
$expected = true; |
98
|
|
|
$actual = $this->hmac->verify($token, $this->data); |
99
|
|
|
$this->assertEquals($expected, $actual); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
public function testVerifyHashWrongKey() |
106
|
|
|
{ |
107
|
|
|
$this->setExpectedException('Exception'); |
|
|
|
|
108
|
|
|
$expected = false; |
109
|
|
|
|
110
|
|
|
$newHmac = $this->hmac->withPrivateKey('some-random-key'); |
111
|
|
|
$token = $newHmac->create($this->data); |
112
|
|
|
|
113
|
|
|
$actual = $this->hmac->verify($token, $this->data); |
114
|
|
|
$this->assertEquals($expected, $actual); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
public function testVerifyHashPayloadMismatch() |
121
|
|
|
{ |
122
|
|
|
$this->setExpectedException('Exception'); |
|
|
|
|
123
|
|
|
$data = md5('someOtherStuff'); |
124
|
|
|
$actualToken = $this->hmac->create(['data' => 'payload']); |
125
|
|
|
$this->hmac->verify($actualToken, ['data' => $data]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
*/ |
131
|
|
|
public function testGetPrivateKey() |
132
|
|
|
{ |
133
|
|
|
$expected = 'privateKey'; |
134
|
|
|
$actual = $this->hmac->getPrivateKey(); |
135
|
|
|
$this->assertEquals($expected, $actual); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
*/ |
141
|
|
|
public function testWithPrivateKey() |
142
|
|
|
{ |
143
|
|
|
$expected = 'newKey'; |
144
|
|
|
$newKey = $this->hmac->withPrivateKey($expected); |
145
|
|
|
$this->assertEquals($expected, $newKey->getPrivateKey()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
|
|
public function testGetAlgorithm() |
152
|
|
|
{ |
153
|
|
|
$expected = 'sha512'; |
154
|
|
|
$actual = $this->hmac->getAlgorithm(); |
155
|
|
|
$this->assertEquals($expected, $actual); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
*/ |
161
|
|
|
public function testVerifyAlgorithm() |
162
|
|
|
{ |
163
|
|
|
$method = $this->reflector->getMethod('verifyAlgorithm'); |
164
|
|
|
$method->setAccessible(true); |
165
|
|
|
$actual = $method->invokeArgs($this->hmac, ['sha256']); |
166
|
|
|
$this->assertEquals($actual, 'sha256'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
|
|
public function testVerifyAlgorithmException() |
173
|
|
|
{ |
174
|
|
|
$this->setExpectedException('Exception'); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$method = $this->reflector->getMethod('verifyAlgorithm'); |
177
|
|
|
$method->setAccessible(true); |
178
|
|
|
$method->invokeArgs($this->hmac, ['sha111']); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: