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.util.List;
20
21 import org.forgerock.json.JsonException;
22 import org.forgerock.json.JsonValue;
23
24
25
26
27 public class OctJWK extends JWK {
28
29
30
31 private final static String K = "k";
32
33
34
35
36
37
38
39
40
41
42
43 public OctJWK(KeyUse use, String alg, String kid, String key, String x5u, String x5t, List<String> x5c) {
44 super(KeyType.OCT, use, alg, kid, x5u, x5t, x5c);
45 if (key == null || key.isEmpty()) {
46 throw new JsonException("key is a required field for an OctJWK");
47 }
48 put(K, key);
49 }
50
51
52
53
54
55 public String getKey() {
56 return get(K).asString();
57 }
58
59
60
61
62
63
64 public static OctJWK parse(String json) {
65 JsonValue jwk = new JsonValue(toJsonValue(json));
66 return parse(jwk);
67 }
68
69
70
71
72
73
74 public static OctJWK parse(JsonValue json) {
75 if (json == null) {
76 throw new JsonException("Cant parse OctJWK. No json data.");
77 }
78
79 KeyType kty = null;
80 KeyUse use = null;
81
82 String k = null, alg = null, kid = null;
83 String x5u = null, x5t = null;
84 List<String> x5c = null;
85
86 k = json.get(K).asString();
87
88 kty = KeyType.getKeyType(json.get(KTY).asString());
89 if (!kty.equals(KeyType.OCT)) {
90 throw new JsonException("Invalid key type. Not an Oct JWK");
91 }
92
93 use = KeyUse.getKeyUse(json.get(USE).asString());
94 alg = json.get(ALG).asString();
95 kid = json.get(KID).asString();
96
97 x5u = json.get(X5U).asString();
98 x5t = json.get(X5T).asString();
99 x5c = json.get(X5C).asList(String.class);
100
101 return new OctJWK(use, alg, kid, k, x5u, x5t, x5c);
102 }
103
104
105
106
107
108 public String toJsonString() {
109 return super.toString();
110 }
111 }