Commit c839854b authored by Noel Alonso's avatar Noel Alonso
Browse files

Reimplementa validación de recaptcha

Cambia la validación del recaptcha, de este modo no se realiza con una
etiqueta de validación, sino con un bean.

Hace uso de la util httpClient
parent a2bef4f7
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -35,6 +35,12 @@
			<version>${redmic.version}</version>
		</dependency>
		
		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>utils</artifactId>
			<version>${redmic.version}</version>
		</dependency>

		<!-- SpringBoot -->

		<dependency>
@@ -55,11 +61,6 @@

		<!--others -->
		
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
+27 −13
Original line number Diff line number Diff line
package es.redmic.user.manager.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

import javax.validation.Constraint;
import javax.validation.Payload;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = RecaptchaValidatorImpl.class)
public @interface RecaptchaValidator {
import es.redmic.exception.user.RecaptchaNotValidException;
import es.redmic.utils.httpclient.HttpClient;

	String message() default "Recaptcha not correct";
@Component
public class RecaptchaValidator {

	Class<?>[] groups() default {};
	@Value("${recaptcha.secret}")
	String RECAPTCHA_SECRET;

	Class<? extends Payload>[] payload() default {};
	HttpClient client = new HttpClient();

	@SuppressWarnings("unchecked")
	public void checkRecaptcha(String reCaptcha) {

		if (reCaptcha == null) {
			throw new RecaptchaNotValidException();
		}

		String url = "https://www.google.com/recaptcha/api/siteverify?secret=" + RECAPTCHA_SECRET + "&response="
				+ reCaptcha;

		Map<String, Object> result = (Map<String, Object>) client.get(url, Map.class);

		Boolean success = (Boolean) result.get("success");
		if (!success) {
			throw new RecaptchaNotValidException();
		}
	}
}
+0 −88
Original line number Diff line number Diff line
package es.redmic.user.manager.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import es.redmic.exception.common.ExceptionType;
import es.redmic.exception.common.InternalException;
import es.redmic.exception.utils.ExternalResourceException;

public class RecaptchaValidatorImpl implements ConstraintValidator<RecaptchaValidator, String> {

	@Override
	public void initialize(RecaptchaValidator arg0) {
	}

	@Override
	public boolean isValid(String recaptchaValue, ConstraintValidatorContext ctx) {

		if (recaptchaValue == null)
			return false;

		Boolean isValidRecaptcha = false;
		isValidRecaptcha = getReCaptchaResponse(recaptchaValue, ctx);
		return isValidRecaptcha;
	}

	public Boolean getReCaptchaResponse(String reCaptcha, ConstraintValidatorContext ctx) {

		String secret = "secret";
		String url = "https://www.google.com/recaptcha/api/siteverify?secret=" + secret + "&response=" + reCaptcha;

		CloseableHttpClient client = HttpClientBuilder.create().build();
		HttpGet request = new HttpGet(url);

		HttpResponse response;
		try {
			response = client.execute(request);
		} catch (IOException e) {
			throw new ExternalResourceException(e, url);
		}

		BufferedReader rd = null;
		try {
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		} catch (UnsupportedOperationException | IOException e) {
			throw new InternalException(ExceptionType.INTERNAL_EXCEPTION, e);
		}

		StringBuffer result = new StringBuffer();
		String line = "";

		try {
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
		} catch (IOException e1) {
			throw new InternalException(ExceptionType.INTERNAL_EXCEPTION, e1);
		}

		ObjectMapper mapper = new ObjectMapper();
		JsonNode resultJSON = null;
		try {
			resultJSON = mapper.readTree(result.toString());
		} catch (IOException e) {
			throw new InternalException(ExceptionType.INTERNAL_EXCEPTION, e);
		}

		if (resultJSON.get("success").asBoolean())
			return true;
		else {
			ctx.disableDefaultConstraintViolation();
			ctx.buildConstraintViolationWithTemplate("Recaptcha not correct").addConstraintViolation();
			return false;
		}
	}
}