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.kernel.util.Validator;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PrefsPropsUtil;
031    import com.liferay.portal.util.PropsValues;
032    
033    import java.io.IOException;
034    
035    import javax.portlet.PortletRequest;
036    import javax.portlet.ResourceRequest;
037    import javax.portlet.ResourceResponse;
038    
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletRequestWrapper;
041    import javax.servlet.http.HttpServletResponse;
042    
043    /**
044     * @author Tagnaouti Boubker
045     * @author Jorge Ferrer
046     * @author Brian Wing Shun Chan
047     * @author Daniel Sanz
048     */
049    public class ReCaptchaImpl extends SimpleCaptchaImpl {
050    
051            @Override
052            public String getTaglibPath() {
053                    return _TAGLIB_PATH;
054            }
055    
056            @Override
057            public void serveImage(
058                    HttpServletRequest request, HttpServletResponse response) {
059    
060                    throw new UnsupportedOperationException();
061            }
062    
063            @Override
064            public void serveImage(
065                    ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
066    
067                    throw new UnsupportedOperationException();
068            }
069    
070            @Override
071            protected boolean validateChallenge(HttpServletRequest request)
072                    throws CaptchaException {
073    
074                    String reCaptchaChallenge = ParamUtil.getString(
075                            request, "recaptcha_challenge_field");
076                    String reCaptchaResponse = ParamUtil.getString(
077                            request, "recaptcha_response_field");
078    
079                    while (Validator.isBlank(reCaptchaChallenge) &&
080                               (request instanceof HttpServletRequestWrapper)) {
081    
082                            HttpServletRequestWrapper httpServletRequestWrapper =
083                                    (HttpServletRequestWrapper)request;
084    
085                            request =
086                                    (HttpServletRequest)httpServletRequestWrapper.getRequest();
087    
088                            reCaptchaChallenge = ParamUtil.getString(
089                                    request, "recaptcha_challenge_field");
090                            reCaptchaResponse = ParamUtil.getString(
091                                    request, "recaptcha_response_field");
092                    }
093    
094                    Http.Options options = new Http.Options();
095    
096                    options.addPart("challenge", reCaptchaChallenge);
097    
098                    try {
099                            options.addPart(
100                                    "privatekey",
101                                    PrefsPropsUtil.getString(
102                                            PropsKeys.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE,
103                                            PropsValues.CAPTCHA_ENGINE_RECAPTCHA_KEY_PRIVATE));
104                    }
105                    catch (SystemException se) {
106                            _log.error(se, se);
107                    }
108    
109                    options.addPart("remoteip", request.getRemoteAddr());
110                    options.addPart("response", reCaptchaResponse);
111                    options.setLocation(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
112                    options.setPost(true);
113    
114                    String content = null;
115    
116                    try {
117                            content = HttpUtil.URLtoString(options);
118                    }
119                    catch (IOException ioe) {
120                            _log.error(ioe, ioe);
121    
122                            throw new CaptchaTextException();
123                    }
124    
125                    if (content == null) {
126                            _log.error("reCAPTCHA did not return a result");
127    
128                            throw new CaptchaTextException();
129                    }
130    
131                    String[] messages = content.split("\r?\n");
132    
133                    if (messages.length < 1) {
134                            _log.error("reCAPTCHA did not return a valid result: " + content);
135    
136                            throw new CaptchaTextException();
137                    }
138    
139                    return GetterUtil.getBoolean(messages[0]);
140            }
141    
142            @Override
143            protected boolean validateChallenge(PortletRequest portletRequest)
144                    throws CaptchaException {
145    
146                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
147                            portletRequest);
148    
149                    return validateChallenge(request);
150            }
151    
152            private static final String _TAGLIB_PATH =
153                    "/html/taglib/ui/captcha/recaptcha.jsp";
154    
155            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
156    
157    }