Skip to content

Commit

Permalink
Merge pull request #32 from ase-101/develop
Browse files Browse the repository at this point in the history
ES-842 captcha secrets configuration changed and renamed DTOs
  • Loading branch information
ckm007 authored Oct 13, 2024
2 parents 401f350 + da901b1 commit a9ff110
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;

import io.mosip.captcha.dto.ExceptionJSONInfoDTO;
import io.mosip.captcha.dto.MainResponseDTO;
import io.mosip.captcha.dto.ResponseWrapper;
import io.mosip.captcha.exception.InvalidRequestCaptchaException;


Expand All @@ -29,8 +29,8 @@ public class CaptchaExceptionHandler {
private String captchaApiVersion;

@ExceptionHandler(InvalidRequestCaptchaException.class)
public MainResponseDTO<?> handleInvalidCaptchaRequest(InvalidRequestCaptchaException ex) {
MainResponseDTO<?> response = new MainResponseDTO<>();
public ResponseWrapper<?> handleInvalidCaptchaRequest(InvalidRequestCaptchaException ex) {
ResponseWrapper<?> response = new ResponseWrapper<>();
response.setId(captchaApiId);
response.setVersion(captchaApiVersion);
response.setResponsetime(CaptchaUtils.getCurrentResponseTime());
Expand All @@ -43,8 +43,8 @@ public MainResponseDTO<?> handleInvalidCaptchaRequest(InvalidRequestCaptchaExcep
}

@ExceptionHandler(CaptchaException.class)
public MainResponseDTO<?> handleCaptchaException(CaptchaException ex) {
MainResponseDTO<?> response = new MainResponseDTO<>();
public ResponseWrapper<?> handleCaptchaException(CaptchaException ex) {
ResponseWrapper<?> response = new ResponseWrapper<>();
response.setId(captchaApiId);
response.setVersion(captchaApiVersion);
response.setResponsetime(CaptchaUtils.getCurrentResponseTime());
Expand All @@ -57,8 +57,8 @@ public MainResponseDTO<?> handleCaptchaException(CaptchaException ex) {
}

@ExceptionHandler(Exception.class)
public MainResponseDTO<?> handleException(Exception ex) {
MainResponseDTO<?> response = new MainResponseDTO<>();
public ResponseWrapper<?> handleException(Exception ex) {
ResponseWrapper<?> response = new ResponseWrapper<>();
response.setId(captchaApiId);
response.setVersion(captchaApiVersion);
response.setResponsetime(CaptchaUtils.getCurrentResponseTime());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.mosip.captcha.controller;

import io.mosip.captcha.exception.CaptchaException;
import io.mosip.captcha.exception.InvalidRequestParameterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -13,7 +12,7 @@
import org.springframework.web.bind.annotation.RestController;

import io.mosip.captcha.dto.CaptchaRequestDTO;
import io.mosip.captcha.dto.MainRequestDTO;
import io.mosip.captcha.dto.RequestWrapper;
import io.mosip.captcha.exception.InvalidRequestCaptchaException;
import io.mosip.captcha.spi.CaptchaService;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,7 +25,7 @@ public class CaptchaController {
private CaptchaService captchaService;

@PostMapping(path = "/validatecaptcha", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> validateCaptcha(@Validated @RequestBody MainRequestDTO<CaptchaRequestDTO> captchaRequest,
public ResponseEntity<?> validateCaptcha(@Validated @RequestBody RequestWrapper<CaptchaRequestDTO> captchaRequest,
Errors errors) throws CaptchaException, InvalidRequestCaptchaException {
log.debug("In captcha-validation-service controller to validate the recaptcha token", captchaRequest);
return new ResponseEntity<>(this.captchaService.validateCaptcha(captchaRequest.getRequest()), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;

@Data
public class GoogleCaptchaDTO implements Serializable{
public class GoogleReCaptchaV2Response implements Serializable{

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Setter
@NoArgsConstructor
@ToString
public class MainRequestDTO<T> implements Serializable {
public class RequestWrapper<T> implements Serializable {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = -4966448852014107698L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Setter
@NoArgsConstructor
@ToString
public class MainResponseDTO<T> implements Serializable{
public class ResponseWrapper<T> implements Serializable{

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 3384945682672832638L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

import io.mosip.captcha.dto.ExceptionJSONInfoDTO;
import io.mosip.captcha.dto.MainResponseDTO;
import io.mosip.captcha.dto.ResponseWrapper;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -21,29 +21,29 @@ public class InvalidRequestParameterException extends Exception {
*/
private static final long serialVersionUID = -3898906527162403384L;

private MainResponseDTO<?> mainResponseDto;
private ResponseWrapper<?> responseWrapper;
private List<ExceptionJSONInfoDTO> exptionList;
private String operation;

public InvalidRequestParameterException() {
super();
}

public InvalidRequestParameterException(String errCode, String errMessage,MainResponseDTO<?> response) {
this.mainResponseDto=response;
public InvalidRequestParameterException(String errCode, String errMessage, ResponseWrapper<?> response) {
this.responseWrapper =response;
}
public InvalidRequestParameterException(String errorCode, String errorMessage, Throwable rootCause,MainResponseDTO<?> response) {
this.mainResponseDto=response;
public InvalidRequestParameterException(String errorCode, String errorMessage, Throwable rootCause, ResponseWrapper<?> response) {
this.responseWrapper =response;
}


public InvalidRequestParameterException(List<ExceptionJSONInfoDTO> exptionList,MainResponseDTO<?> response) {
this.mainResponseDto=response;
public InvalidRequestParameterException(List<ExceptionJSONInfoDTO> exptionList, ResponseWrapper<?> response) {
this.responseWrapper =response;
this.exptionList=exptionList;
}

public InvalidRequestParameterException(List<ExceptionJSONInfoDTO> exptionList,String operation,MainResponseDTO<?> response) {
this.mainResponseDto=response;
public InvalidRequestParameterException(List<ExceptionJSONInfoDTO> exptionList, String operation, ResponseWrapper<?> response) {
this.responseWrapper =response;
this.exptionList=exptionList;
this.operation=operation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import io.mosip.captcha.util.CaptchaErrorCode;
import io.mosip.captcha.exception.CaptchaException;
import io.mosip.captcha.util.CaptchaUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
Expand All @@ -18,18 +22,21 @@
import io.mosip.captcha.dto.CaptchaRequestDTO;
import io.mosip.captcha.dto.CaptchaResponseDTO;
import io.mosip.captcha.dto.ExceptionJSONInfoDTO;
import io.mosip.captcha.dto.GoogleCaptchaDTO;
import io.mosip.captcha.dto.MainResponseDTO;
import io.mosip.captcha.dto.GoogleReCaptchaV2Response;
import io.mosip.captcha.dto.ResponseWrapper;
import io.mosip.captcha.exception.InvalidRequestCaptchaException;
import io.mosip.captcha.spi.CaptchaService;
import lombok.extern.slf4j.Slf4j;


@ConfigurationProperties(prefix = "mosip.captcha.secret-key")
@Service
@Slf4j
public class CaptchaServiceImpl implements CaptchaService {

@Value("#{${mosip.captcha.secret-key}}")
public Map<String,String> secretKeys;
@Getter
@Setter
public Map<String,String> secrets;

@Value("${mosip.captcha.verify-url}")
public String captchaVerifyUrl;
Expand All @@ -50,69 +57,55 @@ public class CaptchaServiceImpl implements CaptchaService {

@Override
public Object validateCaptcha(Object captchaRequest) throws CaptchaException, InvalidRequestCaptchaException {

log.info("In captcha service to validate the token request"
+ ((CaptchaRequestDTO) captchaRequest).getCaptchaToken());

validateCaptchaRequest((CaptchaRequestDTO) captchaRequest);
String moduleName = ((CaptchaRequestDTO) captchaRequest).getModuleName();
MainResponseDTO<CaptchaResponseDTO> mainResponse = new MainResponseDTO<>();

MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
param.add("secret", secretKeys.get(moduleName == null? defaultModuleName : moduleName));
param.add("secret", secrets.get(moduleName == null? defaultModuleName : moduleName));
param.add("response", ((CaptchaRequestDTO) captchaRequest).getCaptchaToken().trim());

GoogleCaptchaDTO captchaResponse = null;
if(param.get("secret") == null) {
log.error("Failed to find secret for module {}", moduleName);
throw new CaptchaException(CaptchaErrorCode.CAPTCHA_VALIDATION_FAILED.getErrorCode(),
CaptchaErrorCode.CAPTCHA_VALIDATION_FAILED.getErrorCode());
}

GoogleReCaptchaV2Response captchaResponse = null;
try {
log.info("In captcha service try block to validate the token request via a google verify site rest call"
+ ((CaptchaRequestDTO) captchaRequest).getCaptchaToken() + captchaVerifyUrl);

captchaResponse = this.restTemplate.postForObject(captchaVerifyUrl, param, GoogleCaptchaDTO.class);
log.info("validate the token request via {}", captchaVerifyUrl);
captchaResponse = this.restTemplate.postForObject(captchaVerifyUrl, param, GoogleReCaptchaV2Response.class);
log.debug(" captchaResponse -> {}", captchaResponse);

} catch (RestClientException ex) {
log.error("In captcha service to validate the token request via a google verify site rest call has failed --->"
+ ((CaptchaRequestDTO) captchaRequest).getCaptchaToken() + captchaVerifyUrl
, ex);
if (captchaResponse != null && captchaResponse.getErrorCodes() !=null) {
throw new CaptchaException(captchaResponse.getErrorCodes().get(0), captchaResponse.getErrorCodes().get(0));
}
log.error("captcha token validation request failed", ex);
}

if (captchaResponse!=null && captchaResponse.isSuccess()) {
log.info("In captcha service token request has been successfully verified --->"
+ captchaResponse.isSuccess());
mainResponse.setId(captchaApiId);
mainResponse.setResponsetime(captchaResponse.getChallengeTs());
mainResponse.setVersion(captchaApiVersion);
if(captchaResponse == null)
throw new CaptchaException(CaptchaErrorCode.CAPTCHA_VALIDATION_FAILED.getErrorCode(),
CaptchaErrorCode.CAPTCHA_VALIDATION_FAILED.getErrorCode());

if(!CollectionUtils.isEmpty(captchaResponse.getErrorCodes()))
throw new CaptchaException(captchaResponse.getErrorCodes().get(0), captchaResponse.getErrorCodes().get(0));

if(captchaResponse.isSuccess()) {
ResponseWrapper<CaptchaResponseDTO> responseWrapper = new ResponseWrapper<>();
responseWrapper.setId(captchaApiId);
responseWrapper.setResponsetime(captchaResponse.getChallengeTs());
responseWrapper.setVersion(captchaApiVersion);
CaptchaResponseDTO response = new CaptchaResponseDTO();
response.setMessage(CAPTCHA_SUCCESS);
response.setSuccess(captchaResponse.isSuccess());
mainResponse.setResponse(response);
} else {
if (captchaResponse != null) {
log.error("In captcha service token request has failed --->"
+ captchaResponse.isSuccess());
}
mainResponse.setId(captchaApiId);
mainResponse.setResponsetime(CaptchaUtils.getCurrentResponseTime());
mainResponse.setVersion(captchaApiVersion);
mainResponse.setResponse(null);
ExceptionJSONInfoDTO error = new ExceptionJSONInfoDTO(CaptchaErrorCode.INVALID_CAPTCHA_CODE.getErrorCode(),
CaptchaErrorCode.INVALID_CAPTCHA_CODE.getErrorMessage());
List<ExceptionJSONInfoDTO> errorList = new ArrayList<ExceptionJSONInfoDTO>();
errorList.add(error);
mainResponse.setErrors(errorList);

responseWrapper.setResponse(response);
return responseWrapper;
}
return mainResponse;

//request is NOT success and error-codes is empty
throw new CaptchaException(CaptchaErrorCode.CAPTCHA_VALIDATION_FAILED.getErrorCode(),
CaptchaErrorCode.CAPTCHA_VALIDATION_FAILED.getErrorCode());
}

private void validateCaptchaRequest(CaptchaRequestDTO captchaRequest) throws InvalidRequestCaptchaException {

if (captchaRequest.getCaptchaToken() == null || captchaRequest.getCaptchaToken().trim().length() == 0) {
log.debug("{}", captchaRequest);
log.debug("{}", captchaRequest);
if (captchaRequest.getCaptchaToken() == null || captchaRequest.getCaptchaToken().trim().length() == 0) {
throw new InvalidRequestCaptchaException(CaptchaErrorCode.INVALID_CAPTCHA_REQUEST.getErrorCode(),
CaptchaErrorCode.INVALID_CAPTCHA_REQUEST.getErrorMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
## Captcha validation service properties

mosip.prereg.captcha.secret.key=prereg
mosip.esignet.captcha.secret.key=esignet
mosip.signup.captcha.secret.key=signup

mosip.captcha.verify-url=https://www.google.com/recaptcha/api/siteverify
mosip.captcha.secret-key={'preregistration' : '${mosip.prereg.captcha.secret.key}', 'esignet' : '${mosip.esignet.captcha.secret.key}', 'signup' : '${mosip.signup.captcha.secret.key}' }

mosip.captcha.api.id=mosip.captcha.id.validate
mosip.captcha.api.version=1.0

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
## Application Name
spring.application.name=captcha-validation-service
spring.cloud.config.name=captcha
spring.profiles.active=default
server.port=9089
server.servlet.context-path=/v1/captcha
spring.cloud.config.name=captcha
spring.application.name=captcha-validation-service
spring.profiles.active=local

management.endpoint.health.show-details=always

springdoc.paths-to-exclude=/swagger-resources/**
Expand Down
Loading

0 comments on commit a9ff110

Please sign in to comment.