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

GitHub Enterprise #1

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.34</version>
<version>1.28</version>
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this dependency version drop a necessary part of this change?

</dependency>

<dependency>
Expand Down
55 changes: 25 additions & 30 deletions src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,35 @@ 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;
import org.acegisecurity.AuthenticationException;
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;
import org.apache.http.util.EntityUtils;
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;
Expand All @@ -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;

/**
*
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);

Expand All @@ -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();
Expand All @@ -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("&");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jenkinsci.plugins;

import junit.framework.TestCase;

/**
* @author Johno Crawford ([email protected])
*/
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);
}
}