View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/ebxml/jaas/DialogAuthenticationCallbackHandler.java,v 1.4 2003/09/11 02:36:14 farrukh_najmi Exp $ 3 * 4 * ==================================================================== 5 * 6 * This code is subject to the freebxml License, Version 1.1 7 * 8 * Copyright (c) 2003 freebxml.org. All rights reserved. 9 * 10 * ==================================================================== 11 */ 12 package com.sun.xml.registry.ebxml.jaas; 13 14 import com.sun.xml.registry.ebxml.util.ProviderProperties; 15 16 import java.awt.Dimension; 17 import java.awt.Frame; 18 import java.awt.event.ActionEvent; 19 import java.awt.event.ActionListener; 20 import java.util.ResourceBundle; 21 22 import javax.security.auth.callback.Callback; 23 import javax.security.auth.callback.CallbackHandler; 24 import javax.security.auth.callback.ConfirmationCallback; 25 import javax.security.auth.callback.NameCallback; 26 import javax.security.auth.callback.PasswordCallback; 27 import javax.security.auth.callback.TextOutputCallback; 28 import javax.security.auth.callback.UnsupportedCallbackException; 29 import javax.swing.JButton; 30 import javax.swing.JDialog; 31 import javax.swing.JLabel; 32 import javax.swing.JPanel; 33 import javax.swing.JPasswordField; 34 import javax.swing.JTextField; 35 import javax.xml.registry.JAXRException; 36 37 import org.apache.commons.logging.Log; 38 39 /*** 40 * <p>Title: </p> 41 * <p>Description: </p> 42 * <p>Copyright: Copyright (c) 2002</p> 43 * <p>Company: </p> 44 * @author Raghu V 45 * @version 1.0 46 * 47 * This class is a CallbackHandler implementation that is suitable for logging into a keystore. 48 * The handle(Callback[]) method has been customized such that it can only handle the login method 49 * of LoginContext. 50 * Although the class has been designed such that it can interactively retrieve the keystore password 51 * from the user, these lines have been commented. The current implementation is such that this 52 * password is read from the property file(s). 53 */ 54 55 public class DialogAuthenticationCallbackHandler implements CallbackHandler { 56 57 private final Log log; 58 59 </* The ResourceBundle for Sun's Auth package/ *//package-summary/html">color="#329900"> The ResourceBundle for Sun's Auth package/ *//package-summary.html">font color="#329900">/* The ResourceBundle for Sun's Auth package/ *//package-summary.html">color="#329900"> The ResourceBundle for Sun's Auth package. */ 60 private static final ResourceBundle authResBundle = 61 ResourceBundle.getBundle("sun.security.util.AuthResources"); 62 63 /* The size of the Dialog to be displayed. This may have to be increased 64 later on when we decide to get the keystore password interactively rather 65 than from the properties file */ 66 private static final Dimension DIALOG_SIZE = new Dimension(400, 150) ; 67 private static final Dimension PANEL_SIZE = new Dimension(375, 25) ; 68 69 private static final int OK = 1; 70 private static final int CANCEL =0 ; 71 72 /* The dialog box shown to the user prompting for security details */ 73 private JDialog confirmationDialog ; 74 75 /* The frame to which this Dialog is bound to */ 76 private Frame ownerFrame ; 77 78 /* Panel and components to prompt the user to enter keystore alias */ 79 private JPanel pnlAlias = new JPanel(null) ; 80 private JLabel lblAlias = new JLabel("Keystore alias:") ; 81 private JTextField txtAlias = new JTextField() ; 82 83 /* Panel and components to prompt the user for keystore password */ 84 private JPanel pnlStorepass = new JPanel(null) ; 85 private JLabel lblStorepass = new JLabel("Keystore password:") ; 86 private JPasswordField txtStorepass = new JPasswordField(8) ; 87 88 /*Panel and components to prompt for private key password */ 89 private JPanel pnlKeypass = new JPanel(null) ; 90 private JLabel lblKeypass = new JLabel("Private key password(optional):") ; 91 private JPasswordField txtKeypass = new JPasswordField(8) ; 92 93 /*Panel and components for the buttons */ 94 private JPanel pnlButtons = new JPanel(null) ; 95 private JButton btnOk = new JButton("OK") ; 96 private JButton btnCancel = new JButton("Cancel") ; 97 98 /* boolean flag that indicates whether or not the OK button was pressed */ 99 private boolean bOkPressed = false ; 100 101 /*** 102 * One arg constructor that accepts a <code>java.awt.Frame</code> 103 * object. The login dialog shown to the user will be modal to this 104 * <code>java.awt.Frame</code> object. 105 * @param frame Instance of <code>java.awt.Frame</code> object to 106 * which the login dialog is modal 107 * @param log Logger to be used 108 */ 109 public DialogAuthenticationCallbackHandler(Frame frame, Log l) { 110 ownerFrame = frame ; 111 this.log = l; 112 initComponents() ; 113 } 114 115 private void initComponents() { 116 int curX = 5 ; 117 int curY = 15 ; 118 int lblWidth = 200 ; 119 int txtWidth = 165 ; 120 int buttonWidth = 75 ; 121 int compHeight = 25 ; 122 int padding = 5 ; 123 124 // Initialize the confirmation dialog 125 confirmationDialog = new JDialog(ownerFrame, "Keystore login", true) ; 126 confirmationDialog.getContentPane().setLayout(null); 127 confirmationDialog.setLocation(20, 20); 128 confirmationDialog.setSize(DIALOG_SIZE); 129 130 //Initialize the components required to get the alias from the user 131 pnlAlias.setSize(PANEL_SIZE); 132 pnlAlias.setLocation(curX, curY); 133 lblAlias.setBounds(0, 0, lblWidth, compHeight); 134 txtAlias.setBounds(lblWidth + padding, 0, txtWidth, compHeight) ; 135 pnlAlias.add(lblAlias); 136 pnlAlias.add(txtAlias) ; 137 confirmationDialog.getContentPane().add(pnlAlias) ; 138 139 /* This Entire block needs to be commented for now. THe current plan is to 140 hide the Keystore password implementation from the user and read it directly from 141 the properties file 142 143 144 //Initialize the components required to get the keystore password from the user 145 curY = curY + PANEL_SIZE.height + padding ; 146 txtStorepass.setEchoChar('*'); 147 pnlStorepass.setSize(PANEL_SIZE) ; 148 pnlStorepass.setLocation(curX, curY) ; 149 lblStorepass.setBounds(0,0, lblWidth, compHeight); 150 txtStorepass.setBounds(lblWidth + padding, 0, txtWidth, compHeight) ; 151 pnlStorepass.add(lblStorepass) ; 152 pnlStorepass.add(txtStorepass) ; 153 confirmationDialog.getContentPane().add(pnlStorepass) ; 154 */ 155 //Initialize the components required to get the key password from the user 156 curY = curY + PANEL_SIZE.height + padding * 2 ; 157 txtKeypass.setEchoChar('*') ; 158 pnlKeypass.setSize(PANEL_SIZE); 159 pnlKeypass.setLocation(curX, curY) ; 160 lblKeypass.setBounds(0,0, lblWidth, compHeight) ; 161 txtKeypass.setBounds(lblWidth + padding, 0, txtWidth, compHeight) ; 162 pnlKeypass.add(lblKeypass) ; 163 pnlKeypass.add(txtKeypass) ; 164 confirmationDialog.getContentPane().add(pnlKeypass) ; 165 166 //Initialize the components required to get the bottom buttons. 167 btnOk.addActionListener( new ActionListener() { 168 public void actionPerformed(ActionEvent e) { 169 bOkPressed = true ; 170 confirmationDialog.setVisible(false) ; 171 } 172 }); 173 btnCancel.addActionListener( new ActionListener() { 174 public void actionPerformed(ActionEvent e) { 175 bOkPressed = false ; 176 confirmationDialog.setVisible(false) ; 177 } 178 }); 179 curY = curY + PANEL_SIZE.height + padding * 2 ; 180 pnlButtons.setSize(PANEL_SIZE) ; 181 pnlButtons.setLocation(curX, curY) ; 182 int buttonsBoundingWidth = buttonWidth * 2 + padding * 2 ; 183 curX = (PANEL_SIZE.width - buttonsBoundingWidth)/2 > 0 ? (PANEL_SIZE.width - buttonsBoundingWidth)/2 : curX ; 184 btnOk.setBounds(curX, 0, buttonWidth, compHeight) ; 185 curX = curX + buttonWidth + padding * 2 ; 186 btnCancel.setBounds(curX, 0, buttonWidth, compHeight) ; 187 curX = pnlButtons.getLocation().x ; 188 pnlButtons.add(btnOk) ; 189 pnlButtons.add(btnCancel) ; 190 confirmationDialog.getContentPane().add(pnlButtons) ; 191 confirmationDialog.getRootPane().setDefaultButton( btnOk ); 192 } // end of of initComponents 193 194 195 private int showDialog() { 196 int returnValue = CANCEL ; 197 confirmationDialog.setVisible(true); 198 if(bOkPressed) { 199 returnValue = OK ; 200 bOkPressed = false ; 201 } else { 202 returnValue = CANCEL ; 203 } 204 return returnValue ; 205 } 206 207 /*** Implementation of the handle method specified by 208 * <code> javax.security.auth.callback.CallbackHandler </code> 209 * @param callbacks <code>Array of javax.security.auth.callback.CallbackHandler</code> 210 * 211 */ 212 public void handle(Callback[] callbacks) throws UnsupportedCallbackException { 213 214 int result = showDialog() ; 215 for (int i = 0; i < callbacks.length; i++) { 216 217 if (callbacks[i] instanceof TextOutputCallback) { 218 // Ignore this section for now. This will be used when a generic callback handler 219 // is being implemented. In our current implementation, we are only expecting the 220 //login type callback handler. 221 } 222 else if (callbacks[i] instanceof NameCallback) { 223 // prompt the user for a username 224 NameCallback nc = (NameCallback)callbacks[i]; 225 String strPrompt = nc.getPrompt() ; 226 String strName = "" ; 227 if (strPrompt.equals(authResBundle.getString("Keystore alias: "))) { 228 strName = txtAlias.getText() ; 229 } 230 nc.setName(strName); 231 } 232 else if (callbacks[i] instanceof PasswordCallback) { 233 // prompt the user for sensitive information 234 PasswordCallback pc = (PasswordCallback)callbacks[i]; 235 String strPrompt = pc.getPrompt() ; 236 char[] chrPass = new char[0] ; 237 if (strPrompt.equals(authResBundle.getString("Keystore password: "))) { 238 /* As of now hide the Keystore password part from the user and 239 read directly from the properties file 240 chrPass = txtStorepass.getPassword() ; */ 241 chrPass = ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.storepass").toCharArray(); 242 if ((chrPass == null) || (chrPass.length == 0)) 243 log.error("Property jaxr-ebxml.security.storepass is undefined"); 244 } 245 else if (strPrompt.equals(authResBundle.getString("Private key password (optional): "))) { 246 chrPass = txtKeypass.getPassword() ; 247 } 248 pc.setPassword(chrPass); 249 } 250 else if (callbacks[i] instanceof ConfirmationCallback) { 251 ConfirmationCallback cc = (ConfirmationCallback)callbacks[i]; 252 if (result == OK ) { 253 cc.setSelectedIndex(ConfirmationCallback.OK); 254 } 255 else { 256 cc.setSelectedIndex(ConfirmationCallback.CANCEL); 257 } 258 } 259 else { 260 throw new UnsupportedCallbackException 261 (callbacks[i], "Unrecognized Callback"); 262 } 263 } 264 } 265 266 }

This page was automatically generated by Maven