001 package com.khubla.pragmatach.plugin.facebook; 002 003 import net.sf.json.JSONObject; 004 import net.sf.json.JSONSerializer; 005 006 import org.apache.commons.lang.StringUtils; 007 import org.apache.http.client.HttpClient; 008 import org.apache.http.client.methods.HttpGet; 009 import org.apache.http.impl.client.BasicResponseHandler; 010 import org.apache.http.impl.client.DefaultHttpClient; 011 012 import com.khubla.pragmatach.framework.api.PragmatachException; 013 import com.khubla.pragmatach.framework.api.Response; 014 import com.khubla.pragmatach.framework.application.Application; 015 import com.khubla.pragmatach.plugin.freemarker.FreemarkerController; 016 017 /** 018 * @author tome 019 */ 020 public class FacebookLoginController extends FreemarkerController { 021 /** 022 * code 023 */ 024 private String code; 025 /** 026 * state 027 */ 028 private String state; 029 /** 030 * facebook application ID 031 */ 032 private String applicationid; 033 /** 034 * facebook application secret 035 */ 036 private String facebooksecret; 037 /** 038 * user's facebook id 039 */ 040 private String facebookId; 041 /** 042 * user's firstname 043 */ 044 private String firstName; 045 /** 046 * user's lastname 047 */ 048 private String lastName; 049 /** 050 * users email 051 */ 052 private String email; 053 /** 054 * redirect URL 055 */ 056 private final String redirectURL; 057 058 /** 059 * ctor 060 */ 061 public FacebookLoginController(String redirectURL) throws PragmatachException { 062 this.redirectURL = redirectURL; 063 applicationid = Application.getConfiguration().getParameter("facebook.applicationid"); 064 facebooksecret = Application.getConfiguration().getParameter("facebook.facebooksecret"); 065 } 066 067 public String getRedirectURL() { 068 return redirectURL; 069 } 070 071 public Response doLogin() throws PragmatachException { 072 final String sessionID = getRequest().getSession().getId(); 073 if (sessionID != getRequest().getSession().getId()) { 074 throw new PragmatachException("CSRF Exception"); 075 } 076 final String accessToken = getFacebookAccessToken(code); 077 getUserDetails(accessToken); 078 return super.render(); 079 } 080 081 public String getApplicationid() { 082 return applicationid; 083 } 084 085 public String getCode() { 086 return code; 087 } 088 089 public String getEmail() { 090 return email; 091 } 092 093 private String getFacebookAccessToken(String faceCode) throws PragmatachException { 094 String token = null; 095 if ((faceCode != null) && !"".equals(faceCode)) { 096 final String redirectUrl = getApplicationURL() + "/plugins/facebook/dologin"; 097 final String newUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + applicationid + "&redirect_uri=" + redirectUrl + "&client_secret=" + facebooksecret + "&code=" + faceCode; 098 final HttpClient httpclient = new DefaultHttpClient(); 099 try { 100 final HttpGet httpget = new HttpGet(newUrl); 101 final BasicResponseHandler responseHandler = new BasicResponseHandler(); 102 final String responseBody = httpclient.execute(httpget, responseHandler); 103 token = StringUtils.removeEnd(StringUtils.removeStart(responseBody, "access_token="), "&expires=5180795"); 104 } catch (final Exception e) { 105 throw new PragmatachException("Exception in getFacebookAccessToken", e); 106 } finally { 107 httpclient.getConnectionManager().shutdown(); 108 } 109 } 110 return token; 111 } 112 113 public String getFacebookId() { 114 return facebookId; 115 } 116 117 public String getFacebooksecret() { 118 return facebooksecret; 119 } 120 121 public String getFacebookUrlAuth() { 122 final String sessionId = getRequest().getSession().getId(); 123 final String redirectUrl = getApplicationURL() + "/plugins/facebook/dologin"; 124 final String returnValue = "https://www.facebook.com/dialog/oauth?client_id=" + applicationid + "&redirect_uri=" + redirectUrl + "&state=" + sessionId; 125 return returnValue; 126 } 127 128 public String getFirstName() { 129 return firstName; 130 } 131 132 public String getLastName() { 133 return lastName; 134 } 135 136 public String getState() { 137 return state; 138 } 139 140 private String getUserDetails(String accessToken) throws PragmatachException { 141 HttpClient httpclient = new DefaultHttpClient(); 142 try { 143 final String newUrl = "https://graph.facebook.com/me?access_token=" + accessToken; 144 httpclient = new DefaultHttpClient(); 145 final HttpGet httpget = new HttpGet(newUrl); 146 final BasicResponseHandler responseHandler = new BasicResponseHandler(); 147 final String responseBody = httpclient.execute(httpget, responseHandler); 148 final JSONObject json = (JSONObject) JSONSerializer.toJSON(responseBody); 149 facebookId = json.getString("id"); 150 firstName = json.getString("first_name"); 151 lastName = json.getString("last_name"); 152 email = json.getString("email"); 153 } catch (final Exception e) { 154 throw new PragmatachException("Exception in getUserDetails", e); 155 } finally { 156 httpclient.getConnectionManager().shutdown(); 157 } 158 return email; 159 } 160 161 public void setApplicationid(String applicationid) { 162 this.applicationid = applicationid; 163 } 164 165 public void setCode(String code) { 166 this.code = code; 167 } 168 169 public void setEmail(String email) { 170 this.email = email; 171 } 172 173 public void setFacebookId(String facebookId) { 174 this.facebookId = facebookId; 175 } 176 177 public void setFacebooksecret(String facebooksecret) { 178 this.facebooksecret = facebooksecret; 179 } 180 181 public void setFirstName(String firstName) { 182 this.firstName = firstName; 183 } 184 185 public void setLastName(String lastName) { 186 this.lastName = lastName; 187 } 188 189 public void setState(String state) { 190 this.state = state; 191 } 192 }