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