Skip to content

Commit

Permalink
[INJIVER-587] - add auto timer for setting expiry
Browse files Browse the repository at this point in the history
Signed-off-by: Sreenadh S <[email protected]>
  • Loading branch information
sree96 committed Nov 27, 2024
1 parent 3632cea commit 5c3b25d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ResponseEntity<AuthorizationRequestCreateResponseDto> createVpRequest(@Va
public ResponseEntity<StatusResponseDto> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class VpSubmissionController {
@GetMapping(path = "/vp-result/{transactionId}")
public ResponseEntity<SubmissionResultDto> 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);
Expand All @@ -48,7 +48,7 @@ public ResponseEntity<VpSubmissionResponseDto> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 5c3b25d

Please sign in to comment.