-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathExamplesTest.php
195 lines (165 loc) · 6.47 KB
/
ExamplesTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace MiladRahimi\Jwt\Tests;
use MiladRahimi\Jwt\Cryptography\Algorithms\Ecdsa\ES384Signer;
use MiladRahimi\Jwt\Cryptography\Algorithms\Ecdsa\ES384Verifier;
use MiladRahimi\Jwt\Cryptography\Algorithms\Eddsa\EdDsaSigner;
use MiladRahimi\Jwt\Cryptography\Algorithms\Eddsa\EdDsaVerifier;
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Signer;
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Verifier;
use MiladRahimi\Jwt\Cryptography\Keys\EcdsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\EcdsaPublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\EdDsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\EdDsaPublicKey;
use MiladRahimi\Jwt\Cryptography\Keys\HmacKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
use MiladRahimi\Jwt\Exceptions\InvalidTokenException;
use MiladRahimi\Jwt\Exceptions\ValidationException;
use MiladRahimi\Jwt\Generator;
use MiladRahimi\Jwt\Parser;
use MiladRahimi\Jwt\Validator\DefaultValidator;
use MiladRahimi\Jwt\Validator\Rules\EqualsTo;
use MiladRahimi\Jwt\Validator\Rules\GreaterThan;
use MiladRahimi\Jwt\VerifierFactory;
use Throwable;
class ExamplesTest extends TestCase
{
/**
* @throws Throwable
*/
public function test_simple_example()
{
$key = new HmacKey('12345678901234567890123456789012');
$signer = new HS256($key);
// Generate a token
$generator = new Generator($signer);
$jwt = $generator->generate(['id' => 13, 'is-admin' => true]);
// Parse the token
$parser = new Parser($signer);
$claims = $parser->parse($jwt);
$this->assertEquals(['id' => 13, 'is-admin' => true], $claims);
}
/**
* @throws Throwable
*/
public function test_rsa_algorithms()
{
// Generate a token
$privateKey = new RsaPrivateKey(__DIR__ . '/../assets/keys/rsa-private.pem');
$signer = new RS256Signer($privateKey);
$generator = new Generator($signer);
$jwt = $generator->generate(['id' => 13, 'is-admin' => true]);
// Parse the token
$publicKey = new RsaPublicKey(__DIR__ . '/../assets/keys/rsa-public.pem');
$verifier = new RS256Verifier($publicKey);
$parser = new Parser($verifier);
$claims = $parser->parse($jwt);
$this->assertEquals(['id' => 13, 'is-admin' => true], $claims);
}
/**
* @throws Throwable
*/
public function test_ecdsa_algorithms()
{
// Generate a token
$privateKey = new EcdsaPrivateKey(__DIR__ . '/../assets/keys/ecdsa384-private.pem');
$signer = new ES384Signer($privateKey);
$generator = new Generator($signer);
$jwt = $generator->generate(['id' => 13, 'is-admin' => true]);
// Parse the token
$publicKey = new EcdsaPublicKey(__DIR__ . '/../assets/keys/ecdsa384-public.pem');
$verifier = new ES384Verifier($publicKey);
$parser = new Parser($verifier);
$claims = $parser->parse($jwt);
$this->assertEquals(['id' => 13, 'is-admin' => true], $claims);
}
/**
* @throws Throwable
*/
public function test_eddsa_algorithms()
{
// Generate a token
$privateKey = new EdDsaPrivateKey(
base64_decode(file_get_contents(__DIR__ . '/../assets/keys/ed25519.sec'))
);
$signer = new EdDsaSigner($privateKey);
$generator = new Generator($signer);
$jwt = $generator->generate(['id' => 666, 'is-admin' => true]);
// Parse the token
$publicKey = new EdDsaPublicKey(
base64_decode(file_get_contents(__DIR__ . '/../assets/keys/ed25519.pub'))
);
$verifier = new EdDsaVerifier($publicKey);
$parser = new Parser($verifier);
$claims = $parser->parse($jwt);
$this->assertEquals(['id' => 666, 'is-admin' => true], $claims);
}
/**
* @throws Throwable
*/
public function test_multiple_keys()
{
$privateKey1 = new RsaPrivateKey(
__DIR__ . '/../assets/keys/rsa-private.pem',
'',
'key-1'
);
$publicKey1 = new RsaPublicKey(__DIR__ . '/../assets/keys/rsa-public.pem', 'key-1');
$privateKey2 = new EcdsaPrivateKey(
__DIR__ . '/../assets/keys/ecdsa384-private.pem',
'',
'key-2'
);
$publicKey2 = new EcdsaPublicKey(__DIR__ . '/../assets/keys/ecdsa384-public.pem', 'key-2');
// Generate tokens
$signer1 = new RS256Signer($privateKey1);
$generator1 = new Generator($signer1);
$jwt1 = $generator1->generate(['id' => 13, 'is-admin' => true]);
$signer2 = new ES384Signer($privateKey2);
$generator2 = new Generator($signer2);
$jwt2 = $generator2->generate(['id' => 13, 'is-admin' => true]);
// Parse tokens
$verifierFactory = new VerifierFactory([
new RS256Verifier($publicKey1),
new ES384Verifier($publicKey2),
]);
$verifier1 = $verifierFactory->getVerifier($jwt1);
$parser1 = new Parser($verifier1);
$claims = $parser1->parse($jwt1);
$this->assertEquals(['id' => 13, 'is-admin' => true], $claims);
$verifier2 = $verifierFactory->getVerifier($jwt2);
$parser2 = new Parser($verifier2);
$claims = $parser2->parse($jwt2);
$this->assertEquals(['id' => 13, 'is-admin' => true], $claims);
$this->expectException(InvalidTokenException::class);
$parser1->parse($jwt2);
$this->expectException(InvalidTokenException::class);
$parser2->parse($jwt1);
}
/**
* @throws Throwable
*/
public function test_validation()
{
$jwt = join('.', [
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
'eyJpZCI6NjY2LCJpcy1hZG1pbiI6dHJ1ZX0',
'Abq2XaKQKCxGEdp9_CHsT8FHL1VGAoE76q7zx8-uqX0',
]);
$signer = new HS256(new HmacKey('12345678901234567890123456789012'));
// Add Validation
$validator = new DefaultValidator();
$validator->addRequiredRule('is-admin', new EqualsTo(true));
$validator->addRequiredRule('id', new GreaterThan(600));
// Parse the token
$parser = new Parser($signer, $validator);
try {
$claims = $parser->parse($jwt);
$this->assertEquals(['id' => 666, 'is-admin' => true], $claims);
} catch (ValidationException $e) {
// Handle error.
$this->fail();
}
}
}