1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.json.jose.jwk;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.forgerock.json.JsonException;
28 import org.forgerock.json.JsonValue;
29 import org.forgerock.json.jose.jwt.Algorithm;
30 import org.forgerock.json.jose.jwt.JWObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36
37
38
39 public class JWKSet extends JWObject {
40
41 private static final Logger logger = LoggerFactory.getLogger(JWKSet.class);
42
43
44
45
46 public JWKSet() {
47 put("keys", Collections.EMPTY_LIST);
48 }
49
50
51
52
53
54 public JWKSet(JWK jwk) {
55 if (jwk == null) {
56 throw new JsonException("JWK must not be null");
57 }
58 put("keys", Collections.singletonList(jwk.toJsonValue().asMap()));
59 }
60
61
62
63
64
65 public JWKSet(JsonValue jwks) {
66 if (jwks == null) {
67 throw new JsonException("JWK set must not be null");
68 }
69 put("keys", jwks.expect(List.class));
70 }
71
72
73
74
75
76 public JWKSet(List<JWK> jwkList) {
77 if (jwkList == null) {
78 throw new JsonException("The list cannot be null");
79 }
80
81 List<Map<String, Object>> jwkListAsJson = new ArrayList<>();
82 for (JWK jwk : jwkList) {
83 jwkListAsJson.add(jwk.toJsonValue().asMap());
84 }
85 put("keys", jwkListAsJson);
86 }
87
88
89
90
91
92 public List<JWK> getJWKsAsList() {
93 List<JWK> listOfJWKs = new LinkedList<>();
94 JsonValue jwks = get("keys");
95 Iterator<JsonValue> i = jwks.iterator();
96 while (i.hasNext()) {
97 listOfJWKs.add(JWK.parse(i.next()));
98 }
99 return listOfJWKs;
100 }
101
102
103
104
105
106 public JsonValue getJWKsAsJsonValue() {
107 return get("keys");
108 }
109
110
111
112
113
114
115
116 protected static JsonValue toJsonValue(String json) {
117 ObjectMapper mapper = new ObjectMapper();
118 try {
119 return new JsonValue(mapper.readValue(json, Map.class));
120 } catch (IOException e) {
121 throw new JsonException("Failed to parse json", e);
122 }
123 }
124
125
126
127
128
129
130 public static JWKSet parse(String json) {
131 JsonValue jwkSet = new JsonValue(toJsonValue(json));
132 return parse(jwkSet);
133 }
134
135
136
137
138
139
140 public static JWKSet parse(JsonValue json) {
141 if (json == null) {
142 throw new JsonException("Cant parse JWKSet. No json data.");
143 }
144 return new JWKSet(json.get("keys"));
145 }
146
147
148
149
150
151 public String toJsonString() {
152 return super.toString();
153 }
154
155
156
157
158
159
160
161
162
163 public JWK findJwk(Algorithm algorithm, KeyUse keyUse) {
164
165 for (JWK jwk : getJWKsAsList()) {
166 try {
167 if (algorithm.getJwaAlgorithmName().equalsIgnoreCase(jwk.getAlgorithm()) && (keyUse == jwk.getUse())) {
168 return jwk;
169 }
170 } catch (IllegalArgumentException e) {
171
172 logger.warn("Can't load JWK with kid'" + jwk.getKeyId() + "'", e);
173 }
174 }
175
176
177 return keyUse != null ? findJwk(algorithm, null) : null;
178 }
179
180
181
182
183
184
185
186 public JWK findJwk(String kid) {
187 for (JWK jwk : getJWKsAsList()) {
188 if (kid.equals(jwk.getKeyId())) {
189 return jwk;
190 }
191 }
192 return null;
193 }
194 }