Skip to content

Commit

Permalink
feat: Issuer get credential API testcases, test case modification for…
Browse files Browse the repository at this point in the history
… self_issued and is_stored
  • Loading branch information
nitin-vavdiya committed Jun 7, 2023
1 parent f29edb3 commit bafaab9
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,20 @@ public VerifiableCredential issueFrameworkCredential(IssueFrameworkCredentialReq

Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());

//validate BPN access
Validate.isFalse(callerBPN.equals(baseWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));

validateAccess(callerBPN, baseWallet);
// get Key
byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId());

//if base wallet issue credentials to itself
boolean isSelfIssued = isSelfIssued(request.getBpn());

Map<String, Object> subject = Map.of("type", request.getType(),
"id", holderWallet.getDid(),
"value", request.getValue(),
"contract-template", request.getContractTemplate(),
"contract-version", request.getContractVersion());
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.USE_CASE_FRAMEWORK_CONDITION_CX);
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), false);
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued);

//save in holder wallet
holdersCredential = holdersCredentialRepository.save(holdersCredential);
Expand All @@ -233,23 +234,25 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR
Wallet holderWallet = walletService.getWalletByIdentifier(request.getBpn());

// Fetch Issuer Wallet
Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());
Wallet issuerWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());

//check BPN access
Validate.isFalse(callerBPN.equals(baseWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));
validateAccess(callerBPN, issuerWallet);

//check duplicate
isCredentialExit(holderWallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);

byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId());
byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(issuerWallet.getId());

//if base wallet issue credentials to itself
boolean isSelfIssued = isSelfIssued(request.getBpn());

Map<String, Object> subject = Map.of("type", MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL,
"id", holderWallet.getDid(),
"holderIdentifier", holderWallet.getBpn(),
"activityType", request.getActivityType(),
"allowedVehicleBrands", request.getAllowedVehicleBrands());
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), false);
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, issuerWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued);


//save in holder wallet
Expand Down Expand Up @@ -280,20 +283,23 @@ public VerifiableCredential issueMembershipCredential(IssueMembershipCredentialR
isCredentialExit(holderWallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX);

// Fetch Issuer Wallet
Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());
Wallet issuerWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());

//validate BPN access
Validate.isFalse(callerBPN.equals(baseWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));
validateAccess(callerBPN, issuerWallet);

byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId());
byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(issuerWallet.getId());
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX);

//if base wallet issue credentials to itself
boolean isSelfIssued = isSelfIssued(issueMembershipCredentialRequest.getBpn());

//VC Subject
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(Map.of("type", VerifiableCredentialType.MEMBERSHIP_CREDENTIAL,
"id", holderWallet.getDid(),
"holderIdentifier", holderWallet.getBpn(),
"memberOf", baseWallet.getName(),
"memberOf", issuerWallet.getName(),
"status", "Active",
"startTime", Instant.now().toString()), types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), false);
"startTime", Instant.now().toString()), types, issuerWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued);


//save in holder wallet
Expand Down Expand Up @@ -321,21 +327,19 @@ public VerifiableCredential issueCredentialUsingBaseWallet(Map<String, Object> d

Wallet issuerWallet = walletService.getWalletByIdentifier(verifiableCredential.getIssuer().toString());

//validate BPN access, VC must be issued by base wallet
Validate.isFalse(callerBpn.equals(issuerWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));

//issuer must be base wallet
Validate.isFalse(issuerWallet.getBpn().equals(miwSettings.authorityWalletBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));
validateAccess(callerBpn, issuerWallet);

// get Key
byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(issuerWallet.getId());

boolean isSelfIssued = isSelfIssued(issuerWallet.getBpn()); //TODO need to pass holder bpn

// Create Credential
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(verifiableCredential.getCredentialSubject().get(0),
verifiableCredential.getTypes(), issuerWallet.getDidDocument(),
privateKeyBytes,
issuerWallet.getDid(), //TODO need to check, how we can identify holder of VC, need to m
verifiableCredential.getContext(), Date.from(verifiableCredential.getExpirationDate()), false);
verifiableCredential.getContext(), Date.from(verifiableCredential.getExpirationDate()), isSelfIssued);


//save in holder wallet
Expand All @@ -349,11 +353,6 @@ public VerifiableCredential issueCredentialUsingBaseWallet(Map<String, Object> d
return issuersCredential.getData();
}


private void isCredentialExit(String holderDid, String credentialType) {
Validate.isTrue(holdersCredentialRepository.existsByHolderDidAndType(holderDid, credentialType)).launch(new DuplicateCredentialProblem("Credential of type " + credentialType + " is already exists "));
}

/**
* Credentials validation map.
*
Expand All @@ -376,4 +375,23 @@ public Map<String, Object> credentialsValidation(Map<String, Object> data) {

return response;
}


private void validateAccess(String callerBpn, Wallet issuerWallet) {
//validate BPN access, VC must be issued by base wallet
Validate.isFalse(callerBpn.equals(issuerWallet.getBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));

//issuer must be base wallet
Validate.isFalse(issuerWallet.getBpn().equals(miwSettings.authorityWalletBpn())).launch(new ForbiddenException(BASE_WALLET_BPN_IS_NOT_MATCHING_WITH_REQUEST_BPN_FROM_TOKEN));
}


private void isCredentialExit(String holderDid, String credentialType) {
Validate.isTrue(holdersCredentialRepository.existsByHolderDidAndType(holderDid, credentialType)).launch(new DuplicateCredentialProblem("Credential of type " + credentialType + " is already exists "));
}

private boolean isSelfIssued(String holderBpn) {
return holderBpn.equals(miwSettings.authorityWalletBpn());
}

}
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ miw:
encryptionKey: ${ENCRYPTION_KEY:Woh9waid4Ei5eez0aitieghoow9so4oe}
authorityWalletBpn: ${AUTHORITY_WALLET_BPN:BPNL000000000000}
authorityWalletName: ${AUTHORITY_WALLET_NAME:Catena-X}
authorityWalletDid: ${AUTHORITY_WALLET_DID:localhost:BPNL000000000000}
authorityWalletDid: ${AUTHORITY_WALLET_DID:did:web:localhost:BPNL000000000000}
vcContexts: ${VC_SCHEMA_LINK:https://www.w3.org/2018/credentials/v1, https://raw.githubusercontent.com/catenax-ng/product-core-schemas/main/businessPartnerData}
vcExpiryDate: ${VC_EXPIRY_DATE:01-01-2025} #dd-MM-yyyy ie. 01-01-2025 expiry date will be 2024-12-31T18:30:00Z in VC
supportedFrameworkVCTypes: ${SUPPORTED_FRAMEWORK_VC_TYPES:cx-behavior-twin=Behavior Twin,cx-pcf=PCF,cx-quality=Quality,cx-resiliency=Resiliency,cx-sustainability=Sustainability,cx-traceability=ID_3.0_Trace}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,20 @@ public static Wallet getWalletFromString(String body) throws JsonProcessingExcep
System.out.println("wallet -- >" + wallet1.getBpn());
return wallet1;
}

public static List<VerifiableCredential> getCredentialsFromString(String body) throws com.fasterxml.jackson.core.JsonProcessingException {
List<VerifiableCredential> credentialList = new ArrayList<>();

JSONArray array = new JSONArray(body);
if (array.length() == 0) {
return credentialList;
}

for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
ObjectMapper objectMapper = new ObjectMapper();
credentialList.add(new VerifiableCredential(objectMapper.readValue(jsonObject.toString(), Map.class)));
}
return credentialList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,22 @@ void issueDismantlerCredentialTest403() {
}


@Test
void issueDismantlerCredentialToBaseWalletTest201() throws JsonProcessingException, JSONException {
ResponseEntity<String> response = issueDismantlerCredential(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletBpn());
Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
Assertions.assertFalse(credentials.isEmpty());
Assertions.assertTrue(credentials.get(0).isSelfIssued()); //self issued must be false
Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false
}


@Test
void issueDismantlerCredentialTest201() throws JsonProcessingException, JSONException {

String bpn = UUID.randomUUID().toString();
String did = "did:web:localhost:"+bpn;
String did = "did:web:localhost:" + bpn;

Wallet wallet = TestUtils.createWallet(bpn, did, walletRepository);
ResponseEntity<String> response = issueDismantlerCredential(bpn, did);
Expand All @@ -116,7 +127,8 @@ void issueDismantlerCredentialTest201() throws JsonProcessingException, JSONExce
List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDidAndType(wallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
Assertions.assertFalse(credentials.isEmpty());
TestUtils.checkVC(credentials.get(0).getData(), miwSettings);

Assertions.assertFalse(credentials.get(0).isSelfIssued()); //self issued must be false
Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false

VerifiableCredential data = credentials.get(0).getData();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ void issueFrameworkCredentialWithInvalidBpnAccessTest403() throws JsonProcessing
Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), response.getStatusCode().value());
}

@Test
void issueFrameWorkVCToBaseWalletTest201() throws JSONException {
String bpn = miwSettings.authorityWalletBpn();
String type = "cx-pcf";
String value = "PCF";
HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn());

IssueFrameworkCredentialRequest twinRequest = TestUtils.getIssueFrameworkCredentialRequest(bpn, type, value);

HttpEntity<IssueFrameworkCredentialRequest> entity = new HttpEntity<>(twinRequest, headers);

ResponseEntity<String> response = restTemplate.exchange(RestURI.API_CREDENTIALS_ISSUER_FRAMEWORK, HttpMethod.POST, entity, String.class);
Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());

List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), MIWVerifiableCredentialType.USE_CASE_FRAMEWORK_CONDITION_CX);
Assertions.assertFalse(credentials.isEmpty());

VerifiableCredential vcFromDB = credentials.get(0).getData();
TestUtils.checkVC(vcFromDB, miwSettings);

Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false
Assertions.assertTrue(credentials.get(0).isSelfIssued()); //self issue must be false
}

@ParameterizedTest
@MethodSource("getTypes")
void issueFrameWorkVCTest201(IssueFrameworkCredentialRequest request) throws JsonProcessingException, JSONException {
Expand Down Expand Up @@ -196,6 +220,8 @@ private void validate(Wallet wallet, String type, String value, ResponseEntity<S
VerifiableCredential vcFromDB = credentials.get(0).getData();
TestUtils.checkVC(vcFromDB, miwSettings);

Assertions.assertFalse(credentials.get(0).isStored()); //stored must be false
Assertions.assertFalse(credentials.get(0).isSelfIssued()); //self issue must be false
Assertions.assertEquals(vcFromDB.getCredentialSubject().get(0).get("type"), type);
Assertions.assertEquals(vcFromDB.getCredentialSubject().get(0).get("value"), value);
Assertions.assertEquals(vcFromDB.getCredentialSubject().get(0).get("id"), wallet.getDid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void issueCredentialTest200() throws JsonProcessingException {
Assertions.assertFalse(credentials.isEmpty());
TestUtils.checkVC(credentials.get(0).getData(), miwSettings);
Assertions.assertTrue(credentials.get(0).isSelfIssued());
Assertions.assertFalse(credentials.get(0).isStored());

}

Expand Down Expand Up @@ -165,21 +166,21 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti

ResponseEntity<String> response = restTemplate.exchange(RestURI.CREDENTIALS + "?issuerIdentifier={did}"
, HttpMethod.GET, entity, String.class, baseDID);
List<VerifiableCredential> credentialList = getCredentialsFromString(response.getBody());
List<VerifiableCredential> credentialList = TestUtils.getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertEquals(6, Objects.requireNonNull(credentialList).size());

response = restTemplate.exchange(RestURI.CREDENTIALS + "?credentialId={id}"
, HttpMethod.GET, entity, String.class, credentialList.get(0).getId());
credentialList = getCredentialsFromString(response.getBody());
credentialList = TestUtils.getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size());

List<String> list = new ArrayList<>();
list.add(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX);
response = restTemplate.exchange(RestURI.CREDENTIALS + "?type={list}"
, HttpMethod.GET, entity, String.class, String.join(",", list));
credentialList = getCredentialsFromString(response.getBody());
credentialList = TestUtils.getCredentialsFromString(response.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
Assertions.assertEquals(1, Objects.requireNonNull(credentialList).size());
}
Expand Down Expand Up @@ -229,22 +230,6 @@ void validateCredentials() throws com.fasterxml.jackson.core.JsonProcessingExcep
}


private List<VerifiableCredential> getCredentialsFromString(String body) throws com.fasterxml.jackson.core.JsonProcessingException {
List<VerifiableCredential> credentialList = new ArrayList<>();

JSONArray array = new JSONArray(body);
if (array.length() == 0) {
return credentialList;
}

for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
ObjectMapper objectMapper = new ObjectMapper();
credentialList.add(new VerifiableCredential(objectMapper.readValue(jsonObject.toString(), Map.class)));
}
return credentialList;
}

private Map<String, Object> issueVC() throws JsonProcessingException {
String bpn = UUID.randomUUID().toString();
HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders(bpn);
Expand Down
Loading

0 comments on commit bafaab9

Please sign in to comment.