View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.support.wss;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.IncomingWssConfig;
17  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
18  import com.eviware.soapui.support.StringUtils;
19  import com.eviware.soapui.support.UISupport;
20  import com.eviware.soapui.support.resolver.ResolveContext;
21  import org.apache.ws.security.WSPasswordCallback;
22  import org.apache.ws.security.WSSecurityEngine;
23  import org.apache.ws.security.WSSecurityException;
24  import org.apache.ws.security.components.crypto.Crypto;
25  import org.apache.ws.security.util.WSSecurityUtil;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  import javax.security.auth.callback.Callback;
30  import javax.security.auth.callback.CallbackHandler;
31  import javax.security.auth.callback.UnsupportedCallbackException;
32  import java.io.IOException;
33  import java.util.Vector;
34  
35  public class IncomingWss
36  {
37  	private IncomingWssConfig wssConfig;
38  	private final WssContainer container;
39  
40  	public IncomingWss( IncomingWssConfig wssConfig, WssContainer container )
41  	{
42  		this.wssConfig = wssConfig;
43  		this.container = container;
44  	}
45  	
46  	public WssContainer getWssContainer()
47  	{
48  		return container;
49  	}
50  	
51  	public String getDecryptCrypto()
52  	{
53  		return wssConfig.getDecryptCrypto();
54  	}
55  
56  	public String getDecryptPassword()
57  	{
58  		return wssConfig.getDecryptPassword();
59  	}
60  
61  	public String getName()
62  	{
63  		return wssConfig.getName();
64  	}
65  
66  	public String getSignatureCrypto()
67  	{
68  		return wssConfig.getSignatureCrypto();
69  	}
70  
71  	public void setDecryptCrypto( String arg0 )
72  	{
73  		wssConfig.setDecryptCrypto( arg0 );
74  	}
75  
76  	public void setDecryptPassword( String arg0 )
77  	{
78  		wssConfig.setDecryptPassword( arg0 );
79  	}
80  
81  	public void setName( String arg0 )
82  	{
83  		wssConfig.setName( arg0 );
84  	}
85  
86  	public void setSignatureCrypto( String arg0 )
87  	{
88  		wssConfig.setSignatureCrypto( arg0 );
89  	}
90  	
91  	@SuppressWarnings("unchecked")
92  	public Vector<Object> processIncoming( Document soapDocument, PropertyExpansionContext context ) throws WSSecurityException
93  	{
94  		Element header = WSSecurityUtil.findWsseSecurityHeaderBlock( soapDocument, soapDocument.getDocumentElement(), false );
95  		if( header == null )
96  			return null;
97  
98  		try
99  		{
100 			WSSecurityEngine wssecurityEngine = WSSecurityEngine.getInstance();
101 			WssCrypto signatureCrypto = getWssContainer().getCryptoByName( getSignatureCrypto() );
102 			WssCrypto decryptCrypto = getWssContainer().getCryptoByName( getDecryptCrypto() );
103 			Crypto sig = signatureCrypto == null ? null : signatureCrypto.getCrypto();
104 			Crypto dec = decryptCrypto == null ? null : decryptCrypto.getCrypto();
105 			
106 			if( sig == null && dec == null )
107 				throw new WSSecurityException( "Missing cryptos" );
108 			
109 			if( sig == null )
110 				sig = dec;
111 			else if( dec == null )
112 				dec = sig;
113 			
114 			return wssecurityEngine.processSecurityHeader( soapDocument, (String)null, 
115 								new WSSCallbackHandler(), sig, dec );
116 		}
117 		catch( WSSecurityException e )
118 		{
119 			SoapUI.logError( e );
120 			throw e;
121 		}
122 	}
123 
124 	public class WSSCallbackHandler implements CallbackHandler
125 	{
126 		public void handle( Callback[] callbacks ) throws IOException, UnsupportedCallbackException
127 		{
128 			for( Callback callback : callbacks )
129 			{
130 				if( callback instanceof WSPasswordCallback )
131 				{
132 					WSPasswordCallback cb = ( WSPasswordCallback ) callback;
133 					if( StringUtils.hasContent( getDecryptPassword()))
134 						cb.setPassword( getDecryptPassword() );
135 					else
136 						cb.setPassword( UISupport.prompt( "Password required for WSS processing", "Specify Password", "" ));
137 				}
138 			}
139 		}
140 	}
141 
142 	public void updateConfig( IncomingWssConfig config )
143 	{
144 		this.wssConfig = config;
145 	}
146 
147 	public void resolve(ResolveContext context)
148 	{
149 	}
150 }