Skip to content

Commit

Permalink
Merge pull request #325 from Cofinity-X/feature/did-document-update
Browse files Browse the repository at this point in the history
feat: did document update
  • Loading branch information
borisrizov-zf authored Jul 18, 2024
2 parents 3bae1fb + 44af067 commit cfc98f7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,11 @@ private StringPool() {
public static final String AS_JWT = "asJwt";

public static final String BPN_CREDENTIAL = "BpnCredential";

public static final String ASSERTION_METHOD = "assertionMethod";
public static final String SERVICE_ENDPOINT = "serviceEndpoint";
public static final String SERVICE = "service";
public static final String SECURITY_TOKEN_SERVICE = "SecurityTokenService";
public static final String CREDENTIAL_SERVICE = "CredentialService";
public static final String HTTPS_SCHEME = "https://";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.exception.BadDataException;
import org.eclipse.tractusx.managedidentitywallets.exception.SignatureFailureException;
import org.eclipse.tractusx.managedidentitywallets.exception.UnsupportedAlgorithmException;
import org.eclipse.tractusx.ssi.lib.model.JsonLdObject;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocumentBuilder;
import org.eclipse.tractusx.ssi.lib.model.did.JWKVerificationMethod;
import org.eclipse.tractusx.ssi.lib.model.did.VerificationMethod;
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.presentation.VerifiablePresentation;
import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentationBuilder;
Expand Down Expand Up @@ -130,7 +133,22 @@ public DidDocument buildDidDocument(String bpn, Did did, List<VerificationMethod
mutableContext.add(uri);
}
});
didDocument.put("@context", mutableContext);
didDocument.put(JsonLdObject.CONTEXT, mutableContext);
//add assertionMethod
List<URI> ids = new ArrayList<>();
jwkVerificationMethods.forEach((verificationMethod) -> {
ids.add(verificationMethod.getId());
});
didDocument.put(StringPool.ASSERTION_METHOD, ids);
//add service
Map<String, Object> tokenServiceData = Map.of(Verifiable.ID, did.toUri()+"#"+StringPool.SECURITY_TOKEN_SERVICE, Verifiable.TYPE, StringPool.SECURITY_TOKEN_SERVICE,
StringPool.SERVICE_ENDPOINT, StringPool.HTTPS_SCHEME + miwSettings.host() + "/api/token");
org.eclipse.tractusx.ssi.lib.model.did.Service tokenService = new org.eclipse.tractusx.ssi.lib.model.did.Service(tokenServiceData);
Map<String, Object> credentialServiceData = Map.of(Verifiable.ID, did.toUri()+"#"+StringPool.CREDENTIAL_SERVICE, Verifiable.TYPE, StringPool.CREDENTIAL_SERVICE,
StringPool.SERVICE_ENDPOINT, StringPool.HTTPS_SCHEME + miwSettings.host());
org.eclipse.tractusx.ssi.lib.model.did.Service credentialService = new org.eclipse.tractusx.ssi.lib.model.did.Service(credentialServiceData);
didDocument.put(StringPool.SERVICE, List.of(tokenService,credentialService));

didDocument = DidDocument.fromJson(didDocument.toJson());
log.debug("did document created for bpn ->{}", StringEscapeUtils.escapeJava(bpn));
return didDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jose.jwk.Curve;
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.RestURI;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.constant.SupportedAlgorithms;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
Expand All @@ -40,6 +42,8 @@
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.did.JWKVerificationMethod;
import org.eclipse.tractusx.ssi.lib.model.did.VerificationMethod;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -60,6 +64,8 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -168,7 +174,25 @@ void createWalletTest201() throws JsonProcessingException, JSONException {

Assertions.assertNotNull(response.getBody());
Assertions.assertNotNull(wallet.getDidDocument());
Assertions.assertEquals(2, wallet.getDidDocument().getVerificationMethods().size());
List<VerificationMethod> verificationMethods = wallet.getDidDocument().getVerificationMethods();
Assertions.assertEquals(2, verificationMethods.size());

// both public keys will include the publicKeyJwk format to express the public key
List<String> curves = verificationMethods.stream().map(vm -> (LinkedHashMap) vm.get(JWKVerificationMethod.PUBLIC_KEY_JWK))
.map(lhm -> lhm.get(JWKVerificationMethod.JWK_CURVE).toString()).toList();
List<String> algorithms = Arrays.asList(Curve.SECP256K1.toString(),Curve.Ed25519.toString());
// both the Ed25519 and the secp256k1 curve keys must be present in the verificationMethod of a did document
Assertions.assertTrue(curves.containsAll(algorithms));
List<URI> assertionMethod = (List<URI>)wallet.getDidDocument().get(StringPool.ASSERTION_METHOD);
// both public keys must be expressed in the assertionMethod
Assertions.assertEquals(2, assertionMethod.size());
// both public keys will use the JsonWebKey2020 verification method type
Assertions.assertTrue(verificationMethods.get(0).getType().equals(JWKVerificationMethod.DEFAULT_TYPE) &&
verificationMethods.get(1).getType().equals(JWKVerificationMethod.DEFAULT_TYPE));
// the controller for the keys is the MIW
Assertions.assertEquals(verificationMethods.get(0).getController().toString(), wallet.getDid());
Assertions.assertEquals(verificationMethods.get(1).getController().toString(), wallet.getDid());

List<URI> context = wallet.getDidDocument().getContext();
miwSettings.didDocumentContextUrls().forEach(uri -> {
Assertions.assertTrue(context.contains(uri));
Expand Down

0 comments on commit cfc98f7

Please sign in to comment.