diff --git a/pom.xml b/pom.xml index d450441f..1e543eff 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ org.jenkins-ci.plugins github-api - 1.34 + 1.28 diff --git a/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java b/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java index cb2aa396..e4b4ada7 100644 --- a/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java +++ b/src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java @@ -26,27 +26,18 @@ of this software and associated documentation files (the "Software"), to deal */ package org.jenkinsci.plugins; +import com.thoughtworks.xstream.converters.ConversionException; +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import hudson.Extension; import hudson.Util; import hudson.model.Descriptor; -import hudson.model.Fingerprint.RangeSet; import hudson.model.User; import hudson.security.GroupDetails; -import hudson.security.Permission; -import hudson.security.HudsonPrivateSecurityRealm.Details; import hudson.security.SecurityRealm; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.Map.Entry; -import java.util.logging.Logger; - import hudson.tasks.Mailer; import jenkins.model.Jenkins; import org.acegisecurity.Authentication; @@ -54,12 +45,9 @@ of this software and associated documentation files (the "Software"), to deal import org.acegisecurity.AuthenticationManager; import org.acegisecurity.BadCredentialsException; import org.acegisecurity.context.SecurityContextHolder; -import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; -import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken; import org.acegisecurity.userdetails.UserDetails; import org.acegisecurity.userdetails.UserDetailsService; import org.acegisecurity.userdetails.UsernameNotFoundException; -import org.apache.bcel.generic.ATHROW; import org.apache.http.HttpEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; @@ -67,7 +55,6 @@ of this software and associated documentation files (the "Software"), to deal import org.jfree.util.Log; import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHUser; -import org.kohsuke.github.GitHub; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.Header; import org.kohsuke.stapler.HttpRedirect; @@ -77,12 +64,10 @@ of this software and associated documentation files (the "Software"), to deal import org.springframework.dao.DataAccessException; import org.springframework.dao.DataRetrievalFailureException; -import com.thoughtworks.xstream.converters.ConversionException; -import com.thoughtworks.xstream.converters.Converter; -import com.thoughtworks.xstream.converters.MarshallingContext; -import com.thoughtworks.xstream.converters.UnmarshallingContext; -import com.thoughtworks.xstream.io.HierarchicalStreamReader; -import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Logger; /** * @@ -268,7 +253,7 @@ public HttpResponse doCommenceLogin(StaplerRequest request, @Header("Referer") f suffix = "&scope="+Util.join(scopes,","); } - return new HttpRedirect(githubUri + "/login/oauth/authorize?client_id=" + return new HttpRedirect(extractAuthenticationUrl(githubUri) + "/login/oauth/authorize?client_id=" + clientID + suffix); } @@ -286,9 +271,7 @@ public HttpResponse doFinishLogin(StaplerRequest request) return HttpResponses.redirectToContextRoot(); } - Log.info("test"); - - HttpPost httpost = new HttpPost(githubUri + HttpPost httpost = new HttpPost(extractAuthenticationUrl(githubUri) + "/login/oauth/access_token?" + "client_id=" + clientID + "&" + "client_secret=" + clientSecret + "&" + "code=" + code); @@ -309,8 +292,10 @@ public HttpResponse doFinishLogin(StaplerRequest request) if (accessToken != null && accessToken.trim().length() > 0) { + String githubServer = githubUri.replaceFirst("http.*\\/\\/", ""); + // only set the access token if it exists. - GithubAuthenticationToken auth = new GithubAuthenticationToken(accessToken,githubUri); + GithubAuthenticationToken auth = new GithubAuthenticationToken(accessToken,githubServer); SecurityContextHolder.getContext().setAuthentication(auth); GHUser self = auth.getGitHub().getMyself(); @@ -327,6 +312,16 @@ public HttpResponse doFinishLogin(StaplerRequest request) return HttpResponses.redirectToContextRoot(); // referer should be always there, but be defensive } + protected String extractAuthenticationUrl(String apiUrl) { + if (!apiUrl.equalsIgnoreCase(DEFAULT_URI)) { + int index = apiUrl.lastIndexOf("/api"); + if (index != -1) { + return apiUrl.substring(0, index); + } + } + return apiUrl; + } + private String extractToken(String content) { String parts[] = content.split("&"); diff --git a/src/test/java/org/jenkinsci/plugins/GithubSecurityRealmTest.java b/src/test/java/org/jenkinsci/plugins/GithubSecurityRealmTest.java new file mode 100644 index 00000000..5f591e30 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/GithubSecurityRealmTest.java @@ -0,0 +1,27 @@ +package org.jenkinsci.plugins; + +import junit.framework.TestCase; + +/** + * @author Johno Crawford (johno@hellface.com) + */ +public class GithubSecurityRealmTest extends TestCase { + + private GithubSecurityRealm realm; + + @Override + public void setUp() throws Exception { + super.setUp(); + realm = new GithubSecurityRealm(null, null, null); + } + + public void testGitHubServerUrl() throws Exception { + String authenticationUrl = realm.extractAuthenticationUrl("https://github.com"); + assertEquals("https://github.com", authenticationUrl); + } + + public void testEnterpriseServerUrl() throws Exception { + String authenticationUrl = realm.extractAuthenticationUrl("http://ghe.acme.com/api/v3/"); + assertEquals("http://ghe.acme.com", authenticationUrl); + } +}