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