001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2016 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.jwk;
018
019import java.util.List;
020
021import org.forgerock.json.JsonException;
022import org.forgerock.json.JsonValue;
023
024/**
025 * Creates an Octet JWK.
026 */
027public class OctJWK extends JWK {
028    /**
029     * The Secret Key key value.
030     */
031    private final static String K = "k";
032
033    /**
034     * Constructs a OctJWK.
035     * @param use the JWK use
036     * @param alg the JWK algorithm
037     * @param kid the JWK key id
038     * @param key the symmetric key
039     * @param x5u the x509 url for the key
040     * @param x5t the x509 thumbnail for the key
041     * @param x5c the x509 chain as a list of Base64 encoded strings
042     */
043    public OctJWK(KeyUse use, String alg, String kid, String key, String x5u, String x5t, List<String> x5c) {
044        super(KeyType.OCT, use, alg, kid, x5u, x5t, x5c);
045        if (key == null || key.isEmpty()) {
046            throw new JsonException("key is a required field for an OctJWK");
047        }
048        put(K, key);
049    }
050
051    /**
052     * Gets the symmetric key.
053     * @return the symmetric key that is Base64url encoded
054     */
055    public String getKey() {
056        return get(K).asString();
057    }
058
059    /**
060     * Parses a OctJWK object from a string json object.
061     * @param json string json object
062     * @return a OctJWK
063     */
064    public static OctJWK parse(String json) {
065        JsonValue jwk = new JsonValue(toJsonValue(json));
066        return parse(jwk);
067    }
068
069    /**
070     * Parses a OctJWK object from a jsonValue object.
071     * @param json an JsonValue object
072     * @return a OctJWK
073     */
074    public static OctJWK parse(JsonValue json) {
075        if (json == null) {
076            throw new JsonException("Cant parse OctJWK. No json data.");
077        }
078
079        KeyType kty = null;
080        KeyUse use = null;
081
082        String k = null, alg = null, kid = null;
083        String x5u = null, x5t = null;
084        List<String> x5c = null;
085
086        k = json.get(K).asString();
087
088        kty = KeyType.getKeyType(json.get(KTY).asString());
089        if (!kty.equals(KeyType.OCT)) {
090            throw new JsonException("Invalid key type. Not an Oct JWK");
091        }
092
093        use = KeyUse.getKeyUse(json.get(USE).asString());
094        alg = json.get(ALG).asString();
095        kid = json.get(KID).asString();
096
097        x5u = json.get(X5U).asString();
098        x5t = json.get(X5T).asString();
099        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}