From e6c82e200335eeb9b8051c288803a8b09be73d53 Mon Sep 17 00:00:00 2001 From: watsonian Date: Fri, 19 Apr 2013 16:27:09 -0700 Subject: [PATCH 1/4] Stop using deprecated API tokens for Enterprise. Authentication by API token is deprecated and doesn't work anymore. Instead, authentication should be done via OAuth token now. --- src/main/java/org/kohsuke/github/GitHub.java | 4 ++-- src/test/java/org/kohsuke/github/GitHubTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 6c7999492b..f43873c56a 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -132,8 +132,8 @@ public static GitHub connect() throws IOException { * "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has /api/v3 in the URL. * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. */ - public static GitHub connectToEnterprise(String apiUrl, String login, String apiToken) { - return new GitHub(apiUrl,login,apiToken,null); + public static GitHub connectToEnterprise(String apiUrl, String accessToken) { + return connectUsingOAuth(apiUrl, accessToken); } public static GitHub connect(String login, String apiToken){ diff --git a/src/test/java/org/kohsuke/github/GitHubTest.java b/src/test/java/org/kohsuke/github/GitHubTest.java index 8fbca796b3..4b0d9fa29f 100644 --- a/src/test/java/org/kohsuke/github/GitHubTest.java +++ b/src/test/java/org/kohsuke/github/GitHubTest.java @@ -8,12 +8,12 @@ public class GitHubTest extends TestCase { public void testGitHubServerWithHttp() throws Exception { - GitHub hub = GitHub.connectToEnterprise("http://enterprise.kohsuke.org/api/v3", "kohsuke", "token"); + GitHub hub = GitHub.connectToEnterprise("http://enterprise.kohsuke.org/api/v3", "token"); assertEquals("http://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } public void testGitHubServerWithHttps() throws Exception { - GitHub hub = GitHub.connectToEnterprise("https://enterprise.kohsuke.org/api/v3", "kohsuke", "token"); + GitHub hub = GitHub.connectToEnterprise("https://enterprise.kohsuke.org/api/v3", "token"); assertEquals("https://enterprise.kohsuke.org/api/v3/test", hub.getApiURL("/test").toString()); } From 8442e7e326a0c57a4611b25687659fd630622a94 Mon Sep 17 00:00:00 2001 From: watsonian Date: Fri, 19 Apr 2013 17:10:29 -0700 Subject: [PATCH 2/4] Declare the exception type. --- src/main/java/org/kohsuke/github/GitHub.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index f43873c56a..c151e790fe 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -132,7 +132,7 @@ public static GitHub connect() throws IOException { * "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has /api/v3 in the URL. * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. */ - public static GitHub connectToEnterprise(String apiUrl, String accessToken) { + public static GitHub connectToEnterprise(String apiUrl, String accessToken) throws IOException { return connectUsingOAuth(apiUrl, accessToken); } From e38eeae533f627c1a448466819fdfb7ad91082d9 Mon Sep 17 00:00:00 2001 From: watsonian Date: Sat, 20 Apr 2013 11:43:54 -0700 Subject: [PATCH 3/4] Update constructor to use OAuth tokens rather than API tokens. --- src/main/java/org/kohsuke/github/GitHub.java | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index c151e790fe..442037f4ba 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -66,7 +66,7 @@ public class GitHub { private final Map users = new HashMap(); private final Map orgs = new HashMap(); /*package*/ String oauthAccessToken; - + private final String apiUrl; private GitHub(String login, String apiToken, String password) { @@ -81,14 +81,14 @@ private GitHub(String login, String apiToken, String password) { * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. * Password is also considered deprecated as it is no longer required for api usage. */ - private GitHub(String apiUrl, String login, String apiToken, String password) { + private GitHub(String apiUrl, String login, String oauthAccessToken, String password) { if (apiUrl.endsWith("/")) apiUrl = apiUrl.substring(0, apiUrl.length()-1); // normalize this.apiUrl = apiUrl; - this.login = login; - this.apiToken = apiToken; + this.login = login; + this.oauthAccessToken = oauthAccessToken; - if (apiToken!=null || password!=null) { - String authorization = password==null ? (login + "/token" + ":" + apiToken) : (login + ':'+password); + if (password!=null) { + String authorization = (login + ':' + password); encodedAuthorization = new String(Base64.encodeBase64(authorization.getBytes())); } else encodedAuthorization = null; @@ -97,14 +97,14 @@ private GitHub(String apiUrl, String login, String apiToken, String password) { private GitHub (String apiUrl, String oauthAccessToken) throws IOException { if (apiUrl.endsWith("/")) apiUrl = apiUrl.substring(0, apiUrl.length()-1); // normalize this.apiUrl = apiUrl; - this.encodedAuthorization = null; - - this.oauthAccessToken = oauthAccessToken; + this.encodedAuthorization = null; + + this.oauthAccessToken = oauthAccessToken; this.apiToken = oauthAccessToken; - - this.login = getMyself().getLogin(); + + this.login = getMyself().getLogin(); } - + /** * Obtains the credential from "~/.github" */ @@ -147,7 +147,7 @@ public static GitHub connect(String login, String apiToken, String password){ public static GitHub connectUsingOAuth (String accessToken) throws IOException { return connectUsingOAuth("github.com", accessToken); } - + public static GitHub connectUsingOAuth (String githubServer, String accessToken) throws IOException { return new GitHub(githubServer, accessToken); } @@ -292,7 +292,7 @@ public T parseEventPayload(Reader r, Class type) t t.wrapUp(this); return t; } - + /** * Creates a new repository. * From da1405a060d968ed140a2e551e84bc59c2ec0585 Mon Sep 17 00:00:00 2001 From: watsonian Date: Sat, 20 Apr 2013 11:58:52 -0700 Subject: [PATCH 4/4] Remove apiToken completely. --- src/main/java/org/kohsuke/github/GitHub.java | 40 ++++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 442037f4ba..430f998fd5 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -61,17 +61,16 @@ public class GitHub { /*package*/ final String encodedAuthorization; - /*package*/ final String apiToken; private final Map users = new HashMap(); private final Map orgs = new HashMap(); - /*package*/ String oauthAccessToken; + /*package*/ String oauthAccessToken; - private final String apiUrl; + private final String apiUrl; - private GitHub(String login, String apiToken, String password) { - this (GITHUB_URL, login, apiToken, password); - } + private GitHub(String login, String oauthAccessToken, String password) { + this (GITHUB_URL, login, oauthAccessToken, password); + } /** * @@ -100,7 +99,6 @@ private GitHub (String apiUrl, String oauthAccessToken) throws IOException { this.encodedAuthorization = null; this.oauthAccessToken = oauthAccessToken; - this.apiToken = oauthAccessToken; this.login = getMyself().getLogin(); } @@ -132,24 +130,24 @@ public static GitHub connect() throws IOException { * "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has /api/v3 in the URL. * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. */ - public static GitHub connectToEnterprise(String apiUrl, String accessToken) throws IOException { - return connectUsingOAuth(apiUrl, accessToken); + public static GitHub connectToEnterprise(String apiUrl, String oauthAccessToken) throws IOException { + return connectUsingOAuth(apiUrl, oauthAccessToken); } - public static GitHub connect(String login, String apiToken){ - return new GitHub(login,apiToken,null); + public static GitHub connect(String login, String oauthAccessToken){ + return new GitHub(login,oauthAccessToken,null); } - public static GitHub connect(String login, String apiToken, String password){ - return new GitHub(login,apiToken,password); + public static GitHub connect(String login, String oauthAccessToken, String password){ + return new GitHub(login,oauthAccessToken,password); } - public static GitHub connectUsingOAuth (String accessToken) throws IOException { - return connectUsingOAuth("github.com", accessToken); + public static GitHub connectUsingOAuth (String oauthAccessToken) throws IOException { + return connectUsingOAuth("github.com", oauthAccessToken); } - public static GitHub connectUsingOAuth (String githubServer, String accessToken) throws IOException { - return new GitHub(githubServer, accessToken); + public static GitHub connectUsingOAuth (String githubServer, String oauthAccessToken) throws IOException { + return new GitHub(githubServer, oauthAccessToken); } /** * Connects to GitHub anonymously. @@ -166,10 +164,10 @@ public static GitHub connectAnonymously() { } /*package*/ URL getApiURL(String tailApiUrl) throws IOException { - if (oauthAccessToken != null) { - // append the access token - tailApiUrl = tailApiUrl + (tailApiUrl.indexOf('?')>=0 ?'&':'?') + "access_token=" + oauthAccessToken; - } + if (oauthAccessToken != null) { + // append the access token + tailApiUrl = tailApiUrl + (tailApiUrl.indexOf('?')>=0 ?'&':'?') + "access_token=" + oauthAccessToken; + } if (tailApiUrl.startsWith("/")) { if ("github.com".equals(apiUrl)) {// backward compatibility