forked from onewelcome/java-spring-oidc-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJwkSetProvider.java
45 lines (33 loc) · 1.27 KB
/
JwkSetProvider.java
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
package com.onegini.oidc.encryption;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.JWKSet;
import net.minidev.json.JSONObject;
@Service
public class JwkSetProvider {
@Resource
private JweKeyGenerator jweKeyGenerator;
private final Map<String, JWKSet> jwksSetMapCache = new HashMap<>();
public JSONObject getPublicJWKS(final JWEAlgorithm jweAlgorithm) {
return getJWKSet(jweAlgorithm).toJSONObject(true);
}
JWKSet getPrivateJWKS(final JWEAlgorithm jweAlgorithm) {
return getJWKSet(jweAlgorithm);
}
private JWKSet getJWKSet(final JWEAlgorithm jweAlgorithm) {
if (jwksSetMapCache.get(jweAlgorithm.getName()) == null) {
jwksSetMapCache.put(jweAlgorithm.getName(), createJwksSetForKeyType(jweAlgorithm));
}
return jwksSetMapCache.get(jweAlgorithm.getName());
}
private JWKSet createJwksSetForKeyType(final JWEAlgorithm jweAlgorithm) {
final JWK jwk1 = jweKeyGenerator.generateKey(jweAlgorithm);
final JWK jwk2 = jweKeyGenerator.generateKey(jweAlgorithm);
return new JWKSet(Lists.newArrayList(jwk1, jwk2));
}
}