1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Tests\Parser; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Gandung\JWT\Parser\Parser; |
7
|
|
|
use Gandung\JWT\Parser\ParserInterface; |
8
|
|
|
use Gandung\JWT\JWTFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ParserTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $token; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
parent::__construct(); |
23
|
|
|
$q = "this_is_me_who_will_sign_your_ass"; |
24
|
|
|
$key = JWTFactory::getKeyManager(); |
25
|
|
|
$key->setPassphrase($q); |
26
|
|
|
$header = JWTFactory::getJoseBuilder() |
27
|
|
|
->algorithm(\Gandung\JWT\Token\Algorithm::HS256) |
28
|
|
|
->type('JWT') |
29
|
|
|
->contentType('application/json'); |
30
|
|
|
$claim = JWTFactory::getClaimBuilder() |
|
|
|
|
31
|
|
|
->issuedBy('me') |
32
|
|
|
->expireAt(new \DateTimeImmutable('@' . (time() + 3600))); |
33
|
|
|
$payload = JWTFactory::getPayloadBuilder() |
34
|
|
|
->claim($claim) |
35
|
|
|
->userData([ |
36
|
|
|
'credentials' => [ |
37
|
|
|
'username' => 'me', |
38
|
|
|
'password' => password_hash($q, PASSWORD_BCRYPT) |
39
|
|
|
] |
40
|
|
|
]); |
41
|
|
|
$this->token = JWTFactory::getJwt()->createToken($header, $payload, $key); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testCanGetInstance() |
45
|
|
|
{ |
46
|
|
|
$this->assertInstanceOf(ParserInterface::class, new Parser); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testCanParseJWTToken() |
50
|
|
|
{ |
51
|
|
|
$parser = new Parser; |
52
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
53
|
|
|
$parser->parse($this->token); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCanGetSignatureInRawModeFromJWTToken() |
57
|
|
|
{ |
58
|
|
|
$parser = new Parser; |
59
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
60
|
|
|
$parser->parse($this->token); |
61
|
|
|
$signature = $parser->getSignature(); |
62
|
|
|
$this->assertInternalType('string', $signature); |
63
|
|
|
$this->assertNotEmpty($signature); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testCanGetSignatureInBase64ModeFromJWTToken() |
67
|
|
|
{ |
68
|
|
|
$parser = new Parser; |
69
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
70
|
|
|
$parser->parse($this->token); |
71
|
|
|
$signature = $parser->getSignature(false); |
72
|
|
|
$this->assertInternalType('string', $signature); |
73
|
|
|
$this->assertNotEmpty($signature); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \RuntimeException |
78
|
|
|
*/ |
79
|
|
|
public function testCanRaiseExceptionWhenGetTokenSignatureFromInvalidJWTToken() |
80
|
|
|
{ |
81
|
|
|
$parser = new Parser; |
82
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
83
|
|
|
$parser->getSignature(false); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testCanGetJoseHeader() |
87
|
|
|
{ |
88
|
|
|
$parser = new Parser; |
89
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
90
|
|
|
$parser->parse($this->token); |
91
|
|
|
$header = $parser->getJoseHeader(); |
92
|
|
|
$this->assertInstanceOf( |
93
|
|
|
\Gandung\JWT\Accessor\JoseHeaderAccessorInterface::class, |
94
|
|
|
$header |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @expectedException \RuntimeException |
100
|
|
|
*/ |
101
|
|
|
public function testCanRaiseExceptionWhenGetTokenHeaderFromInvalidJWTToken() |
102
|
|
|
{ |
103
|
|
|
$parser = new Parser; |
104
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
105
|
|
|
$parser->getJoseHeader(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testCanGetPayload() |
109
|
|
|
{ |
110
|
|
|
$parser = new Parser; |
111
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
112
|
|
|
$parser->parse($this->token); |
113
|
|
|
$payload = $parser->getPayload(); |
114
|
|
|
$this->assertInstanceOf( |
115
|
|
|
\Gandung\JWT\Accessor\PayloadAccessorInterface::class, |
116
|
|
|
$payload |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @expectedException \RuntimeException |
122
|
|
|
*/ |
123
|
|
|
public function testCanRaiseExceptionWhenGetTokenPayloadFromInvalidToken() |
124
|
|
|
{ |
125
|
|
|
$parser = new Parser; |
126
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
127
|
|
|
$parser->getPayload(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @expectedException \InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
public function testCanRaiseExceptionWhenParsingEmptyJWTToken() |
134
|
|
|
{ |
135
|
|
|
$parser = new Parser; |
136
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
137
|
|
|
$parser->parse(''); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @expectedException \RuntimeException |
142
|
|
|
*/ |
143
|
|
|
public function testCanRaiseExceptionWhenParsingInvalidJWTToken() |
144
|
|
|
{ |
145
|
|
|
$q = explode('.', $this->token); |
146
|
|
|
unset($q[0]); |
147
|
|
|
$i = join('.', $q); |
148
|
|
|
$parser = new Parser; |
149
|
|
|
$this->assertInstanceOf(ParserInterface::class, $parser); |
150
|
|
|
$parser->parse($i); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.