diff --git a/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java b/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java index edddb068..09948eaa 100644 --- a/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java +++ b/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java @@ -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; @@ -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); @@ -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("&");