-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/auto france identite num (#707)
* feat(process-file): Add France Identité Numerique API project * chore: 🤖 Add api france identite project and readme to deploy * chore: 🤖 add default value for france_identite_api_url
- Loading branch information
Showing
15 changed files
with
243 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...-common-library/src/main/java/fr/dossierfacile/common/entity/FranceIdentiteApiResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package fr.dossierfacile.common.entity; | ||
|
||
import fr.dossierfacile.common.entity.ocr.ParsedFile; | ||
import fr.dossierfacile.common.enums.ParsedFileClassification; | ||
import fr.dossierfacile.common.enums.ParsedStatus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@Builder | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class FranceIdentiteApiResult implements ParsedFile { | ||
@Builder.Default | ||
ParsedFileClassification classification = ParsedFileClassification.FRANCE_IDENTITE; | ||
ParsedStatus parsedStatus; | ||
String status; | ||
String familyName; | ||
String givenName; | ||
String birthDate; | ||
String birthPlace; | ||
String gender; | ||
String validityDate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...ile/process/file/service/documentrules/FranceIdentiteNumeriqueRulesValidationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package fr.dossierfacile.process.file.service.documentrules; | ||
|
||
import fr.dossierfacile.common.entity.Document; | ||
import fr.dossierfacile.common.entity.DocumentAnalysisReport; | ||
import fr.dossierfacile.common.entity.DocumentAnalysisStatus; | ||
import fr.dossierfacile.common.entity.DocumentBrokenRule; | ||
import fr.dossierfacile.common.entity.DocumentRule; | ||
import fr.dossierfacile.common.entity.File; | ||
import fr.dossierfacile.common.entity.FranceIdentiteApiResult; | ||
import fr.dossierfacile.common.entity.ParsedFileAnalysis; | ||
import fr.dossierfacile.common.entity.Person; | ||
import fr.dossierfacile.common.enums.ParsedFileAnalysisStatus; | ||
import fr.dossierfacile.common.enums.ParsedFileClassification; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static fr.dossierfacile.common.enums.DocumentSubCategory.FRANCE_IDENTITE; | ||
import static fr.dossierfacile.process.file.util.NameUtil.normalizeName; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FranceIdentiteNumeriqueRulesValidationService implements RulesValidationService { | ||
|
||
@Override | ||
public boolean shouldBeApplied(Document document) { | ||
return document.getDocumentSubCategory() == FRANCE_IDENTITE; | ||
} | ||
|
||
@Override | ||
public DocumentAnalysisReport process(Document document, DocumentAnalysisReport report) { | ||
List<DocumentBrokenRule> brokenRules = Optional.ofNullable(report.getBrokenRules()) | ||
.orElseGet(() -> { | ||
report.setBrokenRules(new LinkedList<>()); | ||
return report.getBrokenRules(); | ||
}); | ||
for (File dfFile : document.getFiles()) { | ||
ParsedFileAnalysis analysis = dfFile.getParsedFileAnalysis(); | ||
if (analysis == null || analysis.getAnalysisStatus() == ParsedFileAnalysisStatus.FAILED) { | ||
continue; | ||
} | ||
if (analysis.getClassification() == ParsedFileClassification.FRANCE_IDENTITE) { | ||
FranceIdentiteApiResult parsedDocument = (FranceIdentiteApiResult) analysis.getParsedFile(); | ||
|
||
// Parse Rule | ||
if (parsedDocument == null | ||
|| parsedDocument.getStatus() == null | ||
|| parsedDocument.getFamilyName() == null | ||
|| parsedDocument.getGivenName() == null | ||
|| parsedDocument.getValidityDate() == null) { | ||
brokenRules.add(DocumentBrokenRule.builder() | ||
.rule(DocumentRule.R_FRANCE_IDENTITE_STATUS) | ||
.message(DocumentRule.R_FRANCE_IDENTITE_STATUS.getDefaultMessage()) | ||
.build()); | ||
continue; | ||
} | ||
|
||
// Fake Rule | ||
if (!("VALID".equals(parsedDocument.getStatus()))) { | ||
brokenRules.add(DocumentBrokenRule.builder() | ||
.rule(DocumentRule.R_FRANCE_IDENTITE_STATUS) | ||
.message(DocumentRule.R_FRANCE_IDENTITE_STATUS.getDefaultMessage()) | ||
.build()); | ||
continue; | ||
} | ||
|
||
// TODO : check that France Identité verifies that names on pdf matches qrcode | ||
Person documentOwner = Optional.ofNullable((Person) document.getTenant()).orElseGet(document::getGuarantor); | ||
String firstName = documentOwner.getFirstName(); | ||
String lastName = document.getName(); | ||
if (!(normalizeName(parsedDocument.getGivenName()).contains(normalizeName(firstName)) | ||
&& (normalizeName(parsedDocument.getFamilyName()).contains(normalizeName(lastName))) | ||
)) { | ||
log.error("Le nom/prenom ne correpond pas à l'utilisateur tenantId:" + document.getTenant().getId() + " firstname: " + firstName); | ||
brokenRules.add(DocumentBrokenRule.builder() | ||
.rule(DocumentRule.R_FRANCE_IDENTITE_NAMES) | ||
.message(DocumentRule.R_FRANCE_IDENTITE_NAMES.getDefaultMessage()) | ||
.build()); | ||
} | ||
} | ||
} | ||
if (brokenRules.isEmpty()) { | ||
report.setAnalysisStatus(DocumentAnalysisStatus.CHECKED); | ||
} else if (brokenRules.stream().anyMatch(r -> r.getRule().getLevel() == DocumentRule.Level.CRITICAL)) { | ||
report.setAnalysisStatus(DocumentAnalysisStatus.DENIED); | ||
} else { | ||
report.setAnalysisStatus(DocumentAnalysisStatus.UNDEFINED); | ||
} | ||
return report; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ile/src/main/java/fr/dossierfacile/process/file/service/parsers/FranceIdentiteParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package fr.dossierfacile.process.file.service.parsers; | ||
|
||
import fr.dossierfacile.common.entity.FranceIdentiteApiResult; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.FileSystemResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.client.MultipartBodyBuilder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.File; | ||
|
||
import static fr.dossierfacile.common.enums.DocumentSubCategory.FRANCE_IDENTITE; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class FranceIdentiteParser implements FileParser<FranceIdentiteApiResult> { | ||
private final RestTemplate restTemplate; | ||
private static final String CALL_BACK_RESPONSE = "France Identité callback responseStatus: {}"; | ||
|
||
@Value("${france.identite.api.url:https://dossierfacile-france-identite-numerique-api.osc-secnum-fr1.scalingo.io/api/validation/v1/check-doc-valid}") | ||
private String urlCallback; | ||
|
||
@Override | ||
public FranceIdentiteApiResult parse(File file) { | ||
ResponseEntity<FranceIdentiteApiResult> response; | ||
try { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.MULTIPART_FORM_DATA); | ||
|
||
Resource resource = new FileSystemResource(file.getPath()); | ||
MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder(); | ||
multipartBodyBuilder.part("file", resource, MediaType.APPLICATION_PDF); | ||
|
||
HttpEntity<MultiValueMap<String, HttpEntity<?>>> request = new HttpEntity<>(multipartBodyBuilder.build(), headers); | ||
response = restTemplate.exchange(urlCallback, HttpMethod.POST, request, FranceIdentiteApiResult.class); | ||
log.info(CALL_BACK_RESPONSE, response.getStatusCode()); | ||
} catch (RestClientException e) { | ||
log.error("Unable to parse"); | ||
throw new RuntimeException(e); | ||
} | ||
if ( HttpStatus.OK != response.getStatusCode() && HttpStatus.ACCEPTED != response.getStatusCode()) { | ||
log.error("Failure on France Identité check:" + urlCallback + "- Status:" + response.getStatusCode()); | ||
} | ||
return response.getBody(); | ||
} | ||
|
||
|
||
@Override | ||
public boolean shouldTryToApply(fr.dossierfacile.common.entity.File file) { | ||
return file.getDocument().getDocumentSubCategory() == FRANCE_IDENTITE | ||
&& MediaType.APPLICATION_PDF_VALUE.equalsIgnoreCase(file.getStorageFile().getContentType()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
dossierfacile-process-file/src/main/java/fr/dossierfacile/process/file/util/NameUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package fr.dossierfacile.process.file.util; | ||
|
||
import java.text.Normalizer; | ||
|
||
public class NameUtil { | ||
public static String normalizeName(String name) { | ||
if (name == null) | ||
return null; | ||
String normalized = Normalizer.normalize(name, Normalizer.Form.NFD); | ||
return normalized.replace('-', ' ') | ||
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").toUpperCase(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: java $JAVA_OPTS -jar attest-val-partenaires-api-1.0.3.jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
* Comment déployer le projet | ||
|
||
Se mettre à la racine du projet et faire un tar.gz du projet : | ||
|
||
``` | ||
tar czvf france-identite-numerique-api.tar.gz france-identite-numerique-api | ||
``` | ||
|
||
Puis déployer directement sur scalingo : | ||
|
||
``` | ||
scalingo --app nom-app deploy ./france-identite-numerique-api.tar.gz | ||
``` | ||
|
||
Pour le moment le code source de france-identite-numerique, même s'il est censé être libre et même si nous y avons accès, n'a pas de license et donc n'est pas partageable sur un dépôt publique. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
java.runtime.version=11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
java.runtime.version=21 | ||
maven.version=3.9.5 | ||
|