View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.security.soap;
17  
18  import org.apache.cxf.binding.soap.SoapMessage;
19  import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
20  import org.apache.log4j.Logger;
21  import org.apache.ws.security.components.crypto.Crypto;
22  import org.apache.ws.security.components.crypto.Merlin;
23  import org.apache.ws.security.handler.RequestData;
24  import org.apache.ws.security.handler.WSHandlerConstants;
25  import org.kuali.rice.core.api.config.ConfigurationException;
26  import org.kuali.rice.core.api.config.property.Config;
27  import org.kuali.rice.core.api.config.property.ConfigContext;
28  import org.kuali.rice.core.api.exception.RiceRuntimeException;
29  import org.kuali.rice.core.api.util.ClassLoaderUtils;
30  import org.kuali.rice.ksb.config.wss4j.CryptoPasswordCallbackHandler;
31  import org.springframework.core.io.DefaultResourceLoader;
32  
33  import java.io.IOException;
34  import java.util.Properties;
35  
36  //import javax.xml.ws.handler.MessageContext;
37  
38  
39  /**
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  
44  //TODO: Replace this class with cxf wss4j in interceptor
45  public class CXFWSS4JInInterceptor extends WSS4JInInterceptor{
46  
47  	private static final Logger LOG = Logger.getLogger(CXFWSS4JInInterceptor.class);
48  
49  	private final boolean busSecurity;
50  	
51  	public CXFWSS4JInInterceptor(boolean busSecurity) {
52  		this.busSecurity = busSecurity;
53          if (busSecurity) {
54  		    this.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
55  		    this.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, CryptoPasswordCallbackHandler.class.getName());
56  		    this.setProperty(WSHandlerConstants.SIG_KEY_ID, "IssuerSerial");
57  		    this.setProperty(WSHandlerConstants.USER, ConfigContext.getCurrentContextConfig().getKeystoreAlias());
58          }
59  	}
60  
61  	@Override
62  	public Crypto loadSignatureCrypto(RequestData reqData) {
63  		try {
64  			return new Merlin(getMerlinProperties(), ClassLoaderUtils.getDefaultClassLoader());
65  		} catch (Exception e) {
66  			throw new RiceRuntimeException(e);
67  		}
68  	}
69  
70  	@Override
71  	public Crypto loadDecryptionCrypto(RequestData reqData) {
72  		return loadSignatureCrypto(reqData);
73  	}
74  
75  	protected Properties getMerlinProperties() throws IOException {
76  		Properties props = new Properties();
77  		props.put("org.apache.ws.security.crypto.merlin.keystore.type", "jks");
78  		props.put("org.apache.ws.security.crypto.merlin.keystore.password", ConfigContext.getCurrentContextConfig().getKeystorePassword());
79  		props.put("org.apache.ws.security.crypto.merlin.alias.password", ConfigContext.getCurrentContextConfig().getKeystorePassword());
80  		props.put("org.apache.ws.security.crypto.merlin.keystore.alias", ConfigContext.getCurrentContextConfig().getKeystoreAlias());
81  	    props.put("org.apache.ws.security.crypto.merlin.file", ConfigContext.getCurrentContextConfig().getKeystoreFile());
82  
83  		if (LOG.isDebugEnabled()) {
84  			LOG.debug("Using keystore location " + ConfigContext.getCurrentContextConfig().getKeystoreFile());
85  		}
86  		return props;
87  	}
88  
89  	/**
90  	 * This overridden method will not apply security headers if bus security is disabled.
91  	 * 
92  	 * @see org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor#handleMessage(org.apache.cxf.binding.soap.SoapMessage)
93  	 */
94  	@Override
95  	public void handleMessage(SoapMessage mc)  {
96  		if (busSecurity) {
97  			super.handleMessage(mc);
98  		}
99  	}
100 
101 }