001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
041     * @author Wilson Man
042     * @author Mika Koivisto
043     */
044    @DoPrivileged
045    public class FacebookConnectImpl implements FacebookConnect {
046    
047            @Override
048            public String getAccessToken(long companyId, String redirect, String code)
049                    throws SystemException {
050    
051                    String url = HttpUtil.addParameter(
052                            getAccessTokenURL(companyId), "client_id", getAppId(companyId));
053    
054                    url = HttpUtil.addParameter(
055                            url, "redirect_uri", FacebookConnectUtil.getRedirectURL(companyId));
056    
057                    String facebookConnectRedirectURL = getRedirectURL(companyId);
058    
059                    facebookConnectRedirectURL = HttpUtil.addParameter(
060                            facebookConnectRedirectURL, "redirect", redirect);
061    
062                    url = HttpUtil.addParameter(
063                            url, "redirect_uri", facebookConnectRedirectURL);
064                    url = HttpUtil.addParameter(
065                            url, "client_secret", getAppSecret(companyId));
066                    url = HttpUtil.addParameter(url, "code", code);
067    
068                    Http.Options options = new Http.Options();
069    
070                    options.setLocation(url);
071                    options.setPost(true);
072    
073                    try {
074                            String content = HttpUtil.URLtoString(options);
075    
076                            if (Validator.isNotNull(content)) {
077                                    int x = content.indexOf("access_token=");
078    
079                                    if (x >= 0) {
080                                            int y = content.indexOf(CharPool.AMPERSAND, x);
081    
082                                            if (y < x) {
083                                                    y = content.length();
084                                            }
085    
086                                            return content.substring(x + 13, y);
087                                    }
088                            }
089                    }
090                    catch (Exception e) {
091                            throw new SystemException(
092                                    "Unable to retrieve Facebook access token", e);
093                    }
094    
095                    return null;
096            }
097    
098            @Override
099            public String getAccessTokenURL(long companyId) throws SystemException {
100                    return PrefsPropsUtil.getString(
101                            companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_TOKEN_URL,
102                            PropsValues.FACEBOOK_CONNECT_OAUTH_TOKEN_URL);
103            }
104    
105            @Override
106            public String getAppId(long companyId) throws SystemException {
107                    return PrefsPropsUtil.getString(
108                            companyId, PropsKeys.FACEBOOK_CONNECT_APP_ID,
109                            PropsValues.FACEBOOK_CONNECT_APP_ID);
110            }
111    
112            @Override
113            public String getAppSecret(long companyId) throws SystemException {
114                    return PrefsPropsUtil.getString(
115                            companyId, PropsKeys.FACEBOOK_CONNECT_APP_SECRET,
116                            PropsValues.FACEBOOK_CONNECT_APP_SECRET);
117            }
118    
119            @Override
120            public String getAuthURL(long companyId) throws SystemException {
121                    return PrefsPropsUtil.getString(
122                            companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_AUTH_URL,
123                            PropsValues.FACEBOOK_CONNECT_OAUTH_AUTH_URL);
124            }
125    
126            @Override
127            public JSONObject getGraphResources(
128                    long companyId, String path, String accessToken, String fields) {
129    
130                    try {
131                            String url = HttpUtil.addParameter(
132                                    getGraphURL(companyId).concat(path), "access_token",
133                                    accessToken);
134    
135                            if (Validator.isNotNull(fields)) {
136                                    url = HttpUtil.addParameter(url, "fields", fields);
137                            }
138    
139                            Http.Options options = new Http.Options();
140    
141                            options.setLocation(url);
142    
143                            String json = HttpUtil.URLtoString(options);
144    
145                            return JSONFactoryUtil.createJSONObject(json);
146                    }
147                    catch (Exception e) {
148                            if (_log.isWarnEnabled()) {
149                                    _log.warn(e, e);
150                            }
151                    }
152    
153                    return null;
154            }
155    
156            @Override
157            public String getGraphURL(long companyId) throws SystemException {
158                    return PrefsPropsUtil.getString(
159                            companyId, PropsKeys.FACEBOOK_CONNECT_GRAPH_URL,
160                            PropsValues.FACEBOOK_CONNECT_GRAPH_URL);
161            }
162    
163            @Override
164            public String getProfileImageURL(PortletRequest portletRequest) {
165                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
166                            portletRequest);
167    
168                    request = PortalUtil.getOriginalServletRequest(request);
169    
170                    HttpSession session = request.getSession();
171    
172                    String facebookId = (String)session.getAttribute(
173                            WebKeys.FACEBOOK_USER_ID);
174    
175                    if (Validator.isNull(facebookId)) {
176                            return null;
177                    }
178    
179                    long companyId = PortalUtil.getCompanyId(request);
180    
181                    String token = (String)session.getAttribute(
182                            WebKeys.FACEBOOK_ACCESS_TOKEN);
183    
184                    JSONObject jsonObject = getGraphResources(
185                            companyId, "/me", token, "id,picture");
186    
187                    return jsonObject.getString("picture");
188            }
189    
190            @Override
191            public String getRedirectURL(long companyId) throws SystemException {
192                    return PrefsPropsUtil.getString(
193                            companyId, PropsKeys.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL,
194                            PropsValues.FACEBOOK_CONNECT_OAUTH_REDIRECT_URL);
195            }
196    
197            @Override
198            public boolean isEnabled(long companyId) throws SystemException {
199                    return PrefsPropsUtil.getBoolean(
200                            companyId, PropsKeys.FACEBOOK_CONNECT_AUTH_ENABLED,
201                            PropsValues.FACEBOOK_CONNECT_AUTH_ENABLED);
202            }
203    
204            @Override
205            public boolean isVerifiedAccountRequired(long companyId)
206                    throws SystemException {
207    
208                    return PrefsPropsUtil.getBoolean(
209                            companyId, PropsKeys.FACEBOOK_CONNECT_VERIFIED_ACCOUNT_REQUIRED,
210                            PropsValues.FACEBOOK_CONNECT_VERIFIED_ACCOUNT_REQUIRED);
211            }
212    
213            private static Log _log = LogFactoryUtil.getLog(FacebookConnectImpl.class);
214    
215    }