|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Verification; |
|
4
|
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim\NotBefore; |
|
6
|
|
|
|
|
7
|
|
|
class NotBeforeVerifierTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token\Payload |
|
11
|
|
|
*/ |
|
12
|
|
|
private $payload; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token |
|
16
|
|
|
*/ |
|
17
|
|
|
private $token; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var NotBeforeVerifier |
|
21
|
|
|
*/ |
|
22
|
|
|
private $verifier; |
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
|
27
|
|
|
|
|
28
|
|
|
$this->token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
|
29
|
|
|
|
|
30
|
|
|
$this->token->expects($this->any()) |
|
31
|
|
|
->method('getPayload') |
|
32
|
|
|
->will($this->returnValue($this->payload)); |
|
33
|
|
|
|
|
34
|
|
|
$this->verifier = new NotBeforeVerifier(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function testMissingNotBefore() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
40
|
|
|
->method('findClaimByName') |
|
41
|
|
|
->with(NotBefore::NAME) |
|
42
|
|
|
->will($this->returnValue(null)); |
|
43
|
|
|
|
|
44
|
|
|
$this->verifier->verify($this->token); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @expectedException \Emarref\Jwt\Exception\TooEarlyException |
|
49
|
|
|
* @expectedExceptionMessageRegExp /Token must not be processed before "[\w,:+\d ]+"/ |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function testNotBefore() |
|
52
|
|
|
{ |
|
53
|
|
|
$dateTime = new \DateTime('1 day'); |
|
54
|
|
|
|
|
55
|
|
|
$notBeforeClaim = $this->getMockBuilder('Emarref\Jwt\Claim\NotBefore')->getMock(); |
|
56
|
|
|
|
|
57
|
|
|
$notBeforeClaim->expects($this->exactly(3)) |
|
58
|
|
|
->method('getValue') |
|
59
|
|
|
->will($this->returnValue($dateTime->getTimestamp())); |
|
60
|
|
|
|
|
61
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
62
|
|
|
->method('findClaimByName') |
|
63
|
|
|
->with(NotBefore::NAME) |
|
64
|
|
|
->will($this->returnValue($notBeforeClaim)); |
|
65
|
|
|
|
|
66
|
|
|
$this->verifier->verify($this->token); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @expectedException \InvalidArgumentException |
|
71
|
|
|
* @expectedExceptionMessage Invalid not before timestamp "foobar" |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
public function testUnexpectedValue() |
|
74
|
|
|
{ |
|
75
|
|
|
$notBeforeClaim = $this->getMockBuilder('Emarref\Jwt\Claim\NotBefore')->getMock(); |
|
76
|
|
|
|
|
77
|
|
|
$notBeforeClaim->expects($this->exactly(2)) |
|
78
|
|
|
->method('getValue') |
|
79
|
|
|
->will($this->returnValue('foobar')); |
|
80
|
|
|
|
|
81
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
82
|
|
|
->method('findClaimByName') |
|
83
|
|
|
->with(NotBefore::NAME) |
|
84
|
|
|
->will($this->returnValue($notBeforeClaim)); |
|
85
|
|
|
|
|
86
|
|
|
$this->verifier->verify($this->token); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function testValid() |
|
90
|
|
|
{ |
|
91
|
|
|
$past = new \DateTime('5 minutes ago', new \DateTimeZone('UTC')); |
|
92
|
|
|
|
|
93
|
|
|
$notBeforeClaim = $this->getMockBuilder('Emarref\Jwt\Claim\NotBefore')->getMock(); |
|
94
|
|
|
|
|
95
|
|
|
$notBeforeClaim->expects($this->exactly(2)) |
|
96
|
|
|
->method('getValue') |
|
97
|
|
|
->will($this->returnValue($past->getTimestamp())); |
|
98
|
|
|
|
|
99
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
100
|
|
|
->method('findClaimByName') |
|
101
|
|
|
->with(NotBefore::NAME) |
|
102
|
|
|
->will($this->returnValue($notBeforeClaim)); |
|
103
|
|
|
|
|
104
|
|
|
$this->verifier->verify($this->token); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: