PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / firebase / php-jwt / tests / JWTTest.php

JWTTest.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/firebase/php-jwt/tests/JWTTest.php

265 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 use \Firebase\JWT\JWT;
3
4 class JWTTest extends PHPUnit_Framework_TestCase
5 {
6 public function testEncodeDecode()
7 {
8 $msg = JWT::encode('abc', 'my_key');
9 $this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
10 }
11
12 public function testDecodeFromPython()
13 {
14 $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
15 $this->assertEquals(
16 JWT::decode($msg, 'my_key', array('HS256')),
17 '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123'
18 );
19 }
20
21 public function testUrlSafeCharacters()
22 {
23 $encoded = JWT::encode('f?', 'a');
24 $this->assertEquals('f?', JWT::decode($encoded, 'a', array('HS256')));
25 }
26
27 public function testMalformedUtf8StringsFail()
28 {
29 $this->setExpectedException('DomainException');
30 JWT::encode(pack('c', 128), 'a');
31 }
32
33 public function testMalformedJsonThrowsException()
34 {
35 $this->setExpectedException('DomainException');
36 JWT::jsonDecode('this is not valid JSON string');
37 }
38
39 public function testExpiredToken()
40 {
41 $this->setExpectedException('Firebase\JWT\ExpiredException');
42 $payload = array(
43 "message" => "abc",
44 "exp" => time() - 20); // time in the past
45 $encoded = JWT::encode($payload, 'my_key');
46 JWT::decode($encoded, 'my_key', array('HS256'));
47 }
48
49 public function testBeforeValidTokenWithNbf()
50 {
51 $this->setExpectedException('Firebase\JWT\BeforeValidException');
52 $payload = array(
53 "message" => "abc",
54 "nbf" => time() + 20); // time in the future
55 $encoded = JWT::encode($payload, 'my_key');
56 JWT::decode($encoded, 'my_key', array('HS256'));
57 }
58
59 public function testBeforeValidTokenWithIat()
60 {
61 $this->setExpectedException('Firebase\JWT\BeforeValidException');
62 $payload = array(
63 "message" => "abc",
64 "iat" => time() + 20); // time in the future
65 $encoded = JWT::encode($payload, 'my_key');
66 JWT::decode($encoded, 'my_key', array('HS256'));
67 }
68
69 public function testValidToken()
70 {
71 $payload = array(
72 "message" => "abc",
73 "exp" => time() + JWT::$leeway + 20); // time in the future
74 $encoded = JWT::encode($payload, 'my_key');
75 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
76 $this->assertEquals($decoded->message, 'abc');
77 }
78
79 public function testValidTokenWithLeeway()
80 {
81 JWT::$leeway = 60;
82 $payload = array(
83 "message" => "abc",
84 "exp" => time() - 20); // time in the past
85 $encoded = JWT::encode($payload, 'my_key');
86 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
87 $this->assertEquals($decoded->message, 'abc');
88 JWT::$leeway = 0;
89 }
90
91 public function testExpiredTokenWithLeeway()
92 {
93 JWT::$leeway = 60;
94 $payload = array(
95 "message" => "abc",
96 "exp" => time() - 70); // time far in the past
97 $this->setExpectedException('Firebase\JWT\ExpiredException');
98 $encoded = JWT::encode($payload, 'my_key');
99 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
100 $this->assertEquals($decoded->message, 'abc');
101 JWT::$leeway = 0;
102 }
103
104 public function testValidTokenWithList()
105 {
106 $payload = array(
107 "message" => "abc",
108 "exp" => time() + 20); // time in the future
109 $encoded = JWT::encode($payload, 'my_key');
110 $decoded = JWT::decode($encoded, 'my_key', array('HS256', 'HS512'));
111 $this->assertEquals($decoded->message, 'abc');
112 }
113
114 public function testValidTokenWithNbf()
115 {
116 $payload = array(
117 "message" => "abc",
118 "iat" => time(),
119 "exp" => time() + 20, // time in the future
120 "nbf" => time() - 20);
121 $encoded = JWT::encode($payload, 'my_key');
122 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
123 $this->assertEquals($decoded->message, 'abc');
124 }
125
126 public function testValidTokenWithNbfLeeway()
127 {
128 JWT::$leeway = 60;
129 $payload = array(
130 "message" => "abc",
131 "nbf" => time() + 20); // not before in near (leeway) future
132 $encoded = JWT::encode($payload, 'my_key');
133 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
134 $this->assertEquals($decoded->message, 'abc');
135 JWT::$leeway = 0;
136 }
137
138 public function testInvalidTokenWithNbfLeeway()
139 {
140 JWT::$leeway = 60;
141 $payload = array(
142 "message" => "abc",
143 "nbf" => time() + 65); // not before too far in future
144 $encoded = JWT::encode($payload, 'my_key');
145 $this->setExpectedException('Firebase\JWT\BeforeValidException');
146 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
147 JWT::$leeway = 0;
148 }
149
150 public function testValidTokenWithIatLeeway()
151 {
152 JWT::$leeway = 60;
153 $payload = array(
154 "message" => "abc",
155 "iat" => time() + 20); // issued in near (leeway) future
156 $encoded = JWT::encode($payload, 'my_key');
157 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
158 $this->assertEquals($decoded->message, 'abc');
159 JWT::$leeway = 0;
160 }
161
162 public function testInvalidTokenWithIatLeeway()
163 {
164 JWT::$leeway = 60;
165 $payload = array(
166 "message" => "abc",
167 "iat" => time() + 65); // issued too far in future
168 $encoded = JWT::encode($payload, 'my_key');
169 $this->setExpectedException('Firebase\JWT\BeforeValidException');
170 $decoded = JWT::decode($encoded, 'my_key', array('HS256'));
171 JWT::$leeway = 0;
172 }
173
174 public function testInvalidToken()
175 {
176 $payload = array(
177 "message" => "abc",
178 "exp" => time() + 20); // time in the future
179 $encoded = JWT::encode($payload, 'my_key');
180 $this->setExpectedException('Firebase\JWT\SignatureInvalidException');
181 $decoded = JWT::decode($encoded, 'my_key2', array('HS256'));
182 }
183
184 public function testNullKeyFails()
185 {
186 $payload = array(
187 "message" => "abc",
188 "exp" => time() + JWT::$leeway + 20); // time in the future
189 $encoded = JWT::encode($payload, 'my_key');
190 $this->setExpectedException('InvalidArgumentException');
191 $decoded = JWT::decode($encoded, null, array('HS256'));
192 }
193
194 public function testEmptyKeyFails()
195 {
196 $payload = array(
197 "message" => "abc",
198 "exp" => time() + JWT::$leeway + 20); // time in the future
199 $encoded = JWT::encode($payload, 'my_key');
200 $this->setExpectedException('InvalidArgumentException');
201 $decoded = JWT::decode($encoded, '', array('HS256'));
202 }
203
204 public function testRSEncodeDecode()
205 {
206 $privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
207 'private_key_bits' => 1024,
208 'private_key_type' => OPENSSL_KEYTYPE_RSA));
209 $msg = JWT::encode('abc', $privKey, 'RS256');
210 $pubKey = openssl_pkey_get_details($privKey);
211 $pubKey = $pubKey['key'];
212 $decoded = JWT::decode($msg, $pubKey, array('RS256'));
213 $this->assertEquals($decoded, 'abc');
214 }
215
216 public function testKIDChooser()
217 {
218 $keys = array('1' => 'my_key', '2' => 'my_key2');
219 $msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
220 $decoded = JWT::decode($msg, $keys, array('HS256'));
221 $this->assertEquals($decoded, 'abc');
222 }
223
224 public function testArrayAccessKIDChooser()
225 {
226 $keys = new ArrayObject(array('1' => 'my_key', '2' => 'my_key2'));
227 $msg = JWT::encode('abc', $keys['1'], 'HS256', '1');
228 $decoded = JWT::decode($msg, $keys, array('HS256'));
229 $this->assertEquals($decoded, 'abc');
230 }
231
232 public function testNoneAlgorithm()
233 {
234 $msg = JWT::encode('abc', 'my_key');
235 $this->setExpectedException('DomainException');
236 JWT::decode($msg, 'my_key', array('none'));
237 }
238
239 public function testIncorrectAlgorithm()
240 {
241 $msg = JWT::encode('abc', 'my_key');
242 $this->setExpectedException('DomainException');
243 JWT::decode($msg, 'my_key', array('RS256'));
244 }
245
246 public function testMissingAlgorithm()
247 {
248 $msg = JWT::encode('abc', 'my_key');
249 $this->setExpectedException('DomainException');
250 JWT::decode($msg, 'my_key');
251 }
252
253 public function testAdditionalHeaders()
254 {
255 $msg = JWT::encode('abc', 'my_key', 'HS256', null, array('cty' => 'test-eit;v=1'));
256 $this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
257 }
258
259 public function testInvalidSegmentCount()
260 {
261 $this->setExpectedException('UnexpectedValueException');
262 JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256'));
263 }
264 }
265