1 20 21 package org.apache.directory.ldapstudio.valueeditors.password; 22 23 24 import java.util.Arrays ; 25 26 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter; 27 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 28 import org.apache.directory.ldapstudio.browser.core.jobs.CheckBindJob; 29 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 30 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 31 import org.apache.directory.ldapstudio.browser.core.model.Password; 32 import org.apache.directory.ldapstudio.browser.core.utils.Utils; 33 import org.apache.directory.ldapstudio.valueeditors.ValueEditorsActivator; 34 import org.apache.directory.ldapstudio.valueeditors.ValueEditorsConstants; 35 import org.eclipse.jface.dialogs.Dialog; 36 import org.eclipse.jface.dialogs.IDialogConstants; 37 import org.eclipse.jface.dialogs.MessageDialog; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.events.ModifyEvent; 40 import org.eclipse.swt.events.ModifyListener; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.events.SelectionEvent; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Combo; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Control; 49 import org.eclipse.swt.widgets.Display; 50 import org.eclipse.swt.widgets.Label; 51 import org.eclipse.swt.widgets.Shell; 52 import org.eclipse.swt.widgets.TabFolder; 53 import org.eclipse.swt.widgets.TabItem; 54 import org.eclipse.swt.widgets.Text; 55 56 57 public class PasswordDialog extends Dialog 58 { 59 60 public static final String DIALOG_TITLE = "Password Editor"; 61 62 public static final String [] HASH_METHODS = 63 { Password.HASH_METHOD_SHA, Password.HASH_METHOD_SSHA, Password.HASH_METHOD_MD5, Password.HASH_METHOD_SMD5, 64 Password.HASH_METHOD_CRYPT, Password.HASH_METHOD_NO }; 65 66 public static final int CURRENT_TAB = 0; 67 68 public static final int NEW_TAB = 1; 69 70 public static final String SELECTED_TAB_DIALOGSETTINGS_KEY = PasswordDialog.class.getName() + ".tab"; 71 72 public static final String SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY = PasswordDialog.class.getName() + ".hashMethod"; 73 74 private TabFolder tabFolder; 75 76 private TabItem currentTab; 77 78 private TabItem newTab; 79 80 private IEntry entry; 81 82 private Password currentPassword; 83 84 private Composite currentPasswordContainer; 85 86 private Text currentPasswordText; 87 88 private Text currentPasswordHashMethodText; 89 90 private Text currentPasswordValueHexText; 91 92 private Text currentPasswordSaltHexText; 93 94 private Text testPasswordText; 95 96 private Button verifyPasswordButton; 97 98 private Button bindPasswordButton; 99 100 private Password newPassword; 101 102 private Composite newPasswordContainer; 103 104 private Text newPasswordText; 105 106 private Combo newPasswordHashMethodCombo; 107 108 private Text newPasswordPreviewText; 109 110 private Text newPasswordPreviewValueHexText; 111 112 private Text newPasswordPreviewSaltHexText; 113 114 private Button newSaltButton; 115 116 private byte[] returnPassword; 117 118 private Button okButton; 119 120 121 public PasswordDialog( Shell parentShell, byte[] currentPassword, IEntry entry ) 122 { 123 super( parentShell ); 124 super.setShellStyle( super.getShellStyle() | SWT.RESIZE ); 125 126 try 127 { 128 this.currentPassword = currentPassword != null ? new Password( currentPassword ) : null; 129 } 130 catch ( IllegalArgumentException e ) 131 { 132 } 133 this.entry = entry; 134 135 this.returnPassword = null; 136 } 137 138 139 protected void configureShell( Shell shell ) 140 { 141 super.configureShell( shell ); 142 shell.setText( DIALOG_TITLE ); 143 shell.setImage( ValueEditorsActivator.getDefault().getImage( ValueEditorsConstants.IMG_PASSWORDEDITOR ) ); 144 } 145 146 147 protected void okPressed() 148 { 149 if ( newPassword != null ) 151 { 152 this.returnPassword = this.newPassword.toBytes(); 153 } 154 else 155 { 156 this.returnPassword = null; 157 } 158 159 ValueEditorsActivator.getDefault().getDialogSettings().put( SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY, 162 this.newPasswordHashMethodCombo.getText() ); 163 164 super.okPressed(); 165 } 166 167 168 public boolean close() 169 { 170 ValueEditorsActivator.getDefault().getDialogSettings().put( SELECTED_TAB_DIALOGSETTINGS_KEY, 172 this.tabFolder.getSelectionIndex() ); 173 174 return super.close(); 175 } 176 177 178 protected void createButtonsForButtonBar( Composite parent ) 179 { 180 okButton = createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false ); 181 createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false ); 182 183 try 185 { 186 int tabIndex = ValueEditorsActivator.getDefault().getDialogSettings().getInt( SELECTED_TAB_DIALOGSETTINGS_KEY ); 187 if ( this.currentPassword == null || this.currentPassword.toBytes().length == 0 ) 188 { 189 tabIndex = NEW_TAB; 190 } 191 this.tabFolder.setSelection( tabIndex ); 192 } 193 catch ( Exception e ) 194 { 195 } 196 try 197 { 198 String hashMethod = ValueEditorsActivator.getDefault().getDialogSettings() 199 .get( SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY ); 200 if ( Arrays.asList( HASH_METHODS ).contains( hashMethod ) ) 201 { 202 this.newPasswordHashMethodCombo.setText( hashMethod ); 203 } 204 } 205 catch ( Exception e ) 206 { 207 } 208 209 updateTabFolder(); 211 } 212 213 214 protected Control createDialogArea( Composite parent ) 215 { 216 217 Composite composite = ( Composite ) super.createDialogArea( parent ); 218 GridData gd = new GridData( GridData.FILL_BOTH ); 219 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ) * 3 / 2; 220 gd.heightHint = convertVerticalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ) / 2; 221 composite.setLayoutData( gd ); 222 223 this.tabFolder = new TabFolder( composite, SWT.TOP ); 224 GridLayout mainLayout = new GridLayout(); 225 mainLayout.marginWidth = 0; 226 mainLayout.marginHeight = 0; 227 this.tabFolder.setLayout( mainLayout ); 228 this.tabFolder.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 229 this.tabFolder.addSelectionListener( new SelectionAdapter() 230 { 231 public void widgetSelected( SelectionEvent e ) 232 { 233 updateTabFolder(); 234 } 235 } ); 236 237 if ( this.currentPassword != null && this.currentPassword.toBytes().length > 0 ) 239 { 240 currentPasswordContainer = new Composite( this.tabFolder, SWT.NONE ); 241 GridLayout currentLayout = new GridLayout( 2, false ); 242 currentLayout.marginHeight = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_MARGIN ); 243 currentLayout.marginWidth = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_MARGIN ); 244 currentLayout.verticalSpacing = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_SPACING ); 245 currentLayout.horizontalSpacing = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_SPACING ); 246 currentPasswordContainer.setLayout( currentLayout ); 247 248 BaseWidgetUtils.createLabel( currentPasswordContainer, "Current Password:", 1 ); 249 currentPasswordText = BaseWidgetUtils.createReadonlyText( currentPasswordContainer, "", 1 ); 250 251 new Label( currentPasswordContainer, SWT.NONE ); 252 Composite currentPasswordDetailContainer = BaseWidgetUtils.createColumnContainer( currentPasswordContainer, 253 2, 1 ); 254 BaseWidgetUtils.createLabel( currentPasswordDetailContainer, "Hash Method:", 1 ); 255 currentPasswordHashMethodText = BaseWidgetUtils.createLabeledText( currentPasswordDetailContainer, "", 1 ); 256 BaseWidgetUtils.createLabel( currentPasswordDetailContainer, "Password (Hex):", 1 ); 257 currentPasswordValueHexText = BaseWidgetUtils.createLabeledText( currentPasswordDetailContainer, "", 1 ); 258 BaseWidgetUtils.createLabel( currentPasswordDetailContainer, "Salt (Hex):", 1 ); 259 currentPasswordSaltHexText = BaseWidgetUtils.createLabeledText( currentPasswordDetailContainer, "", 1 ); 260 261 BaseWidgetUtils.createLabel( currentPasswordContainer, "Verify Password:", 1 ); 262 testPasswordText = BaseWidgetUtils.createPasswordText( currentPasswordContainer, "", 1 ); 263 testPasswordText.addModifyListener( new ModifyListener() 264 { 265 public void modifyText( ModifyEvent e ) 266 { 267 updateCurrentPasswordGroup(); 268 } 269 } ); 270 271 new Label( currentPasswordContainer, SWT.NONE ); 272 Composite verifyPasswordButtonContainer = BaseWidgetUtils.createColumnContainer( currentPasswordContainer, 273 2, 1 ); 274 verifyPasswordButton = BaseWidgetUtils.createButton( verifyPasswordButtonContainer, "Verify", 1 ); 275 verifyPasswordButton.setEnabled( false ); 276 verifyPasswordButton.addSelectionListener( new SelectionAdapter() 277 { 278 public void widgetSelected( SelectionEvent event ) 279 { 280 verifyCurrentPassword(); 281 } 282 } ); 283 bindPasswordButton = BaseWidgetUtils.createButton( verifyPasswordButtonContainer, "Bind", 1 ); 284 bindPasswordButton.setEnabled( false ); 285 bindPasswordButton.addSelectionListener( new SelectionAdapter() 286 { 287 public void widgetSelected( SelectionEvent event ) 288 { 289 bindCurrentPassword(); 290 } 291 } ); 292 293 this.currentTab = new TabItem( this.tabFolder, SWT.NONE ); 294 this.currentTab.setText( "Current Password" ); 295 this.currentTab.setControl( currentPasswordContainer ); 296 } 297 298 newPasswordContainer = new Composite( this.tabFolder, SWT.NONE ); 300 GridLayout newLayout = new GridLayout( 2, false ); 301 newLayout.marginHeight = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_MARGIN ); 302 newLayout.marginWidth = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_MARGIN ); 303 newLayout.verticalSpacing = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_SPACING ); 304 newLayout.horizontalSpacing = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_SPACING ); 305 newPasswordContainer.setLayout( newLayout ); 306 307 BaseWidgetUtils.createLabel( newPasswordContainer, "Enter New Password:", 1 ); 308 newPasswordText = BaseWidgetUtils.createPasswordText( newPasswordContainer, "", 1 ); 309 newPasswordText.addModifyListener( new ModifyListener() 310 { 311 public void modifyText( ModifyEvent e ) 312 { 313 updateNewPasswordGroup(); 314 } 315 } ); 316 317 BaseWidgetUtils.createLabel( newPasswordContainer, "Select Hash Method:", 1 ); 318 newPasswordHashMethodCombo = BaseWidgetUtils.createReadonlyCombo( newPasswordContainer, HASH_METHODS, 0, 1 ); 319 newPasswordHashMethodCombo.addSelectionListener( new SelectionAdapter() 320 { 321 public void widgetSelected( SelectionEvent event ) 322 { 323 updateNewPasswordGroup(); 324 } 325 } ); 326 327 BaseWidgetUtils.createLabel( newPasswordContainer, "Password Preview:", 1 ); 328 newPasswordPreviewText = BaseWidgetUtils.createReadonlyText( newPasswordContainer, "", 1 ); 329 330 newSaltButton = BaseWidgetUtils.createButton( newPasswordContainer, "New Salt", 1 ); 331 newSaltButton.setEnabled( false ); 332 newSaltButton.addSelectionListener( new SelectionAdapter() 333 { 334 public void widgetSelected( SelectionEvent event ) 335 { 336 updateNewPasswordGroup(); 337 } 338 } ); 339 Composite newPasswordPreviewDetailContainer = BaseWidgetUtils 340 .createColumnContainer( newPasswordContainer, 2, 1 ); 341 BaseWidgetUtils.createLabel( newPasswordPreviewDetailContainer, "Password (Hex):", 1 ); 342 newPasswordPreviewValueHexText = BaseWidgetUtils.createLabeledText( newPasswordPreviewDetailContainer, ":", 1 ); 343 BaseWidgetUtils.createLabel( newPasswordPreviewDetailContainer, "Salt (Hex):", 1 ); 344 newPasswordPreviewSaltHexText = BaseWidgetUtils.createLabeledText( newPasswordPreviewDetailContainer, "", 1 ); 345 346 this.newTab = new TabItem( this.tabFolder, SWT.NONE ); 347 this.newTab.setText( "New Password" ); 348 this.newTab.setControl( newPasswordContainer ); 349 350 applyDialogFont( composite ); 351 return composite; 352 } 353 354 355 private void updateCurrentPasswordGroup() 356 { 357 if ( this.currentPassword != null ) 358 { 359 this.currentPasswordHashMethodText.setText( Utils.getNonNullString( this.currentPassword.getHashMethod() ) ); 360 this.currentPasswordValueHexText.setText( Utils.getNonNullString( this.currentPassword 361 .getHashedPasswordAsHexString() ) ); 362 this.currentPasswordSaltHexText 363 .setText( Utils.getNonNullString( this.currentPassword.getSaltAsHexString() ) ); 364 this.currentPasswordText.setText( this.currentPassword.toString() ); 365 } 366 367 this.testPasswordText.setEnabled( this.currentPassword != null 368 && this.currentPassword.getHashedPassword() != null && this.currentPassword.toBytes().length > 0 ); 369 this.verifyPasswordButton.setEnabled( this.testPasswordText.isEnabled() 370 && !"".equals( this.testPasswordText.getText() ) ); 371 this.bindPasswordButton.setEnabled( this.testPasswordText.isEnabled() 372 && !"".equals( this.testPasswordText.getText() ) && this.entry != null ); 373 374 if ( this.verifyPasswordButton.isEnabled() ) 375 getShell().setDefaultButton( this.verifyPasswordButton ); 376 else 377 getShell().setDefaultButton( this.okButton ); 378 } 380 381 382 private void verifyCurrentPassword() 383 { 384 String testPassword = this.testPasswordText.getText(); 385 if ( this.currentPassword != null ) 386 { 387 if ( this.currentPassword.verify( testPassword ) ) 388 { 389 MessageDialog dialog = new MessageDialog( getShell(), "Password Verification", getShell().getImage(), 390 "Password verified sucessfully", MessageDialog.INFORMATION, new String [] 391 { IDialogConstants.OK_LABEL }, 0 ); 392 dialog.open(); 393 } 394 else 395 { 396 MessageDialog dialog = new MessageDialog( getShell(), "Password Verification", getShell().getImage(), 397 "Password verification failed", MessageDialog.ERROR, new String [] 398 { IDialogConstants.OK_LABEL }, 0 ); 399 dialog.open(); 400 } 401 } 402 } 403 404 405 private void bindCurrentPassword() 406 { 407 408 if ( !"".equals( this.testPasswordText.getText() ) && this.entry != null ) 409 { 410 411 IConnection connection = ( IConnection ) this.entry.getConnection().clone();; 412 connection.setName( null ); 413 connection.setBindPrincipal( this.entry.getDn().toString() ); 414 connection.setBindPassword( this.testPasswordText.getText() ); 415 connection.setAuthMethod( IConnection.AUTH_SIMPLE ); 416 417 CheckBindJob job = new CheckBindJob( connection ); 418 RunnableContextJobAdapter.execute( job ); 419 if ( job.getExternalResult().isOK() ) 420 { 421 MessageDialog.openInformation( Display.getDefault().getActiveShell(), "Check Authentication", 422 "The authentication was successful." ); 423 } 424 425 } 426 } 427 428 429 private void updateNewPasswordGroup() 430 { 431 this.newPassword = new Password( this.newPasswordHashMethodCombo.getText(), this.newPasswordText.getText() ); 432 if ( !"".equals( this.newPasswordText.getText() ) || this.newPassword.getHashMethod() == null ) 433 { 434 newPasswordPreviewValueHexText.setText( Utils.getNonNullString( this.newPassword 435 .getHashedPasswordAsHexString() ) ); 436 newPasswordPreviewSaltHexText.setText( Utils.getNonNullString( this.newPassword.getSaltAsHexString() ) ); 437 newPasswordPreviewText.setText( this.newPassword.toString() ); 438 newSaltButton.setEnabled( this.newPassword.getSalt() != null ); 439 this.okButton.setEnabled( true ); 440 getShell().setDefaultButton( this.okButton ); 441 } 442 else 443 { 444 this.newPassword = null; 445 newPasswordPreviewValueHexText.setText( Utils.getNonNullString( null ) ); 446 newPasswordPreviewSaltHexText.setText( Utils.getNonNullString( null ) ); 447 newPasswordPreviewText.setText( Utils.getNonNullString( null ) ); 448 newSaltButton.setEnabled( false ); 449 this.okButton.setEnabled( false ); 450 } 451 } 452 453 454 private void updateTabFolder() 455 { 456 if ( testPasswordText != null && newPasswordText != null ) 457 { 458 if ( tabFolder.getSelectionIndex() == CURRENT_TAB ) 459 { 460 testPasswordText.setFocus(); 461 } 462 else if ( tabFolder.getSelectionIndex() == NEW_TAB ) 463 { 464 newPasswordText.setFocus(); 465 } 466 updateCurrentPasswordGroup(); 467 updateNewPasswordGroup(); 468 } 469 } 470 471 472 478 public byte[] getNewPassword() 479 { 480 return this.returnPassword; 481 } 482 483 } 484 | Popular Tags |