Skip to content

Commit

Permalink
Add token authentication option to base repository code #62
Browse files Browse the repository at this point in the history
  • Loading branch information
gnl42 authored and ruspl-afed committed Oct 19, 2023
1 parent f808790 commit d628ac0
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.egit.github.core.client.RequestException;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.TaskRepository;

/**
Expand Down Expand Up @@ -56,7 +57,11 @@ public class GitHub {
* Key for a repository property storing a stringified boolean ("true" or
* "false") telling whether to use token authentication for a Mylyn task
* repository.
*
* Kept to not break existing repository definitions. Replace by
* IRepositoryConstants.PROPERTY_USE_TOKEN
*/
@Deprecated(since = "4.1")
public static final String PROPERTY_USE_TOKEN = GitHub.class.getPackage()
.getName() + ".REPO_USE_TOKEN"; //$NON-NLS-1$

Expand All @@ -82,8 +87,8 @@ public static GitHubClient addCredentials(GitHubClient client,
AuthenticationCredentials credentials = repository
.getCredentials(AuthenticationType.REPOSITORY);
if (credentials != null) {
if (Boolean
.parseBoolean(repository.getProperty(PROPERTY_USE_TOKEN))) {
if (Boolean.parseBoolean(repository
.getProperty(IRepositoryConstants.PROPERTY_USE_TOKEN))) {
client.setOAuth2Token(credentials.getPassword());
} else {
client.setCredentials(credentials.getUserName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.core.GitHubTaskDataHandler;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
Expand Down Expand Up @@ -239,8 +240,8 @@ public RepositoryResponse postTaskData(TaskRepository repository,
AuthenticationCredentials credentials = repository
.getCredentials(AuthenticationType.REPOSITORY);
if (credentials != null) {
if (Boolean.parseBoolean(
repository.getProperty(GitHub.PROPERTY_USE_TOKEN))) {
if (Boolean.parseBoolean(repository.getProperty(IRepositoryConstants.PROPERTY_USE_TOKEN))
|| Boolean.parseBoolean(repository.getProperty(GitHub.PROPERTY_USE_TOKEN))) {
client.setOAuth2Token(credentials.getPassword());
} else {
client.setCredentials(credentials.getUserName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.core.QueryUtils;
import org.eclipse.mylyn.internal.github.core.RepositoryConnector;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static TaskRepository createTaskRepository(Repository repo,
new AuthenticationCredentials(loginName, password), true);
}
repository.setCategory(TaskRepository.CATEGORY_BUGS);
repository.setProperty(GitHub.PROPERTY_USE_TOKEN,
repository.setProperty(IRepositoryConstants.PROPERTY_USE_TOKEN,
Boolean.toString(isToken));
return repository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.mylyn.internal.github.core.QueryUtils;
import org.eclipse.mylyn.internal.github.core.RepositoryConnector;
import org.eclipse.mylyn.internal.github.core.issue.IssueConnector;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
Expand Down Expand Up @@ -95,7 +96,7 @@ public static TaskRepository createTaskRepository(Repository repo,
repository.setCredentials(AuthenticationType.REPOSITORY,
new AuthenticationCredentials(loginName, password), true);
repository.setCategory(TaskRepository.CATEGORY_REVIEW);
repository.setProperty(GitHub.PROPERTY_USE_TOKEN,
repository.setProperty(IRepositoryConstants.PROPERTY_USE_TOKEN,
Boolean.toString(isToken));
return repository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@

import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;

/**
* Base HTTP-based task repository settings page
Expand All @@ -38,10 +33,6 @@ public abstract class HttpRepositorySettingsPage extends

private boolean editingUrl = false;

private boolean needsUser = true;

private Button useToken;

/**
* Create repository settings page
*
Expand Down Expand Up @@ -128,118 +119,65 @@ protected void setInitialUrl(Function<RepositoryId, String> labelProvider) {
}

/**
* Inserts a checkbox into the page where the user can specify that token
* authentication shall be used for the task repository.
* Should the 'Use Token' check box be "checked"
*
* @param userOptional
* whether or not a user name is optional
* Here to not break existing repository definitions
*
* @param taskRepository
*/
protected void addTokenCheckbox(boolean userOptional) {
needsUser = !userOptional;
useToken = new Button(compositeContainer, SWT.CHECK);
useToken.setText(Messages.HttpRepositorySettingsPage_LabelUseToken);
useToken.setToolTipText(
Messages.HttpRepositorySettingsPage_TooltipUseToken);
useToken.moveBelow(savePasswordButton);
GridDataFactory.defaultsFor(useToken).span(3, 1).applyTo(useToken);
String savePasswordText = savePasswordButton.getText();
boolean[] allowAnon = { isAnonymousAccess() };
SelectionAdapter listener = new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
boolean isChecked = useToken.getSelection();
if (isChecked) {
repositoryPasswordEditor.setLabelText(
Messages.HttpRepositorySettingsPage_LabelToken);
savePasswordButton.setText(
Messages.HttpRepositorySettingsPage_LabelSaveToken);
if (anonymousButton != null) {
allowAnon[0] = isAnonymousAccess();
setAnonymous(false);
anonymousButton.setEnabled(false);
}
} else {
repositoryPasswordEditor.setLabelText(LABEL_PASSWORD);
savePasswordButton.setText(savePasswordText);
if (anonymousButton != null) {
anonymousButton.setEnabled(true);
setAnonymous(allowAnon[0]);
}
}
if (userOptional) {
repositoryUserNameEditor.getTextControl(compositeContainer)
.setEnabled(!isChecked);
repositoryUserNameEditor.setEmptyStringAllowed(isChecked);
}
repositoryPasswordEditor.getLabelControl(compositeContainer)
.requestLayout();
// Trigger page validation if needed
if (userOptional && getWizard() != null) {
getWizard().getContainer().updateButtons();
}
}
@Override
protected boolean useTokenChecked(TaskRepository taskRepository) {
return super.useTokenChecked(taskRepository) || //
Boolean.parseBoolean(
taskRepository.getProperty(GitHub.PROPERTY_USE_TOKEN));
}

@Override
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
};
useToken.addSelectionListener(listener);
TaskRepository taskRepository = getRepository();
if (taskRepository != null) {
useToken.setSelection(Boolean.parseBoolean(
taskRepository.getProperty(GitHub.PROPERTY_USE_TOKEN)));
// setSelection does not fire a selection event
listener.widgetSelected(null);
}
/**
* @since 4.1
*/
@Override
protected String getSettingsPageEnterTokenText() {
return Messages.HttpRepositorySettingsPage_EnterToken;
}

/**
* Tells whether the task repository uses token authentication.
*
* @return {@code true} if token authentication shall be used; {@code false}
* otherwise
* @since 4.1
*/
protected boolean useTokenAuth() {
return useToken != null && useToken.getSelection();
@Override
protected String getSettingsPageEnterUserAndTokenText() {
return Messages.HttpRepositorySettingsPage_EnterUserAndToken;
}

/**
* @since 4.1
*/
@Override
protected boolean isMissingCredentials() {
if (!needsUser && useTokenAuth()) {
return repositoryPasswordEditor.getStringValue().trim().isEmpty();
} else {
return super.isMissingCredentials();
}
protected String getSettingsPageGetUseLabelUseTokenText() {
return Messages.HttpRepositorySettingsPage_LabelUseToken;
}

@SuppressWarnings("restriction")
/**
* @since 4.1
*/
@Override
public void setMessage(String newMessage, int newType) {
// This is a bit hacky since it relies on an internal message and the
// way it is used in the super class. But it beats re-implementing
// isPageComplete().
if (useTokenAuth()
&& org.eclipse.mylyn.internal.tasks.ui.wizards.Messages.AbstractRepositorySettingsPage_Enter_a_user_id_Message0
.equals(newMessage)) {
if (needsUser) {
super.setMessage(
Messages.HttpRepositorySettingsPage_EnterUserAndToken,
newType);
} else {
super.setMessage(Messages.HttpRepositorySettingsPage_EnterToken,
newType);
}
} else {
super.setMessage(newMessage, newType);
}
protected String getSettingsPageTooltipUseTokenText() {
return Messages.HttpRepositorySettingsPage_TooltipUseToken;
}

/**
* @since 4.1
*/
@Override
public void applyTo(TaskRepository taskRepository) {
taskRepository.setProperty(GitHub.PROPERTY_USE_TOKEN,
Boolean.toString(useToken != null && useToken.getSelection()));
super.applyTo(taskRepository);
protected String getSettingsPageLabelTokenText() {
return Messages.HttpRepositorySettingsPage_LabelToken;
}

/**
* @since 4.1
*/
@Override
protected String getSettingsPageLabelSaveTokenText() {
return Messages.HttpRepositorySettingsPage_LabelSaveToken;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.core.gist.GistConnector;
import org.eclipse.mylyn.internal.github.core.issue.IssueConnector;
import org.eclipse.mylyn.internal.github.core.pr.PullRequestConnector;
import org.eclipse.mylyn.internal.github.ui.gist.GistRepositorySettingsPage;
import org.eclipse.mylyn.internal.github.ui.gist.Messages;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUi;
Expand Down Expand Up @@ -113,7 +113,7 @@ public boolean performFinish() {
repository.setCredentials(AuthenticationType.REPOSITORY,
credentials, true);
repository.setCategory(TaskRepository.CATEGORY_REVIEW);
repository.setProperty(GitHub.PROPERTY_USE_TOKEN,
repository.setProperty(IRepositoryConstants.PROPERTY_USE_TOKEN,
Boolean.toString(isToken));
manager.addRepository(repository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public GistRepositorySettingsPage(TaskRepository taskRepository) {
super(Messages.GistRepositorySettingsPage_Title,
Messages.GistRepositorySettingsPage_Description, taskRepository);
setNeedsAnonymousLogin(false);
setUseToken(true, false);
}

@Override
Expand All @@ -60,7 +61,6 @@ protected void createAdditionalControls(Composite parent) {
.setStringValue(Messages.GistRepositorySettingsPage_RepositoryLabelDefault);
}
// For gists we still need a user name.
addTokenCheckbox(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public IssueRepositorySettingsPage(final TaskRepository taskRepository) {
super(Messages.IssueRepositorySettingsPage_Title,
Messages.IssueRepositorySettingsPage_Description,
taskRepository);
setUseToken(true, true);

}

@Override
Expand All @@ -59,7 +61,6 @@ protected void createAdditionalControls(Composite parent) {
if (getRepository() == null) {
setAnonymous(false);
}
addTokenCheckbox(true);
// Set the URL now, because serverURL is definitely instantiated.
if (serverUrlCombo.getText().isEmpty()) {
setInitialUrl(IssueConnector::getRepositoryLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.github.core.GitHub;
import org.eclipse.mylyn.internal.github.core.issue.IssueAttribute;
import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPage;
Expand Down Expand Up @@ -104,8 +105,8 @@ public void doSubmit() {
private boolean checkCanSubmit(final int type) {
final TaskRepository taskRepository = getModel().getTaskRepository();
AuthenticationCredentials cred = taskRepository.getCredentials(AuthenticationType.REPOSITORY);
boolean isToken = Boolean.parseBoolean(
taskRepository.getProperty(GitHub.PROPERTY_USE_TOKEN));
boolean isToken = Boolean.parseBoolean(taskRepository.getProperty(IRepositoryConstants.PROPERTY_USE_TOKEN))
|| Boolean.parseBoolean(taskRepository.getProperty(GitHub.PROPERTY_USE_TOKEN));
boolean noUser = !isToken && cred != null
&& StringUtils.isEmpty(cred.getUserName());
if (cred == null || noUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public PullRequestRepositorySettingsPage(final TaskRepository taskRepository) {
super(Messages.PullRequestRepositorySettingsPage_Title,
Messages.PullRequestRepositorySettingsPage_Description,
taskRepository);
setUseToken(true, true);

}

@Override
Expand All @@ -66,7 +68,6 @@ protected void createAdditionalControls(Composite parent) {
if (getRepository() == null) {
setAnonymous(false);
}
addTokenCheckbox(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2004, 2013 Tasktop Technologies and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0
*
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand Down Expand Up @@ -71,4 +71,11 @@ public interface IRepositoryConstants {

public static final String TEMPLATE_VALUE_PREFIX = "template.value.prefix."; //$NON-NLS-1$

/**
* Key for a repository property storing a stringified boolean ("true" or "false") telling whether to use token
* authentication for a Mylyn task repository.
*/
public static final String PROPERTY_USE_TOKEN = IRepositoryConstants.class.getPackage().getName()
+ ".REPO_USE_TOKEN"; //$NON-NLS-1$

}
Loading

0 comments on commit d628ac0

Please sign in to comment.