Java tutorial
/* * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.wso2.carbon.identity.captcha.connector.recaptcha; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.identity.application.common.model.Property; import org.wso2.carbon.identity.captcha.connector.CaptchaPostValidationResponse; import org.wso2.carbon.identity.captcha.connector.CaptchaPreValidationResponse; import org.wso2.carbon.identity.captcha.exception.CaptchaClientException; import org.wso2.carbon.identity.captcha.exception.CaptchaException; import org.wso2.carbon.identity.captcha.internal.CaptchaDataHolder; import org.wso2.carbon.identity.captcha.util.CaptchaConstants; import org.wso2.carbon.identity.captcha.util.CaptchaUtil; import org.wso2.carbon.identity.governance.IdentityGovernanceService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * ReCaptcha Page Based Connector. */ public class SelfSignUpReCaptchaConnector extends AbstractReCaptchaConnector { private static final Log log = LogFactory.getLog(SelfSignUpReCaptchaConnector.class); private static final String SELF_REGISTRATION_INITIATE_URL = "/api/identity/recovery/v0.9/claims"; private static final String SELF_REGISTRATION_URL = "/api/identity/user/v0.9/me"; private final String PROPERTY_ENABLE_RECAPTCHA = "SelfRegistration.ReCaptcha"; private IdentityGovernanceService identityGovernanceService; @Override public void init(IdentityGovernanceService identityGovernanceService) { this.identityGovernanceService = identityGovernanceService; } @Override public int getPriority() { return 10; } @Override public boolean canHandle(ServletRequest servletRequest, ServletResponse servletResponse) throws CaptchaException { if (!CaptchaDataHolder.getInstance().isReCaptchaEnabled()) { return false; } String path = ((HttpServletRequest) servletRequest).getRequestURI(); if (StringUtils.isBlank(path) || (!CaptchaUtil.isPathAvailable(path, SELF_REGISTRATION_INITIATE_URL) && !CaptchaUtil.isPathAvailable(path, SELF_REGISTRATION_URL))) { return false; } Property[] connectorConfigs; try { connectorConfigs = getConnectorConfigs(servletRequest); } catch (Exception e) { // Can happen due to invalid tenant/ invalid configuration if (log.isDebugEnabled()) { log.debug("Unable to load connector configuration.", e); } return false; } String enable = null; for (Property connectorConfig : connectorConfigs) { if ((PROPERTY_ENABLE_RECAPTCHA).equals(connectorConfig.getName())) { enable = connectorConfig.getValue(); } } return Boolean.parseBoolean(enable); } @Override public CaptchaPreValidationResponse preValidate(ServletRequest servletRequest, ServletResponse servletResponse) throws CaptchaException { // reCaptcha is required for all requests. CaptchaPreValidationResponse preValidationResponse = new CaptchaPreValidationResponse(); String path = ((HttpServletRequest) servletRequest).getRequestURI(); HttpServletResponse httpServletResponse = ((HttpServletResponse) servletResponse); if (CaptchaUtil.isPathAvailable(path, SELF_REGISTRATION_INITIATE_URL)) { httpServletResponse.setHeader("reCaptcha", "true"); } else { httpServletResponse.setHeader("reCaptcha", "conditional"); preValidationResponse.setCaptchaValidationRequired(true); preValidationResponse.setMaxFailedLimitReached(true); } httpServletResponse.setHeader("reCaptchaKey", CaptchaDataHolder.getInstance().getReCaptchaSiteKey()); httpServletResponse.setHeader("reCaptchaAPI", CaptchaDataHolder.getInstance().getReCaptchaAPIUrl()); return preValidationResponse; } @Override public boolean verifyCaptcha(ServletRequest servletRequest, ServletResponse servletResponse) throws CaptchaException { String reCaptchaResponse = ((HttpServletRequest) servletRequest).getHeader("g-recaptcha-response"); if (StringUtils.isBlank(reCaptchaResponse)) { throw new CaptchaClientException("reCaptcha response is not available in the request."); } return CaptchaUtil.isValidCaptcha(reCaptchaResponse); } @Override public CaptchaPostValidationResponse postValidate(ServletRequest servletRequest, ServletResponse servletResponse) throws CaptchaException { //No validation at this stage. return null; } private Property[] getConnectorConfigs(ServletRequest servletRequest) throws Exception { String tenantDomain = servletRequest.getParameter("tenantDomain"); if (StringUtils.isBlank(tenantDomain)) { tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; } Property[] connectorConfigs; connectorConfigs = identityGovernanceService.getConfiguration(new String[] { PROPERTY_ENABLE_RECAPTCHA }, tenantDomain); return connectorConfigs; } }