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-2015 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; 023import org.forgerock.util.encode.Base64; 024 025/** 026 * Creates an Octet JWK. 027 */ 028public class OctJWK extends JWK { 029 /** 030 * The Secret Key key value. 031 */ 032 private final static String K = "k"; 033 034 /** 035 * Constructs a OctJWK. 036 * @param use the JWK use 037 * @param alg the JWK algorithm 038 * @param kid the JWK key id 039 * @param key the symmetric key 040 * @param x5u the x509 url for the key 041 * @param x5t the x509 thumbnail for the key 042 * @param x5c the x509 chain 043 */ 044 public OctJWK(KeyUse use, String alg, String kid, String key, String x5u, String x5t, List<Base64> x5c) { 045 super(KeyType.OCT, use, alg, kid, x5u, x5t, x5c); 046 if (key == null || key.isEmpty()) { 047 throw new JsonException("key is a required field for an OctJWK"); 048 } 049 put(K, key); 050 } 051 052 /** 053 * Gets the symmetric key. 054 * @return the symmetric key that is Base64url encoded 055 */ 056 public String getKey() { 057 return get(K).asString(); 058 } 059 060 /** 061 * Parses a OctJWK object from a string json object. 062 * @param json string json object 063 * @return a OctJWK 064 */ 065 public static OctJWK parse(String json) { 066 JsonValue jwk = new JsonValue(toJsonValue(json)); 067 return parse(jwk); 068 } 069 070 /** 071 * Parses a OctJWK object from a jsonValue object. 072 * @param json an JsonValue object 073 * @return a OctJWK 074 */ 075 public static OctJWK parse(JsonValue json) { 076 if (json == null) { 077 throw new JsonException("Cant parse OctJWK. No json data."); 078 } 079 080 KeyType kty = null; 081 KeyUse use = null; 082 083 String k = null, alg = null, kid = null; 084 String x5u = null, x5t = null; 085 List<Base64> x5c = null; 086 087 k = json.get(K).asString(); 088 089 kty = KeyType.getKeyType(json.get(KTY).asString()); 090 if (!kty.equals(KeyType.OCT)) { 091 throw new JsonException("Invalid key type. Not an Oct JWK"); 092 } 093 094 use = KeyUse.getKeyUse(json.get(USE).asString()); 095 alg = json.get(ALG).asString(); 096 kid = json.get(KID).asString(); 097 098 x5u = json.get(X5U).asString(); 099 x5t = json.get(X5T).asString(); 100 x5c = json.get(X5C).asList(Base64.class); 101 102 return new OctJWK(use, alg, kid, k, x5u, x5t, x5c); 103 } 104 105 /** 106 * Prints the JWK as a json string. 107 * @return json string 108 */ 109 public String toJsonString() { 110 return super.toString(); 111 } 112}