diff options
10 files changed, 172 insertions, 256 deletions
diff --git a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java index 45ec228e8..24e4a4d45 100644 --- a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java +++ b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java @@ -83,6 +83,7 @@ import org.eclipse.mylyn.commons.net.WebLocation; import org.eclipse.mylyn.commons.net.WebUtil; import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants.BUGZILLA_REPORT_STATUS_4_0; import org.eclipse.mylyn.internal.bugzilla.core.service.BugzillaXmlRpcClient; +import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryLocation; import org.eclipse.mylyn.tasks.core.IRepositoryQuery; import org.eclipse.mylyn.tasks.core.RepositoryResponse; import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind; @@ -2575,4 +2576,11 @@ public class BugzillaClient { return connector; } + public TaskRepository getTaskRepository() { + if (location instanceof TaskRepositoryLocation) { + return ((TaskRepositoryLocation) location).getTaskRepository(); + } + return null; + } + } diff --git a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaAttachmentHandlerTest.java b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaAttachmentHandlerTest.java index 11ed986fc..3f278c6da 100644 --- a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaAttachmentHandlerTest.java +++ b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaAttachmentHandlerTest.java @@ -480,6 +480,8 @@ public class BugzillaAttachmentHandlerTest extends AbstractBugzillaTest { } public void testContextAttachFailure() throws Exception { + // use the client's repository when setting credentials below + repository = client.getTaskRepository(); TaskData taskData = BugzillaFixture.current().createTask(PrivilegeLevel.USER, null, null); assertNotNull(taskData); ITask task = TasksUi.getRepositoryModel().createTask(repository, taskData.getTaskId()); diff --git a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaRepositoryConnectorTest.java b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaRepositoryConnectorTest.java index 9d1b36208..070db104f 100644 --- a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaRepositoryConnectorTest.java +++ b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/BugzillaRepositoryConnectorTest.java @@ -938,6 +938,8 @@ public class BugzillaRepositoryConnectorTest extends AbstractBugzillaTest { } public void testAuthenticationCredentials() throws Exception { + // use the client's repository when setting credentials below + repository = client.getTaskRepository(); TaskData data = BugzillaFixture.current().createTask(PrivilegeLevel.USER, null, null); assertNotNull(data); ITask task = generateLocalTaskAndDownload(data.getTaskId()); diff --git a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/RepositoryReportFactoryTest.java b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/RepositoryReportFactoryTest.java index c66c4142b..e399b399b 100644 --- a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/RepositoryReportFactoryTest.java +++ b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/RepositoryReportFactoryTest.java @@ -52,6 +52,8 @@ public class RepositoryReportFactoryTest extends TestCase { public void testInvalidCredentials() throws Exception { try { client.logout(new NullProgressMonitor()); + // use the client's repository when setting credentials below + repository = client.getTaskRepository(); repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("invalid", "invalid"), false); connector.getTaskData(repository, "1", new NullProgressMonitor()); diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/ITasksCoreConstants.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/ITasksCoreConstants.java index 3ff03b8c7..32ee78923 100644 --- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/ITasksCoreConstants.java +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/ITasksCoreConstants.java @@ -65,6 +65,10 @@ public interface ITasksCoreConstants { public static final String ATTRIBUTE_TASK_SUPPRESS_INCOMING = "task.common.suppress.incoming"; //$NON-NLS-1$ + /** + * @deprecated Since Mylyn 3.11, all TaskRepositories store their credentials in the secure store. + */ + @Deprecated public static final String PROPERTY_USE_SECURE_STORAGE = "org.eclipse.mylyn.tasklist.repositories.configuration.securestorage"; //$NON-NLS-1$ /** diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskRepositoryLocation.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskRepositoryLocation.java index 682bb1a8c..6882551d2 100644 --- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskRepositoryLocation.java +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/internal/tasks/core/TaskRepositoryLocation.java @@ -59,4 +59,8 @@ public class TaskRepositoryLocation extends AbstractWebLocation { return taskRepository.getCredentials(type); } + public TaskRepository getTaskRepository() { + return taskRepository; + } + }
\ No newline at end of file diff --git a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java index 4374dff8a..3daf85ee3 100644 --- a/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java +++ b/org.eclipse.mylyn.tasks.core/src/org/eclipse/mylyn/tasks/core/TaskRepository.java @@ -15,8 +15,6 @@ package org.eclipse.mylyn.tasks.core; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.net.MalformedURLException; -import java.net.URL; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -26,20 +24,15 @@ import java.util.Set; import java.util.TimeZone; import org.eclipse.core.runtime.Assert; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.PlatformObject; -import org.eclipse.core.runtime.Status; -import org.eclipse.equinox.security.storage.EncodingUtils; -import org.eclipse.equinox.security.storage.ISecurePreferences; -import org.eclipse.equinox.security.storage.SecurePreferencesFactory; -import org.eclipse.equinox.security.storage.StorageException; -import org.eclipse.mylyn.commons.core.StatusHandler; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; import org.eclipse.mylyn.commons.net.AuthenticationType; +import org.eclipse.mylyn.commons.repositories.core.ILocationService; +import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore; +import org.eclipse.mylyn.internal.commons.repositories.core.LocationService; import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants; -import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; import org.eclipse.mylyn.internal.tasks.core.RepositoryPerson; /** @@ -53,14 +46,13 @@ import org.eclipse.mylyn.internal.tasks.core.RepositoryPerson; * <li>The solution we have come up with thus far is not to interpret the date as a DATE object but rather simply use * the date string given to us by the repository itself.</li> * </ul> - * + * * @author Mik Kersten * @author Rob Elves * @author Eugene Kuleshov * @author Steffen Pingel * @since 2.0 */ -@SuppressWarnings("deprecation") public final class TaskRepository extends PlatformObject { public static final String DEFAULT_CHARACTER_ENCODING = "UTF-8"; //$NON-NLS-1$ @@ -115,12 +107,6 @@ public final class TaskRepository extends PlatformObject { public static final String NO_VERSION_SPECIFIED = "unknown"; //$NON-NLS-1$ - private static final String AUTH_SCHEME = "Basic"; //$NON-NLS-1$ - - private static final String AUTH_REALM = ""; //$NON-NLS-1$ - - private static final URL DEFAULT_URL; - private static final String PROPERTY_CONFIG_TIMESTAMP = "org.eclipse.mylyn.tasklist.repositories.configuration.timestamp"; //$NON-NLS-1$ public static final String PROXY_USEDEFAULT = "org.eclipse.mylyn.tasklist.repositories.proxy.usedefault"; //$NON-NLS-1$ @@ -149,7 +135,7 @@ public final class TaskRepository extends PlatformObject { /** * Category for repositories that manage tasks. - * + * * @see #setCategory(String) * @since 3.9 */ @@ -157,7 +143,7 @@ public final class TaskRepository extends PlatformObject { /** * Category for repositories that manage bugs. - * + * * @see #setCategory(String) * @since 3.9 */ @@ -165,7 +151,7 @@ public final class TaskRepository extends PlatformObject { /** * Category for repositories that manage builds. - * + * * @see #setCategory(String) * @since 3.9 */ @@ -173,7 +159,7 @@ public final class TaskRepository extends PlatformObject { /** * Category for repositories that manage reviews. - * + * * @see #setCategory(String) * @since 3.9 */ @@ -190,16 +176,6 @@ public final class TaskRepository extends PlatformObject { private static String CREATED_FROM_TEMPLATE = "org.eclipse.mylyn.tasklist.repositories.template"; //$NON-NLS-1$ - static { - URL url = null; - try { - url = new URL("http://eclipse.org/mylyn"); //$NON-NLS-1$ - } catch (Exception ex) { - // TODO ? - } - DEFAULT_URL = url; - } - private static String getKeyPrefix(AuthenticationType type) { switch (type) { case HTTP: @@ -235,6 +211,8 @@ public final class TaskRepository extends PlatformObject { private boolean shouldPersistCredentials = true; + private final ILocationService service = LocationService.getDefault(); + public TaskRepository(String connectorKind, String repositoryUrl) { this(connectorKind, repositoryUrl, NO_VERSION_SPECIFIED); } @@ -278,48 +256,19 @@ public final class TaskRepository extends PlatformObject { this.setProperty(AUTH_PROXY + SAVE_PASSWORD, String.valueOf(true)); } - private ISecurePreferences getSecurePreferences() { - ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault() - .node(ITasksCoreConstants.ID_PLUGIN); - securePreferences = securePreferences.node(EncodingUtils.encodeSlashes(getRepositoryUrl())); - return securePreferences; + private ICredentialsStore getCredentialsStore() { + return getService().getCredentialsStore(getRepositoryUrl()); } private void addAuthInfo(String username, String password, String userProperty, String passwordProperty) { if (Platform.isRunning() && shouldPersistCredentials()) { - if (useSecureStorage()) { - try { - ISecurePreferences securePreferences = getSecurePreferences(); - if (userProperty.equals(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME)) { - this.setProperty(userProperty, username); - } else { - securePreferences.put(userProperty, username, false); - } - securePreferences.put(passwordProperty, password, true); - } catch (StorageException e) { - StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, - "Could not store authorization credentials", e)); //$NON-NLS-1$ - } + ICredentialsStore credentialsStore = getCredentialsStore(); + if (userProperty.equals(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME)) { + this.setProperty(userProperty, username); } else { - synchronized (LOCK) { - Map<String, String> map = getAuthInfo(); - if (map == null) { - map = new HashMap<String, String>(); - } - try { - try { - map.put(userProperty, username); - map.put(passwordProperty, password); - Platform.addAuthorizationInfo(new URL(getRepositoryUrl()), AUTH_REALM, AUTH_SCHEME, map); - } catch (MalformedURLException ex) { - Platform.addAuthorizationInfo(DEFAULT_URL, getRepositoryUrl(), AUTH_SCHEME, map); - } - } catch (CoreException e) { - StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, - "Could not set authorization credentials", e)); //$NON-NLS-1$ - } - } + credentialsStore.put(userProperty, username, false); } + credentialsStore.put(passwordProperty, password, true); } else { synchronized (LOCK) { Map<String, String> headlessCreds = credentials.get(getRepositoryUrl()); @@ -333,30 +282,6 @@ public final class TaskRepository extends PlatformObject { } } - @SuppressWarnings({ "unchecked" }) - private Map<String, String> getAuthInfo() { - synchronized (LOCK) { - if (Platform.isRunning()) { - try { - return Platform.getAuthorizationInfo(new URL(getRepositoryUrl()), AUTH_REALM, AUTH_SCHEME); - } catch (MalformedURLException ex) { - return Platform.getAuthorizationInfo(DEFAULT_URL, getRepositoryUrl(), AUTH_SCHEME); - } catch (Exception e) { - StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, - "Could not retrieve authorization credentials", e)); //$NON-NLS-1$ - } - } else { - Map<String, String> headlessCreds = credentials.get(getRepositoryUrl()); - if (headlessCreds == null) { - headlessCreds = new HashMap<String, String>(); - credentials.put(getRepositoryUrl(), headlessCreds); - } - return headlessCreds; - } - return null; - } - } - /** * @deprecated use {@code flushAuthenticationCredentials()} */ @@ -388,33 +313,12 @@ public final class TaskRepository extends PlatformObject { isCachedUserName = false; } - synchronized (LOCK) { - if (Platform.isRunning() && shouldPersistCredentials()) { - if (useSecureStorage()) { - if (Platform.isRunning()) { - ISecurePreferences securePreferences = getSecurePreferences(); - securePreferences.removeNode(); - this.setProperty(AuthenticationType.REPOSITORY + USERNAME, ""); //$NON-NLS-1$ - } else { - Map<String, String> headlessCreds = credentials.get(getRepositoryUrl()); - if (headlessCreds != null) { - headlessCreds.clear(); - } - } - } else { - try { - try { - Platform.flushAuthorizationInfo(new URL(getRepositoryUrl()), AUTH_REALM, AUTH_SCHEME); - } catch (MalformedURLException ex) { - Platform.flushAuthorizationInfo(DEFAULT_URL, getRepositoryUrl(), AUTH_SCHEME); - } - } catch (CoreException e) { - // FIXME propagate exception? - StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, - "Could not flush authorization credentials", e)); //$NON-NLS-1$ - } - } - } else { + if (Platform.isRunning() && shouldPersistCredentials()) { + ICredentialsStore credentialsStore = getCredentialsStore(); + credentialsStore.clear(); + this.setProperty(AuthenticationType.REPOSITORY + USERNAME, ""); //$NON-NLS-1$ + } else { + synchronized (LOCK) { Map<String, String> headlessCreds = credentials.get(getRepositoryUrl()); if (headlessCreds != null) { headlessCreds.clear(); @@ -423,54 +327,13 @@ public final class TaskRepository extends PlatformObject { } } - private boolean useSecureStorage() { - String useSecure = getProperty(ITasksCoreConstants.PROPERTY_USE_SECURE_STORAGE); - if (useSecure != null) { - return "true".equals(useSecure); //$NON-NLS-1$ - } - return false; - } - - @SuppressWarnings("unchecked") private String getAuthInfo(String property) { if (Platform.isRunning() && shouldPersistCredentials()) { - if (useSecureStorage()) { - String propertyValue = null; - if (property.equals(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME)) { - propertyValue = this.getProperty(property); - } else { - try { - ISecurePreferences securePreferences = getSecurePreferences(); - propertyValue = securePreferences.get(property, null); - } catch (StorageException e) { - StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, - "Could not retrieve authorization credentials", e)); //$NON-NLS-1$ - } - } - return propertyValue; - } else { - synchronized (LOCK) { - try { - Map<String, String> map = Platform.getAuthorizationInfo(new URL(getRepositoryUrl()), - AUTH_REALM, AUTH_SCHEME); - if (map != null) { - String propertyValue = map.get(property); - return propertyValue; - } - } catch (MalformedURLException ex) { - Map<String, String> map = Platform.getAuthorizationInfo(DEFAULT_URL, getRepositoryUrl(), - AUTH_SCHEME); - if (map != null) { - String propertyValue = map.get(property); - return propertyValue; - } - } catch (Exception e) { - StatusHandler.log(new Status(IStatus.ERROR, ITasksCoreConstants.ID_PLUGIN, - "Could not retrieve authorization credentials", e)); //$NON-NLS-1$ - } - return null; - } + if (property.equals(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME)) { + return getProperty(property); } + ICredentialsStore credentialsStore = getCredentialsStore(); + return credentialsStore.get(property, null); } else { synchronized (LOCK) { Map<String, String> headlessCreds = credentials.get(getRepositoryUrl()); @@ -485,7 +348,7 @@ public final class TaskRepository extends PlatformObject { /** * Returns {@code} if credentials persisted in the platform keystore. - * + * * @since 3.10 * @see #setShouldPersistCredentials(boolean) */ @@ -498,7 +361,7 @@ public final class TaskRepository extends PlatformObject { * will not be persisted in the platform keystore. * <p> * This flag does not have any effect if not running in an OSGi environment. - * + * * @since 3.10 * @see #shouldPersistCredentials() */ @@ -513,7 +376,7 @@ public final class TaskRepository extends PlatformObject { /** * Get the last refresh date as initialized {@link Date} object, null if not set<br /> - * + * * @return {@link Date} configuration date, null if not set */ public Date getConfigurationDate() { @@ -542,7 +405,7 @@ public final class TaskRepository extends PlatformObject { /** * Returns the credentials for an authentication type. - * + * * @param authType * the type of authentication * @return null, if no credentials are set for <code>authType</code> @@ -552,7 +415,7 @@ public final class TaskRepository extends PlatformObject { String key = getKeyPrefix(authType); String enabled = getProperty(key + ENABLED); - if (enabled == null || "true".equals(enabled)) { //$NON-NLS-1$ + if ("true".equals(enabled)) { //$NON-NLS-1$ String userName = getAuthInfo(key + USERNAME); String password; @@ -570,11 +433,6 @@ public final class TaskRepository extends PlatformObject { password = ""; //$NON-NLS-1$ } - if (enabled == null && userName.length() == 0) { - // API30: legacy support for versions prior to 2.2 that did not set the enable flag, remove for 3.0 - return null; - } - return new AuthenticationCredentials(userName, password); } else { return null; @@ -683,12 +541,8 @@ public final class TaskRepository extends PlatformObject { public String getUserName() { // NOTE: if anonymous, user name is "" string so we won't go to keyring if (!isCachedUserName) { - if (useSecureStorage()) { - // do not open secure store for username to avoid prompting user for password during initialization - cachedUserName = getProperty(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME); - } else { - cachedUserName = getUserName(AuthenticationType.REPOSITORY); - } + // do not open secure store for username to avoid prompting user for password during initialization + cachedUserName = getProperty(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME); isCachedUserName = true; } return cachedUserName; @@ -769,12 +623,12 @@ public final class TaskRepository extends PlatformObject { public void setCharacterEncoding(String characterEncoding) { properties.put(IRepositoryConstants.PROPERTY_ENCODING, characterEncoding == null ? DEFAULT_CHARACTER_ENCODING - : characterEncoding); + : characterEncoding); } /** * Set the Configuration date to the {@link Date} indicated. - * + * * @param configuration * date {@link {@link Date} */ @@ -786,7 +640,7 @@ public final class TaskRepository extends PlatformObject { /** * Sets the credentials for <code>authType</code>. - * + * * @param authType * the type of authentication * @param credentials @@ -805,14 +659,14 @@ public final class TaskRepository extends PlatformObject { if (credentials == null) { setProperty(key + ENABLED, String.valueOf(false)); transientProperties.remove(key + PASSWORD); - addAuthInfo("", "", key + USERNAME, key + PASSWORD); //$NON-NLS-1$ //$NON-NLS-2$ + addAuthInfo(null, null, key + USERNAME, key + PASSWORD); } else { setProperty(key + ENABLED, String.valueOf(true)); if (savePassword) { addAuthInfo(credentials.getUserName(), credentials.getPassword(), key + USERNAME, key + PASSWORD); transientProperties.remove(key + PASSWORD); } else { - addAuthInfo(credentials.getUserName(), "", key + USERNAME, key + PASSWORD); //$NON-NLS-1$ + addAuthInfo(credentials.getUserName(), null, key + USERNAME, key + PASSWORD); transientProperties.put(key + PASSWORD, credentials.getPassword()); } } @@ -891,7 +745,7 @@ public final class TaskRepository extends PlatformObject { public void setTimeZoneId(String timeZoneId) { setProperty(IRepositoryConstants.PROPERTY_TIMEZONE, timeZoneId == null ? TimeZone.getDefault().getID() - : timeZoneId); + : timeZoneId); } /** @@ -977,7 +831,7 @@ public final class TaskRepository extends PlatformObject { /** * If this repository was automatically created from a template <code>value</code> should be set to true. - * + * * @since 3.5 * @see #isCreatedFromTemplate() */ @@ -987,7 +841,7 @@ public final class TaskRepository extends PlatformObject { /** * Returns true, if this repository was automatically created from a template. - * + * * @since 3.5 * @see #setCreatedFromTemplate(boolean) */ @@ -1009,4 +863,8 @@ public final class TaskRepository extends PlatformObject { setProperty(IRepositoryConstants.PROPERTY_CATEGORY, category); } + private ILocationService getService() { + return service; + } + } diff --git a/org.eclipse.mylyn.tasks.tests/META-INF/MANIFEST.MF b/org.eclipse.mylyn.tasks.tests/META-INF/MANIFEST.MF index d83c642fd..b5958b8aa 100644 --- a/org.eclipse.mylyn.tasks.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.mylyn.tasks.tests/META-INF/MANIFEST.MF @@ -29,7 +29,8 @@ Require-Bundle: org.junit;bundle-version="4.8.2", org.eclipse.mylyn.tasks.bugs, org.eclipse.mylyn.tasks.search, org.eclipse.mylyn.tasks.ui, - org.eclipse.mylyn.tests.util + org.eclipse.mylyn.tests.util, + org.eclipse.mylyn.commons.repositories.core Export-Package: org.eclipse.mylyn.tasks.tests;x-internal:=true, org.eclipse.mylyn.tasks.tests.bugs;x-internal:=true, org.eclipse.mylyn.tasks.tests.connector;x-friends:="org.eclipse.mylyn.tasks.ui.tests", diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryCredentialsTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryCredentialsTest.java index 554498487..6ef2dedc9 100644 --- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryCredentialsTest.java +++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryCredentialsTest.java @@ -14,13 +14,15 @@ package org.eclipse.mylyn.tasks.tests; import java.net.URL; import java.util.Collections; import java.util.Date; -import java.util.Map; import junit.framework.TestCase; import org.eclipse.core.runtime.Platform; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; import org.eclipse.mylyn.commons.net.AuthenticationType; +import org.eclipse.mylyn.commons.repositories.core.ILocationService; +import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore; +import org.eclipse.mylyn.internal.commons.repositories.core.LocationService; import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants; import org.eclipse.mylyn.tasks.core.TaskRepository; @@ -29,6 +31,51 @@ import org.eclipse.mylyn.tasks.core.TaskRepository; */ public class TaskRepositoryCredentialsTest extends TestCase { + private static final String AUTH_REPOSITORY = "org.eclipse.mylyn.tasklist.repositories"; + + private static final String AUTH_HTTP = "org.eclipse.mylyn.tasklist.repositories.httpauth"; + + private static final String AUTH_CERT = "org.eclipse.mylyn.tasklist.repositories.certauth"; + + private static final String AUTH_PROXY = "org.eclipse.mylyn.tasklist.repositories.proxy"; + + private static final String PASSWORD = ".password"; + + private static String getKeyPrefix(AuthenticationType type) { + switch (type) { + case HTTP: + return AUTH_HTTP; + case CERTIFICATE: + return AUTH_CERT; + case PROXY: + return AUTH_PROXY; + case REPOSITORY: + return AUTH_REPOSITORY; + } + throw new IllegalArgumentException("Unknown authentication type: " + type); //$NON-NLS-1$ + } + + private String getPassword(AuthenticationType authType) { + ICredentialsStore store = service.getCredentialsStore(taskRepository.getRepositoryUrl()); + String password = store.get(getKeyPrefix(authType) + PASSWORD, null); + return password; + } + + private TaskRepository taskRepository; + + private ILocationService service; + + @Override + protected void setUp() throws Exception { + service = LocationService.getDefault(); + taskRepository = new TaskRepository("kind", "http://url"); + } + + @Override + protected void tearDown() throws Exception { + taskRepository.flushAuthenticationCredentials(); + } + @SuppressWarnings("deprecation") public void testPlatformAuthHandlerAvailable() throws Exception { URL url = new URL("http://mylyn"); @@ -47,10 +94,9 @@ public class TaskRepositoryCredentialsTest extends TestCase { @SuppressWarnings("deprecation") public void testPassword() throws Exception { - password(AuthenticationType.REPOSITORY); + assertCredentials(AuthenticationType.REPOSITORY); // test old API - TaskRepository taskRepository = new TaskRepository("kind", "http://url"); taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); assertEquals("user", taskRepository.getUserName()); assertEquals("pwd", taskRepository.getPassword()); @@ -61,7 +107,7 @@ public class TaskRepositoryCredentialsTest extends TestCase { @SuppressWarnings("deprecation") public void testHttpPassword() throws Exception { - password(AuthenticationType.HTTP); + assertCredentials(AuthenticationType.HTTP); TaskRepository taskRepository = new TaskRepository("kind", "url"); taskRepository.setCredentials(AuthenticationType.HTTP, new AuthenticationCredentials("user", "pwd"), true); @@ -71,7 +117,7 @@ public class TaskRepositoryCredentialsTest extends TestCase { @SuppressWarnings("deprecation") public void testProxyPassword() throws Exception { - password(AuthenticationType.PROXY); + assertCredentials(AuthenticationType.PROXY); TaskRepository taskRepository = new TaskRepository("kind", "url"); taskRepository.setCredentials(AuthenticationType.PROXY, new AuthenticationCredentials("user", "pwd"), false); @@ -85,11 +131,21 @@ public class TaskRepositoryCredentialsTest extends TestCase { taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), false); taskRepository.setCredentials(AuthenticationType.HTTP, new AuthenticationCredentials("user", "pwd"), true); + taskRepository.setCredentials(AuthenticationType.PROXY, new AuthenticationCredentials("user", "pwd"), true); + + assertNotNull(taskRepository.getCredentials(AuthenticationType.REPOSITORY)); + assertNotNull(taskRepository.getCredentials(AuthenticationType.HTTP)); + assertNotNull(taskRepository.getCredentials(AuthenticationType.PROXY)); + taskRepository.flushAuthenticationCredentials(); - assertEquals(null, taskRepository.getUserName()); - assertEquals(null, taskRepository.getPassword()); - assertEquals(null, taskRepository.getHttpUser()); - assertEquals(null, taskRepository.getHttpPassword()); + + assertNotNull(taskRepository.getUserName());// username is not flushed + assertNull(taskRepository.getPassword()); + assertNull(taskRepository.getHttpUser()); + assertNull(taskRepository.getHttpPassword()); + assertNull(taskRepository.getProxyUsername()); + assertNull(taskRepository.getProxyPassword()); + assertNull(taskRepository.getCredentials(AuthenticationType.REPOSITORY)); assertNull(taskRepository.getCredentials(AuthenticationType.HTTP)); assertNull(taskRepository.getCredentials(AuthenticationType.PROXY)); @@ -99,10 +155,8 @@ public class TaskRepositoryCredentialsTest extends TestCase { assertTrue(Platform.isRunning()); } - @SuppressWarnings("deprecation") - public void password(AuthenticationType authType) throws Exception { - URL url = new URL("http://url"); - TaskRepository taskRepository = new TaskRepository("kind", url.toString()); + public void assertCredentials(AuthenticationType authType) throws Exception { + assertNull(getPassword(authType)); try { taskRepository.flushAuthenticationCredentials(); @@ -115,10 +169,7 @@ public class TaskRepositoryCredentialsTest extends TestCase { assertEquals("user", credentials.getUserName()); assertEquals("pwd", credentials.getPassword()); - Map<?, ?> map = Platform.getAuthorizationInfo(url, "", "Basic"); - assertNotNull(map); - assertTrue(map.containsValue("user")); - assertTrue(map.containsValue("pwd")); + assertEquals("pwd", getPassword(authType)); // test not saving password taskRepository.setCredentials(authType, new AuthenticationCredentials("user1", "pwd1"), false); @@ -128,13 +179,8 @@ public class TaskRepositoryCredentialsTest extends TestCase { assertEquals("user1", credentials.getUserName()); assertEquals("pwd1", credentials.getPassword()); - // make sure old passwords are not in the key ring - map = Platform.getAuthorizationInfo(url, "", "Basic"); - assertNotNull(map); - assertTrue(map.containsValue("user1")); - assertFalse(map.containsValue("pwd1")); - assertFalse(map.containsValue("user")); - assertFalse(map.containsValue("pwd")); + // make sure old passwords are not stored + assertNull(getPassword(authType)); taskRepository.setCredentials(authType, new AuthenticationCredentials("user2", "pwd2"), true); assertTrue(taskRepository.getSavePassword(authType)); @@ -148,8 +194,6 @@ public class TaskRepositoryCredentialsTest extends TestCase { } public void testConfigUpdateStoring() throws Exception { - URL url = new URL("http://url"); - TaskRepository taskRepository = new TaskRepository("kind", url.toString()); Date stamp = taskRepository.getConfigurationDate(); assertNull("unset configuration date returns null", stamp); stamp = new Date(); @@ -160,44 +204,43 @@ public class TaskRepositoryCredentialsTest extends TestCase { } public void testDoNotPersistCredentials() throws Exception { - TaskRepository repository = new TaskRepository("kind", "http://url"); - repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); - assertEquals("pwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); + assertEquals("pwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); - repository.setShouldPersistCredentials(false); - repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "newpwd"), true); - assertEquals("newpwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + taskRepository.setShouldPersistCredentials(false); + taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "newpwd"), + true); + assertEquals("newpwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); - repository.setShouldPersistCredentials(true); - assertEquals("pwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + taskRepository.setShouldPersistCredentials(true); + assertEquals("pwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); } public void testSetCredentialsDoesNotAffectExistingRepositoryWhenShouldNotPersistIsSetToTrue() throws Exception { - TaskRepository repository = new TaskRepository("kind", "http://url"); - repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); - assertEquals("pwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); + assertEquals("pwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); TaskRepository newRepository = new TaskRepository("kind", "http://url"); newRepository.setShouldPersistCredentials(false); newRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("newuser", "newpwd"), true); - assertEquals("pwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + assertEquals("pwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); assertEquals("newpwd", newRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); - repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd2"), true); - assertEquals("pwd2", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd2"), + true); + assertEquals("pwd2", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); assertEquals("newpwd", newRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); } public void testSetCredentialsAffectExistingRepository() throws Exception { - TaskRepository repository = new TaskRepository("kind", "http://url"); - repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); - assertEquals("pwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + taskRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd"), true); + assertEquals("pwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); TaskRepository newRepository = new TaskRepository("kind", "http://url"); newRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("newuser", "newpwd"), true); - assertEquals("newpwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + assertEquals("newpwd", taskRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); assertEquals("newpwd", newRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); } diff --git a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryManagerTest.java b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryManagerTest.java index e1a9d8284..fa176c870 100644 --- a/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryManagerTest.java +++ b/org.eclipse.mylyn.tasks.tests/src/org/eclipse/mylyn/tasks/tests/TaskRepositoryManagerTest.java @@ -12,23 +12,19 @@ package org.eclipse.mylyn.tasks.tests; import java.net.MalformedURLException; -import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; -import java.util.Map; import junit.framework.TestCase; -import org.eclipse.core.runtime.Platform; import org.eclipse.equinox.security.storage.EncodingUtils; import org.eclipse.equinox.security.storage.ISecurePreferences; import org.eclipse.equinox.security.storage.SecurePreferencesFactory; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; import org.eclipse.mylyn.commons.net.AuthenticationType; import org.eclipse.mylyn.internal.tasks.core.AbstractTask; -import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants; import org.eclipse.mylyn.internal.tasks.core.LocalRepositoryConnector; import org.eclipse.mylyn.internal.tasks.core.RepositoryTaskHandleUtil; import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager; @@ -62,6 +58,8 @@ public class TaskRepositoryManagerTest extends TestCase { private final String AUTH_USERNAME = AUTH_REPOSITORY + USERNAME; + private static final String SECURE_CREDENTIALS_STORE_NODE_ID = "org.eclipse.mylyn.commons.repository"; //$NON-NLS-1$ + private final String AUTH_HTTP = "org.eclipse.mylyn.tasklist.repositories.httpauth"; //$NON-NLS-1$ private final String AUTH_HTTP_PASSWORD = AUTH_HTTP + PASSWORD; @@ -90,24 +88,24 @@ public class TaskRepositoryManagerTest extends TestCase { } } - @SuppressWarnings("deprecation") public void testsUseSecureStorage() throws Exception { TaskRepository repository = new TaskRepository("bugzilla", "http://repository2/"); - repository.setProperty(ITasksCoreConstants.PROPERTY_USE_SECURE_STORAGE, "true"); repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("testUserName", "testPassword"), true); - ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault() - .node(ITasksCoreConstants.ID_PLUGIN); + repository.setCredentials(AuthenticationType.HTTP, + new AuthenticationCredentials("httpUserName", "httpPassword"), true); + + ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault().node( + SECURE_CREDENTIALS_STORE_NODE_ID); securePreferences = securePreferences.node(EncodingUtils.encodeSlashes(repository.getUrl())); assertEquals("testPassword", securePreferences.get(AUTH_PASSWORD, null)); - assertEquals("testUserName", repository.getProperty(AUTH_USERNAME)); - assertEquals("shouldbenull", securePreferences.get(AUTH_USERNAME, "shouldbenull")); - assertNull(Platform.getAuthorizationInfo(new URL(repository.getUrl()), AUTH_REALM, AUTH_SCHEME)); + assertNull(securePreferences.get(AUTH_USERNAME, null)); + assertEquals("httpUserName", securePreferences.get(AUTH_HTTP_USERNAME, null)); + assertEquals("httpPassword", securePreferences.get(AUTH_HTTP_PASSWORD, null)); } - @SuppressWarnings("deprecation") - public void testsUseKeyring() throws Exception { + public void testsSaveCredentials() throws Exception { TaskRepository repository = new TaskRepository("bugzilla", "http://repository3/"); repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("testUserName", "testPassword"), true); @@ -115,16 +113,10 @@ public class TaskRepositoryManagerTest extends TestCase { repository.setCredentials(AuthenticationType.HTTP, new AuthenticationCredentials("httpUserName", "httpPassword"), true); - ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault() - .node(ITasksCoreConstants.ID_PLUGIN); - securePreferences = securePreferences.node(EncodingUtils.encodeSlashes(repository.getUrl())); - assertNull(securePreferences.get(AUTH_PASSWORD, null)); - assertNull("testUserName", repository.getProperty(AUTH_USERNAME)); - Map<?, ?> map = Platform.getAuthorizationInfo(new URL(repository.getUrl()), AUTH_REALM, AUTH_SCHEME); - assertEquals("testUserName", map.get(AUTH_USERNAME)); - assertEquals("testPassword", map.get(AUTH_PASSWORD)); - assertEquals("httpUserName", map.get(AUTH_HTTP_USERNAME)); - assertEquals("httpPassword", map.get(AUTH_HTTP_PASSWORD)); + assertEquals("testUserName", repository.getCredentials(AuthenticationType.REPOSITORY).getUserName()); + assertEquals("testPassword", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + assertEquals("httpUserName", repository.getCredentials(AuthenticationType.HTTP).getUserName()); + assertEquals("httpPassword", repository.getCredentials(AuthenticationType.HTTP).getPassword()); } // FIXME 3.5 re-enable test |