Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial support for DCP namespace v1.0 #4767

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import static java.lang.String.format;
import static org.eclipse.edc.iam.identitytrust.spi.DcpConstants.DCP_CONTEXT_URL;
import static org.eclipse.edc.iam.identitytrust.spi.DcpConstants.DSPACE_DCP_V_1_0_CONTEXT;
import static org.eclipse.edc.spi.constants.CoreConstants.JSON_LD;

@Extension(value = IdentityTrustTransformExtension.NAME, categories = { "iam", "transform", "jsonld" })
Expand All @@ -55,23 +57,24 @@ public class IdentityTrustTransformExtension implements ServiceExtension {
@Inject
private TypeManager typeManager;

@Inject
private Monitor monitor;

@Override
public void initialize(ServiceExtensionContext context) {
getResourceUri("document" + File.separator + "credentials.v2.jsonld")
.onSuccess(uri -> jsonLdService.registerCachedDocument("https://www.w3.org/2018/credentials/v2", uri))
.onFailure(failure -> context.getMonitor().warning("Failed to register cached json-ld document: " + failure.getFailureDetail()));

getResourceUri("document" + File.separator + "credentials.v1.jsonld")
.onSuccess(uri -> jsonLdService.registerCachedDocument("https://www.w3.org/2018/credentials/v1", uri))
.onFailure(failure -> context.getMonitor().warning("Failed to register cached json-ld document: " + failure.getFailureDetail()));
var contexts = Map.of("credentials.v2.jsonld", "https://www.w3.org/2018/credentials/v2",
"credentials.v1.jsonld", "https://www.w3.org/2018/credentials/v1",
"dcp.v08.jsonld", DCP_CONTEXT_URL,
"dcp.v1.0.jsonld", DSPACE_DCP_V_1_0_CONTEXT);

getResourceUri("document" + File.separator + "dcp.v08.jsonld")
.onSuccess(uri -> jsonLdService.registerCachedDocument(DCP_CONTEXT_URL, uri))
.onFailure(failure -> context.getMonitor().warning("Failed to register cached json-ld document: " + failure.getFailureDetail()));
contexts.forEach((key, value) -> getResourceUri("document/" + key)
.onSuccess(uri -> jsonLdService.registerCachedDocument(value, uri))
.onFailure(failure -> monitor.warning("Failed to register cached json-ld document: " + failure.getFailureDetail())));

typeTransformerRegistry.register(new JsonObjectToPresentationQueryTransformer(typeManager, JSON_LD));
typeTransformerRegistry.register(new JsonObjectToPresentationResponseMessageTransformer(typeManager, JSON_LD));
typeTransformerRegistry.register(new JsonObjectFromPresentationQueryTransformer());
typeTransformerRegistry.register(new JsonObjectFromPresentationQueryTransformer(typeManager, JSON_LD));
typeTransformerRegistry.register(new JsonObjectFromPresentationResponseMessageTransformer());
typeTransformerRegistry.register(new JsonObjectToVerifiablePresentationTransformer());
typeTransformerRegistry.register(new JsonObjectToVerifiableCredentialTransformer());
Expand All @@ -95,4 +98,5 @@ private Result<URI> getResourceUri(String name) {
return Result.failure(format("Cannot read resource %s: %s", name, e.getMessage()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private JsonObject createPresentationQuery(List<String> scopes) {
.add(JsonLdKeywords.CONTEXT, jsonFactory.createArrayBuilder()
.add(VcConstants.PRESENTATION_EXCHANGE_URL)
.add(DcpConstants.DCP_CONTEXT_URL))
.add(JsonLdKeywords.TYPE, PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_TYPE)
.add(JsonLdKeywords.TYPE, PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_TERM)
.add("scope", scopeArray.build())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"@context": {
"@version": 1.1,
"@protected": true,
"dcp": "https://w3id.org/dspace-dcp/v1.0/",
"cred": "https://www.w3.org/2018/credentials/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"id": "@id",
"type": "@type",
"CredentialContainer": {
"@id": "dcp:CredentialContainer",
"@context": {
"payload": {
"@id": "dcp:payload",
"@type": "xsd:string"
}
}
},
"CredentialMessage": {
"@id": "dcp:CredentialMessage",
"@context": {
"credentials": {
"@id": "dcp:credentials",
"@container": "@set"
},
"requestId": {
"@id": "dcp:requestId",
"@type": "@id"
}
}
},
"CredentialObject": {
"@id": "dcp:CredentialObject",
"@context": {
"credentialType": {
"@id": "dcp:credentialType",
"@container": "@set"
},
"offerReason": {
"@id": "dcp:offerReason",
"@type": "xsd:string"
},
"bindingMethods": {
"@id": "dcp:bindingMethods",
"@type": "xsd:string",
"@container": "@set"
},
"profiles": {
"@id": "dcp:profiles",
"@type": "xsd:string",
"@container": "@set"
},
"issuancePolicy": {
"@id": "dcp:issuancePolicy",
"@type": "@json"
}
}
},
"CredentialOfferMessage": {
"@id": "dcp:CredentialOfferMessage",
"@context": {
"credentialIssuer": "cred:issuer",
"credentials": {
"@id": "dcp:credentials",
"@container": "@set"
}
}
},
"CredentialRequestMessage": {
"@id": "dcp:CredentialRequestMessage",
"@context": {
"format": "dcp:format",
"credentialType": {
"@id": "dcp:credentialType",
"@type": "@vocab",
"@container": "@set"
}
}
},
"CredentialService": "dcp:CredentialService",
"CredentialStatus": {
"@id": "dcp:CredentialStatus",
"@context": {
"requestId": {
"@id": "dcp:requestId",
"@type": "@id"
},
"status": {
"@id": "dcp:status",
"@type": "@vocab"
},
"RECEIVED": "dcp:RECEIVED",
"REJECTED": "dcp:REJECTED",
"ISSUED": "dcp:ISSUED"
}
},
"IssuerMetadata": {
"@id": "dcp:IssuerMetadata",
"@context": {
"credentialIssuer": "cred:issuer",
"credentialsSupported": {
"@id": "dcp:credentialsSupported",
"@container": "@set"
}
}
},
"IssuerService": "dcp:IssuerService",
"PresentationQueryMessage": {
"@id": "dcp:PresentationQueryMessage",
"@context": {
"presentationDefinition": {
"@id": "dcp:presentationDefinition",
"@type": "@json"
},
"scope": {
"@id": "dcp:scope",
"@type": "xsd:string",
"@container": "@set"
}
}
},
"PresentationResponseMessage": {
"@id": "dcp:PresentationResponseMessage",
"@context": {
"presentation": {
"@id": "dcp:presentation",
"@container": "@set",
"@type": "@json"
},
"presentationSubmission": {
"@id": "dcp:presentationSubmission",
"@type": "@json"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,51 @@

package org.eclipse.edc.iam.identitytrust.transform.from;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
import org.eclipse.edc.jsonld.spi.JsonLdNamespace;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JsonObjectFromPresentationQueryTransformer extends AbstractJsonLdTransformer<PresentationQueryMessage, JsonObject> {
import static org.eclipse.edc.iam.identitytrust.spi.DcpConstants.DSPACE_DCP_NAMESPACE_V_0_8;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_DEFINITION_TERM;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_SCOPE_TERM;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_TERM;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public JsonObjectFromPresentationQueryTransformer() {
super(PresentationQueryMessage.class, JsonObject.class);
public class JsonObjectFromPresentationQueryTransformer extends AbstractNamespaceAwareJsonLdTransformer<PresentationQueryMessage, JsonObject> {
private final TypeManager typeManager;
private final String typeContext;

public JsonObjectFromPresentationQueryTransformer(TypeManager typeManager, String typeContext) {
this(typeManager, typeContext, DSPACE_DCP_NAMESPACE_V_0_8);
}

public JsonObjectFromPresentationQueryTransformer(TypeManager typeManager, String typeContext, JsonLdNamespace namespace) {
super(PresentationQueryMessage.class, JsonObject.class, namespace);
this.typeManager = typeManager;
this.typeContext = typeContext;
}

@Override
public @Nullable JsonObject transform(@NotNull PresentationQueryMessage presentationQueryMessage, @NotNull TransformerContext context) {
return null;
var builder = Json.createObjectBuilder()
.add(TYPE, forNamespace(PRESENTATION_QUERY_MESSAGE_TERM));

if (presentationQueryMessage.getPresentationDefinition() != null) {
var presentationDefinition = Json.createObjectBuilder();
presentationDefinition.add(JsonLdKeywords.VALUE, typeManager.getMapper(typeContext).convertValue(presentationQueryMessage.getPresentationDefinition(), JsonObject.class));
presentationDefinition.add(JsonLdKeywords.TYPE, JsonLdKeywords.JSON);
builder.add(forNamespace(PRESENTATION_QUERY_MESSAGE_DEFINITION_TERM), presentationDefinition);
} else {
builder.add(forNamespace(PRESENTATION_QUERY_MESSAGE_SCOPE_TERM), Json.createArrayBuilder(presentationQueryMessage.getScopes()));
}
return builder.build();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,36 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.iam.identitytrust.spi.model.PresentationResponseMessage;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.JsonLdNamespace;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationResponseMessage.PRESENTATION_RESPONSE_MESSAGE_PRESENTATION_PROPERTY;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationResponseMessage.PRESENTATION_RESPONSE_MESSAGE_TYPE_PROPERTY;
import static org.eclipse.edc.iam.identitytrust.spi.DcpConstants.DSPACE_DCP_NAMESPACE_V_0_8;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationResponseMessage.PRESENTATION_RESPONSE_MESSAGE_PRESENTATION_TERM;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationResponseMessage.PRESENTATION_RESPONSE_MESSAGE_TYPE_TERM;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;


/**
* Transforms a {@link PresentationResponseMessage} into a {@link JsonObject} object.
*/
public class JsonObjectFromPresentationResponseMessageTransformer extends AbstractJsonLdTransformer<PresentationResponseMessage, JsonObject> {
public class JsonObjectFromPresentationResponseMessageTransformer extends AbstractNamespaceAwareJsonLdTransformer<PresentationResponseMessage, JsonObject> {
public JsonObjectFromPresentationResponseMessageTransformer() {
super(PresentationResponseMessage.class, JsonObject.class);
this(DSPACE_DCP_NAMESPACE_V_0_8);
}

public JsonObjectFromPresentationResponseMessageTransformer(JsonLdNamespace namespace) {
super(PresentationResponseMessage.class, JsonObject.class, namespace);
}

@Override
public @Nullable JsonObject transform(@NotNull PresentationResponseMessage responseMessage, @NotNull TransformerContext context) {
// Presentation Submission not supported yet
return Json.createObjectBuilder()
.add(TYPE, PRESENTATION_RESPONSE_MESSAGE_TYPE_PROPERTY)
.add(PRESENTATION_RESPONSE_MESSAGE_PRESENTATION_PROPERTY, createJson(responseMessage))
.add(TYPE, forNamespace(PRESENTATION_RESPONSE_MESSAGE_TYPE_TERM))
.add(forNamespace(PRESENTATION_RESPONSE_MESSAGE_PRESENTATION_TERM), createJson(responseMessage))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,50 @@
import org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage;
import org.eclipse.edc.iam.verifiablecredentials.spi.model.presentationdefinition.PresentationDefinition;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.jsonld.spi.JsonLdNamespace;
import org.eclipse.edc.jsonld.spi.transformer.AbstractNamespaceAwareJsonLdTransformer;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import static org.eclipse.edc.iam.identitytrust.spi.DcpConstants.DSPACE_DCP_NAMESPACE_V_0_8;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_DEFINITION_TERM;
import static org.eclipse.edc.iam.identitytrust.spi.model.PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_SCOPE_TERM;

/**
* Transforms a JsonObject into a PresentationQuery object.
*/
public class JsonObjectToPresentationQueryTransformer extends AbstractJsonLdTransformer<JsonObject, PresentationQueryMessage> {
public class JsonObjectToPresentationQueryTransformer extends AbstractNamespaceAwareJsonLdTransformer<JsonObject, PresentationQueryMessage> {

private final TypeManager typeManager;
private final String typeContext;

public JsonObjectToPresentationQueryTransformer(TypeManager typeManager, String typeContext) {
super(JsonObject.class, PresentationQueryMessage.class);
this(typeManager, typeContext, DSPACE_DCP_NAMESPACE_V_0_8);
}

public JsonObjectToPresentationQueryTransformer(TypeManager typeManager, String typeContext, JsonLdNamespace namespace) {
super(JsonObject.class, PresentationQueryMessage.class, namespace);
this.typeManager = typeManager;
this.typeContext = typeContext;
}

@Override
public @Nullable PresentationQueryMessage transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) {
var bldr = PresentationQueryMessage.Builder.newinstance();
visitProperties(jsonObject, (k, v) -> {
switch (k) {
case PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_DEFINITION_PROPERTY ->
bldr.presentationDefinition(readPresentationDefinition(v, context));
case PresentationQueryMessage.PRESENTATION_QUERY_MESSAGE_SCOPE_PROPERTY ->
transformArrayOrObject(v, Object.class, o -> bldr.scopes(List.of(o.toString().split(" "))), context);
default -> context.reportProblem("Unknown property '%s'".formatted(k));
}
});

var definition = jsonObject.get(forNamespace(PRESENTATION_QUERY_MESSAGE_DEFINITION_TERM));
if (definition != null) {
bldr.presentationDefinition(readPresentationDefinition(definition, context));
}

var scopes = jsonObject.get(forNamespace(PRESENTATION_QUERY_MESSAGE_SCOPE_TERM));
if (scopes != null) {
transformArrayOrObject(scopes, Object.class, o -> bldr.scopes(List.of(o.toString().split(" "))), context);
}

return bldr.build();
}
Expand Down
Loading