diff options
4 files changed, 80 insertions, 3 deletions
diff --git a/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaRepositorySettingsPage.java b/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaRepositorySettingsPage.java index eabb20130..c16408e83 100644 --- a/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaRepositorySettingsPage.java +++ b/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaRepositorySettingsPage.java @@ -697,6 +697,8 @@ public class BugzillaRepositorySettingsPage extends AbstractRepositorySettingsPa @Override public TaskRepository createTaskRepository() { TaskRepository repository = new TaskRepository(connector.getConnectorKind(), getRepositoryUrl()); + // do not modify the secure storage for a temporary repository + repository.setShouldPersistCredentials(false); applyToInternal(repository); return repository; } 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 3cc5f42a8..4dc4d7995 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 @@ -233,6 +233,8 @@ public final class TaskRepository extends PlatformObject { private transient volatile boolean updating; + private boolean shouldPersistCredentials = true; + public TaskRepository(String connectorKind, String repositoryUrl) { this(connectorKind, repositoryUrl, NO_VERSION_SPECIFIED); } @@ -284,7 +286,7 @@ public final class TaskRepository extends PlatformObject { } private void addAuthInfo(String username, String password, String userProperty, String passwordProperty) { - if (Platform.isRunning()) { + if (Platform.isRunning() && shouldPersistCredentials()) { if (useSecureStorage()) { try { ISecurePreferences securePreferences = getSecurePreferences(); @@ -387,7 +389,7 @@ public final class TaskRepository extends PlatformObject { } synchronized (LOCK) { - if (Platform.isRunning()) { + if (Platform.isRunning() && shouldPersistCredentials()) { if (useSecureStorage()) { if (Platform.isRunning()) { ISecurePreferences securePreferences = getSecurePreferences(); @@ -431,7 +433,7 @@ public final class TaskRepository extends PlatformObject { @SuppressWarnings("unchecked") private String getAuthInfo(String property) { - if (Platform.isRunning()) { + if (Platform.isRunning() && shouldPersistCredentials()) { if (useSecureStorage()) { String propertyValue = null; if (property.equals(getKeyPrefix(AuthenticationType.REPOSITORY) + USERNAME)) { @@ -481,6 +483,29 @@ public final class TaskRepository extends PlatformObject { } } + /** + * Returns {@code} if credentials persisted in the platform keystore. + * + * @since 3.10 + * @see #setShouldPersistCredentials(boolean) + */ + public boolean shouldPersistCredentials() { + return shouldPersistCredentials; + } + + /** + * Toggles the flag for persisting credentials. If {@code shouldPersistCredentials} is {@code false} credentials + * 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() + */ + public void setShouldPersistCredentials(boolean shouldPersistCredentials) { + this.shouldPersistCredentials = shouldPersistCredentials; + } + public String getCharacterEncoding() { final String encoding = properties.get(IRepositoryConstants.PROPERTY_ENCODING); return encoding == null || "".equals(encoding) ? DEFAULT_CHARACTER_ENCODING : encoding; //$NON-NLS-1$ 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 dcec4ed99..554498487 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 @@ -159,4 +159,46 @@ public class TaskRepositoryCredentialsTest extends TestCase { assertEquals("Time stamp set", stamp.getTime(), taskRepository.getConfigurationDate().getTime()); } + 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()); + + repository.setShouldPersistCredentials(false); + repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "newpwd"), true); + assertEquals("newpwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + + repository.setShouldPersistCredentials(true); + assertEquals("pwd", repository.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 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("newpwd", newRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + + repository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("user", "pwd2"), true); + assertEquals("pwd2", repository.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 newRepository = new TaskRepository("kind", "http://url"); + newRepository.setCredentials(AuthenticationType.REPOSITORY, new AuthenticationCredentials("newuser", "newpwd"), + true); + assertEquals("newpwd", repository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + assertEquals("newpwd", newRepository.getCredentials(AuthenticationType.REPOSITORY).getPassword()); + } + } diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/wizards/AbstractRepositorySettingsPage.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/wizards/AbstractRepositorySettingsPage.java index 77f824855..fccefa236 100644 --- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/wizards/AbstractRepositorySettingsPage.java +++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/wizards/AbstractRepositorySettingsPage.java @@ -1672,10 +1672,18 @@ public abstract class AbstractRepositorySettingsPage extends AbstractTaskReposit } /** + * Creates a {@link TaskRepository} based on the current settings. + * <p> + * Note: The credentials of the created repository are not persisted in the platform keystore. When overriding, + * subclasses must either call super or call {@link TaskRepository#setShouldPersistCredentials(boolean) + * setShouldPersistCredentials(false)} before calling {@link #applyTo(TaskRepository)}. + * * @since 2.0 */ public TaskRepository createTaskRepository() { TaskRepository repository = new TaskRepository(connector.getConnectorKind(), getRepositoryUrl()); + // do not modify the secure storage for a temporary repository + repository.setShouldPersistCredentials(false); applyTo(repository); return repository; } |