Skip to content

Commit

Permalink
Merge from release to develop
Browse files Browse the repository at this point in the history
Signed-off-by: ase-101 <[email protected]>
  • Loading branch information
ase-101 committed Dec 18, 2024
1 parent 0ac6347 commit 536c526
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,17 +23,15 @@
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;
import org.springframework.messaging.simp.SimpMessagingTemplate;
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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<FrameDetail> 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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 536c526

Please sign in to comment.