diff --git a/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java b/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java index 268147d9..98038bfa 100644 --- a/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java +++ b/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java @@ -19,7 +19,6 @@ import io.mosip.signup.exception.SignUpException; import io.mosip.signup.helper.CryptoHelper; import io.mosip.signup.util.*; -import io.mosip.signup.exception.CaptchaException; import io.mosip.signup.exception.GenerateChallengeException; import io.mosip.signup.helper.NotificationHelper; import lombok.extern.slf4j.Slf4j; @@ -96,10 +95,8 @@ public class RegistrationService { * @throws SignUpException */ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest generateChallengeRequest, String transactionId) throws SignUpException { - if (captchaRequired && !captchaHelper.validateCaptcha(generateChallengeRequest.getCaptchaToken())) { - log.error("generate-challenge failed: invalid captcha"); - throw new CaptchaException(ErrorConstants.INVALID_CAPTCHA); - } + if (captchaRequired) + captchaHelper.validateCaptcha(generateChallengeRequest.getCaptchaToken()); String identifier = generateChallengeRequest.getIdentifier(); RegistrationTransaction transaction = null; diff --git a/signup-service/src/main/java/io/mosip/signup/services/WebSocketHandler.java b/signup-service/src/main/java/io/mosip/signup/services/WebSocketHandler.java index b175f5ab..2280a3c2 100644 --- a/signup-service/src/main/java/io/mosip/signup/services/WebSocketHandler.java +++ b/signup-service/src/main/java/io/mosip/signup/services/WebSocketHandler.java @@ -13,6 +13,7 @@ import io.mosip.signup.api.exception.ProfileException; import io.mosip.signup.api.spi.IdentityVerifierPlugin; import io.mosip.signup.api.spi.ProfileRegistryPlugin; +import io.mosip.signup.api.util.ProcessFeedbackType; import io.mosip.signup.api.util.VerificationStatus; import io.mosip.signup.dto.IdentityVerificationRequest; import io.mosip.signup.dto.IdentityVerificationTransaction; @@ -22,6 +23,7 @@ import io.mosip.signup.helper.AuditHelper; import io.mosip.signup.util.AuditEvent; import io.mosip.signup.util.AuditEventType; +import io.mosip.signup.util.ErrorConstants; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -29,10 +31,7 @@ import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; import static io.mosip.signup.api.util.ErrorConstants.IDENTITY_VERIFICATION_FAILED; import static io.mosip.signup.api.util.ErrorConstants.PLUGIN_NOT_FOUND; @@ -66,25 +65,36 @@ public class WebSocketHandler { public void processFrames(IdentityVerificationRequest identityVerificationRequest) { - IdentityVerificationTransaction transaction = cacheUtilService.getVerifiedSlotTransaction(identityVerificationRequest.getSlotId()); - if(transaction == null) - throw new InvalidTransactionException(); - - IdentityVerifierPlugin plugin = identityVerifierFactory.getIdentityVerifier(transaction.getVerifierId()); - if(plugin == null) - throw new SignUpException(PLUGIN_NOT_FOUND); - - if(plugin.isStartStep(identityVerificationRequest.getStepCode())) { - IdentityVerificationInitDto identityVerificationInitDto = new IdentityVerificationInitDto(); - identityVerificationInitDto.setIndividualId(transaction.getIndividualId()); - identityVerificationInitDto.setDisabilityType(transaction.getDisabilityType()); - plugin.initialize(identityVerificationRequest.getSlotId(), identityVerificationInitDto); - } + String errorCode = null; + try { + validate(identityVerificationRequest); + IdentityVerificationTransaction transaction = cacheUtilService.getVerifiedSlotTransaction(identityVerificationRequest.getSlotId()); + if(transaction == null) + throw new InvalidTransactionException(); + + IdentityVerifierPlugin plugin = identityVerifierFactory.getIdentityVerifier(transaction.getVerifierId()); + if(plugin == null) + throw new SignUpException(PLUGIN_NOT_FOUND); + + if(plugin.isStartStep(identityVerificationRequest.getStepCode())) { + IdentityVerificationInitDto identityVerificationInitDto = new IdentityVerificationInitDto(); + identityVerificationInitDto.setIndividualId(transaction.getIndividualId()); + identityVerificationInitDto.setDisabilityType(transaction.getDisabilityType()); + plugin.initialize(identityVerificationRequest.getSlotId(), identityVerificationInitDto); + } - IdentityVerificationDto dto = new IdentityVerificationDto(); - dto.setStepCode(identityVerificationRequest.getStepCode()); - dto.setFrames(identityVerificationRequest.getFrames()); - plugin.verify(identityVerificationRequest.getSlotId(), dto); + IdentityVerificationDto dto = new IdentityVerificationDto(); + dto.setStepCode(identityVerificationRequest.getStepCode()); + dto.setFrames(identityVerificationRequest.getFrames()); + plugin.verify(identityVerificationRequest.getSlotId(), dto); + } catch (SignUpException e) { + errorCode = e.getErrorCode(); + log.error("An error occurred while processing frames", e); + } finally { + if (errorCode != null) { + sendErrorFeedback(identityVerificationRequest.getSlotId(), errorCode); + } + } } public void processVerificationResult(IdentityVerificationResult identityVerificationResult) { @@ -178,4 +188,29 @@ private long getVerificationProcessExpireTimeInMillis(IdentityVerifierDetail ide int processDurationInSeconds = identityVerifierDetail.getProcessDuration() <= 0 ? slotExpireInSeconds : identityVerifierDetail.getProcessDuration(); return System.currentTimeMillis() + ( processDurationInSeconds * 1000L ); } + + private void sendErrorFeedback(String slotId, String errorCode) { + IDVProcessFeedback idvProcessFeedback = new IDVProcessFeedback(); + idvProcessFeedback.setType(ProcessFeedbackType.ERROR); + idvProcessFeedback.setCode(errorCode); + IdentityVerificationResult identityVerificationResult = new IdentityVerificationResult(); + identityVerificationResult.setFeedback(idvProcessFeedback); + simpMessagingTemplate.convertAndSend("/topic/" + slotId, identityVerificationResult); + } + private void validate(IdentityVerificationRequest request) { + if (request.getStepCode() == null || request.getStepCode().isBlank()) { + throw new SignUpException(ErrorConstants.INVALID_STEP_CODE); + } + List frames = request.getFrames(); + if (frames != null && !frames.isEmpty()) { + for (FrameDetail frame : frames) { + if (frame.getFrame() == null || frame.getFrame().isBlank()) { + throw new SignUpException(ErrorConstants.INVALID_FRAME); + } + if (frame.getOrder() < 0) { + throw new SignUpException(ErrorConstants.INVALID_ORDER); + } + } + } + } } diff --git a/signup-service/src/test/java/io/mosip/signup/helper/NotificationHelperTest.java b/signup-service/src/test/java/io/mosip/signup/helper/NotificationHelperTest.java index 9af18c09..27de03df 100644 --- a/signup-service/src/test/java/io/mosip/signup/helper/NotificationHelperTest.java +++ b/signup-service/src/test/java/io/mosip/signup/helper/NotificationHelperTest.java @@ -46,6 +46,7 @@ public void setUp() { ReflectionTestUtils.setField(notificationHelper, "sendNotificationEndpoint", sendNotificationEndpoint); ReflectionTestUtils.setField(notificationHelper, "defaultLanguage", defaultLanguage); ReflectionTestUtils.setField(notificationHelper, "encodedLangCodes", encodedLangCodes); + ReflectionTestUtils.setField(notificationHelper, "identifierPrefix", ""); } @Test diff --git a/signup-service/src/test/java/io/mosip/signup/services/WebsocketHandlerTest.java b/signup-service/src/test/java/io/mosip/signup/services/WebsocketHandlerTest.java index d18cf1c3..03d0fcae 100644 --- a/signup-service/src/test/java/io/mosip/signup/services/WebsocketHandlerTest.java +++ b/signup-service/src/test/java/io/mosip/signup/services/WebsocketHandlerTest.java @@ -10,11 +10,11 @@ import io.mosip.signup.api.util.VerificationStatus; import io.mosip.signup.dto.IdentityVerificationRequest; import io.mosip.signup.dto.IdentityVerificationTransaction; -import io.mosip.signup.exception.InvalidTransactionException; import io.mosip.signup.exception.SignUpException; import io.mosip.signup.helper.AuditHelper; import io.mosip.signup.util.AuditEvent; import io.mosip.signup.util.AuditEventType; +import io.mosip.signup.util.ErrorConstants; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -32,7 +32,6 @@ import java.util.List; import static io.mosip.signup.api.util.ErrorConstants.IDENTITY_VERIFICATION_FAILED; -import static io.mosip.signup.api.util.ErrorConstants.PLUGIN_NOT_FOUND; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @RunWith(SpringRunner.class) @@ -105,29 +104,26 @@ public void processFrames_invalidTransaction_thenFail() { identityVerificationRequest.setSlotId("test"); identityVerificationRequest.setStepCode("stepCode"); Mockito.when(cacheUtilService.getVerifiedSlotTransaction(identityVerificationRequest.getSlotId())).thenReturn(null); - try { - webSocketHandler.processFrames(identityVerificationRequest); - Assert.fail(); - } catch (InvalidTransactionException e) { - Assert.assertNotNull(e.getErrorCode()); - } + webSocketHandler.processFrames(identityVerificationRequest); + Mockito.verify(cacheUtilService, Mockito.times(1)).getVerifiedSlotTransaction(identityVerificationRequest.getSlotId()); + Mockito.verify(simpMessagingTemplate, Mockito.times(1)) + .convertAndSend(Mockito.eq("/topic/" + identityVerificationRequest.getSlotId()), Mockito.any(IdentityVerificationResult.class)); } @Test public void processFrames_invalidVerifierId_thenFail() { IdentityVerificationRequest identityVerificationRequest = new IdentityVerificationRequest(); identityVerificationRequest.setSlotId("test"); - + identityVerificationRequest.setStepCode("stepCode"); IdentityVerificationTransaction identityVerificationTransaction = new IdentityVerificationTransaction(); identityVerificationTransaction.setVerifierId("verifier-id"); Mockito.when(cacheUtilService.getVerifiedSlotTransaction(identityVerificationRequest.getSlotId())).thenReturn(identityVerificationTransaction); Mockito.when(identityVerifierFactory.getIdentityVerifier("verifier-id")).thenReturn(null); - try { - webSocketHandler.processFrames(identityVerificationRequest); - Assert.fail(); - } catch (SignUpException e) { - Assert.assertEquals(PLUGIN_NOT_FOUND, e.getErrorCode()); - } + webSocketHandler.processFrames(identityVerificationRequest); + Mockito.verify(cacheUtilService, Mockito.times(1)).getVerifiedSlotTransaction(identityVerificationRequest.getSlotId()); + Mockito.verify(identityVerifierFactory, Mockito.times(1)).getIdentityVerifier("verifier-id"); + Mockito.verify(simpMessagingTemplate, Mockito.times(1)) + .convertAndSend(Mockito.eq("/topic/" + identityVerificationRequest.getSlotId()), Mockito.any(IdentityVerificationResult.class)); } @Test @@ -369,7 +365,7 @@ public void testValidate_WithInvalidFrameContent_thenFail() { try{ webSocketHandler.processFrames(request); }catch (SignUpException e){ - Assert.assertEquals(e.getErrorCode(),ErrorConstants.INVALID_FRAME); + Assert.assertEquals(e.getErrorCode(), ErrorConstants.INVALID_FRAME); } }