Skip to content

Commit

Permalink
[INJIVER-587] - remove state ana use dynamic calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Sreenadh S <[email protected]>
  • Loading branch information
sree96 committed Dec 13, 2024
1 parent 663470a commit 2019970
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.inji.verify.controller;

import io.inji.verify.dto.authorizationRequest.StatusDto;
import io.inji.verify.dto.authorizationRequest.VPRequestCreateDto;
import io.inji.verify.dto.authorizationRequest.VPRequestResponseDto;
import io.inji.verify.dto.authorizationRequest.StatusDto;
import io.inji.verify.enums.SubmissionState;
import io.inji.verify.spi.VerifiablePresentationRequestService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -31,15 +30,10 @@ public ResponseEntity<VPRequestResponseDto> createVPRequest(@Valid @RequestBody
@GetMapping(path = "/{requestId}/status")
public ResponseEntity<StatusDto> getStatus(@PathVariable String requestId) {

//is there submisn --> completed
// no submisn --> is expired --> expired
// no submisn --> is not expired --> pending

String transactionId = verifiablePresentationRequestService.getTransactionIdFor(requestId);
SubmissionState currentSubmissionState = verifiablePresentationRequestService.getCurrentAuthorizationRequestStateFor(requestId);
if (currentSubmissionState == null || transactionId == null)
StatusDto statusDto = verifiablePresentationRequestService.getCurrentAuthorizationRequestStateFor(requestId);
if (statusDto == null)
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);

return new ResponseEntity<>(new StatusDto(currentSubmissionState), HttpStatus.OK);
return new ResponseEntity<>(statusDto, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.inji.verify.controller;

import io.inji.verify.dto.authorizationRequest.StatusDto;
import io.inji.verify.dto.submission.PresentationSubmissionDto;
import io.inji.verify.dto.submission.ResponseAcknowledgementDto;
import io.inji.verify.dto.submission.VPSubmissionDto;
Expand Down Expand Up @@ -30,9 +31,9 @@ public class VPSubmissionController {
@GetMapping(path = "/vp-result/{transactionId}")
public ResponseEntity<VPTokenResultDto> getVPResult(@PathVariable String transactionId) {
String requestId = verifiablePresentationRequestService.getLatestRequestIdFor(transactionId);
SubmissionState authRequestState = verifiablePresentationRequestService.getCurrentAuthorizationRequestStateFor(requestId);
StatusDto authRequestState = verifiablePresentationRequestService.getCurrentAuthorizationRequestStateFor(requestId);

if (transactionId.isEmpty() || authRequestState != SubmissionState.COMPLETED) {
if (transactionId.isEmpty() || authRequestState.getSubmissionState() != SubmissionState.COMPLETED) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

Expand All @@ -49,7 +50,7 @@ public ResponseEntity<ResponseAcknowledgementDto> submitVP(@RequestParam(value =
VPSubmissionDto vpSubmissionDto = new VPSubmissionDto(vpToken, presentationSubmissionDto, state);
verifiablePresentationSubmissionService.submit(vpSubmissionDto);

SubmissionState authRequestState = verifiablePresentationRequestService.getCurrentAuthorizationRequestStateFor(vpSubmissionDto.getState());
StatusDto authRequestState = verifiablePresentationRequestService.getCurrentAuthorizationRequestStateFor(vpSubmissionDto.getState());
if (authRequestState == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@ public class AuthorizationRequestCreateResponse implements Serializable {
@NotNull
@Column
long expiresAt;

@NotNull
SubmissionState submissionState = SubmissionState.PENDING;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.inji.verify.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.nimbusds.jose.shaded.gson.annotations.SerializedName;
import io.inji.verify.dto.submission.PresentationSubmissionDto;
import io.inji.verify.enums.SubmissionStatus;
import jakarta.persistence.Column;
Expand All @@ -21,7 +23,9 @@
@NoArgsConstructor
public class VPSubmission {
@Id
String state;
@JsonProperty("state")
@SerializedName("state")
String requestId;

@JdbcTypeCode(SqlTypes.CLOB)
String vpToken;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.inji.verify.services;

import io.inji.verify.dto.authorizationRequest.AuthorizationRequestResponseDto;
import io.inji.verify.dto.authorizationRequest.StatusDto;
import io.inji.verify.dto.authorizationRequest.VPRequestCreateDto;
import io.inji.verify.dto.authorizationRequest.VPRequestResponseDto;
import io.inji.verify.dto.presentation.PresentationDefinitionDto;
import io.inji.verify.models.AuthorizationRequestCreateResponse;
import io.inji.verify.models.PresentationDefinition;
import io.inji.verify.models.VPSubmission;
import io.inji.verify.repository.VPSubmissionRepository;
import io.inji.verify.shared.Constants;
import io.inji.verify.spi.VerifiablePresentationRequestService;
import io.inji.verify.utils.SecurityUtils;
Expand All @@ -26,6 +29,8 @@ public class VerifiablePresentationRequestServiceImpl implements VerifiablePrese
PresentationDefinitionRepository presentationDefinitionRepository;
@Autowired
AuthorizationRequestCreateResponseRepository authorizationRequestCreateResponseRepository;
@Autowired
VPSubmissionRepository vpSubmissionRepository;
public VerifiablePresentationRequestServiceImpl() {}

@Override
Expand All @@ -40,18 +45,29 @@ public VPRequestResponseDto createAuthorizationRequest(VPRequestCreateDto vpRequ
PresentationDefinition presentationDefinition = new PresentationDefinition(presentationDefinitionDto.getId(),presentationDefinitionDto.getInputDescriptors(), presentationDefinitionDto.getSubmissionRequirements());

AuthorizationRequestResponseDto authorizationRequestResponseDto = new AuthorizationRequestResponseDto(vpRequestCreate.getClientId(), presentationDefinition,nonce);
AuthorizationRequestCreateResponse authorizationRequestCreateResponse = new AuthorizationRequestCreateResponse(requestId, transactionId, authorizationRequestResponseDto, expiresAt, SubmissionState.PENDING);
AuthorizationRequestCreateResponse authorizationRequestCreateResponse = new AuthorizationRequestCreateResponse(requestId, transactionId, authorizationRequestResponseDto, expiresAt);

presentationDefinitionRepository.save(presentationDefinition);
authorizationRequestCreateResponseRepository.save(authorizationRequestCreateResponse);
startStatusAutoTimer(requestId);

return new VPRequestResponseDto(authorizationRequestCreateResponse.getTransactionId(),authorizationRequestCreateResponse.getRequestId(),authorizationRequestCreateResponse.getAuthorizationDetails(),authorizationRequestCreateResponse.getExpiresAt());
}

@Override
public SubmissionState getCurrentAuthorizationRequestStateFor(String requestId) {
return authorizationRequestCreateResponseRepository.findById(requestId).map(AuthorizationRequestCreateResponse::getSubmissionState).orElse(null);
public StatusDto getCurrentAuthorizationRequestStateFor(String requestId) {
VPSubmission vpSubmission = vpSubmissionRepository.findById(requestId).orElse(null);

if (vpSubmission != null) {
return new StatusDto(SubmissionState.COMPLETED);
}
Long expiresAt = authorizationRequestCreateResponseRepository.findById(requestId).map(AuthorizationRequestCreateResponse::getExpiresAt).orElse(null);
if (expiresAt == null){
return null;
}
if(Instant.now().toEpochMilli() > expiresAt){
return new StatusDto(SubmissionState.EXPIRED);
}
return new StatusDto(SubmissionState.PENDING);
}

@Override
Expand All @@ -63,23 +79,4 @@ public String getTransactionIdFor(String requestId) {
public String getLatestRequestIdFor(String transactionId) {
return authorizationRequestCreateResponseRepository.findFirstByTransactionIdOrderByExpiresAtDesc(transactionId).map(AuthorizationRequestCreateResponse::getRequestId).orElse(null);
}

private void updateStatusToExpired(String requestId){
authorizationRequestCreateResponseRepository.findById(requestId).map(authorizationRequestCreateResponse -> {
if (authorizationRequestCreateResponse.getSubmissionState() == SubmissionState.PENDING){
authorizationRequestCreateResponse.setSubmissionState(SubmissionState.EXPIRED);
authorizationRequestCreateResponseRepository.save(authorizationRequestCreateResponse);
}
return null;
});
}

private void startStatusAutoTimer(String requestId) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
updateStatusToExpired(requestId);
}
}, Constants.DEFAULT_EXPIRY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.auth0.jwt.algorithms.Algorithm;
import io.inji.verify.dto.submission.ResponseAcknowledgementDto;
import io.inji.verify.dto.submission.VPSubmissionDto;
import io.inji.verify.dto.submission.VPTokenResultDto;
import io.inji.verify.enums.SubmissionStatus;
import io.inji.verify.enums.VerificationStatus;
import io.inji.verify.exception.VerificationFailedException;
import io.inji.verify.models.VCResult;
import io.inji.verify.models.VPSubmission;
import io.inji.verify.repository.AuthorizationRequestCreateResponseRepository;
import io.inji.verify.repository.VPSubmissionRepository;
Expand All @@ -18,10 +21,6 @@
import io.inji.verify.utils.Utils;
import io.mosip.vercred.vcverifier.constants.CredentialFormat;
import io.mosip.vercred.vcverifier.data.VerificationResult;
import io.inji.verify.dto.submission.VPTokenResultDto;
import io.inji.verify.enums.SubmissionState;
import io.inji.verify.enums.SubmissionStatus;
import io.inji.verify.models.VCResult;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -80,7 +79,6 @@ private void processSubmission(VPSubmissionDto vpSubmissionDto) {
}

authorizationRequestCreateResponseRepository.findById(vpSubmissionDto.getState()).map(authorizationRequestCreateResponse -> {
authorizationRequestCreateResponse.setSubmissionState(SubmissionState.COMPLETED);
authorizationRequestCreateResponseRepository.save(authorizationRequestCreateResponse);
return null;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.inji.verify.spi;

import io.inji.verify.dto.authorizationRequest.StatusDto;
import io.inji.verify.dto.authorizationRequest.VPRequestCreateDto;
import io.inji.verify.dto.authorizationRequest.VPRequestResponseDto;
import io.inji.verify.enums.SubmissionState;

public interface VerifiablePresentationRequestService {
VPRequestResponseDto createAuthorizationRequest(VPRequestCreateDto vpRequestCreate);
SubmissionState getCurrentAuthorizationRequestStateFor(String requestId);

StatusDto getCurrentAuthorizationRequestStateFor(String requestId);
String getTransactionIdFor(String requestId);
String getLatestRequestIdFor(String transactionId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.inji.verify.services;

import io.inji.verify.dto.authorizationRequest.StatusDto;
import io.inji.verify.dto.authorizationRequest.VPRequestCreateDto;
import io.inji.verify.dto.authorizationRequest.VPRequestResponseDto;
import io.inji.verify.dto.presentation.InputDescriptorDto;
Expand All @@ -10,12 +11,14 @@
import io.inji.verify.enums.SubmissionState;
import io.inji.verify.models.AuthorizationRequestCreateResponse;
import io.inji.verify.models.PresentationDefinition;
import io.inji.verify.repository.VPSubmissionRepository;
import io.inji.verify.shared.Constants;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -26,24 +29,23 @@ class VerifiablePresentationRequestServiceImplTest {
static VerifiablePresentationRequestServiceImpl service;
static AuthorizationRequestCreateResponseRepository mockAuthorizationRequestCreateResponseRepository;
static PresentationDefinitionRepository mockPresentationDefinitionRepository;
static VPSubmissionRepository mockVPSubmissionRepository;
@BeforeAll
public static void beforeAll(){
mockPresentationDefinitionRepository = mock(PresentationDefinitionRepository.class);
mockAuthorizationRequestCreateResponseRepository = mock(AuthorizationRequestCreateResponseRepository.class);
mockVPSubmissionRepository = mock(VPSubmissionRepository.class);
service = new VerifiablePresentationRequestServiceImpl();
service.presentationDefinitionRepository = mockPresentationDefinitionRepository;
service.authorizationRequestCreateResponseRepository = mockAuthorizationRequestCreateResponseRepository;
service.vpSubmissionRepository = mockVPSubmissionRepository;

}
@Test
public void shouldCreateNewAuthorizationRequest() {

when(mockPresentationDefinitionRepository.save(any(PresentationDefinition.class))).thenReturn(null);
when(mockAuthorizationRequestCreateResponseRepository.save(any(AuthorizationRequestCreateResponse.class))).thenReturn(null);

service.presentationDefinitionRepository = mockPresentationDefinitionRepository;
service.authorizationRequestCreateResponseRepository = mockAuthorizationRequestCreateResponseRepository;

VPRequestCreateDto vpRequestCreateDto = new VPRequestCreateDto();
vpRequestCreateDto.setTransactionId("test_transaction_id");
vpRequestCreateDto.setClientId("test_client_id");
Expand Down Expand Up @@ -76,19 +78,23 @@ public void shouldCreateAuthorizationRequestWithMissingTransactionId() {

@Test
public void shouldGetCurrentAuthorizationRequestStateForExistingRequest() {
AuthorizationRequestCreateResponse mockResponse = new AuthorizationRequestCreateResponse("req_id", "tx_id", null, 0, SubmissionState.PENDING);
AuthorizationRequestCreateResponse mockResponse = new AuthorizationRequestCreateResponse("req_id", "tx_id", null, Instant.now().toEpochMilli()+10000);
when(mockAuthorizationRequestCreateResponseRepository.findById("req_id")).thenReturn(java.util.Optional.of(mockResponse));
when(mockVPSubmissionRepository.findById("req_id")).thenReturn(Optional.empty());

SubmissionState state = service.getCurrentAuthorizationRequestStateFor("req_id");
StatusDto state = service.getCurrentAuthorizationRequestStateFor("req_id");

assertEquals(SubmissionState.PENDING, state);
assertEquals(SubmissionState.PENDING, state.getSubmissionState());
}

@Test
public void shouldGetCurrentAuthorizationRequestStateForNonexistentRequest() {
when(mockAuthorizationRequestCreateResponseRepository.findById("nonexistent_id")).thenReturn(java.util.Optional.empty());
when(mockVPSubmissionRepository.findById("req_id")).thenReturn(Optional.empty());
AuthorizationRequestCreateResponse mockResponse = new AuthorizationRequestCreateResponse("req_id", "tx_id", null, Instant.now().toEpochMilli()+10000);
when(mockAuthorizationRequestCreateResponseRepository.findById("req_id")).thenReturn(java.util.Optional.of(mockResponse));


SubmissionState state = service.getCurrentAuthorizationRequestStateFor("nonexistent_id");
StatusDto state = service.getCurrentAuthorizationRequestStateFor("nonexistent_id");

assertNull(state);
}
Expand Down
Loading

0 comments on commit 2019970

Please sign in to comment.