|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Gandung\JWT\JWT; |
|
7
|
|
|
use Gandung\JWT\JWTFactory; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class JWTTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
private function callValidateKeyDirectly() |
|
15
|
|
|
{ |
|
16
|
|
|
$key = JWTFactory::getKeyManager(); |
|
17
|
|
|
$key->setContent('this is a text.'); |
|
18
|
|
|
$refl = new \ReflectionClass(JWT::class); |
|
19
|
|
|
$method = $refl->getMethod('validateKey'); |
|
20
|
|
|
$method->setAccessible(true); |
|
21
|
|
|
$method->invokeArgs(JWT::create(), [$key, \Gandung\JWT\Token\Algorithm::ES256]); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testCanGetInstance() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->assertInstanceOf(JWT::class, JWT::create()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testCanGetInstanceFromFactory() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->assertInstanceOf(JWT::class, JWTFactory::getJwt()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCanCreateTokenWithNoCertAlgoProvided() |
|
35
|
|
|
{ |
|
36
|
|
|
$key = JWTFactory::getKeyManager(); |
|
37
|
|
|
$key->setPassphrase('gandung31337'); |
|
38
|
|
|
$jose = JWTFactory::getJoseBuilder() |
|
39
|
|
|
->algorithm(\Gandung\JWT\Token\Algorithm::HS256) |
|
40
|
|
|
->type('JWT') |
|
41
|
|
|
->contentType('application/json'); |
|
42
|
|
|
$claim = JWTFactory::getClaimBuilder() |
|
|
|
|
|
|
43
|
|
|
->issuedBy('Paulus Gandung Prakosa') |
|
44
|
|
|
->expireAt(new \DateTimeImmutable('@' . (time() + (3600 * 3)))); |
|
45
|
|
|
$payload = JWTFactory::getPayloadBuilder() |
|
46
|
|
|
->claim($claim) |
|
47
|
|
|
->userData([ |
|
48
|
|
|
'credentials' => [ |
|
49
|
|
|
'username' => 'gandung', |
|
50
|
|
|
'password' => 'gandung31337' |
|
51
|
|
|
] |
|
52
|
|
|
]); |
|
53
|
|
|
$jwt = JWT::create(); |
|
54
|
|
|
$token = $jwt->createToken($jose, $payload, $key); |
|
55
|
|
|
$this->assertInternalType('string', $token); |
|
56
|
|
|
$this->assertNotEmpty($token); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testCanCreateToken() |
|
60
|
|
|
{ |
|
61
|
|
|
$key = JWTFactory::getKeyManager(); |
|
62
|
|
|
$key->setContentFromCertFile('cert/dummy256.pem'); |
|
63
|
|
|
$key->setPassphrase('umar123'); |
|
64
|
|
|
$jose = JWTFactory::getJoseBuilder() |
|
65
|
|
|
->algorithm(\Gandung\JWT\Token\Algorithm::RS256) |
|
66
|
|
|
->type('JWT') |
|
67
|
|
|
->contentType('application/json'); |
|
68
|
|
|
$claim = JWTFactory::getClaimBuilder() |
|
|
|
|
|
|
69
|
|
|
->issuedBy('Paulus Gandung Prakosa') |
|
70
|
|
|
->expireAt(new \DateTimeImmutable('@' . (time() + (3600 * 3)))); |
|
71
|
|
|
$payload = JWTFactory::getPayloadBuilder() |
|
72
|
|
|
->claim($claim) |
|
73
|
|
|
->userData([ |
|
74
|
|
|
'credentials' => [ |
|
75
|
|
|
'username' => 'gandung', |
|
76
|
|
|
'password' => 'gandung31337' |
|
77
|
|
|
] |
|
78
|
|
|
]); |
|
79
|
|
|
$jwt = JWT::create(); |
|
80
|
|
|
$token = $jwt->createToken($jose, $payload, $key); |
|
81
|
|
|
$this->assertInternalType('string', $token); |
|
82
|
|
|
$this->assertNotEmpty($token); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testCanVerifyToken() |
|
86
|
|
|
{ |
|
87
|
|
|
$key = JWTFactory::getKeyManager(); |
|
88
|
|
|
$key->setContentFromCertFile('file://cert/secp256.pem'); |
|
89
|
|
|
$jose = JWTFactory::getJoseBuilder() |
|
90
|
|
|
->algorithm(\Gandung\JWT\Token\Algorithm::ES256) |
|
91
|
|
|
->type('JWT') |
|
92
|
|
|
->contentType('application/json'); |
|
93
|
|
|
$claim = JWTFactory::getClaimBuilder() |
|
|
|
|
|
|
94
|
|
|
->issuedBy('Paulus Gandung Prakosa') |
|
95
|
|
|
->expireAt(new \DateTimeImmutable('@' . (time() * (3600 * 3)))); |
|
96
|
|
|
$payload = JWTFactory::getPayloadBuilder() |
|
97
|
|
|
->claim($claim) |
|
98
|
|
|
->userData([ |
|
99
|
|
|
'credentials' => [ |
|
100
|
|
|
'username' => 'gandung', |
|
101
|
|
|
'password' => 'gandung31337' |
|
102
|
|
|
] |
|
103
|
|
|
]); |
|
104
|
|
|
$jwt = JWT::create(); |
|
105
|
|
|
$token = $jwt->createToken($jose, $payload, $key); |
|
106
|
|
|
$this->assertInternalType('string', $token); |
|
107
|
|
|
$this->assertNotEmpty($token); |
|
108
|
|
|
$key->setContentFromCertFile('file://cert/secp256.pub.pem'); |
|
109
|
|
|
$isSignatureMatched = $jwt->verifyToken($token, $jose, $payload, $key); |
|
110
|
|
|
$this->assertInternalType('boolean', $isSignatureMatched); |
|
111
|
|
|
$this->assertTrue($isSignatureMatched); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @expectedException \InvalidArgumentException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testCanRaiseExceptionWhenValidateInvalidCertContent() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->callValidateKeyDirectly(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.