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