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.entries;
14  
15  import java.security.MessageDigest;
16  
17  import javax.swing.JComponent;
18  
19  import org.apache.ws.security.WSConstants;
20  import org.apache.ws.security.message.WSSecHeader;
21  import org.apache.ws.security.message.WSSecUsernameToken;
22  import org.apache.ws.security.util.Base64;
23  import org.w3c.dom.Document;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.config.WSSEntryConfig;
27  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
28  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
29  import com.eviware.soapui.support.StringUtils;
30  import com.eviware.soapui.support.components.SimpleBindingForm;
31  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
32  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
33  import com.jgoodies.binding.PresentationModel;
34  
35  public class AddUsernameEntry extends WssEntryBase 
36  {
37  	private static final String PASSWORD_DIGEST_EXT = "PasswordDigest Ext";
38  
39  	private static final String PASSWORD_DIGEST = "PasswordDigest";
40  
41  	private static final String PASSWORD_TEXT = "PasswordText";
42  
43  	public static final String TYPE = "Username";
44  	
45  	private boolean addCreated;
46  	private boolean addNonce;
47  	private String passwordType;
48  
49  	public void init( WSSEntryConfig config, OutgoingWss container )
50  	{
51  		super.init( config, container, TYPE );
52  	}
53  
54  	public void process( WSSecHeader secHeader, Document doc, PropertyExpansionContext context )
55  	{
56  		WSSecUsernameToken token = new WSSecUsernameToken();
57  		if( addCreated )
58  			token.addCreated();
59  		
60  		if( addNonce )
61  			token.addNonce();
62  		
63  		if( StringUtils.hasContent( passwordType ))
64  		{
65  			if( passwordType.equals( PASSWORD_TEXT ))
66  				token.setPasswordType( WSConstants.PASSWORD_TEXT );
67  			else if( passwordType.equals( PASSWORD_DIGEST ) || passwordType.equals( PASSWORD_DIGEST_EXT ))
68  				token.setPasswordType( WSConstants.PASSWORD_DIGEST );
69  		}
70  		
71  		String password = context.expand( getPassword() );
72  		
73  		if( PASSWORD_DIGEST_EXT.equals( password ))
74  		{
75  			try
76  			{
77  				MessageDigest sha = MessageDigest.getInstance("SHA-1");
78  				sha.reset();
79  				sha.update(password.getBytes("UTF-8"));
80  				password = Base64.encode(sha.digest());
81  			}
82  			catch( Exception e )
83  			{
84  				SoapUI.logError( e );
85  			}
86  		}
87  		
88  		token.setUserInfo( context.expand( getUsername()), password);
89  		
90  		token.build( doc, secHeader );
91  	}
92  
93  	@Override
94  	protected JComponent buildUI()
95  	{
96  		SimpleBindingForm form = new SimpleBindingForm( new PresentationModel<AddUsernameEntry>( this ) ); 
97  		form.addSpace(5);
98  		form.appendTextField( "username", "Username", "The username for this token" );
99  		form.appendPasswordField( "password", "Password", "The password for this token" );
100 		
101 		form.appendCheckBox( "addNonce", "Add Nonce", "Adds a nonce" );
102 		form.appendCheckBox( "addCreated", "Add Created", "Adds a created" );
103 		
104 		form.appendComboBox( "passwordType", "Password Type", new String[] 
105 		       {PASSWORD_TEXT, PASSWORD_DIGEST, PASSWORD_DIGEST_EXT}, "The password type to generate" );
106 		
107 		return form.getPanel();
108 	}
109 
110 	@Override
111 	protected void load( XmlObjectConfigurationReader reader )
112 	{
113 		addCreated = reader.readBoolean( "addCreated", true );
114 		addNonce = reader.readBoolean( "addNonce", true );
115 		passwordType = reader.readString( "passwordType", WSConstants.PASSWORD_DIGEST );
116 	}
117 
118 	@Override
119 	protected void save( XmlObjectConfigurationBuilder builder )
120 	{
121 		builder.add( "addCreated", addCreated );
122 		builder.add( "addNonce", addNonce );
123 		builder.add( "passwordType", passwordType );
124 	}
125 
126 	public boolean isAddCreated()
127 	{
128 		return addCreated;
129 	}
130 
131 	public void setAddCreated( boolean addCreated )
132 	{
133 		this.addCreated = addCreated;
134 		saveConfig();
135 	}
136 
137 	public boolean isAddNonce()
138 	{
139 		return addNonce;
140 	}
141 
142 	public void setAddNonce( boolean addNonce )
143 	{
144 		this.addNonce = addNonce;
145 		saveConfig();
146 	}
147 
148 	public String getPasswordType()
149 	{
150 		return passwordType;
151 	}
152 
153 	public void setPasswordType( String passwordType )
154 	{
155 		this.passwordType = passwordType;
156 		saveConfig();
157 	}
158 }