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 Copyrighted [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.jwk;
018
019import java.io.IOException;
020import java.util.Iterator;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.Map;
024
025import org.forgerock.json.JsonException;
026import org.forgerock.json.JsonValue;
027import org.forgerock.json.jose.jwt.JWObject;
028
029import com.fasterxml.jackson.databind.ObjectMapper;
030
031/**
032 * Holds a Set of JWKs.
033 */
034public class JWKSet extends JWObject {
035
036    /**
037     * Constructs an empty JWKSet.
038     */
039    public JWKSet() {
040
041    }
042
043    /**
044     * Construct a JWKSet from a single JWK.
045     * @param jwk the jwk to construct the set from
046     */
047    public JWKSet(JWK jwk) {
048        if (jwk == null) {
049            throw new JsonException("JWK must not be null");
050        }
051        put("keys", jwk);
052    }
053
054    /**
055     * Construct a JWKSet from a single JWK.
056     * @param jwk contains a list of json web keys
057     */
058    public JWKSet(JsonValue jwk) {
059        if (jwk == null) {
060            throw new JsonException("JWK must not be null");
061        }
062        put("keys", jwk);
063    }
064
065    /**
066     * Construct a JWKSet from a List of JWKs.
067     * @param jwkList a list of jwks
068     */
069    public JWKSet(List<JWK> jwkList) {
070        if (jwkList == null) {
071            throw new JsonException("The list cant be null");
072        }
073        put("keys", jwkList);
074    }
075
076    /**
077     * Get the JWKs in the set.
078     * @return a list of JWKs
079     */
080    public List<JWK> getJWKsAsList() {
081        List<JWK> listOfJWKs = new LinkedList<>();
082        JsonValue jwks = get("keys");
083        Iterator<JsonValue> i = jwks.iterator();
084        while (i.hasNext()) {
085            listOfJWKs.add(JWK.parse(i.next()));
086        }
087        return listOfJWKs;
088    }
089
090    /**
091     * Get the JWKs in the set.
092     * @return a list of JWKs as JsonValues
093     */
094    public JsonValue getJWKsAsJsonValue() {
095        return get("keys");
096    }
097
098    /**
099     * Converts a json string to a jsonValue.
100     * @param json a json jwk set object string
101     * @return a json value of the son string
102     * @throws JsonException if unable to parse
103     */
104    protected static JsonValue toJsonValue(String json) {
105        ObjectMapper mapper = new ObjectMapper();
106        try {
107            return new JsonValue(mapper.readValue(json, Map.class));
108        } catch (IOException e) {
109            throw new JsonException("Failed to parse json", e);
110        }
111    }
112
113    /**
114     * Parses a JWKSet object from a string json object.
115     * @param json string json object
116     * @return a JWKSet
117     */
118    public static JWKSet parse(String json) {
119        JsonValue jwkSet = new JsonValue(toJsonValue(json));
120        return parse(jwkSet);
121    }
122
123    /**
124     * Parses a JWKSet object from a jsonValue object.
125     * @param json an JsonValue object
126     * @return a JWKSet
127     */
128    public static JWKSet parse(JsonValue json) {
129        if (json == null) {
130            throw new JsonException("Cant parse JWKSet. No json data.");
131        }
132        return new JWKSet(json.get("keys"));
133    }
134
135    /**
136     * Prints the JWK Set as a json string.
137     * @return A String representing JWK
138     */
139    public String toJsonString() {
140        return super.toString();
141    }
142
143}