Skip to content

Commit

Permalink
Add support for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamás Földesi committed Oct 4, 2013
1 parent 1683aa1 commit c6ad077
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ of this software and associated documentation files (the "Software"), to deal
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jfree.util.Log;
Expand Down Expand Up @@ -346,7 +349,11 @@ public HttpResponse doFinishLogin(StaplerRequest request)
+ "/login/oauth/access_token?" + "client_id=" + clientID + "&"
+ "client_secret=" + clientSecret + "&" + "code=" + code);

DefaultHttpClient httpclient = new DefaultHttpClient();
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = getProxy(githubWebUri);
if (proxy != null) {
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}

org.apache.http.HttpResponse response = httpclient.execute(httpost);

Expand Down Expand Up @@ -383,6 +390,29 @@ public HttpResponse doFinishLogin(StaplerRequest request)
return HttpResponses.redirectToContextRoot(); // referer should be always there, but be defensive
}

/**
* Returns the proxy to be used when connecting to the given URI.
*/
private HttpHost getProxy(String uri) {
if (uri == null) {
return null;
}

String prefix = uri.startsWith("https") ? "https" : "http";
String proxyHost = System.getProperty(prefix + ".proxyHost");
if (StringUtils.isBlank(proxyHost)) {
return null;
}

String proxyPortStr = System.getProperty(prefix + ".proxyPort");
if (StringUtils.isBlank(proxyPortStr)) {
return new HttpHost(proxyHost);
} else {
int proxyPort = Integer.parseInt(proxyPortStr);
return new HttpHost(proxyHost, proxyPort);
}
}

private String extractToken(String content) {

String parts[] = content.split("&");
Expand Down

0 comments on commit c6ad077

Please sign in to comment.