aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-03-25 20:29:21 -0400
committerKevin Sawicki2011-03-25 20:29:21 -0400
commit3491e763858c7191f35d4d31edf0ac9a7c485a6c (patch)
tree79b37c570f68b4574f7e12592f36881163754d61
parent8758fb2dd13ed6031ead593e4fc092c9c2e7f1eb (diff)
downloadegit-github-3491e763858c7191f35d4d31edf0ac9a7c485a6c.zip
egit-github-3491e763858c7191f35d4d31edf0ac9a7c485a6c.tar.gz
egit-github-3491e763858c7191f35d4d31edf0ac9a7c485a6c.tar.xz
Use Basic authentication in GitHubService for all requests.
This removes use of the deprecated POST data authentication option in the GitHub API and also fixes the issue where list and search requests would fail for private repositories. Bug: 340890 Change-Id: I9f9669a97385a827b09666339a31330928a37dfc Signed-off-by: Kevin Sawicki <kevin@github.com>
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubRepositoryConnector.java6
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java87
-rw-r--r--org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/GitHubServiceTest.java3
-rw-r--r--org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubRepositorySettingsPage.java11
4 files changed, 66 insertions, 41 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubRepositoryConnector.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubRepositoryConnector.java
index c263cb9..c86ffe0 100644
--- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubRepositoryConnector.java
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubRepositoryConnector.java
@@ -119,13 +119,13 @@ public class GitHubRepositoryConnector extends AbstractRepositoryConnector {
try {
String user = GitHub.computeTaskRepositoryUser(repository.getUrl());
String project = GitHub.computeTaskRepositoryProject(repository.getUrl());
+ GitHubCredentials credentials = GitHubCredentials.create(repository);
// perform query
for (String status: statuses) {
- GitHubIssues issues = service.searchIssues(user,project,
- status, query
- .getAttribute("queryText"));
+ GitHubIssues issues = service.searchIssues(user, project,
+ status, query.getAttribute("queryText"), credentials);
// collect task data
for (GitHubIssue issue : issues.getIssues()) {
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java
index 07d884d..2c83210 100644
--- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java
@@ -14,11 +14,15 @@ package org.eclipse.mylyn.github.internal;
import java.io.IOException;
+import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.auth.BasicScheme;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
@@ -60,6 +64,7 @@ public class GitHubService {
// private final static String COMMENT = "comment/";
private final static String ADD_LABEL = "label/add/"; // Implemented
private final static String REMOVE_LABEL = "label/remove/"; // Implemented
+ private final static String TOKEN = "/token";
private static final String EMAILS = "emails";
@@ -73,23 +78,48 @@ public class GitHubService {
}
/**
+ * Set credentials to use for authentication
+ *
+ * @param credentials
+ * @return this service
+ */
+ public GitHubService setCredentials(GitHubCredentials credentials) {
+ Credentials apiCredentials = new UsernamePasswordCredentials(
+ credentials.getUsername() + TOKEN, credentials.getApiToken());
+ this.httpClient.getState().setCredentials(AuthScope.ANY, apiCredentials);
+ return this;
+ }
+
+ /**
+ * Set method defaults
+ *
+ * @param method
+ * @return method param
+ */
+ protected HttpMethod setMethodDefaults(HttpMethod method) {
+ if (this.httpClient.getState().getCredentials(AuthScope.ANY) != null) {
+ method.setDoAuthentication(true);
+ method.getHostAuthState().setPreemptive();
+ method.getHostAuthState().setAuthScheme(new BasicScheme());
+ }
+ return method;
+ }
+
+ /**
* Verify that the provided credentials are correct
* @param credentials
*
* @return true if and only if the credentials are correct
*/
public boolean verifyCredentials(GitHubCredentials credentials) throws GitHubServiceException {
- PostMethod method = null;
+ GetMethod method = null;
boolean success = false;
try {
- method = new PostMethod(gitURLBase + gitUserRoot + EMAILS);
-
- // Set the users login and API token
- final NameValuePair login = new NameValuePair("login", credentials.getUsername());
- final NameValuePair token = new NameValuePair("token", credentials.getApiToken());
- method.setRequestBody(new NameValuePair[] { login, token });
+ method = new GetMethod(gitURLBase + gitUserRoot + EMAILS);
+
+ setCredentials(credentials);
executeMethod(method);
@@ -122,6 +152,8 @@ public class GitHubService {
* - The issue state you want to filter your search by
* @param searchTerm
* - The text search term to find in the issues.
+ * @param credentials
+ * GitHub credentials to use for authentication
*
* @return A GitHubIssues object containing all issues from the search
* results
@@ -131,7 +163,7 @@ public class GitHubService {
* @note API Doc: /issues/search/:user/:repo/:state/:search_term
*/
public GitHubIssues searchIssues(final String user, final String repo,
- final String state, final String searchTerm)
+ final String state, final String searchTerm, GitHubCredentials credentials)
throws GitHubServiceException {
GitHubIssues issues = null;
GetMethod method = null;
@@ -144,6 +176,7 @@ public class GitHubService {
method = new GetMethod(gitURLBase + gitIssueRoot + SEARCH
+ user + "/" + repo + "/" + state + "/" + searchTerm);
}
+ setCredentials(credentials);
// execute HTTP GET method
executeMethod(method);
// transform JSON to Java object
@@ -181,7 +214,6 @@ public class GitHubService {
* @throws GitHubServiceException
*
* @note API Doc: issues/label/add/:user/:repo/:label/:number API POST
- * Variables: login, api-token
*/
public boolean addLabel(final String user, final String repo,
final String label, final int issueNumber,final GitHubCredentials credentials)
@@ -196,10 +228,7 @@ public class GitHubService {
+ user + "/" + repo + "/" + label + "/"
+ Integer.toString(issueNumber));
- // Set the users login and API token
- final NameValuePair login = new NameValuePair("login", credentials.getUsername());
- final NameValuePair token = new NameValuePair("token", credentials.getApiToken());
- method.setRequestBody(new NameValuePair[] { login, token });
+ setCredentials(credentials);
// execute HTTP GET method
executeMethod(method);
@@ -242,7 +271,6 @@ public class GitHubService {
* @throws GitHubServiceException
*
* API Doc: issues/label/remove/:user/:repo/:label/:number API
- * POST Variables: login, api-token
*/
public boolean removeLabel(final String user, final String repo,
final String label, final int issueNumber, final GitHubCredentials credentials)
@@ -298,8 +326,8 @@ public class GitHubService {
*
* @throws GitHubServiceException
*
- * API Doc: issues/open/:user/:repo API POST Variables: login,
- * api-token, title, body
+ * API Doc: issues/open/:user/:repo
+ * API POST Variables: title, body
*/
public GitHubIssue openIssue(final String user, final String repo,
final GitHubIssue issue, final GitHubCredentials credentials)
@@ -312,16 +340,13 @@ public class GitHubService {
// Create the HTTP POST method
method = new PostMethod(gitURLBase + gitIssueRoot + OPEN + user
+ "/" + repo);
- // Set the users login and API token
- final NameValuePair login = new NameValuePair("login", credentials.getUsername());
- final NameValuePair token = new NameValuePair("token", credentials.getApiToken());
+ setCredentials(credentials);
final NameValuePair body = new NameValuePair("body", issue
.getBody());
final NameValuePair title = new NameValuePair("title", issue
.getTitle());
- method.setRequestBody(new NameValuePair[] { login, token, body,
- title });
+ method.setRequestBody(new NameValuePair[] { body, title });
executeMethod(method);
showIssue = gson.fromJson(new String(method.getResponseBody()),
@@ -367,7 +392,7 @@ public class GitHubService {
* @throws GitHubServiceException
*
* API Doc: issues/edit/:user/:repo/:number API POST Variables:
- * login, api-token, title, body
+ * title, body
*/
public GitHubIssue editIssue(final String user, final String repo,
final GitHubIssue issue,final GitHubCredentials credentials)
@@ -378,16 +403,14 @@ public class GitHubService {
// Create the HTTP POST method
method = new PostMethod(gitURLBase + gitIssueRoot + EDIT + user
+ "/" + repo + "/" + issue.getNumber());
- // Set the users login and API token
- final NameValuePair login = new NameValuePair("login", credentials.getUsername());
- final NameValuePair token = new NameValuePair("token", credentials.getApiToken());
+
+ setCredentials(credentials);
final NameValuePair body = new NameValuePair("body", issue
.getBody());
final NameValuePair title = new NameValuePair("title", issue
.getTitle());
- method.setRequestBody(new NameValuePair[] { login, token, body,
- title });
+ method.setRequestBody(new NameValuePair[] { body, title });
executeMethod(method);
GitHubShowIssue showIssue = gson.fromJson(method.getResponseBodyAsString(),
@@ -448,6 +471,7 @@ public class GitHubService {
private void executeMethod(HttpMethod method) throws GitHubServiceException {
int status;
try {
+ setMethodDefaults(method);
status = httpClient.executeMethod(method);
} catch (HttpException e) {
throw new GitHubServiceException(e);
@@ -480,7 +504,7 @@ public class GitHubService {
* @throws GitHubServiceException
*
* API Doc: issues/reopen/:user/:repo/:number API POST Variables:
- * login, api-token, title, body
+ * title, body
*/
public GitHubIssue reopenIssue(String user, String repo, GitHubIssue issue,
GitHubCredentials credentials) throws GitHubServiceException {
@@ -503,7 +527,7 @@ public class GitHubService {
* @throws GitHubServiceException
*
* API Doc: issues/close/:user/:repo/:number API POST Variables:
- * login, api-token, title, body
+ * title, body
*/
public GitHubIssue closeIssue(String user, String repo, GitHubIssue issue,
GitHubCredentials credentials) throws GitHubServiceException {
@@ -521,11 +545,8 @@ public class GitHubService {
// Create the HTTP POST method
method = new PostMethod(gitURLBase + gitIssueRoot + githubOperation + user
+ "/" + repo + "/" + issue.getNumber());
- // Set the users login and API token
- final NameValuePair login = new NameValuePair("login", credentials.getUsername());
- final NameValuePair token = new NameValuePair("token", credentials.getApiToken());
- method.setRequestBody(new NameValuePair[] { login, token });
+ setCredentials(credentials);
executeMethod(method);
GitHubShowIssue showIssue = gson.fromJson(method.getResponseBodyAsString(),
diff --git a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/GitHubServiceTest.java b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/GitHubServiceTest.java
index d05be84..9eabcfd 100644
--- a/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/GitHubServiceTest.java
+++ b/org.eclipse.mylyn.github.tests/src/org/eclipse/mylyn/github/tests/GitHubServiceTest.java
@@ -46,7 +46,8 @@ public class GitHubServiceTest {
public void searchIssues() throws Exception {
final GitHubService service = new GitHubService();
final GitHubIssues issues = service.searchIssues(TEST_USER,
- TEST_PROJECT, "open", "test");
+ TEST_PROJECT, "open", "test", new GitHubCredentials(TEST_USER,
+ API_KEY));
assertEquals(0, issues.getIssues().length);
}
diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubRepositorySettingsPage.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubRepositorySettingsPage.java
index 28cd3a5..d2b4187 100644
--- a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubRepositorySettingsPage.java
+++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/GitHubRepositorySettingsPage.java
@@ -108,20 +108,23 @@ public class GitHubRepositorySettingsPage extends
monitor.subTask("Contacting server...");
try {
- // verify the repo
- service.searchIssues(user, repo, new String("open"),"");
- monitor.worked(400);
-
// verify the credentials
if (auth == null) {
setStatus(GitHubUi.createErrorStatus("Credentials are required. Please specify username and API Token."));
return;
}
+ monitor.worked(250);
+
GitHubCredentials credentials = new GitHubCredentials(auth.getUserName(), auth.getPassword());
if (!service.verifyCredentials(credentials)) {
setStatus(GitHubUi.createErrorStatus("Invalid credentials. Please check your GitHub User ID and API Token.\nYou can find your API Token on your GitHub account settings page."));
return;
}
+ monitor.worked(250);
+
+ // verify the repo
+ service.searchIssues(user, repo, new String("open"),"", credentials);
+ monitor.worked(400);
} catch (GitHubServiceException e) {
setStatus(GitHubUi.createErrorStatus("Repository Test failed:"+ e.getMessage()));
return;