001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016
017package org.mitre.oauth2.model.convert;
018
019import java.text.ParseException;
020
021import javax.persistence.AttributeConverter;
022import javax.persistence.Converter;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import com.nimbusds.jose.jwk.JWKSet;
028
029/**
030 * @author jricher
031 *
032 */
033@Converter
034public class JWKSetStringConverter implements AttributeConverter<JWKSet, String> {
035
036        private static Logger logger = LoggerFactory.getLogger(JWKSetStringConverter.class);
037
038        @Override
039        public String convertToDatabaseColumn(JWKSet attribute) {
040                if (attribute != null) {
041                        return attribute.toString();
042                } else {
043                        return null;
044                }
045        }
046
047        /* (non-Javadoc)
048         * @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
049         */
050        @Override
051        public JWKSet convertToEntityAttribute(String dbData) {
052                if (dbData != null) {
053                        try {
054                                JWKSet jwks = JWKSet.parse(dbData);
055                                return jwks;
056                        } catch (ParseException e) {
057                                logger.error("Unable to parse JWK Set", e);
058                                return null;
059                        }
060                } else {
061                        return null;
062                }
063
064        }
065
066}