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.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(PropsValues.CAPTCHA_ENGINE_RECAPTCHA_URL_VERIFY);
095                    options.setPost(true);
096    
097                    String content = null;
098    
099                    try {
100                            content = HttpUtil.URLtoString(options);
101                    }
102                    catch (IOException ioe) {
103                            _log.error(ioe, ioe);
104    
105                            throw new CaptchaTextException();
106                    }
107    
108                    if (content == null) {
109                            _log.error("reCAPTCHA did not return a result");
110    
111                            throw new CaptchaTextException();
112                    }
113    
114                    String[] messages = content.split("\r?\n");
115    
116                    if (messages.length < 1) {
117                            _log.error("reCAPTCHA did not return a valid result: " + content);
118    
119                            throw new CaptchaTextException();
120                    }
121    
122                    return GetterUtil.getBoolean(messages[0]);
123            }
124    
125            @Override
126            protected boolean validateChallenge(PortletRequest portletRequest)
127                    throws CaptchaException {
128    
129                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
130                            portletRequest);
131    
132                    return validateChallenge(request);
133            }
134    
135            private static final String _TAGLIB_PATH =
136                    "/html/taglib/ui/captcha/recaptcha.jsp";
137    
138            private static Log _log = LogFactoryUtil.getLog(ReCaptchaImpl.class);
139    
140    }