Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using deprecated API tokens for Enterprise #33

Merged
merged 4 commits into from
Apr 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 33 additions & 35 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ public class GitHub {


/*package*/ final String encodedAuthorization;
/*package*/ final String apiToken;

private final Map<String,GHUser> users = new HashMap<String, GHUser>();
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
/*package*/ String oauthAccessToken;

private final String apiUrl;
/*package*/ String oauthAccessToken;

private GitHub(String login, String apiToken, String password) {
this (GITHUB_URL, login, apiToken, password);
}
private final String apiUrl;

private GitHub(String login, String oauthAccessToken, String password) {
this (GITHUB_URL, login, oauthAccessToken, password);
}

/**
*
Expand All @@ -81,14 +80,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;
Expand All @@ -97,14 +96,13 @@ 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.apiToken = oauthAccessToken;

this.login = getMyself().getLogin();
this.encodedAuthorization = null;

this.oauthAccessToken = oauthAccessToken;

this.login = getMyself().getLogin();
}

/**
* Obtains the credential from "~/.github"
*/
Expand Down Expand Up @@ -132,24 +130,24 @@ public static GitHub connect() throws IOException {
* "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has <tt>/api/v3</tt> 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 oauthAccessToken) throws IOException {
Copy link

@iraleigh iraleigh Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this is late, because this has already been merged. There is a problem with removing the login parameter makes this connection fail. From my understanding the login and OAuth are associated with each other, an OAuth cannot be used to authenticate by itself.

I see the call to getMyself(), but I am confused about the source of truth for "myself". If you have not explicitly set "myself" with the login, how can you call getMyself() for the login.

@watsonian @kohsuke

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.
Expand All @@ -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
Expand Down Expand Up @@ -292,7 +290,7 @@ public <T extends GHEventPayload> T parseEventPayload(Reader r, Class<T> type) t
t.wrapUp(this);
return t;
}

/**
* Creates a new repository.
*
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down