Skip to content

Commit

Permalink
Merge pull request #100 from nathandunn/fix_build
Browse files Browse the repository at this point in the history
Fix build and added alternate authentication strategy.
Seth sez: well, it will burn down on Wednesday anyways...
  • Loading branch information
kltm authored Nov 28, 2016
2 parents 8423f8e + 4b0a9b5 commit d14458b
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 31 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea/**
*.swp
*.swo
target/**
.project
.classpath
*.iml
*.ipr
*.iws
out
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ Instances exist for the following ontologies:
* [ENVO TermGenie](http://envo.termgenie.org)

To get write access to any of these TG instances, contact the administrators of the ontology (list available from obofoundry.org)

#Updates to TermeGene
Need to install https://github.com/RitwikSaikia/jsonrpc/ locally and maven install the server code.

Add user here:

https://github.com/geneontology/go-site/blob/master/metadata/users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ protected void bind(String name, File value) {
}

/**
* Convenience method for binding a Map<IRI, String> parameter. Check system
* Convenience method for binding a Map&lt;IRI, String&gt; parameter. Check system
* properties for overwrites.
*
* @param name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum Modification {
}

/**
* Mark a new object of type <T> as added for the commit.
* Mark a new object of type &lt;T&gt; as added for the commit.
*
* @param <T> type
* @param object the object to be committed
Expand All @@ -29,7 +29,7 @@ public static <T> CommitObject<T> add(T object) {
}

/**
* Mark a new object of type <T> as modified for the commit.
* Mark a new object of type &lt;T&gt; as modified for the commit.
*
* @param <T> type
* @param object the object to be committed
Expand All @@ -40,7 +40,7 @@ public static <T> CommitObject<T> modify(T object) {
}

/**
* Mark a new object of type <T> as to be deleted for the commit.
* Mark a new object of type &lt;T&gt; as to be deleted for the commit.
*
* @param <T> type
* @param object the object to be committed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.3.8</version>
<version>1.8.14</version>
<!--<version>1.9.0-r10609-atlassian-hosted</version>-->
<exclusions>
<exclusion>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
</exclusion>
</exclusions>

</dependency>
<dependency>
<groupId>org.bbop.termgenie</groupId>
Expand Down
1 change: 0 additions & 1 deletion TermGenie/WebApplications/TermGenieJQuery/.gitignore

This file was deleted.

22 changes: 5 additions & 17 deletions TermGenie/WebApplications/TermGenieJQuery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@

<name>TermGenieJQuery</name>

<repositories>
<repository>
<id>json-rpc</id>
<url>http://json-rpc.googlecode.com/svn/mavenrepo</url>
</repository>
</repositories>

<dependencies>
<dependency>
Expand All @@ -30,17 +24,11 @@
<artifactId>TermGenieRulesDynamic</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.json.rpc</groupId>
<artifactId>jsonrpc</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.json.rpc</groupId>
<artifactId>jsonrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.bbop.termgenie</groupId>
<artifactId>TermGenieOntologyCommit-Core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class BrowserIdHandlerImpl implements BrowserIdHandler {
public BrowserIdHandlerImpl(@Named("BrowserIdVerificationUrl") String browserIdVerificationUrl,
@Named("TermGenieBrowserIdAudience") String termgenieBrowserIdAudience,
InternalSessionHandler sessionHandler,
UserDataProvider userDataProvider)
UserDataProvider userDataProvider
)
{
super();
this.browserIdVerificationUrl = browserIdVerificationUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.bbop.termgenie.services.info;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* Created by nathandunn on 11/22/16.
*/
public class ConfigurationHandler {

private static ConfigurationHandler configurationHandler ;
private Properties properties = new Properties();

private ConfigurationHandler(){
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("termgenie.properties");
try {
properties.load(input);
} catch (IOException e) {
e.printStackTrace();
}
}

public static ConfigurationHandler getConfigurationHandler() {
if(configurationHandler==null) configurationHandler = new ConfigurationHandler();
return configurationHandler;
}

public String getValue(String key){
return properties.getProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protected final class TermGenieServletModule extends ServletModule {
protected void configureServlets() {
serve("/jsonrpc").with(TermGenieJsonRPCServlet.class);
serve("/termlookup").with(TermLookupServlet.class);
serve("/gh-request").with(GHAuthenticationRequestServlet.class);
serve("/gh-access").with(GHAuthenticationAccessServlet.class); // configured in GH
}

@Provides
Expand Down Expand Up @@ -114,6 +116,7 @@ protected final synchronized Injector getInjector() {
private IOCModule[] getConfiguration() {
List<IOCModule> modules = new ArrayList<IOCModule>();
modules.add(new ManagementServiceModule(applicationProperties));
modules.add(new GHAuthenticationModule(applicationProperties));
add(modules, getServiceModule(), true, "ServiceModule");
add(modules, getAuthenticationModule(), true, "Authentication");
add(modules, getUserPermissionModule(), true, "UserPermission");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.bbop.termgenie.servlets;

import com.google.gson.Gson;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.bbop.termgenie.services.InternalSessionHandler;
import org.bbop.termgenie.services.info.ConfigurationHandler;
import org.bbop.termgenie.user.UserData;
import org.bbop.termgenie.user.UserDataProvider;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
* https://developer.github.com/v3/oauth/#2-github-redirects-back-to-your-site
*/
@Singleton
public class GHAuthenticationAccessServlet extends HttpServlet {

private static final Logger logger = Logger.getLogger(GHAuthenticationAccessServlet.class);

// generated
private static final long serialVersionUID = 4604786454943166862L;

private final Gson gson;
private final UserDataProvider userDataProvider;
private final InternalSessionHandler sessionHandler;
private final String clientId ;
private final String clientSecret ;

@Inject
public GHAuthenticationAccessServlet(
InternalSessionHandler sessionHandler,
UserDataProvider userDataProvider,
@Named("github_client_id")
String clientId,
@Named("github_client_secret")
String clientSecret
) {
super();
// this.lookupService = lookupService;
this.gson = new Gson();
this.userDataProvider = userDataProvider;
this.sessionHandler = sessionHandler;
this.clientId = clientId ;
this.clientSecret = clientSecret;
}



@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String code = req.getParameter("code");

String accessToken = getAccessToken(code);

HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet("https://api.github.com/user?access_token="+accessToken);
HttpResponse response = httpClient.execute(getRequest);

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));

StringBuilder result = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
GHUserResponse ghUserResponse = gson.fromJson(result.toString(), GHUserResponse.class);

boolean isAuthenticated = ghUserResponse.email!=null;

if(isAuthenticated){
UserData userData = userDataProvider.getUserDataPerEMail(ghUserResponse.email);
HttpSession httpSession = req.getSession();
sessionHandler.setAuthenticated(userData, httpSession);
}
else{
throw new RuntimeException("Failed to authenticate");
}
}

private String getAccessToken(String code) throws IOException{
// we have to pull the returned "code" off of the server
// and then do a post to github to get the access_code
// String clientId= ConfigurationHandler.getConfigurationHandler().getValue("client_id");
// String clientSecret = ConfigurationHandler.getConfigurationHandler().getValue("github.client_secret");

// https://developer.github.com/v3/oauth/#2-github-redirects-back-to-your-site
// TODO: 1 post to the client to get the acces token

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("client_id",this.clientId));
urlParameters.add(new BasicNameValuePair("client_secret",this.clientSecret));
urlParameters.add(new BasicNameValuePair("code",code));

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("https://github.com/login/oauth/access_token");
postRequest.addHeader("User-Agent", "TermGenie/1.0");
postRequest.addHeader("Accept","application/json");
postRequest.addHeader("Accept","application/xml");

System.out.println("posting '${urlParameters}'");

postRequest.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = httpClient.execute(postRequest);

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}

GHAccessResponse ghAccessResponse = gson.fromJson(result.toString(), GHAccessResponse.class);

String accessToken = ghAccessResponse.access_token;
return accessToken ;
}

static class GHUserResponse {
String email;
String username;
}

static class GHAccessResponse {
String access_token;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.bbop.termgenie.servlets;

import org.bbop.termgenie.core.ioc.IOCModule;

import java.util.Properties;

/**
* Created by nathandunn on 11/28/16.
*/
public class GHAuthenticationModule extends IOCModule{

public GHAuthenticationModule(Properties properties){
super(properties);
}

@Override
protected void configure() {
bindSecret("github_client_id");
bindSecret("github_client_secret");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.bbop.termgenie.servlets;

import com.google.gson.Gson;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import org.apache.log4j.Logger;
import org.bbop.termgenie.services.info.ConfigurationHandler;
import org.bbop.termgenie.services.lookup.TermLookupService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* https://developer.github.com/v3/oauth/#1-redirect-users-to-request-github-access
*/
@Singleton
public class GHAuthenticationRequestServlet extends HttpServlet {

private static final Logger logger = Logger.getLogger(GHAuthenticationRequestServlet.class);

// generated
private static final long serialVersionUID = 4604786454943166862L;

private final String clientId ;

@Inject
public GHAuthenticationRequestServlet(
@Named("github_client_id")
String clientId
) {
super();
this.clientId = clientId ;
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// String clientId = ConfigurationHandler.getConfigurationHandler().getValue("client_id");
String url="https://github.com/login/oauth/authorize?client_id="+clientId+"&scope=user:email";
resp.sendRedirect(url);
}


}
Loading

0 comments on commit d14458b

Please sign in to comment.