View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.xml.registry.client.browser.registration; 12 13 import com.sun.xml.registry.client.browser.JAXRClient; 14 import com.sun.xml.registry.client.browser.RegistryBrowser; 15 import com.sun.xml.registry.ebxml.ConnectionImpl; 16 import com.sun.xml.registry.ebxml.RegistryServiceImpl; 17 import com.sun.xml.registry.ebxml.util.KeystoreUtil; 18 19 import org.apache.commons.logging.Log; 20 21 import java.io.File; 22 23 import java.util.ArrayList; 24 import java.util.Collection; 25 26 import javax.xml.registry.BusinessLifeCycleManager; 27 import javax.xml.registry.JAXRException; 28 import javax.xml.registry.infomodel.PersonName; 29 import javax.xml.registry.infomodel.PostalAddress; 30 import javax.xml.registry.infomodel.User; 31 32 33 /*** 34 * User registration tool. 35 */ 36 public class UserManager { 37 38 /*** DOCUMENT ME! */ 39 private static final UserManager instance = new UserManager(); 40 41 /*** Create a static reference to the logging service. */ 42 private static Log log = null; 43 static { 44 try { 45 JAXRClient client = RegistryBrowser.getInstance().getClient(); 46 BusinessLifeCycleManager lcm = client.getBusinessLifeCycleManager(); 47 log = ((RegistryServiceImpl)(lcm.getRegistryService())).getConnection() 48 .getConnectionFactory() 49 .getLog(); 50 } 51 catch (JAXRException e) { 52 e.printStackTrace(); 53 } 54 } 55 56 /*** 57 * Creates a new UserManager object. 58 */ 59 private UserManager() { 60 } 61 62 /*** 63 * DOCUMENT ME! 64 * 65 * @return DOCUMENT ME! 66 */ 67 public static UserManager getInstance() { 68 69 return instance; 70 } 71 72 /* 73 * Register a new user 74 * 75 */ 76 public void registerNewUser() throws Exception { 77 78 try { 79 80 JAXRClient client = RegistryBrowser.getInstance().getClient(); 81 BusinessLifeCycleManager lcm = client.getBusinessLifeCycleManager(); 82 83 UserModel userModel = new UserModel(lcm.createUser()); 84 UserRegistrationPanel userRegPanel = 85 new UserRegistrationPanel(userModel); 86 UserRegistrationDialog dialog = 87 new UserRegistrationDialog(userRegPanel, userModel); 88 89 dialog.setVisible(true); 90 91 if (dialog.getStatus() != UserRegistrationDialog.OK_STATUS) { 92 return; 93 } 94 } 95 catch (JAXRException e) { 96 RegistryBrowser.displayError(e); 97 } 98 } 99 100 /*** 101 * First check if certificate already exists in client keystore. If it does 102 * use it. If not then create a self signed certificate for the user and use it to 103 * authenticate with the ebxmlrr server. 104 * If the authentication is sucessful, save the 105 * user model to the server. 106 * 107 * @throw Exception 108 * An exception could indicate either a communications problem or an 109 * authentication error. 110 */ 111 public static void authenticateAndSaveUser(UserModel userModel) 112 throws Exception 113 { 114 try { 115 JAXRClient client = RegistryBrowser.getInstance().getClient(); 116 BusinessLifeCycleManager lcm = client.getBusinessLifeCycleManager(); 117 RegistryServiceImpl rs = (RegistryServiceImpl)lcm.getRegistryService(); 118 ConnectionImpl connection = (ConnectionImpl)rs.getConnection(); 119 120 if (!certificateExists(userModel.getAlias(), 121 userModel.getStorePassword())) { 122 123 UserManager.generateSelfSignedCertificate(userModel.getUser(), 124 userModel.getAlias(), 125 userModel.getKeyPassword(), 126 userModel.getStorePassword()); 127 } 128 129 // Force re-authentication in case credentials are already set 130 connection.authenticate(); 131 132 RegistryBrowser.setWaitCursor(); 133 134 // Now save the User 135 ArrayList objects = new ArrayList(); 136 objects.add(userModel.getUser()); 137 client.saveObjects(objects); 138 139 // saveObjects uses XML-Security which overwrites the log4j 140 // configuration and we never get to see this: 141 log.info("Saved user \"" 142 + userModel.getUser().getPersonName().getFullName() 143 + "\" on server."); 144 } 145 catch (Exception e) { 146 147 // Remove the self-signed certificate from the keystore, if one 148 // was created during the self-registration process 149 try { 150 if (userModel != null) { 151 String alias = userModel.getAlias(); 152 if (alias != null) { 153 UserManager.removeCertificate(alias, userModel.getStorePassword()); 154 } 155 } 156 } 157 catch (Exception removeCertException) { 158 log.warn("Failed to remove the certificate from the keystore " + 159 "that was generated during the self-registration process.", 160 removeCertException); 161 } 162 163 throw e; 164 } 165 finally { 166 RegistryBrowser.setDefaultCursor(); 167 } 168 } 169 170 /* 171 * Return true if certificate exists in client keystore, false if not. 172 * 173 */ 174 public static boolean certificateExists(String alias, 175 char[] storePass) 176 throws Exception 177 { 178 boolean exists = false; 179 180 try { 181 File keystoreFile = KeystoreUtil.getKeystoreFile(); 182 KeystoreUtil.createKeystoreDirectory(keystoreFile); 183 184 String[] args = 185 { 186 "-list", "-alias", alias, 187 "-keystore", keystoreFile.getAbsolutePath(), "-storepass", 188 new String(storePass) 189 }; 190 191 KeyTool keytool = new KeyTool(); 192 keytool.run(args, System.out); 193 194 exists = true; 195 196 log.info("Alias exists \"" + alias + "\" in keystore \"" 197 + keystoreFile.getAbsolutePath() + "\""); 198 } 199 catch (Exception e) { 200 //Cert does not exists. 201 } 202 return exists; 203 } 204 205 /* 206 * Generate a self signed certificate 207 * 208 */ 209 public static void generateSelfSignedCertificate(User user, 210 String alias, 211 char[] keyPass, 212 char[] storePass) 213 throws Exception 214 { 215 String dname = getDNameFromUser(user); 216 File keystoreFile = KeystoreUtil.getKeystoreFile(); 217 KeystoreUtil.createKeystoreDirectory(keystoreFile); 218 219 String[] args = 220 { 221 "-genkey", "-alias", alias, "-keypass", new String(keyPass), 222 "-keystore", keystoreFile.getAbsolutePath(), "-storepass", 223 new String(storePass), "-dname", dname 224 }; 225 226 KeyTool keytool = new KeyTool(); 227 keytool.run(args, System.out); 228 229 log.info("Stored user \"" + alias + "\" in keystore \"" 230 + keystoreFile.getAbsolutePath() + "\""); 231 } 232 233 /*** Remove an alias from the keystore. 234 * <p> 235 * Currently, this is only used to "backout" a generated key when self 236 * registration fails. 237 */ 238 public static void removeCertificate(String alias, char[] storePass) 239 throws Exception 240 { 241 File keystoreFile = KeystoreUtil.getKeystoreFile(); 242 String[] args = { 243 "-delete", "-alias", alias, "-keystore", keystoreFile.getAbsolutePath(), 244 "-storepass", new String(storePass) 245 }; 246 KeyTool keytool = new KeyTool(); 247 keytool.run(args, System.out); 248 log.info("Removed user \"" + alias + "\" from keystore \"" 249 + keystoreFile.getAbsolutePath() + "\""); 250 } 251 252 /*** 253 * DOCUMENT ME! 254 * 255 * @param user DOCUMENT ME! 256 * 257 * @return DOCUMENT ME! 258 * 259 * @throws JAXRException DOCUMENT ME! 260 */ 261 private static String getDNameFromUser(User user) 262 throws JAXRException { 263 264 String dname = "CN="; 265 266 JAXRClient client = 267 RegistryBrowser.getInstance().getClient(); 268 BusinessLifeCycleManager lcm = 269 client.getBusinessLifeCycleManager(); 270 271 Collection addresses = user.getPostalAddresses(); 272 PostalAddress address; 273 PersonName personName = user.getPersonName(); 274 275 //CN=Farrukh Najmi, OU=freebxml.org, O=ebxmlrr, L=Islamabad, ST=Punjab, C=PK 276 if (personName == null) { 277 personName = 278 lcm.createPersonName("firstName", "middleName", 279 "lastName"); 280 } 281 282 if ((addresses != null) && (addresses.size() > 0)) { 283 address = (PostalAddress)(addresses.iterator().next()); 284 } else { 285 address = 286 lcm.createPostalAddress("number", "street", "city", 287 "state", "country", 288 "postalCode", "Office"); 289 } 290 291 String city = address.getCity(); 292 293 if ((city == null) || (city.length() == 0)) { 294 city = "Unknown"; 295 } 296 297 String state = address.getStateOrProvince(); 298 299 if ((state == null) || (state.length() == 0)) { 300 state = "Unknown"; 301 } 302 303 String country = address.getCountry(); 304 305 if ((country == null) || (country.length() == 0)) { 306 country = "US"; 307 } 308 309 if (country.length() > 0) { 310 country = country.substring(0, 2); 311 } 312 313 dname += (personName.getFirstName() + " " 314 + personName.getMiddleName() + " " + personName.getLastName() 315 + ", OU=Unknown, O=Unknown, L=" + city + ", ST=" + state 316 + ", C=" + country); 317 318 return dname; 319 } 320 }

This page was automatically generated by Maven