View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2013-2016 ForgeRock AS.
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   * Creates an Octet JWK.
26   */
27  public class OctJWK extends JWK {
28      /**
29       * The Secret Key key value.
30       */
31      private final static String K = "k";
32  
33      /**
34       * Constructs a OctJWK.
35       * @param use the JWK use
36       * @param alg the JWK algorithm
37       * @param kid the JWK key id
38       * @param key the symmetric key
39       * @param x5u the x509 url for the key
40       * @param x5t the x509 thumbnail for the key
41       * @param x5c the x509 chain as a list of Base64 encoded strings
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       * Gets the symmetric key.
53       * @return the symmetric key that is Base64url encoded
54       */
55      public String getKey() {
56          return get(K).asString();
57      }
58  
59      /**
60       * Parses a OctJWK object from a string json object.
61       * @param json string json object
62       * @return a OctJWK
63       */
64      public static OctJWK parse(String json) {
65          JsonValue jwk = new JsonValue(toJsonValue(json));
66          return parse(jwk);
67      }
68  
69      /**
70       * Parses a OctJWK object from a jsonValue object.
71       * @param json an JsonValue object
72       * @return a OctJWK
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      * Prints the JWK as a json string.
106      * @return json string
107      */
108     public String toJsonString() {
109         return super.toString();
110     }
111 }