|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Verification; |
|
4
|
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim; |
|
6
|
|
|
|
|
7
|
|
|
class AudienceVerifierTest 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
|
|
|
public function setUp() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
|
22
|
|
|
|
|
23
|
|
|
$this->token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @expectedException \InvalidArgumentException |
|
28
|
|
|
* @expectedExceptionMessage Cannot verify invalid audience value. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testInvalidAudience() |
|
31
|
|
|
{ |
|
32
|
|
|
new AudienceVerifier(new \stdClass()); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testNoAudienceInToken() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
|
|
38
|
|
|
->method('getPayload') |
|
39
|
|
|
->will($this->returnValue($this->payload)); |
|
40
|
|
|
|
|
41
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
42
|
|
|
->method('findClaimByName') |
|
43
|
|
|
->with(Claim\Audience::NAME) |
|
44
|
|
|
->will($this->returnValue(null)); |
|
45
|
|
|
|
|
46
|
|
|
$verifier = new AudienceVerifier(); |
|
47
|
|
|
$verifier->verify($this->token); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidAudienceException |
|
52
|
|
|
* @expectedExceptionMessage Audience is invalid. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testInvalidAudienceInToken() |
|
55
|
|
|
{ |
|
56
|
|
|
$expectedAudience = 'urn://myaudience'; |
|
57
|
|
|
|
|
58
|
|
|
$audienceClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Audience')->getMock(); |
|
59
|
|
|
|
|
60
|
|
|
$audienceClaim->expects($this->once()) |
|
61
|
|
|
->method('getValue') |
|
62
|
|
|
->will($this->returnValue($expectedAudience)); |
|
63
|
|
|
|
|
64
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
65
|
|
|
->method('findClaimByName') |
|
66
|
|
|
->with(Claim\Audience::NAME) |
|
67
|
|
|
->will($this->returnValue($audienceClaim)); |
|
68
|
|
|
|
|
69
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
|
|
70
|
|
|
->method('getPayload') |
|
71
|
|
|
->will($this->returnValue($this->payload)); |
|
72
|
|
|
|
|
73
|
|
|
$verifier = new AudienceVerifier('foobar'); |
|
74
|
|
|
$verifier->verify($this->token); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testArrayAudience() |
|
78
|
|
|
{ |
|
79
|
|
|
$audienceClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Audience')->getMock(); |
|
80
|
|
|
|
|
81
|
|
|
$audienceClaim->expects($this->once()) |
|
82
|
|
|
->method('getValue') |
|
83
|
|
|
->will($this->returnValue(['urn://audienceone', 'urn://audiencetwo'])); |
|
84
|
|
|
|
|
85
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
86
|
|
|
->method('findClaimByName') |
|
87
|
|
|
->with(Claim\Audience::NAME) |
|
88
|
|
|
->will($this->returnValue($audienceClaim)); |
|
89
|
|
|
|
|
90
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
|
|
91
|
|
|
->method('getPayload') |
|
92
|
|
|
->will($this->returnValue($this->payload)); |
|
93
|
|
|
|
|
94
|
|
|
$verifier = new AudienceVerifier('urn://audienceone'); |
|
95
|
|
|
$verifier->verify($this->token); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testStringAudience() |
|
99
|
|
|
{ |
|
100
|
|
|
$audienceClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Audience')->getMock(); |
|
101
|
|
|
|
|
102
|
|
|
$audienceClaim->expects($this->once()) |
|
103
|
|
|
->method('getValue') |
|
104
|
|
|
->will($this->returnValue('urn://audienceone')); |
|
105
|
|
|
|
|
106
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
|
|
107
|
|
|
->method('findClaimByName') |
|
108
|
|
|
->with(Claim\Audience::NAME) |
|
109
|
|
|
->will($this->returnValue($audienceClaim)); |
|
110
|
|
|
|
|
111
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
|
|
112
|
|
|
->method('getPayload') |
|
113
|
|
|
->will($this->returnValue($this->payload)); |
|
114
|
|
|
|
|
115
|
|
|
$verifier = new AudienceVerifier('urn://audienceone'); |
|
116
|
|
|
$verifier->verify($this->token); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: