Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public enum IdRepoErrorConstants {

UIN_GENERATION_FAILED("IDR-IDS-011","Failed to generate UIN"),

/** The authorization failed. */
AUTHENTICATION_FAILED("IDR-IDS-012", "Authentication Failed"),

INVALID_BIOMETRIC("IDR-IDS-013", "Failed to extract the valid biometric data"),

// VID Service

/** The invalid vid. */
Expand All @@ -109,9 +114,6 @@ public enum IdRepoErrorConstants {
/** The uin hash mismatch. */
UIN_HASH_MISMATCH("IDR-VID-006", "Uin hash does not match"),

/** The authorization failed. */
/** The document hash mismatch. */
AUTHENTICATION_FAILED("IDR-IDS-012", "Authentication Failed"),

INVALID_CBEFF("IDR-BIE-001", "Invalid CBEFF"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.mosip.idrepository.core.constant.IdRepoConstants.EXTRACTION_FORMAT_QUERY_PARAM_SUFFIX;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.BIO_EXTRACTION_ERROR;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.UNKNOWN_ERROR;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.INVALID_BIOMETRIC;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,21 +64,33 @@ public class BiometricExtractionServiceImpl implements BiometricExtractionServic
* @param fileName the file name
* @param extractionType the extraction type
* @param extractionFormat the extraction format
* @param birsForModality the birs for modality
* @param cbeffBirsForModality the birs for modality
* @return the completable future
* @throws IdRepoAppException the id repo app exception
*/
@Async
public CompletableFuture<List<BIR>> extractTemplate(String uinHash, String fileName,
String extractionType, String extractionFormat, List<BIR> birsForModality) throws IdRepoAppException {
String extractionType, String extractionFormat, List<BIR> cbeffBirsForModality) throws IdRepoAppException {
try {
String extractionFileName = fileName.split("\\.")[0] + DOT + getModalityForFormat(extractionType) + DOT + extractionFormat;
Map<String, String> formatFlag = Map.of(getFormatFlag(extractionType), extractionFormat);
try {
if (objectStoreHelper.biometricObjectExists(uinHash, extractionFileName)) {
mosipLogger.info(IdRepoSecurityManager.getUser(), this.getClass().getSimpleName(), EXTRACT_TEMPLATE,
"RETURNING EXISTING EXTRACTED BIOMETRICS FOR FORMAT: " + extractionType +" : "+ extractionFormat);
byte[] xmlBytes = objectStoreHelper.getBiometricObject(uinHash, extractionFileName);
List<BIR> existingBirs = cbeffUtil.getBIRDataFromXML(xmlBytes);
List<BIR> existingBirs;
try {
existingBirs = cbeffUtil.getBIRDataFromXML(xmlBytes);
} catch (Exception e) {
existingBirs = List.of();
mosipLogger.error(IdRepoSecurityManager.getUser(), this.getClass().getSimpleName(),
EXTRACT_TEMPLATE, e.getMessage());
}
if (!validateCbeff(existingBirs, cbeffBirsForModality)) {
return extractBiometricTemplateData(uinHash, extractionFileName, formatFlag,
cbeffBirsForModality);
}
return CompletableFuture.completedFuture(existingBirs);
}
} catch (ObjectStoreAdapterException e) {
Expand All @@ -87,20 +100,28 @@ public CompletableFuture<List<BIR>> extractTemplate(String uinHash, String fileN

mosipLogger.info(IdRepoSecurityManager.getUser(), this.getClass().getSimpleName(), EXTRACT_TEMPLATE,
"EXTRATCING BIOMETRICS FOR FORMAT: " + extractionType +" : "+ extractionFormat);
Map<String, String> formatFlag = Map.of(getFormatFlag(extractionType), extractionFormat);
List<BIR> extractedBiometrics = extractBiometricTemplate(formatFlag, birsForModality);
if (!extractedBiometrics.isEmpty()) {
objectStoreHelper.putBiometricObject(uinHash, extractionFileName, cbeffUtil.createXML(extractedBiometrics));
}
return CompletableFuture.completedFuture(extractedBiometrics);
return extractBiometricTemplateData(uinHash, extractionFileName, formatFlag, cbeffBirsForModality);
} catch (BiometricExtractionException e) {
if(e.getErrorCode().equalsIgnoreCase(INVALID_BIOMETRIC.getErrorCode())) {
throw e;
}
mosipLogger.error(IdRepoSecurityManager.getUser(), this.getClass().getSimpleName(), EXTRACT_TEMPLATE, e.getMessage());
throw new IdRepoAppException(BIO_EXTRACTION_ERROR, e);
} catch (Exception e) {
mosipLogger.error(IdRepoSecurityManager.getUser(), this.getClass().getSimpleName(), EXTRACT_TEMPLATE, e.getMessage());
throw new IdRepoAppException(UNKNOWN_ERROR, e);
}
}

private CompletableFuture<List<BIR>> extractBiometricTemplateData(String uinHash, String extractionFileName,
Map<String, String> formatFlag, List<BIR> cbeffBirsForModality)
throws BiometricExtractionException, IdRepoAppException, Exception {
List<BIR> extractedBiometrics = extractBiometricTemplate(formatFlag, cbeffBirsForModality);
if (!extractedBiometrics.isEmpty()) {
objectStoreHelper.putBiometricObject(uinHash, extractionFileName, cbeffUtil.createXML(extractedBiometrics));
}
return CompletableFuture.completedFuture(extractedBiometrics);
}

/**
* Gets the format flag.
Expand All @@ -126,20 +147,24 @@ private String getModalityForFormat(String formatQueryParam) {
* Extract biometric template.
*
* @param extractionFormats the extraction formats
* @param birs the birs
* @param cbeffBirsForModality the birs
* @return the list
* @throws BiometricExtractionException the biometric extraction exception
*/
private List<BIR> extractBiometricTemplate(Map<String, String> extractionFormats, List<BIR> birs)
private List<BIR> extractBiometricTemplate(Map<String, String> extractionFormats, List<BIR> cbeffBirsForModality)
throws BiometricExtractionException {
mosipLogger.debug(IdRepoSecurityManager.getUser(), this.getClass().getSimpleName(), "extractBiometricTemplate",
"INVOKING BIOMETRIC EXTRACTION FOR THE FORMAT: " + extractionFormats);

BioExtractRequestDTO bioExtractReq = new BioExtractRequestDTO();
bioExtractReq.setBiometrics(birs);
bioExtractReq.setBiometrics(cbeffBirsForModality);
bioExtractReq.setExtractionFormats(extractionFormats);

BioExtractResponseDTO bioExtractResponseDTO = extractBiometrics(bioExtractReq);
if (!validateCbeff(bioExtractResponseDTO.getExtractedBiometrics(), cbeffBirsForModality)) {
throw new BiometricExtractionException(INVALID_BIOMETRIC.getErrorCode(),
String.format(INVALID_BIOMETRIC.getErrorMessage()));
}
return bioExtractResponseDTO.getExtractedBiometrics();
}

Expand All @@ -153,23 +178,26 @@ private List<BIR> extractBiometricTemplate(Map<String, String> extractionFormats
private BioExtractResponseDTO extractBiometrics(BioExtractRequestDTO bioExtractRequestDTO)
throws BiometricExtractionException {
BioExtractResponseDTO bioExtractPromiseResponseDTO = new BioExtractResponseDTO();
List<BIR> birs = bioExtractRequestDTO.getBiometrics();
List<BIR> encodedExtractedBiometrics = doBioExtraction(birs, bioExtractRequestDTO.getExtractionFormats());
List<BIR> encodedExtractedBiometrics = doBioExtraction(bioExtractRequestDTO.getBiometrics(), bioExtractRequestDTO.getExtractionFormats());
bioExtractPromiseResponseDTO.setExtractedBiometrics(encodedExtractedBiometrics);
return bioExtractPromiseResponseDTO;
}

/**
* Do bio extraction.
*
* @param birs the birs
* @param cbeffBirsForModality the birs
* @param extractionFormats the extraction formats
* @return the list
* @throws BiometricExtractionException the biometric extraction exception
*/
private List<BIR> doBioExtraction(List<BIR> birs, Map<String, String> extractionFormats)
private List<BIR> doBioExtraction(List<BIR> cbeffBirsForModality, Map<String, String> extractionFormats)
throws BiometricExtractionException {
return bioExractionHelper.extractTemplates(birs, extractionFormats);
return bioExractionHelper.extractTemplates(cbeffBirsForModality, extractionFormats);
}

private boolean validateCbeff(List<BIR> extractedBirs, List<BIR> cbeffBirsForModality) {
return extractedBirs != null && cbeffBirsForModality != null
&& Integer.valueOf(extractedBirs.size()).equals(Integer.valueOf(cbeffBirsForModality.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.ID_OBJECT_PROCESSING_FAILED;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.NO_RECORD_FOUND;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.RECORD_EXISTS;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.INVALID_BIOMETRIC;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -452,6 +453,10 @@ protected byte[] getBiometricsForRequestedFormats(String uinHash, String fileNam
mosipLogger.error(IdRepoSecurityManager.getUser(), ID_REPO_SERVICE_IMPL, "extractTemplate", e.getMessage());
throw new IdRepoAppException(BIO_EXTRACTION_ERROR, e);
} catch (Exception e) {
ExceptionUtils.getStackTrace(e);
if(e.getMessage().contains(INVALID_BIOMETRIC.getErrorCode())) {
throw new IdRepoAppException(INVALID_BIOMETRIC, e);
}
mosipLogger.error(IdRepoSecurityManager.getUser(), ID_REPO_SERVICE_IMPL, "extractTemplate", e.getMessage());
throw new IdRepoAppException(BIO_EXTRACTION_ERROR, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.io.IOUtils;
Expand All @@ -25,6 +26,7 @@

import io.mosip.commons.khazana.exception.ObjectStoreAdapterException;
import io.mosip.idrepository.core.constant.IdRepoErrorConstants;
import io.mosip.idrepository.core.constant.IdType;
import io.mosip.idrepository.core.exception.BiometricExtractionException;
import io.mosip.idrepository.core.exception.IdRepoAppException;
import io.mosip.idrepository.core.util.EnvUtil;
Expand Down Expand Up @@ -91,8 +93,12 @@ public void testExtractTemplateExtractedBioIsEmpty() throws Exception {
when(objectStoreHelper.getBiometricObject(any(), any())).thenReturn(CryptoUtil.decodeURLSafeBase64(cbeff));
when(cbeffUtil.getBIRDataFromXML(any())).thenReturn(birDataFromXMLType);
when(bioExractionHelper.extractTemplates(any(), any())).thenReturn(List.of());
CompletableFuture<List<BIR>> extractTemplate = extractionServiceImpl.extractTemplate("", "", "a", "ExtractionFormat", birDataFromXMLType);
assertEquals(0, extractTemplate.join().size());
try {
extractionServiceImpl.extractTemplate("", "", "a", "ExtractionFormat", birDataFromXMLType);
} catch (BiometricExtractionException e) {
assertEquals(IdRepoErrorConstants.INVALID_BIOMETRIC.getErrorCode(), e.getErrorCode());
assertEquals(IdRepoErrorConstants.INVALID_BIOMETRIC.getErrorMessage(), e.getErrorText());
}
verify(objectStoreHelper, never()).putBiometricObject(any(), any(), any());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.mosip.idrepository.identity.test.service.impl;

import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.INVALID_BIOMETRIC;
import static io.mosip.idrepository.core.constant.IdRepoErrorConstants.INVALID_INPUT_PARAMETER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -65,6 +66,7 @@
import io.mosip.idrepository.core.dto.ResponseDTO;
import io.mosip.idrepository.core.dto.RestRequestDTO;
import io.mosip.idrepository.core.entity.CredentialRequestStatus;
import io.mosip.idrepository.core.exception.BiometricExtractionException;
import io.mosip.idrepository.core.exception.IdRepoAppException;
import io.mosip.idrepository.core.exception.IdRepoAppUncheckedException;
import io.mosip.idrepository.core.exception.IdRepoDataValidationException;
Expand Down Expand Up @@ -2027,7 +2029,8 @@ public void testRetrieveIdentityWithBioDocumentsBioExtractionFailed() throws Exc
when(cbeffUtil.createXML(any())).thenReturn(cbeffXml);
when(objectStoreHelper.getBiometricObject(Mockito.any(), Mockito.any())).thenReturn(cbeffXml);
when(biometricExtractionService.extractTemplate(any(), any(), any(), any(), any()))
.thenThrow(new NullPointerException());
.thenThrow(new BiometricExtractionException(INVALID_BIOMETRIC.getErrorCode(),
String.format(INVALID_BIOMETRIC.getErrorMessage())));
Uin uinObj = new Uin();
uinObj.setUin("1234");
uinObj.setUinRefId("1234");
Expand All @@ -2048,8 +2051,8 @@ public void testRetrieveIdentityWithBioDocumentsBioExtractionFailed() throws Exc
try {
proxyService.retrieveIdentity("1234", IdType.UIN, "bio", Map.of("fingerExtractionFormat", "format"));
} catch (IdRepoAppException e) {
assertEquals(IdRepoErrorConstants.BIO_EXTRACTION_ERROR.getErrorCode(), e.getErrorCode());
assertEquals(IdRepoErrorConstants.BIO_EXTRACTION_ERROR.getErrorMessage(), e.getErrorText());
assertEquals(IdRepoErrorConstants.INVALID_BIOMETRIC.getErrorCode(), e.getErrorCode());
assertEquals(IdRepoErrorConstants.INVALID_BIOMETRIC.getErrorMessage(), e.getErrorText());
}
}

Expand Down