This project has retired. For details please refer to its Attic page.
UddiDigitalSignatureBusiness xref
View Javadoc
1   /*
2    * Copyright 2001-2013 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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   */
17  package org.apache.juddi.v3.client.cli;
18  
19  import java.util.concurrent.atomic.AtomicReference;
20  
21  import org.apache.juddi.v3.client.config.UDDIClient;
22  import org.apache.juddi.v3.client.cryptor.DigSigUtil;
23  import org.apache.juddi.v3.client.transport.Transport;
24  import org.uddi.api_v3.*;
25  import org.uddi.v3_service.UDDIInquiryPortType;
26  import org.uddi.v3_service.UDDIPublicationPortType;
27  import org.uddi.v3_service.UDDISecurityPortType;
28  
29  /**
30   * This class shows you how to digital sign a business
31   *
32   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
33   */
34  public class UddiDigitalSignatureBusiness {
35  
36          private UDDISecurityPortType security = null;
37          private UDDIInquiryPortType inquiry = null;
38          private UDDIPublicationPortType publish = null;
39          private UDDIClient clerkManager = null;
40  
41          /**
42           * This sets up the ws proxies using uddi.xml in META-INF
43           */
44          public UddiDigitalSignatureBusiness() {
45                  try {
46                          // create a manager and read the config in the archive; 
47                          // you can use your config file name
48                          clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
49                          Transport transport = clerkManager.getTransport();
50                          // Now you create a reference to the UDDI API
51                          security = transport.getUDDISecurityService();
52                          inquiry = transport.getUDDIInquiryService();
53                          publish = transport.getUDDIPublishService();
54                  } catch (Exception e) {
55                          e.printStackTrace();
56                  }
57          }
58          
59          public UddiDigitalSignatureBusiness(Transport transport) {
60                  try {
61                         
62                          // Now you create a reference to the UDDI API
63                          security = transport.getUDDISecurityService();
64                          inquiry = transport.getUDDIInquiryService();
65                          publish = transport.getUDDIPublishService();
66                  } catch (Exception e) {
67                          e.printStackTrace();
68                  }
69          }
70  
71          /**
72           * Main entry point
73           *
74           * @param args
75           */
76          public static void main(String args[]) {
77  
78                  UddiDigitalSignatureBusiness sp = new UddiDigitalSignatureBusiness();
79                  sp.fire(null, null);
80          }
81  
82          public void fire(String token, String key) {
83                  try {
84  
85                          DigSigUtil ds = null;
86  
87                          //option 1), set everything manually
88                          ds = new DigSigUtil();
89                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE, "keystore.jks");
90                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILETYPE, "JKS");
91                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE_PASSWORD, "Test");
92                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_KEY_ALIAS, "Test");
93                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_BASE64, "true");
94  
95                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SERIAL, "true");
96                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SUBJECTDN, "true");
97                          ds.put(DigSigUtil.TRUSTSTORE_FILE, "truststore.jks");
98                          ds.put(DigSigUtil.TRUSTSTORE_FILETYPE, "JKS");
99                          ds.put(DigSigUtil.TRUSTSTORE_FILE_PASSWORD, "Test");
100 
101                         //option 2), load it from the juddi config file
102                         //ds = new DigSigUtil(clerkManager.getClientConfig().getDigitalSignatureConfiguration());
103                         //login
104                         if (token == null) //option, load from juddi config
105                         {
106                                 token = getAuthKey(clerkManager.getClerk("default").getPublisher(),
107                                         clerkManager.getClerk("default").getPassword());
108                         }
109 
110                         if (key == null) {
111                                 //make a new business
112                                 SaveBusiness sb = new SaveBusiness();
113                                 sb.setAuthInfo(token);
114                                 BusinessEntity ob = new BusinessEntity();
115                                 Name name = new Name();
116                                 name.setValue("My Signed Business");
117                                 ob.getName().add(name);
118                                 sb.getBusinessEntity().add(ob);
119                                 //save it
120                                 BusinessDetail saveBusiness = publish.saveBusiness(sb);
121 
122                                 System.out.println("business created with key " + saveBusiness.getBusinessEntity().get(0).getBusinessKey());
123 
124                                 BusinessEntity be = saveBusiness.getBusinessEntity().get(0);
125                                 key = be.getBusinessKey();
126                         }
127                         BusinessEntity be = clerkManager.getClerk("default").getBusinessDetail(key);
128                         //sign the copy returned from the UDDI node (it may have made changes)
129                         DigSigUtil.JAXB_ToStdOut(be);
130                         if (!be.getSignature().isEmpty())
131                         {
132                                 System.out.println("WARN, the entity with the key " + key + " is already signed! aborting");
133                                 return;
134                         }
135 
136                         //if it's already signed, remove all existing signatures
137                         
138                         System.out.println("signing");
139                         BusinessEntity signUDDI_JAXBObject = ds.signUddiEntity(be);
140                         DigSigUtil.JAXB_ToStdOut(signUDDI_JAXBObject);
141                         System.out.println("signed, saving");
142 
143                         SaveBusiness sb = new SaveBusiness();
144                         sb.setAuthInfo(token);
145                         sb.getBusinessEntity().add(signUDDI_JAXBObject);
146                         publish.saveBusiness(sb);
147                         System.out.println("saved, fetching");
148 
149                         //validate it again from the server, confirming that it was transformed correctly
150                         GetBusinessDetail gb = new GetBusinessDetail();
151                         gb.setAuthInfo(token);
152                         gb.getBusinessKey().add(be.getBusinessKey());
153                         be = inquiry.getBusinessDetail(gb).getBusinessEntity().get(0);
154                         DigSigUtil.JAXB_ToStdOut(be);
155                         System.out.println("verifing");
156                         AtomicReference<String> msg = new AtomicReference<String>();
157                         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(be, msg);
158                         if (verifySigned_UDDI_JAXB_Object) {
159                                 System.out.println("signature validation passed (expected)");
160                         } else {
161                                 System.out.println("signature validation failed (not expected)");
162                         }
163                         System.out.println(msg.get());
164 
165                 } catch (Exception e) {
166                         e.printStackTrace();
167                 }
168         }
169 
170         /**
171          * Gets a UDDI style auth token, otherwise, appends credentials to the
172          * ws proxies (not yet implemented)
173          *
174          * @param username
175          * @param password
176          * @param style
177          * @return
178          */
179         private String getAuthKey(String username, String password) {
180                 try {
181 
182                         GetAuthToken getAuthTokenRoot = new GetAuthToken();
183                         getAuthTokenRoot.setUserID(username);
184                         getAuthTokenRoot.setCred(password);
185 
186                         // Making API call that retrieves the authentication token for the 'root' user.
187                         AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
188                         System.out.println("root AUTHTOKEN = " + "don't log auth tokens!");
189                         return rootAuthToken.getAuthInfo();
190                 } catch (Exception ex) {
191                         System.out.println("Could not authenticate with the provided credentials " + ex.getMessage());
192                 }
193                 return null;
194         }
195 }