1
|
|
|
package org.apereo.cas.util; |
2
|
|
|
|
3
|
|
|
import org.apereo.cas.util.crypto.PrivateKeyFactoryBean; |
4
|
|
|
import org.apereo.cas.util.crypto.PublicKeyFactoryBean; |
5
|
|
|
import org.jose4j.keys.AesKey; |
6
|
|
|
import org.jose4j.keys.RsaKeyUtil; |
7
|
|
|
import org.junit.Test; |
8
|
|
|
import org.springframework.core.io.ClassPathResource; |
9
|
|
|
|
10
|
|
|
import java.nio.charset.StandardCharsets; |
11
|
|
|
import java.security.Key; |
12
|
|
|
import java.security.PrivateKey; |
13
|
|
|
import java.security.PublicKey; |
14
|
|
|
|
15
|
|
|
import static org.junit.Assert.*; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This is {@link EncodingUtilsTests}. |
19
|
|
|
* |
20
|
|
|
* @author Misagh Moayyed |
21
|
|
|
* @since 5.2.0 |
22
|
|
|
*/ |
23
|
|
|
public class EncodingUtilsTests { |
24
|
|
|
|
25
|
|
|
@Test |
26
|
|
|
public void verifyAesKeyForJwtSigning() { |
27
|
|
|
final String secret = EncodingUtils.generateJsonWebKey(512); |
28
|
|
|
final Key key = new AesKey(secret.getBytes(StandardCharsets.UTF_8)); |
29
|
|
|
final String value = "ThisValue"; |
30
|
|
|
final byte[] signed = EncodingUtils.signJwsHMACSha512(key, value.getBytes(StandardCharsets.UTF_8)); |
31
|
|
|
final byte[] jwt = EncodingUtils.verifyJwsSignature(key, signed); |
32
|
|
|
final String result = new String(jwt, StandardCharsets.UTF_8); |
33
|
|
|
assertTrue(result.equals(value)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
@Test |
37
|
|
|
public void verifyRsaKeyForJwtSigning() { |
38
|
|
|
final String value = "ThisValue"; |
39
|
|
|
final byte[] signed = EncodingUtils.signJwsRSASha512(getPrivateKey(), value.getBytes(StandardCharsets.UTF_8)); |
40
|
|
|
final byte[] jwt = EncodingUtils.verifyJwsSignature(getPublicKey(), signed); |
41
|
|
|
final String result = new String(jwt, StandardCharsets.UTF_8); |
42
|
|
|
assertTrue(result.equals(value)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
@Test |
46
|
|
|
public void verifyAesKeyForJwtEncryption() { |
47
|
|
|
final String secret = EncodingUtils.generateJsonWebKey(256); |
48
|
|
|
final Key key = EncodingUtils.generateJsonWebKey(secret); |
49
|
|
|
final String value = "ThisValue"; |
50
|
|
|
final String found = EncodingUtils.encryptValueAsJwtDirectAes128Sha256(key, value); |
51
|
|
|
final String jwt = EncodingUtils.decryptJwtValue(key, found); |
52
|
|
|
assertTrue(jwt.equals(value)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
@Test |
56
|
|
|
public void verifyRsaKeyForJwtEncryption() { |
57
|
|
|
final String value = "ThisValue"; |
58
|
|
|
final String found = EncodingUtils.encryptValueAsJwtRsaOeap256Aes256Sha512(getPublicKey(), value); |
59
|
|
|
final String jwt = EncodingUtils.decryptJwtValue(getPrivateKey(), found); |
60
|
|
|
assertTrue(jwt.equals(value)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private static PrivateKey getPrivateKey() { |
64
|
|
|
try { |
65
|
|
|
final PrivateKeyFactoryBean factory = new PrivateKeyFactoryBean(); |
66
|
|
|
factory.setAlgorithm(RsaKeyUtil.RSA); |
67
|
|
|
factory.setLocation(new ClassPathResource("keys/RSA2048Private.key")); |
68
|
|
|
factory.setSingleton(false); |
69
|
|
|
return factory.getObject(); |
70
|
|
|
} catch (final Exception e) { |
71
|
|
|
throw new RuntimeException(e); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private static PublicKey getPublicKey() { |
76
|
|
|
try { |
77
|
|
|
final PublicKeyFactoryBean factory = new PublicKeyFactoryBean(); |
78
|
|
|
factory.setAlgorithm(RsaKeyUtil.RSA); |
79
|
|
|
factory.setLocation(new ClassPathResource("keys/RSA2048Public.key")); |
80
|
|
|
factory.setSingleton(false); |
81
|
|
|
return factory.getObject(); |
82
|
|
|
} catch (final Exception e) { |
83
|
|
|
throw new RuntimeException(e); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|