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