-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from nathandunn/fix_build
Fix build and added alternate authentication strategy. Seth sez: well, it will burn down on Wednesday anyways...
- Loading branch information
Showing
18 changed files
with
310 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.idea/** | ||
*.swp | ||
*.swo | ||
target/** | ||
.project | ||
.classpath | ||
*.iml | ||
*.ipr | ||
*.iws | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
.../TermGenieJQuery/src/main/java/org/bbop/termgenie/services/info/ConfigurationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...mGenieJQuery/src/main/java/org/bbop/termgenie/servlets/GHAuthenticationAccessServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...ons/TermGenieJQuery/src/main/java/org/bbop/termgenie/servlets/GHAuthenticationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...GenieJQuery/src/main/java/org/bbop/termgenie/servlets/GHAuthenticationRequestServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.