From da88421f3737f215c9e7e247f6847f69ed363d09 Mon Sep 17 00:00:00 2001 From: Sam Davis Date: Mon, 14 Jan 2013 16:37:48 -0800 Subject: 278474: store TaskRepository credentials in SecureCredentialsStore Change-Id: I93daacac2280c009b98d02395d4a5989f5c0a16a Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=278474 --- org.eclipse.mylyn.tasks.tests/META-INF/MANIFEST.MF | 3 +- .../tasks/tests/TaskRepositoryCredentialsTest.java | 131 ++++++++++++++------- .../tasks/tests/TaskRepositoryManagerTest.java | 38 +++--- 3 files changed, 104 insertions(+), 68 deletions(-) (limited to 'org.eclipse.mylyn.tasks.tests') 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 -- cgit v1.2.3