|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Guarded Authentication package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Jafar Jabr <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Jafar\Bundle\GuardedAuthenticationBundle\Tests\Api\KeyLoader; |
|
12
|
|
|
|
|
13
|
|
|
use Jafar\Bundle\GuardedAuthenticationBundle\Api\KeyLoader\LoadedJWS; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class LoadedJWSTest. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Jafar Jabr <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class LoadedJWSTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
private $goodPayload; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->goodPayload = [ |
|
31
|
|
|
'username' => 'jafaronly', |
|
32
|
|
|
'exp' => time() + 3600, |
|
33
|
|
|
'iat' => time(), |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testVerifiedWithEmptyPayload() |
|
38
|
|
|
{ |
|
39
|
|
|
$jws = new LoadedJWS($payload = [], true); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertSame($payload, $jws->getPayload()); |
|
42
|
|
|
$this->assertFalse($jws->isVerified()); |
|
43
|
|
|
$this->assertFalse($jws->isExpired()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testUnverifiedWithGoodPayload() |
|
47
|
|
|
{ |
|
48
|
|
|
$jws = new LoadedJWS($this->goodPayload, false); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($this->goodPayload, $jws->getPayload()); |
|
51
|
|
|
$this->assertFalse($jws->isExpired()); |
|
52
|
|
|
$this->assertFalse($jws->isVerified()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testVerifiedWithGoodPayload() |
|
56
|
|
|
{ |
|
57
|
|
|
$jws = new LoadedJWS($this->goodPayload, true); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame($this->goodPayload, $jws->getPayload()); |
|
60
|
|
|
$this->assertFalse($jws->isExpired()); |
|
61
|
|
|
$this->assertTrue($jws->isVerified()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testVerifiedWithExpiredPayload() |
|
65
|
|
|
{ |
|
66
|
|
|
$payload = $this->goodPayload; |
|
67
|
|
|
$payload['exp'] -= 3600; |
|
68
|
|
|
|
|
69
|
|
|
$jws = new LoadedJWS($payload, true); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertFalse($jws->isVerified()); |
|
72
|
|
|
$this->assertTrue($jws->isExpired()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testIsInvalidReturnsTrueWithIssuedAtSetInTheFuture() |
|
76
|
|
|
{ |
|
77
|
|
|
$payload = $this->goodPayload; |
|
78
|
|
|
$payload['iat'] += 3600; |
|
79
|
|
|
|
|
80
|
|
|
$jws = new LoadedJWS($payload, true); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertFalse($jws->isVerified()); |
|
83
|
|
|
$this->assertFalse($jws->isExpired()); |
|
84
|
|
|
$this->assertTrue($jws->isInvalid()); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|