@@ -22,6 +22,7 @@ import io.jsonwebtoken.impl.io.Streams
22
22
import io.jsonwebtoken.impl.lang.Bytes
23
23
import io.jsonwebtoken.impl.lang.Services
24
24
import io.jsonwebtoken.impl.security.*
25
+ import io.jsonwebtoken.io.CompressionAlgorithm
25
26
import io.jsonwebtoken.io.Decoders
26
27
import io.jsonwebtoken.io.Deserializer
27
28
import io.jsonwebtoken.io.Encoders
@@ -1398,6 +1399,97 @@ class JwtsTest {
1398
1399
}
1399
1400
}
1400
1401
1402
+ @Test
1403
+ void testJweCompressionWithArbitraryContentString () {
1404
+ def codecs = [Jwts . ZIP . DEF , Jwts . ZIP . GZIP ]
1405
+
1406
+ for (CompressionAlgorithm zip : codecs) {
1407
+
1408
+ for (AeadAlgorithm enc : Jwts . ENC . get(). values()) {
1409
+
1410
+ SecretKey key = enc. key(). build()
1411
+
1412
+ String payload = ' hello, world!'
1413
+
1414
+ // encrypt and compress:
1415
+ String jwe = Jwts . builder()
1416
+ .content(payload)
1417
+ .compressWith(zip)
1418
+ .encryptWith(key, enc)
1419
+ .compact()
1420
+
1421
+ // decompress and decrypt:
1422
+ def jwt = Jwts . parser()
1423
+ .decryptWith(key)
1424
+ .build()
1425
+ .parseEncryptedContent(jwe)
1426
+ assertEquals payload, new String (jwt. getPayload(), StandardCharsets . UTF_8 )
1427
+ }
1428
+ }
1429
+ }
1430
+
1431
+ @Test
1432
+ void testJweCompressionWithArbitraryContentByteArray () {
1433
+ def codecs = [Jwts . ZIP . DEF , Jwts . ZIP . GZIP ]
1434
+
1435
+ for (CompressionAlgorithm zip : codecs) {
1436
+
1437
+ for (AeadAlgorithm enc : Jwts . ENC . get(). values()) {
1438
+
1439
+ SecretKey key = enc. key(). build()
1440
+
1441
+ byte [] payload = new byte [14 ];
1442
+ Randoms . secureRandom(). nextBytes(payload)
1443
+
1444
+ // encrypt and compress:
1445
+ String jwe = Jwts . builder()
1446
+ .content(payload)
1447
+ .compressWith(zip)
1448
+ .encryptWith(key, enc)
1449
+ .compact()
1450
+
1451
+ // decompress and decrypt:
1452
+ def jwt = Jwts . parser()
1453
+ .decryptWith(key)
1454
+ .build()
1455
+ .parseEncryptedContent(jwe)
1456
+ assertArrayEquals payload, jwt. getPayload()
1457
+ }
1458
+ }
1459
+ }
1460
+
1461
+ @Test
1462
+ void testJweCompressionWithArbitraryContentInputStream () {
1463
+ def codecs = [Jwts . ZIP . DEF , Jwts . ZIP . GZIP ]
1464
+
1465
+ for (CompressionAlgorithm zip : codecs) {
1466
+
1467
+ for (AeadAlgorithm enc : Jwts . ENC . get(). values()) {
1468
+
1469
+ SecretKey key = enc. key(). build()
1470
+
1471
+ byte [] payloadBytes = new byte [14 ];
1472
+ Randoms . secureRandom(). nextBytes(payloadBytes)
1473
+
1474
+ ByteArrayInputStream payload = new ByteArrayInputStream (payloadBytes)
1475
+
1476
+ // encrypt and compress:
1477
+ String jwe = Jwts . builder()
1478
+ .content(payload)
1479
+ .compressWith(zip)
1480
+ .encryptWith(key, enc)
1481
+ .compact()
1482
+
1483
+ // decompress and decrypt:
1484
+ def jwt = Jwts . parser()
1485
+ .decryptWith(key)
1486
+ .build()
1487
+ .parseEncryptedContent(jwe)
1488
+ assertArrayEquals payloadBytes, jwt. getPayload()
1489
+ }
1490
+ }
1491
+ }
1492
+
1401
1493
@Test
1402
1494
void testPasswordJwes () {
1403
1495
0 commit comments