diff --git a/miw/src/main/java/org/eclipse/tractusx/managedidentitywallets/constant/StringPool.java b/miw/src/main/java/org/eclipse/tractusx/managedidentitywallets/constant/StringPool.java index 9a42fac7..dc137b8b 100644 --- a/miw/src/main/java/org/eclipse/tractusx/managedidentitywallets/constant/StringPool.java +++ b/miw/src/main/java/org/eclipse/tractusx/managedidentitywallets/constant/StringPool.java @@ -92,4 +92,6 @@ private StringPool() { public static final String VC_JWT_KEY = "jwt"; public static final String AS_JWT = "asJwt"; + + public static final String BPN_CREDENTIAL = "BpnCredential"; } diff --git a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java index c86086cf..0304ed18 100644 --- a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java +++ b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/utils/TestUtils.java @@ -42,6 +42,7 @@ import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest; import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; +import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject; @@ -64,6 +65,7 @@ import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.UUID; import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.ACCESS_TOKEN; @@ -222,10 +224,10 @@ public static Wallet buildWallet(String bpn, String did, String didJson) { } @SneakyThrows - public static VerifiableCredential issueCustomVCUsingBaseWallet(String holderDid, String issuerDid, String type, HttpHeaders headers, + public static VerifiableCredential issueCustomVCUsingBaseWallet(String holderBPn, String holderDid, String issuerDid, String type, HttpHeaders headers, MIWSettings miwSettings, ObjectMapper objectMapper, TestRestTemplate restTemplate) { - Map map = getCredentialAsMap(issuerDid, type, miwSettings, objectMapper); + Map map = getCredentialAsMap(holderBPn, holderDid, issuerDid, type, miwSettings, objectMapper); HttpEntity entity = new HttpEntity<>(map, headers); ResponseEntity response = restTemplate.exchange(RestURI.ISSUERS_CREDENTIALS + "?holderDid={did}", HttpMethod.POST, entity, String.class, holderDid); if (response.getStatusCode().value() == HttpStatus.FORBIDDEN.value()) { @@ -235,15 +237,21 @@ public static VerifiableCredential issueCustomVCUsingBaseWallet(String holderDid return new VerifiableCredential(new ObjectMapper().readValue(response.getBody(), Map.class)); } - public static Map getCredentialAsMap(String issuerDid, String type, MIWSettings miwSettings, ObjectMapper objectMapper) throws JsonProcessingException { + public static Map getCredentialAsMap(String holderBpn, String holderDid, String issuerDid, String type, MIWSettings miwSettings, ObjectMapper objectMapper) throws JsonProcessingException { // Create VC without proof //VC Builder VerifiableCredentialBuilder verifiableCredentialBuilder = new VerifiableCredentialBuilder(); + Map subjectData; + if (Objects.equals(type, StringPool.BPN_CREDENTIAL)) { + subjectData = Map.of(Verifiable.ID, holderDid, StringPool.BPN, holderBpn); + } else { + subjectData = Map.of(Verifiable.ID, "test"); + } //VC Subject VerifiableCredentialSubject verifiableCredentialSubject = - new VerifiableCredentialSubject(Map.of("id", "test")); + new VerifiableCredentialSubject(subjectData); //Using Builder VerifiableCredential credentialWithoutProof = diff --git a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java index b605dc31..dcd0344e 100644 --- a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java +++ b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/HoldersCredentialTest.java @@ -164,7 +164,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti List typesOfVcs = List.of("Type1", "Type2", "Type3"); typesOfVcs.forEach(type -> { - VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); vcs.add(verifiableCredential); }); @@ -215,7 +215,7 @@ void getCredentialsAsJWT200() throws JSONException { List typesOfVcs = List.of("Type1", "Type2", "Type3"); typesOfVcs.forEach(type -> { - VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); vcs.add(verifiableCredential); }); @@ -344,7 +344,7 @@ private Map issueVC() throws JsonProcessingException { String defaultLocation = miwSettings.host() + COLON_SEPARATOR + bpn; ResponseEntity response = TestUtils.createWallet(bpn, "Test Wallet", restTemplate, baseBpn, defaultLocation); Wallet wallet = TestUtils.getWalletFromString(response.getBody()); - VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), "Type1", AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, wallet.getDid(), miwSettings.authorityWalletDid(), "Type1", AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); Map map = objectMapper.readValue(verifiableCredential.toJson(), Map.class); return map; } diff --git a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java index 1cdbffc0..46f602d2 100644 --- a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java +++ b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/IssuersCredentialTest.java @@ -94,7 +94,7 @@ void getCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcepti //issue some VCs List typesOfVcs = List.of("Type1", "Type2", "Type3"); typesOfVcs.forEach(type -> { - TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + TestUtils.issueCustomVCUsingBaseWallet(holderBpn, wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); }); @@ -146,7 +146,7 @@ void getCredentialsAsJWT200() throws JSONException { //create test data List typesOfVcs = List.of("Type1", "Type2", "Type3"); typesOfVcs.forEach(type -> { - TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + TestUtils.issueCustomVCUsingBaseWallet(holderBpn, wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); }); HttpEntity entity = new HttpEntity<>(headers); @@ -191,7 +191,7 @@ void issueCredentialsWithoutBaseWalletBPN403() throws JsonProcessingException { Assertions.assertThrows(ForbiddenException.class, () -> { - TestUtils.issueCustomVCUsingBaseWallet(holderDid, holderDid, type, headers, miwSettings, objectMapper, restTemplate); + TestUtils.issueCustomVCUsingBaseWallet(bpn, holderDid, holderDid, type, headers, miwSettings, objectMapper, restTemplate); }); } @@ -206,7 +206,7 @@ void issueCredentialsToBaseWallet200() throws JsonProcessingException { String defaultLocation = miwSettings.host() + COLON_SEPARATOR + bpn; TestUtils.createWallet(bpn, bpn, restTemplate, baseBpn, defaultLocation); - VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate); + VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(baseBpn, miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate); Assertions.assertNotNull(verifiableCredential.getProof()); @@ -229,7 +229,7 @@ void issueCredentials200() throws com.fasterxml.jackson.core.JsonProcessingExcep String defaultLocation = miwSettings.host() + COLON_SEPARATOR + bpn; TestUtils.createWallet(bpn, bpn, restTemplate, baseBpn, defaultLocation); - VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(did, miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate); + VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), type, headers, miwSettings, objectMapper, restTemplate); Assertions.assertNotNull(verifiableCredential.getProof()); diff --git a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/PresentationValidationTest.java b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/PresentationValidationTest.java index f652f636..4441d638 100644 --- a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/PresentationValidationTest.java +++ b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vc/PresentationValidationTest.java @@ -118,13 +118,13 @@ public void setup() throws DidParseException { tenant_2 = DidParser.parse(tenantWallet2.getDid()); - Map type1 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletDid(), "Type1", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper()); + Map type1 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), "Type1", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper()); CredentialsResponse rs1 = issuersCredentialService.issueCredentialUsingBaseWallet(tenantWallet.getDid(), type1, false, bpnOperator); vc_1 = new ObjectMapper().convertValue(rs1, VerifiableCredential.class); - Map type2 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletDid(), "Type2", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper()); + Map type2 = TestUtils.getCredentialAsMap(miwSettings.authorityWalletBpn(), miwSettings.authorityWalletDid(), miwSettings.authorityWalletDid(), "Type2", miwSettings, new com.fasterxml.jackson.databind.ObjectMapper()); CredentialsResponse rs2 = issuersCredentialService.issueCredentialUsingBaseWallet(tenantWallet.getDid(), type2, false, bpnOperator); vc_2 = new ObjectMapper().convertValue(rs2, VerifiableCredential.class); diff --git a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationServiceTest.java b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationServiceTest.java index dd12af4c..cfe0c819 100644 --- a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationServiceTest.java +++ b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationServiceTest.java @@ -21,6 +21,7 @@ package org.eclipse.tractusx.managedidentitywallets.vp; +import com.fasterxml.jackson.databind.ObjectMapper; import com.nimbusds.jwt.JWT; import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.JWTParser; @@ -29,6 +30,7 @@ import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication; import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings; import org.eclipse.tractusx.managedidentitywallets.config.TestContextInitializer; +import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; import org.eclipse.tractusx.managedidentitywallets.dao.entity.JtiRecord; import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; import org.eclipse.tractusx.managedidentitywallets.dao.repository.JtiRepository; @@ -38,8 +40,10 @@ import org.eclipse.tractusx.managedidentitywallets.exception.PermissionViolationException; import org.eclipse.tractusx.managedidentitywallets.service.IssuersCredentialService; import org.eclipse.tractusx.managedidentitywallets.service.PresentationService; +import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils; import org.eclipse.tractusx.managedidentitywallets.utils.TestConstants; import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils; +import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject; import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation; @@ -71,7 +75,7 @@ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = { ManagedIdentityWalletsApplication.class }) @ContextConfiguration(initializers = { TestContextInitializer.class }) -public class PresentationServiceTest { +class PresentationServiceTest { @Autowired private MIWSettings miwSettings; @@ -88,6 +92,9 @@ public class PresentationServiceTest { @Autowired private IssuersCredentialService issuersCredentialService; + @Autowired + private ObjectMapper objectMapper; + @Autowired private WalletRepository walletRepository; @@ -102,6 +109,10 @@ void createPresentation200ResponseAsJWT() { JtiRecord jtiRecord = buildJti(jtiValue, false); jtiRepository.save(jtiRecord); + //issue BPN vc + VerifiableCredential bpnVc = TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), StringPool.BPN_CREDENTIAL, + AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + Map presentation = presentationService.createVpWithRequiredScopes(SignedJWT.parse(accessToken), asJwt); String vpAsJwt = String.valueOf(presentation.get(VERIFIABLE_PRESENTATION)); JWT jwt = JWTParser.parse(vpAsJwt); @@ -122,6 +133,10 @@ void createPresentation200ResponseAsJsonLD() { JtiRecord jtiRecord = buildJti(jtiValue, false); jtiRepository.save(jtiRecord); + //issue BPN vc + TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), StringPool.BPN_CREDENTIAL, + AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + Map presentation = presentationService.createVpWithRequiredScopes(SignedJWT.parse(accessToken), asJwt); Assertions.assertNotNull(presentation); @@ -130,8 +145,8 @@ void createPresentation200ResponseAsJsonLD() { VerifiableCredential verifiableCredential = vp.getVerifiableCredentials().get(0); VerifiableCredentialSubject verifiableCredentialSubject = verifiableCredential.getCredentialSubject().get(0); Assertions.assertNotNull(verifiableCredentialSubject); - Assertions.assertEquals(bpn, verifiableCredentialSubject.get("bpn")); - Assertions.assertEquals(did, verifiableCredentialSubject.get("id")); + Assertions.assertEquals(bpn, verifiableCredentialSubject.get(StringPool.BPN)); + Assertions.assertEquals(did, verifiableCredentialSubject.get(Verifiable.ID)); } @SneakyThrows @@ -143,6 +158,11 @@ void createPresentation200ResponseNoJtiRecord() { String jtiValue = generateUuid(); String accessToken = generateAccessToken(did, did, did, BPN_CREDENTIAL_READ, jtiValue); + //issue BPN vc + TestUtils.issueCustomVCUsingBaseWallet(bpn, did, miwSettings.authorityWalletDid(), StringPool.BPN_CREDENTIAL, + AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + + Map presentation = presentationService.createVpWithRequiredScopes(SignedJWT.parse(accessToken), asJwt); String vpAsJwt = String.valueOf(presentation.get(VERIFIABLE_PRESENTATION)); JWT jwt = JWTParser.parse(vpAsJwt); diff --git a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java index 8d489f40..6b9e55b2 100644 --- a/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java +++ b/miw/src/test/java/org/eclipse/tractusx/managedidentitywallets/vp/PresentationTest.java @@ -277,7 +277,7 @@ private Map getIssueVPRequest(String bpn) throws JsonProcessingE List typesOfVcs = List.of("Type1", "Type2", "Type3"); typesOfVcs.forEach(type -> { - VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); + VerifiableCredential verifiableCredential = TestUtils.issueCustomVCUsingBaseWallet(wallet.getBpn(), wallet.getDid(), miwSettings.authorityWalletDid(), type, AuthenticationUtils.getValidUserHttpHeaders(miwSettings.authorityWalletBpn()), miwSettings, objectMapper, restTemplate); vcs.add(verifiableCredential); }); List credentials = holdersCredentialRepository.getByHolderDid(wallet.getDid());