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.List;
21 import java.util.Map;
22
23 import org.forgerock.json.JsonException;
24 import org.forgerock.json.JsonValue;
25 import org.forgerock.json.jose.jwt.JWObject;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29
30
31
32 public abstract class JWK extends JWObject {
33
34
35
36 protected static final String KTY = "kty";
37
38
39
40
41 protected static final String USE = "use";
42
43
44
45
46 protected static final String ALG = "alg";
47
48
49
50
51 protected static final String KID = "kid";
52
53
54
55
56 protected static final String X5U = "x5u";
57
58
59
60
61 protected static final String X5T = "x5t";
62
63
64
65
66 protected static final String X5C = "x5c";
67
68
69
70
71
72
73
74
75 protected JWK(KeyType kty, KeyUse use, String alg, String kid) {
76 this(kty, use, alg, kid, null, null, null);
77 }
78
79
80
81
82
83
84
85
86
87
88
89 protected JWK(KeyType kty, KeyUse use, String alg, String kid, String x5u, String x5t, List<String> x5c) {
90 super();
91 if (kty == null) {
92 new JsonException("kty is a required field");
93 }
94 put(KTY, kty.toString());
95 if (kid == null || kid.isEmpty()) {
96 new JsonException("kid is a required field");
97 }
98 put(KID, kid);
99 if (use != null) {
100 put(USE, use.toString());
101 }
102 if (alg != null && !alg.isEmpty()) {
103 put(ALG, alg);
104 }
105 if (x5c != null && !x5c.isEmpty()) {
106 put(X5C, x5c);
107 }
108 if (x5t != null && !x5t.isEmpty()) {
109 put(X5T, x5t);
110 }
111 if (x5u != null && !x5u.isEmpty()) {
112 put(X5U, x5u);
113 }
114 }
115
116
117
118
119
120 public KeyType getKeyType() {
121 return KeyType.getKeyType(get(KTY).asString());
122 }
123
124
125
126
127
128 public KeyUse getUse() {
129 return KeyUse.getKeyUse(get(USE).asString());
130 }
131
132
133
134
135
136 public String getAlgorithm() {
137 return get(ALG).asString();
138 }
139
140
141
142
143
144 public String getKeyId() {
145 return get(KID).asString();
146 }
147
148
149
150
151
152 public String toJsonString() {
153 return toString();
154 }
155
156
157
158
159
160
161
162
163 public static JWK parse(String json) {
164 JsonValue jwk = new JsonValue(toJsonValue(json));
165 return parse(jwk);
166 }
167
168
169
170
171
172
173
174
175 public static JWK parse(JsonValue jwk) {
176 KeyType kty = KeyType.getKeyType(jwk.get(KTY).asString());
177
178 if (kty.equals(KeyType.RSA)) {
179 return RsaJWK.parse(jwk);
180 } else if (kty.equals(KeyType.OCT)) {
181 return OctJWK.parse(jwk);
182 } else if (kty.equals(KeyType.EC)) {
183 return EcJWK.parse(jwk);
184 } else {
185 throw new JsonException("Failed to parse json invalid kty parameter");
186 }
187 }
188
189
190
191
192
193
194
195
196 protected static JsonValue toJsonValue(String json) {
197 ObjectMapper mapper = new ObjectMapper();
198 try {
199 return new JsonValue(mapper.readValue(json, Map.class));
200 } catch (IOException e) {
201 throw new JsonException("Failed to parse json", e);
202 }
203 }
204
205
206
207
208
209 public String getX509URL() {
210 return get(X5U).asString();
211 }
212
213
214
215
216
217 public String getX509Thumbnail() {
218 return get(X5T).asString();
219 }
220
221
222
223
224
225 public List<String> getX509Chain() {
226 return get(X5C).asList(String.class);
227 }
228 }