Skip to content

Commit

Permalink
feat: framework VC name changes and test case changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Jun 15, 2023
1 parent 365c608 commit ce0c230
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* The type Miw settings.
Expand All @@ -34,6 +35,6 @@
public record MIWSettings(String host, String encryptionKey, String authorityWalletBpn, String authorityWalletDid,
String authorityWalletName,
List<String> vcContexts, @DateTimeFormat(pattern = "dd-MM-yyyy") Date vcExpiryDate,
String supportedFrameworkVCTypes,
Set<String> supportedFrameworkVCTypes,
boolean enforceHttps, String contractTemplatesUrl) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
*/
public class MIWVerifiableCredentialType extends VerifiableCredentialType {

/**
* The constant DISMANTLER_CREDENTIAL_CX.
*/
public static final String DISMANTLER_CREDENTIAL_CX = "DismantlerCredentialCX";
public static final String DISMANTLER_CREDENTIAL = "DismantlerCredential";

/**
Expand All @@ -41,11 +37,7 @@ public class MIWVerifiableCredentialType extends VerifiableCredentialType {

public static final String BPN_CREDENTIAL = "BpnCredential";

public static final String BPN_CREDENTIAL_CX = "BpnCredentialCX";

public static final String MEMBERSHIP_CREDENTIAL_CX = "MembershipCredentialCX";
public static final String SUMMARY_CREDENTIAL = "SummaryCredential";
public static final String SUMMARY_LIST_CREDENTIAL = "Summary-List";


}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private StringPool() {
public static final String NAME = "name";
public static final String CONTRACT_TEMPLATES = "contract-templates";
public static final String CONTRACT_TEMPLATE = "contract-template";
public static final String CX_CREDENTIALS = "CX-Credentials";
public static final String TYPE = "type";
public static final String MEMBER_OF = "memberOf";
public static final String STATUS = "status";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ public ResponseEntity<VerifiableCredential> issueDismantlerCredential(@Valid @Re
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = {
@Content(examples = @ExampleObject("""
{
"bpn": "BPNL000000000000",
"value": "PCF",
"type": "cx-pcf",
"holderIdentifier": "BPNL000000000000",
"type": "BehaviorTwinCredential",
"contract-template": "https://public.catena-x.org/contracts/traceabilty.v1.pdf",
"contract-version": "1.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@
@Builder
public class IssueFrameworkCredentialRequest {

@NotBlank(message = "Please provide BPN")
@Size(min = 5, max = 255, message = "Please provide valid BPN")
private String bpn;

@NotBlank(message = "Please provide value")
private String value;
@NotBlank(message = "Please provide holder identifier")
@Size(min = 5, max = 255, message = "Please provide valid identifier")
private String holderIdentifier;

@NotBlank(message = "Please provide type")
private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package org.eclipse.tractusx.managedidentitywallets.service;

import com.google.common.collect.ImmutableMap;
import com.smartsensesolutions.java.commons.FilterRequest;
import com.smartsensesolutions.java.commons.base.repository.BaseRepository;
import com.smartsensesolutions.java.commons.base.service.BaseService;
Expand Down Expand Up @@ -88,8 +87,6 @@ public class IssuersCredentialService extends BaseService<IssuersCredential, Lon

private final WalletKeyService walletKeyService;

private final Map<String, String> supportedFrameworkVCTypes;

private final HoldersCredentialRepository holdersCredentialRepository;

private final CommonService commonService;
Expand All @@ -113,11 +110,6 @@ public IssuersCredentialService(IssuersCredentialRepository issuersCredentialRep
this.walletKeyService = walletKeyService;
this.holdersCredentialRepository = holdersCredentialRepository;
this.commonService = commonService;
Map<String, String> tmpMap = new HashMap<>();
for (String type : org.apache.commons.lang3.StringUtils.split(miwSettings.supportedFrameworkVCTypes(), ",")) {
tmpMap.put(type.split("=")[0].trim(), type.split("=")[1].trim());
}
supportedFrameworkVCTypes = ImmutableMap.copyOf(tmpMap);
}


Expand Down Expand Up @@ -194,7 +186,7 @@ public PageImpl<VerifiableCredential> getCredentials(String credentialId, String
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
public VerifiableCredential issueBpnCredential(Wallet baseWallet, Wallet holderWallet, boolean authority) {
byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId());
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.BPN_CREDENTIAL_CX);
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.BPN_CREDENTIAL);
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(Map.of(StringPool.TYPE, MIWVerifiableCredentialType.BPN_CREDENTIAL,
StringPool.ID, holderWallet.getDid(),
StringPool.BPN, holderWallet.getBpn()), types, baseWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), authority);
Expand All @@ -207,7 +199,7 @@ public VerifiableCredential issueBpnCredential(Wallet baseWallet, Wallet holderW
issuersCredentialRepository.save(issuersCredential);

//update summery VC
updateSummeryCredentials(baseWallet.getDidDocument(), privateKeyBytes, baseWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), MIWVerifiableCredentialType.BPN_CREDENTIAL_CX);
updateSummeryCredentials(baseWallet.getDidDocument(), privateKeyBytes, baseWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), MIWVerifiableCredentialType.BPN_CREDENTIAL);

log.debug("BPN credential issued for bpn -{}", holderWallet.getBpn());

Expand All @@ -225,13 +217,10 @@ public VerifiableCredential issueBpnCredential(Wallet baseWallet, Wallet holderW
public VerifiableCredential issueFrameworkCredential(IssueFrameworkCredentialRequest request, String callerBPN) {

//validate type
Validate.isFalse(supportedFrameworkVCTypes.containsKey(request.getType())).launch(new BadDataException("Framework credential of type " + request.getType() + " is not supported"));

//validate value
Validate.isFalse(request.getValue().equals(supportedFrameworkVCTypes.get(request.getType()))).launch(new BadDataException("Invalid value of credential type " + request.getType()));
Validate.isFalse(miwSettings.supportedFrameworkVCTypes().contains(request.getType())).launch(new BadDataException("Framework credential of type " + request.getType() + " is not supported, supported values are " + miwSettings.supportedFrameworkVCTypes()));

//Fetch Holder Wallet
Wallet holderWallet = commonService.getWalletByIdentifier(request.getBpn());
Wallet holderWallet = commonService.getWalletByIdentifier(request.getHolderIdentifier());

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

Expand All @@ -240,11 +229,11 @@ public VerifiableCredential issueFrameworkCredential(IssueFrameworkCredentialReq
byte[] privateKeyBytes = walletKeyService.getPrivateKeyByWalletIdentifierAsBytes(baseWallet.getId());

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

Map<String, Object> subject = Map.of(StringPool.TYPE, request.getType(),
Map<String, Object> subject = Map.of(
StringPool.TYPE, request.getType(),
StringPool.ID, holderWallet.getDid(),
StringPool.VALUE, request.getValue(),
StringPool.CONTRACT_TEMPLATE, request.getContractTemplate(),
StringPool.CONTRACT_VERSION, request.getContractVersion());
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.USE_CASE_FRAMEWORK_CONDITION_CX);
Expand All @@ -260,7 +249,7 @@ public VerifiableCredential issueFrameworkCredential(IssueFrameworkCredentialReq
//update summery cred
updateSummeryCredentials(baseWallet.getDidDocument(), privateKeyBytes, baseWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), request.getType());

log.debug("Framework VC of type ->{} issued to bpn ->{}", request.getType(), request.getBpn());
log.debug("Framework VC of type ->{} issued to bpn ->{}", request.getType(), holderWallet.getBpn());

// Return VC
return issuersCredential.getData();
Expand All @@ -285,7 +274,7 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR
validateAccess(callerBPN, issuerWallet);

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

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

Expand All @@ -297,7 +286,7 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR
StringPool.HOLDER_IDENTIFIER, holderWallet.getBpn(),
StringPool.ACTIVITY_TYPE, request.getActivityType(),
StringPool.ALLOWED_VEHICLE_BRANDS, request.getAllowedVehicleBrands());
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
List<String> types = List.of(VerifiableCredentialType.VERIFIABLE_CREDENTIAL, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL);
HoldersCredential holdersCredential = CommonUtils.getHoldersCredential(subject, types, issuerWallet.getDidDocument(), privateKeyBytes, holderWallet.getDid(), miwSettings.vcContexts(), miwSettings.vcExpiryDate(), isSelfIssued);


Expand All @@ -309,7 +298,7 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR
issuersCredential = create(issuersCredential);

//update summery VC
updateSummeryCredentials(issuerWallet.getDidDocument(), privateKeyBytes, issuerWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
updateSummeryCredentials(issuerWallet.getDidDocument(), privateKeyBytes, issuerWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL);

log.debug("Dismantler VC issued to bpn -> {}", request.getBpn());

Expand All @@ -331,15 +320,15 @@ public VerifiableCredential issueMembershipCredential(IssueMembershipCredentialR
Wallet holderWallet = commonService.getWalletByIdentifier(issueMembershipCredentialRequest.getBpn());

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

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

validateAccess(callerBPN, issuerWallet);

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

//if base wallet issue credentials to itself
boolean isSelfIssued = isSelfIssued(issueMembershipCredentialRequest.getBpn());
Expand All @@ -362,7 +351,7 @@ public VerifiableCredential issueMembershipCredential(IssueMembershipCredentialR
issuersCredential = create(issuersCredential);

//update summery VC
updateSummeryCredentials(issuerWallet.getDidDocument(), privateKeyBytes, issuerWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX);
updateSummeryCredentials(issuerWallet.getDidDocument(), privateKeyBytes, issuerWallet.getDid(), holderWallet.getBpn(), holderWallet.getDid(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL);

log.debug("Membership VC issued to bpn ->{}", issueMembershipCredentialRequest.getBpn());

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ miw:
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-10-2023} #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}
supportedFrameworkVCTypes: ${SUPPORTED_FRAMEWORK_VC_TYPES:PcfCredential, SustainabilityCredential, QualityCredential, TraceabilityCredential, BehaviorTwinCredential, ResiliencyCredential}
enforceHttps: ${ENFORCE_HTTPS_IN_DID_RESOLUTION:true}
contractTemplatesUrl: ${CONTRACT_TEMPLATES_URL:https://public.catena-x.org/contracts/}
security:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ public static ResponseEntity<String> issueMembershipVC(TestRestTemplate restTemp
return restTemplate.exchange(RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, HttpMethod.POST, entity, String.class);
}

public static IssueFrameworkCredentialRequest getIssueFrameworkCredentialRequest(String bpn, String type, String value) {
public static IssueFrameworkCredentialRequest getIssueFrameworkCredentialRequest(String bpn, String type) {
IssueFrameworkCredentialRequest twinRequest = IssueFrameworkCredentialRequest.builder()
.contractTemplate("http://localhost")
.contractVersion("v1")
.type(type)
.value(value)
.bpn(bpn)
.holderIdentifier(bpn)
.build();
return twinRequest;
}
Expand Down Expand Up @@ -153,22 +152,6 @@ public static Wallet getWalletFromString(String body) throws JsonProcessingExcep
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;
}


public static String getSummaryCredentialId(String holderDID, HoldersCredentialRepository holdersCredentialRepository) {
List<HoldersCredential> holderVCs = holdersCredentialRepository.getByHolderDidAndType(holderDID, MIWVerifiableCredentialType.SUMMARY_CREDENTIAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ void issueDismantlerCredentialToBaseWalletTest201() throws JsonProcessingExcepti
String oldSummaryCredentialId = TestUtils.getSummaryCredentialId(wallet.getDid(), holdersCredentialRepository);
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);
List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDidAndType(miwSettings.authorityWalletDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL);
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
//check summary credential
TestUtils.checkSummaryCredential(miwSettings.authorityWalletDid(), wallet.getDid(), holdersCredentialRepository, issuersCredentialRepository, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX, oldSummaryCredentialId);
TestUtils.checkSummaryCredential(miwSettings.authorityWalletDid(), wallet.getDid(), holdersCredentialRepository, issuersCredentialRepository, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL, oldSummaryCredentialId);
}


Expand All @@ -125,14 +125,14 @@ void issueDismantlerCredentialTest201() throws JsonProcessingException, JSONExce
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.readValue(response.getBody(), Map.class);
VerifiableCredential verifiableCredential = new VerifiableCredential(map);
Assertions.assertTrue(verifiableCredential.getTypes().contains(MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX));
Assertions.assertTrue(verifiableCredential.getTypes().contains(MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL));

TestUtils.checkVC(verifiableCredential, miwSettings);


Assertions.assertEquals(StringPool.VEHICLE_DISMANTLE, verifiableCredential.getCredentialSubject().get(0).get(StringPool.ACTIVITY_TYPE).toString());

List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDidAndType(wallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
List<HoldersCredential> credentials = holdersCredentialRepository.getByHolderDidAndType(wallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL);
Assertions.assertFalse(credentials.isEmpty());
TestUtils.checkVC(credentials.get(0).getData(), miwSettings);
Assertions.assertFalse(credentials.get(0).isSelfIssued()); //self issued must be false
Expand All @@ -143,13 +143,13 @@ void issueDismantlerCredentialTest201() throws JsonProcessingException, JSONExce
Assertions.assertEquals(StringPool.VEHICLE_DISMANTLE, data.getCredentialSubject().get(0).get(StringPool.ACTIVITY_TYPE).toString());

//check in issuer wallet
List<IssuersCredential> issuerVCs = issuersCredentialRepository.getByIssuerDidAndHolderDidAndType(miwSettings.authorityWalletDid(), wallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX);
List<IssuersCredential> issuerVCs = issuersCredentialRepository.getByIssuerDidAndHolderDidAndType(miwSettings.authorityWalletDid(), wallet.getDid(), MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL);
Assertions.assertEquals(1, issuerVCs.size());
TestUtils.checkVC(issuerVCs.get(0).getData(), miwSettings);
Assertions.assertEquals(StringPool.VEHICLE_DISMANTLE, issuerVCs.get(0).getData().getCredentialSubject().get(0).get(StringPool.ACTIVITY_TYPE).toString());

//check summary credential
TestUtils.checkSummaryCredential(miwSettings.authorityWalletDid(), wallet.getDid(), holdersCredentialRepository, issuersCredentialRepository, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX, oldSummaryCredentialId);
TestUtils.checkSummaryCredential(miwSettings.authorityWalletDid(), wallet.getDid(), holdersCredentialRepository, issuersCredentialRepository, MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL, oldSummaryCredentialId);
}

@Test
Expand Down
Loading

0 comments on commit ce0c230

Please sign in to comment.