001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.ksb.security.soap;
017
018import org.apache.cxf.binding.soap.SoapMessage;
019import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
020import org.apache.log4j.Logger;
021import org.apache.ws.security.components.crypto.Crypto;
022import org.apache.ws.security.components.crypto.Merlin;
023import org.apache.ws.security.handler.RequestData;
024import org.apache.ws.security.handler.WSHandlerConstants;
025import org.kuali.rice.core.api.config.property.ConfigContext;
026import org.kuali.rice.core.api.exception.RiceRuntimeException;
027import org.kuali.rice.core.api.util.ClassLoaderUtils;
028import org.kuali.rice.ksb.config.wss4j.CryptoPasswordCallbackHandler;
029
030import java.util.Properties;
031
032
033/**
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037
038public class CXFWSS4JOutInterceptor extends WSS4JOutInterceptor {
039
040        private static final Logger LOG = Logger.getLogger(CXFWSS4JOutInterceptor.class);
041
042        private final boolean busSecurity;
043
044        public CXFWSS4JOutInterceptor(boolean busSecurity) {
045                this.busSecurity = busSecurity;
046        if (busSecurity) {
047                    this.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
048                    this.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, CryptoPasswordCallbackHandler.class.getName());
049                    this.setProperty(WSHandlerConstants.SIG_KEY_ID, "IssuerSerial");
050                    this.setProperty(WSHandlerConstants.USER, ConfigContext.getCurrentContextConfig().getKeystoreAlias());
051        }
052        }
053
054        @Override
055        public Crypto loadSignatureCrypto(RequestData reqData) {
056                try {
057                        return new Merlin(getMerlinProperties(), ClassLoaderUtils.getDefaultClassLoader());
058                } catch (Exception e) {
059                        throw new RiceRuntimeException(e);
060                }
061        }
062
063        @Override
064        public Crypto loadDecryptionCrypto(RequestData reqData) {
065                return loadSignatureCrypto(reqData);
066        }
067
068        protected Properties getMerlinProperties() {
069                Properties props = new Properties();
070                props.put("org.apache.ws.security.crypto.merlin.keystore.type", "jks");
071                props.put("org.apache.ws.security.crypto.merlin.keystore.password", ConfigContext.getCurrentContextConfig().getKeystorePassword());
072                props.put("org.apache.ws.security.crypto.merlin.alias.password", ConfigContext.getCurrentContextConfig().getKeystorePassword());
073                props.put("org.apache.ws.security.crypto.merlin.keystore.alias", ConfigContext.getCurrentContextConfig().getKeystoreAlias());
074                props.put("org.apache.ws.security.crypto.merlin.file", ConfigContext.getCurrentContextConfig().getKeystoreFile());
075
076                if (LOG.isDebugEnabled()) {
077                        LOG.debug("Using keystore location " + ConfigContext.getCurrentContextConfig().getKeystoreFile());
078                }
079
080                return props;
081        }
082
083        /**
084         * This overridden method will not apply security headers if bus security is disabled.
085         * 
086         * @see org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor#handleMessage(org.apache.cxf.binding.soap.SoapMessage)
087         */
088        @Override
089        public void handleMessage(SoapMessage mc) {
090                if (busSecurity) {
091                        super.handleMessage(mc);
092                }
093        }
094
095}