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