unified the handling of the password and API token.

This commit is contained in:
Kohsuke Kawaguchi
2010-12-09 16:04:17 -08:00
parent c6558c6527
commit e413912edd
2 changed files with 15 additions and 8 deletions

View File

@@ -51,16 +51,22 @@ import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.*;
*/
public class GitHub {
/*package*/ final String login;
/*package*/ final String token;
/*package*/ final String password;
/*package*/ final String encodedAuthorization;
final String password;
private final Map<String,GHUser> users = new HashMap<String, GHUser>();
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
private GitHub(String login, String apiToken, String password) {
this.login = login;
this.token = apiToken;
this.password = password;
BASE64Encoder enc = new sun.misc.BASE64Encoder();
if (apiToken!=null || password!=null) {
String userpassword = apiToken!=null ? (login + "/token" + ":" + apiToken) : (login + ':'+password);
encodedAuthorization = enc.encode(userpassword.getBytes());
} else
encodedAuthorization = null;
}
/**
@@ -96,7 +102,7 @@ public class GitHub {
}
/*package*/ void requireCredential() {
if (login ==null || token ==null)
if (login==null || encodedAuthorization==null)
throw new IllegalStateException("This operation requires a credential but none is given to the GitHub constructor");
}
@@ -114,9 +120,6 @@ public class GitHub {
/*package*/ <T> T retrieveWithAuth(URL url, Class<T> type, String method) throws IOException {
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = login + "/token" + ":" + token;
String encodedAuthorization = enc.encode(userpassword.getBytes());
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
uc.setRequestMethod(method);

View File

@@ -45,6 +45,7 @@ import static org.kohsuke.github.GitHub.MAPPER;
class Poster {
private final GitHub root;
private final Map<String,String> args = new HashMap<String, String>();
private boolean authenticate;
Poster(GitHub root) {
this.root = root;
@@ -52,7 +53,8 @@ class Poster {
public Poster withCredential() {
root.requireCredential();
return with("login",root.login).with("token",root.token);
authenticate = true;
return this;
}
public Poster with(String key, int value) {
@@ -86,6 +88,8 @@ class Poster {
uc.setDoOutput(true);
uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");
if (authenticate)
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
uc.setRequestMethod(method);