From b3db3e0ce58e028dd42cad9d2072d794dd735413 Mon Sep 17 00:00:00 2001 From: Mustafa Alsalfiti Date: Tue, 12 Mar 2024 14:08:18 +0100 Subject: [PATCH] fix: add test and validation --- .../service/IssuersCredentialService.java | 38 +++++++++---------- .../service/IssuersCredentialServiceTest.java | 3 +- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java index 8df5145d4..d8b335eb6 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialService.java @@ -460,29 +460,30 @@ private JWTVerificationResult verifyVCAsJWT(String jwt, DidResolver didResolver, VerifiableCredential verifiableCredential = new VerifiableCredential(map); //took this approach to avoid issues in sonarQube - return new JWTVerificationResult(validateSignature(withCredentialsValidation , signedJWT, didResolver) && validateJWTExpiryDate(withCredentialExpiryDate, signedJWT), verifiableCredential); + return new JWTVerificationResult(validateSignature(withCredentialsValidation, signedJWT, didResolver) && validateJWTExpiryDate(withCredentialExpiryDate, signedJWT), verifiableCredential); - } + } - private record JWTVerificationResult(boolean valid, VerifiableCredential verifiableCredential) { + private record JWTVerificationResult(boolean valid, VerifiableCredential verifiableCredential) { - } + } - private boolean validateSignature(boolean withValidateSignature, SignedJWT signedJWT, DidResolver didResolver) { - if(!withValidateSignature) { - return true; - } - //validate jwt signature - try { - SignedJwtVerifier jwtVerifier = new SignedJwtVerifier(didResolver); - return jwtVerifier.verify(signedJWT); - } catch (Exception e) { - log.error("Can not verify signature of jwt", e); - return false; - } + private boolean validateSignature(boolean withValidateSignature, SignedJWT signedJWT, DidResolver didResolver) { + if (!withValidateSignature) { + return true; } - private boolean validateJWTExpiryDate(boolean withExpiryDate , SignedJWT signedJWT) { - if(!withExpiryDate) { + //validate jwt signature + try { + SignedJwtVerifier jwtVerifier = new SignedJwtVerifier(didResolver); + return jwtVerifier.verify(signedJWT); + } catch (Exception e) { + log.error("Can not verify signature of jwt", e); + return false; + } + } + + private boolean validateJWTExpiryDate(boolean withExpiryDate, SignedJWT signedJWT) { + if (!withExpiryDate) { return true; } try { @@ -501,7 +502,6 @@ private boolean validateJWTExpiryDate(boolean withExpiryDate , SignedJWT signedJ /** * Credentials validation map. * - * @param verificationRequest the verifiable credential * @param withCredentialExpiryDate the with credential expiry date * @return the map */ diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java index 95fe88da8..82ee3516b 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/service/IssuersCredentialServiceTest.java @@ -26,9 +26,7 @@ import com.nimbusds.jose.JWSObject; import com.nimbusds.jwt.SignedJWT; import com.smartsensesolutions.java.commons.specification.SpecificationUtil; - import lombok.SneakyThrows; - import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings; import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType; import org.eclipse.tractusx.managedidentitywallets.constant.StringPool; @@ -291,6 +289,7 @@ void shouldIssueCredentialAsJwt() throws IOException, ParseException, InvalidPri Map wallets = mockBaseAndHolderWallet(); Wallet baseWallet = (Wallet) wallets.get("base"); String baseWalletBpn = baseWallet.getBpn(); + String baseWalletDid = baseWallet.getDid(); Wallet holderWallet = (Wallet) wallets.get("holder"); String holderWalletBpn = holderWallet.getBpn(); String walletKeyId = "key-1";