001
014
015 package com.liferay.portal.facebook;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.facebook.FacebookConnect;
019 import com.liferay.portal.kernel.facebook.FacebookConnectUtil;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
025 import com.liferay.portal.kernel.util.CharPool;
026 import com.liferay.portal.kernel.util.Http;
027 import com.liferay.portal.kernel.util.HttpUtil;
028 import com.liferay.portal.kernel.util.PropsKeys;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.util.PortalUtil;
031 import com.liferay.portal.util.PrefsPropsUtil;
032 import com.liferay.portal.util.PropsValues;
033 import com.liferay.portal.util.WebKeys;
034
035 import javax.portlet.PortletRequest;
036
037 import javax.servlet.http.HttpServletRequest;
038 import javax.servlet.http.HttpSession;
039
040
044 @DoPrivileged
045 public class FacebookConnectImpl implements FacebookConnect {
046
047 public String getAccessToken(long companyId, String redirect, String code)
048 throws SystemException {
049
050 String url = HttpUtil.addParameter(
051 getAccessTokenURL(companyId), "client_id", getAppId(companyId));
052
053 url = HttpUtil.addParameter(
054 url, "redirect_uri", FacebookConnectUtil.getRedirectURL(companyId));
055
056 String facebookConnectRedirectURL = getRedirectURL(companyId);
057
058 facebookConnectRedirectURL = HttpUtil.addParameter(
059 facebookConnectRedirectURL, "redirect", redirect);
060
061 url = HttpUtil.addParameter(
062 url, "redirect_uri", facebookConnectRedirectURL);
063 url = HttpUtil.addParameter(
064 url, "client_secret", getAppSecret(companyId));
065 url = HttpUtil.addParameter(url, "code", code);
066
067 Http.Options options = new Http.Options();
068
069 options.setLocation(url);
070 options.setPost(true);
071
072 try {
073 String content = HttpUtil.URLtoString(options);
074
075 if (Validator.isNotNull(content)) {
076 int x = content.indexOf("access_token=");
077
078 if (x >= 0) {
079 int y = content.indexOf(CharPool.AMPERSAND, x);
080
081 if (y < x) {
082 y = content.length();
083 }
084
085 return content.substring(x + 13, y);
086 }
087 }
088 }
089 catch (Exception e) {
090 throw new SystemException(
091 "Unable to retrieve Facebook access token", e);
092 }
093
094 return null;
095 }
096
097 public String getAccessTokenURL(long companyId) throws SystemException {
098 return PrefsPropsUtil.getString(
099 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_TOKEN_URL,
100 PropsValues.FACEBOOK_CONNECT_OAUTH_TOKEN_URL);
101 }
102
103 public String getAppId(long companyId) throws SystemException {
104 return PrefsPropsUtil.getString(
105 companyId, PropsKeys.FACEBOOK_CONNECT_APP_ID,
106 PropsValues.FACEBOOK_CONNECT_APP_ID);
107 }
108
109 public String getAppSecret(long companyId) throws SystemException {
110 return PrefsPropsUtil.getString(
111 companyId, PropsKeys.FACEBOOK_CONNECT_APP_SECRET,
112 PropsValues.FACEBOOK_CONNECT_APP_SECRET);
113 }
114
115 public String getAuthURL(long companyId) throws SystemException {
116 return PrefsPropsUtil.getString(
117 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_AUTH_URL,
118 PropsValues.FACEBOOK_CONNECT_OAUTH_AUTH_URL);
119 }
120
121 public JSONObject getGraphResources(
122 long companyId, String path, String accessToken, String fields) {
123
124 try {
125 String url = HttpUtil.addParameter(
126 getGraphURL(companyId).concat(path), "access_token",
127 accessToken);
128
129 if (Validator.isNotNull(fields)) {
130 url = HttpUtil.addParameter(url, "fields", fields);
131 }
132
133 Http.Options options = new Http.Options();
134
135 options.setLocation(url);
136
137 String json = HttpUtil.URLtoString(options);
138
139 return JSONFactoryUtil.createJSONObject(json);
140 }
141 catch (Exception e) {
142 if (_log.isWarnEnabled()) {
143 _log.warn(e, e);
144 }
145 }
146
147 return null;
148 }
149
150 public String getGraphURL(long companyId) throws SystemException {
151 return PrefsPropsUtil.getString(
152 companyId, PropsKeys.FACEBOOK_CONNECT_GRAPH_URL,
153 PropsValues.FACEBOOK_CONNECT_GRAPH_URL);
154 }
155
156 public String getProfileImageURL(PortletRequest portletRequest) {
157 HttpServletRequest request = PortalUtil.getHttpServletRequest(
158 portletRequest);
159
160 request = PortalUtil.getOriginalServletRequest(request);
161
162 HttpSession session = request.getSession();
163
164 String facebookId = (String)session.getAttribute(
165 WebKeys.FACEBOOK_USER_ID);
166
167 if (Validator.isNull(facebookId)) {
168 return null;
169 }
170
171 long companyId = PortalUtil.getCompanyId(request);
172
173 String token = (String)session.getAttribute(
174 WebKeys.FACEBOOK_ACCESS_TOKEN);
175
176 JSONObject jsonObject = getGraphResources(
177 companyId, "/me", token, "id,picture");
178
179 return jsonObject.getString("picture");
180 }
181
182 public String getRedirectURL(long companyId) throws SystemException {
183 return PrefsPropsUtil.getString(
184 companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL,
185 PropsValues.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL);
186 }
187
188 public boolean isEnabled(long companyId) throws SystemException {
189 return PrefsPropsUtil.getBoolean(
190 companyId, PropsKeys.FACEBOOK_CONNECT_AUTH_ENABLED,
191 PropsValues.FACEBOOK_CONNECT_AUTH_ENABLED);
192 }
193
194 public boolean isVerifiedAccountRequired(long companyId)
195 throws SystemException {
196
197 return PrefsPropsUtil.getBoolean(
198 companyId, PropsKeys.FACEBOOK_CONNECT_VERIFIED_ACCOUNT_REQUIRED,
199 PropsValues.FACEBOOK_CONNECT_VERIFIED_ACCOUNT_REQUIRED);
200 }
201
202 private static Log _log = LogFactoryUtil.getLog(FacebookConnectImpl.class);
203
204 }