Skip to content

Commit

Permalink
feat: CGD-204: changes related to did document resolver in ssi-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Aug 8, 2023
1 parent d49a2b0 commit aed05de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.utils.CommonUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.Validate;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
Expand Down Expand Up @@ -428,25 +427,18 @@ public Map<String, Object> credentialsValidation(Map<String, Object> data, boole
VerifiableCredential verifiableCredential = new VerifiableCredential(data);

// DID Resolver Constracture params
DidDocumentResolverRegistry didDocumentResolverRegistry = new DidDocumentResolverRegistryImpl();
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps()));

DidResolver resolver = new DidWebResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps());
String proofTye = verifiableCredential.getProof().get(StringPool.TYPE).toString();
LinkedDataProofValidation proofValidation;
if (SignatureType.ED21559.toString().equals(proofTye)) {
proofValidation = LinkedDataProofValidation.newInstance(
SignatureType.ED21559,
didDocumentResolverRegistry);
proofValidation = LinkedDataProofValidation.newInstance(SignatureType.ED21559, resolver);
} else if (SignatureType.JWS.toString().equals(proofTye)) {
proofValidation = LinkedDataProofValidation.newInstance(
SignatureType.JWS,
didDocumentResolverRegistry);
proofValidation = LinkedDataProofValidation.newInstance(SignatureType.JWS, resolver);
} else {
throw new BadDataException(String.format("Invalid proof type: %s", proofTye));
}

boolean valid = proofValidation.verifiyProof(verifiableCredential);
boolean valid = proofValidation.verifyProof(verifiableCredential);

Map<String, Object> response = new TreeMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
import org.eclipse.tractusx.ssi.lib.crypt.ed25519.Ed25519Key;
import org.eclipse.tractusx.ssi.lib.crypt.octet.OctetKeyPairFactory;
import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PrivateKey;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException;
import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat;
Expand Down Expand Up @@ -228,11 +227,8 @@ public Map<String, Object> validatePresentation(Map<String, Object> vp, boolean
private boolean validateSignature(SignedJWT signedJWT) {
//validate jwt signature
try {
DidDocumentResolverRegistry didDocumentResolverRegistry = new DidDocumentResolverRegistryImpl();
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps()));

SignedJwtVerifier jwtVerifier = new SignedJwtVerifier(didDocumentResolverRegistry);
DidResolver resolver = new DidWebResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps());
SignedJwtVerifier jwtVerifier = new SignedJwtVerifier(resolver);
return jwtVerifier.verify(signedJWT);
} catch (Exception e) {
log.error("Can not verify signature of jwt", e);
Expand Down Expand Up @@ -268,27 +264,24 @@ private boolean validateAudience(String audience, SignedJWT signedJWT) {

private boolean validateCredential(VerifiableCredential credential)
throws UnsupportedSignatureTypeException {
DidDocumentResolverRegistry didDocumentResolverRegistry = new DidDocumentResolverRegistryImpl();
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps()));

DidResolver resolver = new DidWebResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps());
String proofType = credential.getProof().getType();
LinkedDataProofValidation linkedDataProofValidation;
if (SignatureType.ED21559.toString().equals(proofType)) {
linkedDataProofValidation = LinkedDataProofValidation.newInstance(
SignatureType.ED21559,
didDocumentResolverRegistry
resolver
);
} else if (SignatureType.JWS.toString().equals(proofType)) {
linkedDataProofValidation = LinkedDataProofValidation.newInstance(
SignatureType.JWS,
didDocumentResolverRegistry
resolver
);
} else {
throw new UnsupportedSignatureTypeException(proofType);
}

boolean isValid = linkedDataProofValidation.verifiyProof(credential);
boolean isValid = linkedDataProofValidation.verifyProof(credential);
if (isValid) {
log.debug("Credential validation result: (valid: {}, credential-id: {})", isValid, credential.getId());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder;
Expand Down Expand Up @@ -214,9 +214,9 @@ void validateCredentialsWithInvalidVC() throws com.fasterxml.jackson.core.JsonPr
//mock setup
LinkedDataProofValidation mock = Mockito.mock(LinkedDataProofValidation.class);
utils.when(() -> {
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidDocumentResolverRegistryImpl.class));
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidResolver.class));
}).thenReturn(mock);
Mockito.when(mock.verifiyProof(Mockito.any(VerifiableCredential.class))).thenReturn(false);
Mockito.when(mock.verifyProof(Mockito.any(VerifiableCredential.class))).thenReturn(false);

Map<String, Object> stringObjectMap = credentialController.credentialsValidation(map, false).getBody();
Assertions.assertFalse(Boolean.parseBoolean(stringObjectMap.get(StringPool.VALID).toString()));
Expand All @@ -237,9 +237,9 @@ void validateCredentialsWithExpiryCheckTrue() throws com.fasterxml.jackson.core.
//mock setup
LinkedDataProofValidation mock = Mockito.mock(LinkedDataProofValidation.class);
utils.when(() -> {
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidDocumentResolverRegistryImpl.class));
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidResolver.class));
}).thenReturn(mock);
Mockito.when(mock.verifiyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);
Mockito.when(mock.verifyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);

Map<String, Object> stringObjectMap = credentialController.credentialsValidation(map, true).getBody();
Assertions.assertTrue(Boolean.parseBoolean(stringObjectMap.get(StringPool.VALID).toString()));
Expand All @@ -264,9 +264,9 @@ void validateCredentialsWithExpiryCheckFalse() throws com.fasterxml.jackson.core
//mock setup
LinkedDataProofValidation mock = Mockito.mock(LinkedDataProofValidation.class);
utils.when(() -> {
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidDocumentResolverRegistryImpl.class));
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidResolver.class));
}).thenReturn(mock);
Mockito.when(mock.verifiyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);
Mockito.when(mock.verifyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);

Map<String, Object> stringObjectMap = credentialController.credentialsValidation(map, false).getBody();
Assertions.assertTrue(Boolean.parseBoolean(stringObjectMap.get(StringPool.VALID).toString()));
Expand All @@ -290,9 +290,9 @@ void validateExpiredCredentialsWithExpiryCheckTrue() throws com.fasterxml.jackso
//mock setup
LinkedDataProofValidation mock = Mockito.mock(LinkedDataProofValidation.class);
utils.when(() -> {
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidDocumentResolverRegistryImpl.class));
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidResolver.class));
}).thenReturn(mock);
Mockito.when(mock.verifiyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);
Mockito.when(mock.verifyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);

Map<String, Object> stringObjectMap = credentialController.credentialsValidation(map, true).getBody();
Assertions.assertFalse(Boolean.parseBoolean(stringObjectMap.get(StringPool.VALID).toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
import org.eclipse.tractusx.managedidentitywallets.dao.repository.HoldersCredentialRepository;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebResolver;
import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException;
import org.eclipse.tractusx.ssi.lib.exception.JwtException;
import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtVerifier;
Expand Down Expand Up @@ -126,8 +127,8 @@ void validateVPAsJwtWithInvalidSignatureAndInValidAudienceAndExpiryDateValidatio

try (MockedConstruction<SignedJwtVerifier> mocked = Mockito.mockConstruction(SignedJwtVerifier.class)) {

DidDocumentResolverRegistry didDocumentResolverRegistry = Mockito.mock(DidDocumentResolverRegistry.class);
SignedJwtVerifier signedJwtVerifier = new SignedJwtVerifier(didDocumentResolverRegistry);
DidResolver didResolver = Mockito.mock(DidWebResolver.class);
SignedJwtVerifier signedJwtVerifier = new SignedJwtVerifier(didResolver);

Mockito.doThrow(new JwtException("invalid")).when(signedJwtVerifier).verify(Mockito.any(SignedJWT.class));

Expand Down

0 comments on commit aed05de

Please sign in to comment.