Skip to content

Commit 160d557

Browse files
committed
增加 JWKS test与使用说明示例
1 parent fdfeeb3 commit 160d557

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package myoidc.server.infrastructure;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.io.CharStreams;
5+
import org.jose4j.jwk.JsonWebKey;
6+
import org.jose4j.jwk.JsonWebKeySet;
7+
import org.jose4j.jws.AlgorithmIdentifiers;
8+
import org.jose4j.jws.JsonWebSignature;
9+
import org.jose4j.jwt.JwtClaims;
10+
import org.junit.Test;
11+
12+
import java.io.InputStream;
13+
import java.io.InputStreamReader;
14+
15+
import static org.junit.Assert.assertNotNull;
16+
17+
/**
18+
* 2020/6/3
19+
*
20+
* @author Shengzhao Li
21+
* @since 1.1.1
22+
*/
23+
public class JsonWebKeySetTest {
24+
25+
26+
//RSA
27+
@Test
28+
public void keySet() throws Exception {
29+
30+
JsonWebKeySet jwkSet;
31+
try (InputStream is = getClass().getClassLoader().getResourceAsStream("jwks_rsa.json")) {
32+
String keyJson = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
33+
jwkSet = new JsonWebKeySet(keyJson);
34+
}
35+
36+
JsonWebKey jsonWebKey = jwkSet.getJsonWebKeys().get(0);
37+
assertNotNull(jsonWebKey);
38+
39+
40+
}
41+
42+
43+
//ECC
44+
@Test
45+
public void keyECCSet() throws Exception {
46+
47+
JsonWebKeySet jwkSet;
48+
try (InputStream is = getClass().getClassLoader().getResourceAsStream("jwks_ec.json")) {
49+
String keyJson = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
50+
jwkSet = new JsonWebKeySet(keyJson);
51+
}
52+
53+
JsonWebKey jsonWebKey = jwkSet.getJsonWebKeys().get(0);
54+
assertNotNull(jsonWebKey);
55+
56+
57+
}
58+
59+
60+
//OCT
61+
@Test
62+
public void keyOCTSet() throws Exception {
63+
64+
JsonWebKeySet jwkSet;
65+
try (InputStream is = getClass().getClassLoader().getResourceAsStream("jwks_oct.json")) {
66+
String keyJson = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
67+
jwkSet = new JsonWebKeySet(keyJson);
68+
}
69+
70+
JsonWebKey jsonWebKey = jwkSet.getJsonWebKeys().get(0);
71+
assertNotNull(jsonWebKey);
72+
73+
74+
JsonWebSignature jws = new JsonWebSignature();
75+
jws.setKey(jsonWebKey.getKey());
76+
77+
JwtClaims claims = new JwtClaims();
78+
claims.setGeneratedJwtId();
79+
claims.setIssuedAtToNow();
80+
claims.setSubject("subject");
81+
jws.setPayload(claims.toJson());
82+
83+
jws.setKeyIdHeaderValue(jsonWebKey.getKeyId());
84+
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
85+
86+
String idToken = jws.getCompactSerialization();
87+
assertNotNull(idToken);
88+
// System.out.println(idToken);
89+
90+
}
91+
92+
93+
}

0 commit comments

Comments
 (0)