KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > WorkbenchUserAuthenticator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Atsuhiko Yamanaka, JCraft,Inc. - implementation of promptForKeyboradInteractive
11  *******************************************************************************/

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 /**
24  * An authenticator that prompts the user for authentication info,
25  * and stores the results in the Platform's authentication key-ring.
26  */

27 public class WorkbenchUserAuthenticator implements IUserAuthenticator {
28     public static boolean USE_ALTERNATE_PROMPTER = false;
29     
30     /**
31      * WorkbenchUserAuthenticator constructor.
32      */

33     public WorkbenchUserAuthenticator() {
34         super();
35         // Initialize USE_ALTERNATE_PROMPTER
36
IIgnoreInfo[] ignores = Team.getAllIgnores();
37         boolean found = false;
38         for (int i = 0; i < ignores.length; i++) {
39             if (ignores[i].getPattern().equals("*.notes")) { //$NON-NLS-1$
40
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")) { //$NON-NLS-1$
47
USE_ALTERNATE_PROMPTER = true;
48                 return;
49             }
50         }
51         USE_ALTERNATE_PROMPTER = false;
52     }
53     /**
54      * @see IUserAuthenticator#authenticateUser
55      */

56     public void promptForUserInfo(final ICVSRepositoryLocation location, final IUserInfo userinfo, final String JavaDoc message) throws CVSException {
57         if (!userinfo.isUsernameMutable() && USE_ALTERNATE_PROMPTER) {
58             alternatePromptForUserInfo(userinfo);
59             return;
60         }
61         // ask the user for a password
62
final String JavaDoc[] result = new String JavaDoc[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             // sync exec in default thread
69
final CVSException[] exception = new CVSException[] { null };
70             Display.getDefault().syncExec(new Runnable JavaDoc() {
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     /**
104      * Asks the user to enter a password. Places the
105      * results in the supplied string[]. result[0] must
106      * contain the username, result[1] must contain the password.
107      * If the user canceled, both values must be zero.
108      *
109      * @param location the location to obtain the password for
110      * @param username the username
111      * @param message a message to display to the user
112      * @param userMutable whether the user can be changed in the dialog
113      * @param result a String array of length two in which to put the result
114      * @throws CVSException
115      */

116     private boolean promptForPassword(final ICVSRepositoryLocation location, final String JavaDoc username, final String JavaDoc message, final boolean userMutable, final String JavaDoc[] result) throws CVSException {
117         String JavaDoc domain = location == null ? null : location.getLocation(true);
118         boolean cachingCheckbox=true;
119         
120         if(location != null && location.getMethod().getName().equals("pserverssh2")){ //$NON-NLS-1$
121
/**
122              * If this method 'promptForPassword' is invoked for ssh2 connection,
123              * we want to disable the checkbox for password caching, because it will
124              * overwrite the password for pserver with the password for ssh2.
125              *
126              * In pserverssh2 connection type, its location name will be like
127              * pserverssh2:pserver_username@ssh2_username@ssh2_host@pserver_host
128              *
129              * The problem is that there is not a method to know if we are invoked from ssh2 or pserver.
130              * The following code will guess that if 'username' is equals to 'ssh2_userver' we are from ssh2,
131              * but if 'pserver2_username' is equals to 'ssh2_username', this guess will not be correct.
132              */

133             String JavaDoc host = location.getHost(); // ssh2_username@ssh2_host@pserver_host
134
int index = host.indexOf("@"); //$NON-NLS-1$
135
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);//$NON-NLS-1$
146
dialog.setUsernameMutable(userMutable);
147         dialog.open();
148         result[0] = dialog.getUsername();
149         result[1] = dialog.getPassword();
150         return dialog.getAllowCaching();
151     }
152
153     /**
154      * Asks the user to enter values.
155      *
156      * @param location the location to obtain the password for
157      * @param destication the location
158      * @param name the name
159      * @param instruction the instruction
160      * @param prompt the titles for text fields
161      * @param echo '*' should be used or not
162      * @param result the entered values, or null if user canceled.
163      */

164     public String JavaDoc[] promptForKeyboradInteractive(final ICVSRepositoryLocation location,
165                              final String JavaDoc destination,
166                              final String JavaDoc name,
167                              final String JavaDoc instruction,
168                              final String JavaDoc[] prompt,
169                              final boolean[] echo) throws CVSException {
170         final String JavaDoc[][] result = new String JavaDoc[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             // sync exec in default thread
178
Display.getDefault().syncExec(new Runnable JavaDoc() {
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 JavaDoc[] _promptForUserInteractive(final ICVSRepositoryLocation location,
193                            final String JavaDoc destination,
194                            final String JavaDoc name,
195                            final String JavaDoc instruction,
196                            final String JavaDoc[] prompt,
197                            final boolean[] echo,
198                            final boolean[] allowCaching) {
199     
200         String JavaDoc domain = location == null ? null : location.getLocation(true);
201         String JavaDoc userName = location == null ? null : location.getUsername();
202         boolean cachingCheckbox=true;
203         
204         if (location != null
205                 && location.getMethod().getName().equals("pserverssh2")) { //$NON-NLS-1$
206
/**
207              * We want to disable the checkbox for password caching, because it will
208              * overwrite the password for pserver with the password for ssh2.
209              *
210              * In pserverssh2 connection type, its location name will be like
211              * pserverssh2:pserver_username@ssh2_username@ssh2_host@pserver_host
212              *
213              * 'userName' is 'pserver_username', so we also have to change it to 'ssh2_username'.
214              */

215             String JavaDoc host = location.getHost(); // ssh2_username@ssh2_host@pserver_host
216
int index = host.indexOf("@"); //$NON-NLS-1$
217
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 JavaDoc[] _result=dialog.getResult();
236         if(_result!=null)
237           allowCaching[0]=dialog.getAllowCaching();
238         return _result;
239     }
240     
241     /**
242      * Special alternate prompting. Returns the password. Username must be fixed.
243      */

244     private String JavaDoc alternatePromptForPassword(final String JavaDoc username) {
245         AlternateUserValidationDialog dialog = new AlternateUserValidationDialog(null, (username == null) ? "" : username); //$NON-NLS-1$
246
dialog.setUsername(username);
247         int result = dialog.open();
248         if (result == Window.CANCEL) return null;
249         return dialog.getPassword();
250     }
251     
252     /**
253      * Special alternate prompting.
254      */

255     public void alternatePromptForUserInfo(final IUserInfo userinfo) {
256         // ask the user for a password
257
final String JavaDoc[] result = new String JavaDoc[1];
258         Display display = Display.getCurrent();
259         if (display != null) {
260             result[0] = alternatePromptForPassword(userinfo.getUsername());
261         } else {
262             // sync exec in default thread
263
Display.getDefault().syncExec(new Runnable JavaDoc() {
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     /* (non-Javadoc)
278      * @see org.eclipse.team.internal.ccvs.core.IUserAuthenticator#prompt(org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation, int, java.lang.String, java.lang.String, int[], int)
279      */

280     public int prompt(final ICVSRepositoryLocation location, final int promptType, final String JavaDoc title, final String JavaDoc message, final int[] promptResponses, final int defaultResponse) {
281         final Display display = CVSUIPlugin.getStandardDisplay();
282         final int[] retval = new int[1];
283         final String JavaDoc[] buttons = new String JavaDoc[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 JavaDoc() {
295             public void run() {
296                 final MessageDialog dialog = new MessageDialog(
297                         new Shell(display),
298                         title,
299                         null /* title image */,
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 JavaDoc() {
315             public void run() {
316                 openConfirm[0] = MessageDialog.openConfirm(null, CVSUIMessages.WorkbenchUserAuthenticator_1, NLS.bind(CVSUIMessages.WorkbenchUserAuthenticator_2, new String JavaDoc[] { location.getHost() })); //
317
}
318         });
319         if (!openConfirm[0]) {
320             throw new OperationCanceledException();
321         }
322         return openConfirm[0];
323     }
324 }
325
Popular Tags