From 32174f893c6b1de4665d712a29f87220475946f7 Mon Sep 17 00:00:00 2001 From: Sreenadh S <32409698+sree96@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:30:03 +0530 Subject: [PATCH] [INJIVER-587] - add auto timer for setting expiry Signed-off-by: Sreenadh S <32409698+sree96@users.noreply.github.com> --- .../io/mosip/verifycore/shared/Config.java | 2 +- .../VerifiablePresentationRequestService.java | 2 +- .../controller/VpRequestController.java | 2 +- .../controller/VpSubmissionController.java | 4 +-- ...ifiablePresentationRequestServiceImpl.java | 35 +++++++++++++------ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/verify-core/src/main/java/io/mosip/verifycore/shared/Config.java b/verify-core/src/main/java/io/mosip/verifycore/shared/Config.java index 998b3b88..46db4819 100644 --- a/verify-core/src/main/java/io/mosip/verifycore/shared/Config.java +++ b/verify-core/src/main/java/io/mosip/verifycore/shared/Config.java @@ -1,5 +1,5 @@ package io.mosip.verifycore.shared; public class Config { - public static int DEFAULT_EXPIRY = 300; + public static int DEFAULT_EXPIRY = 300 * 1000; } diff --git a/verify-core/src/main/java/io/mosip/verifycore/spi/VerifiablePresentationRequestService.java b/verify-core/src/main/java/io/mosip/verifycore/spi/VerifiablePresentationRequestService.java index 719da292..71dba22b 100644 --- a/verify-core/src/main/java/io/mosip/verifycore/spi/VerifiablePresentationRequestService.java +++ b/verify-core/src/main/java/io/mosip/verifycore/spi/VerifiablePresentationRequestService.java @@ -6,7 +6,7 @@ public interface VerifiablePresentationRequestService { AuthorizationRequestCreateResponseDto createAuthorizationRequest(AuthorizationRequestCreateDto vpRequestCreate); - Status getStatusFor(String requestId); + Status getCurrentStatusFor(String requestId); String getTransactionIdFor(String requestId); String getStatusForRequestIdFor(String transactionId); } diff --git a/verify-service/src/main/java/io/mosip/verifyservice/controller/VpRequestController.java b/verify-service/src/main/java/io/mosip/verifyservice/controller/VpRequestController.java index 60ac53e5..80b6a2c2 100644 --- a/verify-service/src/main/java/io/mosip/verifyservice/controller/VpRequestController.java +++ b/verify-service/src/main/java/io/mosip/verifyservice/controller/VpRequestController.java @@ -33,7 +33,7 @@ public ResponseEntity createVpRequest(@Va public ResponseEntity getStatus(@PathVariable String requestId) { String transactionId = verifiablePresentationRequestService.getTransactionIdFor(requestId); - Status currentstatus = verifiablePresentationRequestService.getStatusFor(requestId); + Status currentstatus = verifiablePresentationRequestService.getCurrentStatusFor(requestId); if (currentstatus == null || transactionId == null) return new ResponseEntity<>(null, HttpStatus.NOT_FOUND); diff --git a/verify-service/src/main/java/io/mosip/verifyservice/controller/VpSubmissionController.java b/verify-service/src/main/java/io/mosip/verifyservice/controller/VpSubmissionController.java index 6d09a5c0..c5ddb15f 100644 --- a/verify-service/src/main/java/io/mosip/verifyservice/controller/VpSubmissionController.java +++ b/verify-service/src/main/java/io/mosip/verifyservice/controller/VpSubmissionController.java @@ -29,7 +29,7 @@ public class VpSubmissionController { @GetMapping(path = "/vp-result/{transactionId}") public ResponseEntity getVpResult(@PathVariable String transactionId) { String requestId = verifiablePresentationRequestService.getStatusForRequestIdFor(transactionId); - Status authRequestStatus = verifiablePresentationRequestService.getStatusFor(requestId); + Status authRequestStatus = verifiablePresentationRequestService.getCurrentStatusFor(requestId); if (transactionId.isEmpty() || authRequestStatus != Status.COMPLETED) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); @@ -48,7 +48,7 @@ public ResponseEntity submitVp(@RequestParam(value = "v VpSubmissionDto vpSubmissionDto = new VpSubmissionDto(vpToken, presentationSubmissionDto, state); System.out.println(vpSubmissionDto); - Status authRequestStatus = verifiablePresentationRequestService.getStatusFor(vpSubmissionDto.getState()); + Status authRequestStatus = verifiablePresentationRequestService.getCurrentStatusFor(vpSubmissionDto.getState()); if (authRequestStatus == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } diff --git a/verify-service/src/main/java/io/mosip/verifyservice/services/VerifiablePresentationRequestServiceImpl.java b/verify-service/src/main/java/io/mosip/verifyservice/services/VerifiablePresentationRequestServiceImpl.java index 453a65fc..b6b6beb3 100644 --- a/verify-service/src/main/java/io/mosip/verifyservice/services/VerifiablePresentationRequestServiceImpl.java +++ b/verify-service/src/main/java/io/mosip/verifyservice/services/VerifiablePresentationRequestServiceImpl.java @@ -16,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.Instant; +import java.util.Timer; +import java.util.TimerTask; import static io.mosip.verifycore.shared.Config.DEFAULT_EXPIRY; @@ -44,22 +46,14 @@ public AuthorizationRequestCreateResponseDto createAuthorizationRequest(Authoriz presentationDefinitionRepository.save(presentationDefinition); authorizationRequestCreateResponseRepository.save(authorizationRequestCreateResponse); + startStatusAutoTimer(requestId); return new AuthorizationRequestCreateResponseDto(authorizationRequestCreateResponse); } @Override - public Status getStatusFor(String requestId) { - return authorizationRequestCreateResponseRepository.findById(requestId).map(authorizationRequestCreateResponse -> { - Status currentStatus = authorizationRequestCreateResponse.getStatus(); - System.out.println(currentStatus); - if (currentStatus == Status.PENDING && authorizationRequestCreateResponse.getExpiresAt() < Instant.now().toEpochMilli()){ - authorizationRequestCreateResponse.setStatus(Status.EXPIRED); - authorizationRequestCreateResponseRepository.save(authorizationRequestCreateResponse); - return Status.EXPIRED; - } - return currentStatus; - }).orElse(null); + public Status getCurrentStatusFor(String requestId) { + return authorizationRequestCreateResponseRepository.findById(requestId).map(AuthorizationRequestCreateResponse::getStatus).orElse(null); } @Override @@ -71,4 +65,23 @@ public String getTransactionIdFor(String requestId) { public String getStatusForRequestIdFor(String transactionId) { return authorizationRequestCreateResponseRepository.findFirstByTransactionIdOrderByExpiresAtDesc(transactionId).map(AuthorizationRequestCreateResponse::getRequestId).orElse(null); } + + private void updateStatusToExpired(String requestId){ + authorizationRequestCreateResponseRepository.findById(requestId).map(authorizationRequestCreateResponse -> { + if (authorizationRequestCreateResponse.getStatus() == Status.PENDING){ + authorizationRequestCreateResponse.setStatus(Status.EXPIRED); + authorizationRequestCreateResponseRepository.save(authorizationRequestCreateResponse); + } + return null; + }); + } + + private void startStatusAutoTimer(String requestId) { + new Timer().schedule(new TimerTask() { + @Override + public void run() { + updateStatusToExpired(requestId); + } + }, DEFAULT_EXPIRY); + } }