001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.captcha.recaptcha;
016    
017    import com.liferay.portal.captcha.simplecaptcha.SimpleCaptchaImpl;
018    import com.liferay.portal.kernel.captcha.CaptchaException;
019    import com.liferay.portal.kernel.captcha.CaptchaTextException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.Http;
025    import com.liferay.portal.kernel.util.HttpUtil;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PrefsPropsUtil;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.io.IOException;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.ResourceRequest;
036    import javax.portlet.ResourceResponse;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    /**
042     * @author Tagnaouti Boubker
043     * @author Jorge Ferrer
044     * @author Brian Wing Shun Chan
045     * @author Daniel Sanz
046     */
047    public class ReCaptchaImpl extends SimpleCaptchaImpl {
048    
049            @Override
050            public String getTaglibPath() {
051                    return _TAGLIB_PATH;
052            }
053    
054            @Override
055            public void serveImage(
056                    HttpServletRequest request, HttpServletResponse response) {
057    
058                    throw new UnsupportedOperationException();
059            }
060    
061            @Override
062            public void serveImage(
063                    ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
064    
065                    throw new UnsupportedOperationException();
066            }
067    
068            @Override
069            protected boolean validateChallenge(HttpServletRequest request)
070                    throws CaptchaException {
071    
072                    String reCaptchaChallenge = ParamUtil.getString(
073                            request, "recaptcha_challenge_field");
074                    String reCaptchaResponse = ParamUtil.getString(
075                            request, "recaptcha_response_field");
076    
077                    Http.Options options = new Http.Options();
078    
079                    options.addPart("challenge", reCaptchaChallenge);
080    
081                    try {
082                            options.addPart(
083                                    "privatekey",
084                                    PrefsPropsUtil.getString(
085                                            PropsKeys.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE,
086                                            PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE));
087                    }
088                    catch (SystemException se) {
089                            _log.error(se, se);
090                    }
091    
092                    options.addPart("remoteip", request.getRemoteAddr());
093                    options.addPart("response", reCaptchaResponse);
094                    options.setLocation(
095                            HttpUtil.protocolize(
096                                    PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY,
097                                    request.isSecure()));
098                    options.setPost(true);
099    
100                    String content = null;
101    
102                    try {
103                            content = HttpUtil.URLtoString(options);
104                    }
105                    catch (IOException ioe) {
106                            _log.error(ioe, ioe);
107    
108                            throw new CaptchaTextException();
109                    }
110    
111                    if (content == null) {
112                            _log.error("reCAPTCHA did not return a result");
113    
114                            throw new CaptchaTextException();
115                    }
116    
117                    String[] messages = content.split("\r?\n");
118    
119                    if (messages.length < 1) {
120                            _log.error("reCAPTCHA did not return a valid result: " + content);
121    
122                            throw new CaptchaTextException();
123                    }
124    
125                    return GetterUtil.getBoolean(messages[0]);
126            }
127    
128            @Override
129            protected boolean validateChallenge(PortletRequest portletRequest)
130                    throws CaptchaException {
131    
132                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
133                            portletRequest);
134    
135                    return validateChallenge(request);
136            }
137    
138            private static final String _TAGLIB_PATH =
139                    "/html/taglib/ui/captcha/recaptcha.jsp";
140    
141            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
142    
143    }