Skip to content

Commit

Permalink
28/4/2014 1:57
Browse files Browse the repository at this point in the history
Preparation for authentication.
  • Loading branch information
MarianMacik committed Apr 27, 2014
1 parent 579a5a4 commit 2787506
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -30,6 +30,12 @@
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.brickred</groupId>
<artifactId>socialauth</artifactId>
<version>4.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
Expand Down
165 changes: 165 additions & 0 deletions src/main/java/cz/muni/fi/pv168/kartoteka/AuthenticationBean.java
@@ -0,0 +1,165 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cz.muni.fi.pv168.kartoteka;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.Properties;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.Permission;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.SocialAuthConfig;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.exception.UserDeniedPermissionException;
import org.brickred.socialauth.util.SocialAuthUtil;

@Named
@SessionScoped
public class AuthenticationBean implements Serializable {

private SocialAuthManager manager;
private String originalURL;
private String providerID;
private Profile profile;

public AuthenticationBean() {
}

public void socialConnect() throws Exception {
// Put your keys and secrets from the providers here
Properties props = System.getProperties();
String FACEBOOK_APP_ID = "672049939523091";
String FACEBOOK_APP_SECRET = "031990dbcc5d28705901d5f9db0777d1";

String GOOGLE_ID = "668977671514-museeg57hpglh6p812cneqgfl5ut033s.apps.googleusercontent.com";
String GOOGLE_SECRET = "x6SjfTpLd7UUFivLJO9IksHx";

if ("facebook".equals(providerID)) {
props.put("graph.facebook.com.consumer_key", FACEBOOK_APP_ID);
props.put("graph.facebook.com.consumer_secret", FACEBOOK_APP_SECRET);
props.put("graph.facebook.com.custom_permissions", "publish_stream,email,user_birthday,user_location,offline_access");
} else {
props.put("www.google.com.consumer_key", GOOGLE_ID);
props.put("www.google.com.consumer_secret", GOOGLE_SECRET);
}

// Define your custom permission if needed
//props.put("graph.facebook.com.custom_permissions", "publish_stream,email,user_birthday,user_location,offline_access");
//props.put("googleapis.com.custom_permissions", "https://www.googleapis.com/auth/userinfo.profile,profile,email");
// Initiate required components
SocialAuthConfig config = SocialAuthConfig.getDefault();
config.load(props);
manager = new SocialAuthManager();
manager.setSocialAuthConfig(config);

String authenticationURL;
if (providerID.equals("facebook")) {
authenticationURL = manager.getAuthenticationUrl(providerID, "http://localhost:8080/Kartoteka/index.xhtml");
} else {
authenticationURL = manager.getAuthenticationUrl(providerID, "http://localhost:8080/Kartoteka/index.xhtml", Permission.AUTHENTICATE_ONLY);
}

FacesContext.getCurrentInstance().getExternalContext().redirect(authenticationURL);
System.out.println(authenticationURL);
}

public void pullUserInfo() throws IOException {
try {
// Pull user's data from the provider
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
Map map = SocialAuthUtil.getRequestParametersMap(request);
if (this.manager != null) {
AuthProvider provider = manager.connect(map);
this.profile = provider.getUserProfile();

// Do what you want with the data (e.g. persist to the database, etc.)
System.out.println("User's Social profile: " + profile);

// Redirect the user back to where they have been before logging in
FacesContext.getCurrentInstance().getExternalContext().redirect(originalURL);

} else {
FacesContext.getCurrentInstance().getExternalContext().redirect(externalContext.getRequestContextPath() + "/home.xhtml");
}
} catch (UserDeniedPermissionException ex) {
FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/FacebookAuthentication/deniedPermission.xhtml");
} catch (Exception ex) {
System.out.println("UserSession - Exception: " + ex.toString());
}
}

public void logOut() {
try {
// Disconnect from the provider
String userToken = new String();
if (providerID.equals("facebook")) {
userToken = manager.getCurrentAuthProvider().getAccessGrant().getKey();
}
//String userToken = manager.getCurrentAuthProvider().getAccessGrant().getKey();
manager.disconnectProvider(providerID);

// Invalidate session
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
externalContext.invalidateSession();



if (providerID.equals("facebook")) {
String logoutUrl = "https://www.facebook.com/logout.php?next=http://localhost:8080/FacebookAuthentication/home.xhtml&access_token=" + userToken;
FacesContext.getCurrentInstance().getExternalContext().redirect(logoutUrl);
} else {
FacesContext.getCurrentInstance().getExternalContext().redirect(externalContext.getRequestContextPath() + "/home.xhtml");
}

// Redirect to home page
//FacesContext.getCurrentInstance().getExternalContext().redirect(externalContext.getRequestContextPath() + "/home.xhtml");
} catch (IOException ex) {
System.out.println("UserSessionBean - IOException: " + ex.toString());
}
}

// Getters and Setters
public SocialAuthManager getManager() {
return manager;
}

public void setManager(SocialAuthManager manager) {
this.manager = manager;
}

public String getOriginalURL() {
return originalURL;
}

public void setOriginalURL(String originalURL) {
this.originalURL = originalURL;
}

public String getProviderID() {
return providerID;
}

public void setProviderID(String providerID) {
this.providerID = providerID;
}

public Profile getProfile() {
return profile;
}

public void setProfile(Profile profile) {
this.profile = profile;
}

}
Expand Up @@ -92,6 +92,8 @@ public Schema loadSchema(ObjectId schemaId, String selectedDB) {

public String setSchemaAndShow(ObjectId id, String selectedDB) {
this.schema = loadSchema(id, selectedDB);
//if user forgot to finish editing - on next load it is not in edit mode
this.schemaNameEditMode = false;
return "schema.xhtml?faces-redirect=true";
}

Expand Down Expand Up @@ -302,6 +304,9 @@ public void addSchema(List<Entry<ObjectId, String>> schemas, String selectedDB)
} else if (schemaNames.contains(newSchemaToAdd.getTitle())) {
FacesContext.getCurrentInstance().addMessage("schemaValidationErrorMessage", new FacesMessage(FacesMessage.SEVERITY_WARN, "Schema name must be unique!", null));
return;
} else if(newSchemaToAdd.getTitle().equals("Schemas")){
FacesContext.getCurrentInstance().addMessage("schemaValidationErrorMessage", new FacesMessage(FacesMessage.SEVERITY_WARN, "Schema name cannot be 'Schemas'!", null));
return;
}

BasicDBObject schemaToAdd = newSchemaToAdd.schemaToDBObject();
Expand Down Expand Up @@ -693,6 +698,9 @@ private boolean invalidSchemaName(String title, List<Map.Entry<ObjectId, String>
FacesContext.getCurrentInstance().addMessage("schemaNameErrorMessage", new FacesMessage(FacesMessage.SEVERITY_WARN, "Schema name must be unique!", null));
return true;
}
} else if(title.equals("Schemas")){
FacesContext.getCurrentInstance().addMessage("schemaValidationErrorMessage", new FacesMessage(FacesMessage.SEVERITY_WARN, "Schema name cannot be 'Schemas'!", null));
return true;
}

return false;
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/resources/images/google-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions src/main/webapp/welcome.xhtml
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>TODO supply a title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</h:head>
<h:body>
<center style="margin: 15px"><h2>Please select the way you want to log in</h2>

<div style="margin:200px ">

<h:form>
<h:panelGroup rendered="#{empty authenticationBean.profile}" id="socialButtons" >
<h:commandButton id="facebook" action="#{authenticationBean.socialConnect}" image="resources/images/facebook-icon.png">
<f:setPropertyActionListener target="#{authenticationBean.providerID}" value="facebook" />
</h:commandButton>
<br/>
<p:commandLink id="google" action="#{authenticationBean.socialConnect}" >
<f:setPropertyActionListener target="#{authenticationBean.providerID}" value="google" />
<p:graphicImage library="images" name="google-icon.png"/>
</p:commandLink>
</h:panelGroup>
</h:form>
</div>
</center>

</h:body>
</html>

0 comments on commit 2787506

Please sign in to comment.