View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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 java.io.IOException;
16  import java.util.Vector;
17  
18  import javax.security.auth.callback.Callback;
19  import javax.security.auth.callback.CallbackHandler;
20  import javax.security.auth.callback.UnsupportedCallbackException;
21  
22  import org.apache.ws.security.WSPasswordCallback;
23  import org.apache.ws.security.WSSecurityEngine;
24  import org.apache.ws.security.WSSecurityException;
25  import org.apache.ws.security.components.crypto.Crypto;
26  import org.apache.ws.security.util.WSSecurityUtil;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.config.IncomingWssConfig;
32  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
33  import com.eviware.soapui.support.StringUtils;
34  import com.eviware.soapui.support.UISupport;
35  
36  public class IncomingWss
37  {
38  	private IncomingWssConfig wssConfig;
39  	private final WssContainer container;
40  
41  	public IncomingWss( IncomingWssConfig wssConfig, WssContainer container )
42  	{
43  		this.wssConfig = wssConfig;
44  		this.container = container;
45  	}
46  	
47  	public WssContainer getWssContainer()
48  	{
49  		return container;
50  	}
51  	
52  	public String getDecryptCrypto()
53  	{
54  		return wssConfig.getDecryptCrypto();
55  	}
56  
57  	public String getDecryptPassword()
58  	{
59  		return wssConfig.getDecryptPassword();
60  	}
61  
62  	public String getName()
63  	{
64  		return wssConfig.getName();
65  	}
66  
67  	public String getSignatureCrypto()
68  	{
69  		return wssConfig.getSignatureCrypto();
70  	}
71  
72  	public void setDecryptCrypto( String arg0 )
73  	{
74  		wssConfig.setDecryptCrypto( arg0 );
75  	}
76  
77  	public void setDecryptPassword( String arg0 )
78  	{
79  		wssConfig.setDecryptPassword( arg0 );
80  	}
81  
82  	public void setName( String arg0 )
83  	{
84  		wssConfig.setName( arg0 );
85  	}
86  
87  	public void setSignatureCrypto( String arg0 )
88  	{
89  		wssConfig.setSignatureCrypto( arg0 );
90  	}
91  	
92  	public Vector 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 }