001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 003 * 004 * Portions copyright 2011-2013 The MITRE Corporation 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 *******************************************************************************/ 018/** 019 * 020 */ 021package org.mitre.jose.keystore; 022 023import java.io.IOException; 024import java.io.InputStreamReader; 025import java.text.ParseException; 026import java.util.List; 027 028import org.springframework.core.io.Resource; 029 030import com.google.common.base.Charsets; 031import com.google.common.io.CharStreams; 032import com.nimbusds.jose.jwk.JWK; 033import com.nimbusds.jose.jwk.JWKSet; 034 035/** 036 * @author jricher 037 * 038 */ 039public class JWKSetKeyStore { 040 041 private JWKSet jwkSet; 042 043 private Resource location; 044 045 public JWKSetKeyStore() { 046 047 } 048 049 public JWKSetKeyStore(JWKSet jwkSet) { 050 this.jwkSet = jwkSet; 051 initializeJwkSet(); 052 } 053 054 private void initializeJwkSet() { 055 056 if (jwkSet == null) { 057 if (location != null) { 058 059 if (location.exists() && location.isReadable()) { 060 061 try { 062 // read in the file from disk 063 String s = CharStreams.toString(new InputStreamReader(location.getInputStream(), Charsets.UTF_8)); 064 065 // parse it into a jwkSet object 066 jwkSet = JWKSet.parse(s); 067 } catch (IOException e) { 068 throw new IllegalArgumentException("Key Set resource could not be read: " + location); 069 } catch (ParseException e) { 070 throw new IllegalArgumentException("Key Set resource could not be parsed: " + location); } 071 072 } else { 073 throw new IllegalArgumentException("Key Set resource could not be read: " + location); 074 } 075 076 } else { 077 throw new IllegalArgumentException("Key store must be initialized with at least one of a jwkSet or a location."); 078 } 079 } 080 } 081 082 /** 083 * @return the jwkSet 084 */ 085 public JWKSet getJwkSet() { 086 return jwkSet; 087 } 088 089 /** 090 * @param jwkSet the jwkSet to set 091 */ 092 public void setJwkSet(JWKSet jwkSet) { 093 this.jwkSet = jwkSet; 094 initializeJwkSet(); 095 } 096 097 /** 098 * @return the location 099 */ 100 public Resource getLocation() { 101 return location; 102 } 103 104 /** 105 * @param location the location to set 106 */ 107 public void setLocation(Resource location) { 108 this.location = location; 109 initializeJwkSet(); 110 } 111 112 /** 113 * Get the list of keys in this keystore. This is a passthrough to the underlying JWK Set 114 */ 115 public List<JWK> getKeys() { 116 if (jwkSet == null) { 117 initializeJwkSet(); 118 } 119 return jwkSet.getKeys(); 120 } 121 122 123 124}