Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
217 additions
and
0 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
165 changes: 165 additions & 0 deletions
165
src/main/java/cz/muni/fi/pv168/kartoteka/AuthenticationBean.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,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; | ||
} | ||
|
||
} |
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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> |