1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package org.eclipse.team.internal.ccvs.ui;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.ccvs.core.IUserAuthenticator;
import org.eclipse.team.ccvs.core.IUserInfo;
import org.eclipse.team.internal.ccvs.core.CVSException;
/**
* An authenticator that prompts the user for authentication info,
* and stores the results in the Platform's authentication keyring.
*/
public class WorkbenchUserAuthenticator implements IUserAuthenticator {
public static final String INFO_PASSWORD = "org.eclipse.team.ccvs.ui.password";
public static final String INFO_USERNAME = "org.eclipse.team.ccvs.ui.username";
public static final String AUTH_SCHEME = "";
public static final URL FAKE_URL;
static {
URL temp = null;
try {
temp = new URL("http://org.eclipse.team.ccvs.ui");
} catch (MalformedURLException e) {
}
FAKE_URL = temp;
}
/**
* WorkbenchUserAuthenticator constructor.
*/
public WorkbenchUserAuthenticator() {
super();
}
/**
* @see IUserAuthenticator#authenticateUser
*/
public boolean authenticateUser(final ICVSRepositoryLocation location, final IUserInfo userinfo, final boolean retry, final String message) throws CVSException {
// first check to see if there is a cached username and password
if ((!retry) && (retrievePassword(location, userinfo))) {
return true;
}
// ask the user for a password
final String[] result = new String[2];
Display display = Display.getCurrent();
if (display != null) {
promptForPassword(location, userinfo.getUsername(), message, userinfo.isUsernameMutable(), result);
} else {
// sync exec in default thread
Display.getDefault().syncExec(new Runnable() {
public void run() {
promptForPassword(location, userinfo.getUsername(), message, userinfo.isUsernameMutable(), result);
}
});
}
if (result[0] == null) {
throw new OperationCanceledException(Policy.bind("WorkbenchUserAuthenticator.cancelled"));
}
updateAndCache(location, userinfo, result[0], result[1]);
return true;
}
/**
* Asks the user to enter a password. Places the
* results in the supplied string[]. result[0] must
* contain the username, result[1] must contain the password.
* If the user canceled, both values must be zero.
*
* @param location the location to obtain the password for
* @param username the username
* @param message a message to display to the user
* @param userMutable whether the user can be changed in the dialog
* @param result a String array of length two in which to put the result
*/
private void promptForPassword(ICVSRepositoryLocation location, String username, String message, boolean userMutable, String[] result) {
Shell shell = new Shell();
UserValidationDialog dialog = new UserValidationDialog(shell, location.getLocation(), (username==null)?"":username, message);
dialog.setUsernameMutable(userMutable);
dialog.open();
shell.dispose();
result[0] = dialog.getUsername();
result[1] = dialog.getPassword();
}
/**
* @see IUserAuthenticator#cachePassword
*/
public void cachePassword(ICVSRepositoryLocation location, IUserInfo userinfo, String password) throws CVSException {
updateAndCache(location, userinfo, userinfo.getUsername(), password);
}
/**
* @see IUserAuthenticator#retrievePassword
*/
public boolean retrievePassword(ICVSRepositoryLocation location, IUserInfo userinfo) throws CVSException {
Map map = Platform.getAuthorizationInfo(FAKE_URL, location.getLocation(), AUTH_SCHEME);
if (map != null) {
String username = (String) map.get(INFO_USERNAME);
String password = (String) map.get(INFO_PASSWORD);
if (password != null) {
if (userinfo.isUsernameMutable())
userinfo.setUsername(username);
userinfo.setPassword(password);
return true;
}
}
return false;
}
/**
* @see IUserAuthenticator#dispose(IRepositoryLocation)
*/
public void dispose(ICVSRepositoryLocation location) throws CVSException {
try {
Platform.flushAuthorizationInfo(FAKE_URL, location.getLocation(), AUTH_SCHEME);
} catch (CoreException e) {
// We should probably wrap the CoreException here!
CVSUIPlugin.log(e.getStatus());
throw new CVSException(IStatus.ERROR, IStatus.ERROR, Policy.bind("WorkbenchUserAuthenticator.errorFlushing", location.getLocation()), e);
}
}
/**
* Updates the pasword in the platform keyring.
*
* @param location the repository location
* @param userinfo the user information
* @param username the name of the user
* @param password the password
* @throws CVSException if a CVS error occurs
*/
public void updateAndCache(ICVSRepositoryLocation location, IUserInfo userinfo, String username, String password) throws CVSException {
// put the password into the Platform map
Map map = Platform.getAuthorizationInfo(FAKE_URL, location.getLocation(), AUTH_SCHEME);
if (map == null) {
map = new java.util.HashMap(10);
}
map.put(INFO_USERNAME, username);
map.put(INFO_PASSWORD, password);
try {
Platform.addAuthorizationInfo(FAKE_URL, location.getLocation(), AUTH_SCHEME, map);
} catch (CoreException e) {
// We should probably wrap the CoreException here!
CVSUIPlugin.log(e.getStatus());
throw new CVSException(IStatus.ERROR, IStatus.ERROR, Policy.bind("WorkbenchUserAuthenticator.errorSaving", location.getLocation()), e);
}
if (userinfo.isUsernameMutable()) {
userinfo.setUsername(username);
}
userinfo.setPassword(password);;
}
}
|