1 12 package org.eclipse.team.internal.ccvs.ui; 13 14 import org.eclipse.core.runtime.OperationCanceledException; 15 import org.eclipse.jface.dialogs.*; 16 import org.eclipse.jface.window.Window; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.swt.widgets.Shell; 20 import org.eclipse.team.core.*; 21 import org.eclipse.team.internal.ccvs.core.*; 22 23 27 public class WorkbenchUserAuthenticator implements IUserAuthenticator { 28 public static boolean USE_ALTERNATE_PROMPTER = false; 29 30 33 public WorkbenchUserAuthenticator() { 34 super(); 35 IIgnoreInfo[] ignores = Team.getAllIgnores(); 37 boolean found = false; 38 for (int i = 0; i < ignores.length; i++) { 39 if (ignores[i].getPattern().equals("*.notes")) { found = true; 41 } 42 } 43 if (!found) return; 44 IStringMapping[] mappings = Team.getFileContentManager().getExtensionMappings(); 45 for (int i = 0; i < mappings.length; i++) { 46 if (mappings[i].getString().equals("notes")) { USE_ALTERNATE_PROMPTER = true; 48 return; 49 } 50 } 51 USE_ALTERNATE_PROMPTER = false; 52 } 53 56 public void promptForUserInfo(final ICVSRepositoryLocation location, final IUserInfo userinfo, final String message) throws CVSException { 57 if (!userinfo.isUsernameMutable() && USE_ALTERNATE_PROMPTER) { 58 alternatePromptForUserInfo(userinfo); 59 return; 60 } 61 final String [] result = new String [2]; 63 Display display = Display.getCurrent(); 64 final boolean allowCaching[] = {false}; 65 if (display != null) { 66 allowCaching[0] = promptForPassword(location, userinfo.getUsername(), message, userinfo.isUsernameMutable(), result); 67 } else { 68 final CVSException[] exception = new CVSException[] { null }; 70 Display.getDefault().syncExec(new Runnable () { 71 public void run() { 72 try { 73 allowCaching[0] = promptForPassword(location, userinfo.getUsername(), message, userinfo.isUsernameMutable(), result); 74 } catch (CVSException e) { 75 exception[0] = e; 76 } 77 } 78 }); 79 if (exception[0] != null) { 80 throw exception[0]; 81 } 82 } 83 84 if (result[0] == null) { 85 throw new OperationCanceledException(CVSUIMessages.WorkbenchUserAuthenticator_cancelled); 86 } 87 88 if (userinfo.isUsernameMutable()) { 89 userinfo.setUsername(result[0]); 90 91 } 92 userinfo.setPassword(result[1]); 93 94 if(location != null) { 95 if (userinfo.isUsernameMutable()) { 96 location.setUsername(result[0]); 97 } 98 location.setPassword(result[1]); 99 location.setAllowCaching(allowCaching[0]); 100 } 101 } 102 103 116 private boolean promptForPassword(final ICVSRepositoryLocation location, final String username, final String message, final boolean userMutable, final String [] result) throws CVSException { 117 String domain = location == null ? null : location.getLocation(true); 118 boolean cachingCheckbox=true; 119 120 if(location != null && location.getMethod().getName().equals("pserverssh2")){ 133 String host = location.getHost(); int index = host.indexOf("@"); if (index != -1) { 136 cachingCheckbox = false; 137 if (index != 0) { 138 if (!username.equals(host.substring(0, index))) { 139 cachingCheckbox = true; 140 } 141 } 142 } 143 } 144 145 UserValidationDialog dialog = new UserValidationDialog(null, domain, (username==null)?"":username, message, cachingCheckbox); dialog.setUsernameMutable(userMutable); 147 dialog.open(); 148 result[0] = dialog.getUsername(); 149 result[1] = dialog.getPassword(); 150 return dialog.getAllowCaching(); 151 } 152 153 164 public String [] promptForKeyboradInteractive(final ICVSRepositoryLocation location, 165 final String destination, 166 final String name, 167 final String instruction, 168 final String [] prompt, 169 final boolean[] echo) throws CVSException { 170 final String [][] result = new String [1][]; 171 final boolean[] allowCaching=new boolean[1]; 172 Display display = Display.getCurrent(); 173 if (display != null) { 174 result[0]=_promptForUserInteractive(location, destination, name, instruction, prompt, echo, allowCaching); 175 } 176 else { 177 Display.getDefault().syncExec(new Runnable () { 179 public void run() { 180 result[0]=_promptForUserInteractive(location, destination, name, instruction, prompt, echo, allowCaching); 181 } 182 }); 183 } 184 if (result[0] != null && location != null && 185 KeyboardInteractiveDialog.isPasswordAuth(prompt)) { 186 location.setPassword(result[0][0]); 187 location.setAllowCaching(allowCaching[0]); 188 } 189 return result[0]; 190 } 191 192 private String [] _promptForUserInteractive(final ICVSRepositoryLocation location, 193 final String destination, 194 final String name, 195 final String instruction, 196 final String [] prompt, 197 final boolean[] echo, 198 final boolean[] allowCaching) { 199 200 String domain = location == null ? null : location.getLocation(true); 201 String userName = location == null ? null : location.getUsername(); 202 boolean cachingCheckbox=true; 203 204 if (location != null 205 && location.getMethod().getName().equals("pserverssh2")) { 215 String host = location.getHost(); int index = host.indexOf("@"); if (index != -1) { 218 if (index != 0) { 219 userName = host.substring(0, index); 220 } 221 cachingCheckbox = false; 222 } 223 } 224 225 KeyboardInteractiveDialog dialog = new KeyboardInteractiveDialog(null, 226 domain, 227 destination, 228 name, 229 userName, 230 instruction, 231 prompt, 232 echo, 233 cachingCheckbox); 234 dialog.open(); 235 String [] _result=dialog.getResult(); 236 if(_result!=null) 237 allowCaching[0]=dialog.getAllowCaching(); 238 return _result; 239 } 240 241 244 private String alternatePromptForPassword(final String username) { 245 AlternateUserValidationDialog dialog = new AlternateUserValidationDialog(null, (username == null) ? "" : username); dialog.setUsername(username); 247 int result = dialog.open(); 248 if (result == Window.CANCEL) return null; 249 return dialog.getPassword(); 250 } 251 252 255 public void alternatePromptForUserInfo(final IUserInfo userinfo) { 256 final String [] result = new String [1]; 258 Display display = Display.getCurrent(); 259 if (display != null) { 260 result[0] = alternatePromptForPassword(userinfo.getUsername()); 261 } else { 262 Display.getDefault().syncExec(new Runnable () { 264 public void run() { 265 result[0] = alternatePromptForPassword(userinfo.getUsername()); 266 } 267 }); 268 } 269 270 if (result[0] == null) { 271 throw new OperationCanceledException(CVSUIMessages.WorkbenchUserAuthenticator_The_operation_was_canceled_by_the_user_1); 272 } 273 274 userinfo.setPassword(result[0]); 275 } 276 277 280 public int prompt(final ICVSRepositoryLocation location, final int promptType, final String title, final String message, final int[] promptResponses, final int defaultResponse) { 281 final Display display = CVSUIPlugin.getStandardDisplay(); 282 final int[] retval = new int[1]; 283 final String [] buttons = new String [promptResponses.length]; 284 for (int i = 0; i < promptResponses.length; i++) { 285 int prompt = promptResponses[i]; 286 switch(prompt) { 287 case IUserAuthenticator.OK_ID: buttons[i] = IDialogConstants.OK_LABEL; break; 288 case IUserAuthenticator.CANCEL_ID: buttons[i] = IDialogConstants.CANCEL_LABEL; break; 289 case IUserAuthenticator.NO_ID: buttons[i] = IDialogConstants.NO_LABEL; break; 290 case IUserAuthenticator.YES_ID: buttons[i] = IDialogConstants.YES_LABEL; break; 291 } 292 } 293 294 display.syncExec(new Runnable () { 295 public void run() { 296 final MessageDialog dialog = new MessageDialog( 297 new Shell(display), 298 title, 299 null , 300 NLS.bind(CVSUIMessages.WorkbenchUserAuthenticator_0, message, location.getLocation(true)), 301 promptType, 302 buttons, 303 1 304 ); 305 retval[0] = dialog.open(); 306 } 307 }); 308 return retval[0]; 309 } 310 311 public boolean promptForHostKeyChange(final ICVSRepositoryLocation location) { 312 final boolean[] openConfirm = new boolean[] { false }; 313 final Display display = CVSUIPlugin.getStandardDisplay(); 314 display.syncExec(new Runnable () { 315 public void run() { 316 openConfirm[0] = MessageDialog.openConfirm(null, CVSUIMessages.WorkbenchUserAuthenticator_1, NLS.bind(CVSUIMessages.WorkbenchUserAuthenticator_2, new String [] { location.getHost() })); } 318 }); 319 if (!openConfirm[0]) { 320 throw new OperationCanceledException(); 321 } 322 return openConfirm[0]; 323 } 324 } 325 | Popular Tags |