-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: moved IATP model classes up from IdentityHub
- Loading branch information
1 parent
5b13175
commit 93e2c56
Showing
19 changed files
with
869 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../eclipse/edc/iam/identitytrust/transform/to/JsonObjectToPresentationQueryTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.iam.identitytrust.transform.to; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.identitytrust.model.credentialservice.PresentationQuery; | ||
import org.eclipse.edc.identitytrust.model.presentationdefinition.PresentationDefinition; | ||
import org.eclipse.edc.jsonld.spi.JsonLdKeywords; | ||
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Transforms a JsonObject into a PresentationQuery object. | ||
*/ | ||
public class JsonObjectToPresentationQueryTransformer extends AbstractJsonLdTransformer<JsonObject, PresentationQuery> { | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public JsonObjectToPresentationQueryTransformer(ObjectMapper mapper) { | ||
super(JsonObject.class, PresentationQuery.class); | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public @Nullable PresentationQuery transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) { | ||
var bldr = PresentationQuery.Builder.newinstance(); | ||
visitProperties(jsonObject, (k, v) -> { | ||
switch (k) { | ||
case PresentationQuery.PRESENTATION_QUERY_DEFINITION_PROPERTY -> | ||
bldr.presentationDefinition(readPresentationDefinition(v, context)); | ||
case PresentationQuery.PRESENTATION_QUERY_SCOPE_PROPERTY -> | ||
transformArrayOrObject(v, Object.class, o -> bldr.scope(o.toString()), context); | ||
default -> context.reportProblem("Unknown property '%s'".formatted(k)); | ||
} | ||
}); | ||
|
||
return bldr.build(); | ||
} | ||
|
||
private PresentationDefinition readPresentationDefinition(JsonValue v, TransformerContext context) { | ||
JsonObject jo; | ||
if (v.getValueType() == JsonValue.ValueType.ARRAY && !((JsonArray) v).isEmpty()) { | ||
jo = v.asJsonArray().getJsonObject(0); | ||
} else { | ||
jo = v.asJsonObject(); | ||
} | ||
var rawJson = jo.get(JsonLdKeywords.VALUE); | ||
try { | ||
return mapper.readValue(rawJson.toString(), PresentationDefinition.class); | ||
} catch (JsonProcessingException e) { | ||
context.reportProblem("Error reading JSON literal: %s".formatted(e.getMessage())); | ||
return null; | ||
} | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
...ipse/edc/iam/identitytrust/transform/to/JsonObjectToPresentationQueryTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.iam.identitytrust.transform.to; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.core.transform.TransformerContextImpl; | ||
import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl; | ||
import org.eclipse.edc.core.transform.transformer.to.JsonValueToGenericTypeTransformer; | ||
import org.eclipse.edc.jsonld.TitaniumJsonLd; | ||
import org.eclipse.edc.jsonld.spi.JsonLd; | ||
import org.eclipse.edc.jsonld.util.JacksonJsonLd; | ||
import org.eclipse.edc.junit.testfixtures.TestUtils; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.eclipse.edc.transform.spi.TypeTransformerRegistry; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class JsonObjectToPresentationQueryTransformerTest { | ||
private final ObjectMapper mapper = JacksonJsonLd.createObjectMapper(); | ||
private final JsonObjectToPresentationQueryTransformer transformer = new JsonObjectToPresentationQueryTransformer(mapper); | ||
private final JsonLd jsonLd = new TitaniumJsonLd(mock()); | ||
private final TypeTransformerRegistry trr = new TypeTransformerRegistryImpl(); | ||
private final TransformerContext context = new TransformerContextImpl(trr); | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
jsonLd.registerCachedDocument("https://identity.foundation/presentation-exchange/submission/v1", TestUtils.getFileFromResourceName("presentation_ex.json").toURI()); | ||
jsonLd.registerCachedDocument("https://w3id.org/tractusx-trust/v0.8", TestUtils.getFileFromResourceName("presentation_query.json").toURI()); | ||
// delegate to the generic transformer | ||
|
||
trr.register(new JsonValueToGenericTypeTransformer(mapper)); | ||
} | ||
|
||
@Test | ||
void transform_withScopes() throws JsonProcessingException { | ||
var obj = """ | ||
{ | ||
"@context": [ | ||
"https://identity.foundation/presentation-exchange/submission/v1", | ||
"https://w3id.org/tractusx-trust/v0.8" | ||
], | ||
"@type": "Query", | ||
"scope": [ | ||
"org.eclipse.edc.vc.type:TestCredential:read", | ||
"org.eclipse.edc.vc.type:AnotherCredential:all" | ||
] | ||
} | ||
"""; | ||
var json = mapper.readValue(obj, JsonObject.class); | ||
var jo = jsonLd.expand(json); | ||
assertThat(jo.succeeded()).withFailMessage(jo::getFailureDetail).isTrue(); | ||
|
||
var query = transformer.transform(jo.getContent(), context); | ||
assertThat(query).isNotNull(); | ||
assertThat(query.getScopes()).hasSize(2) | ||
.containsExactlyInAnyOrder( | ||
"org.eclipse.edc.vc.type:TestCredential:read", | ||
"org.eclipse.edc.vc.type:AnotherCredential:all"); | ||
assertThat(query.getPresentationDefinition()).isNull(); | ||
} | ||
|
||
@Test | ||
void transform_withPresentationDefinition() throws JsonProcessingException { | ||
var json = """ | ||
{ | ||
"@context": [ | ||
"https://identity.foundation/presentation-exchange/submission/v1", | ||
"https://w3id.org/tractusx-trust/v0.8" | ||
], | ||
"@type": "Query", | ||
"presentationDefinition": { | ||
"id": "first simple example", | ||
"input_descriptors": [ | ||
{ | ||
"id": "descriptor-id-1", | ||
"name": "A specific type of VC", | ||
"purpose": "We want a VC of this type", | ||
"constraints": { | ||
"fields": [ | ||
{ | ||
"path": [ | ||
"$.type" | ||
], | ||
"filter": { | ||
"type": "string", | ||
"pattern": "<the type of VC e.g. degree certificate>" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
"""; | ||
var jobj = mapper.readValue(json, JsonObject.class); | ||
|
||
var expansion = jsonLd.expand(jobj); | ||
assertThat(expansion.succeeded()).withFailMessage(expansion::getFailureDetail).isTrue(); | ||
|
||
var query = transformer.transform(expansion.getContent(), context); | ||
assertThat(query).isNotNull(); | ||
assertThat(query.getScopes()).isNotNull().isEmpty(); | ||
assertThat(query.getPresentationDefinition()).isNotNull(); | ||
assertThat(query.getPresentationDefinition().getInputDescriptors()).isNotEmpty() | ||
.allSatisfy(id -> assertThat(id.getId()).isEqualTo("descriptor-id-1")); | ||
|
||
} | ||
|
||
@Test | ||
void transform_withScopesAndPresDef() throws JsonProcessingException { | ||
var json = """ | ||
{ | ||
"@context": [ | ||
"https://identity.foundation/presentation-exchange/submission/v1", | ||
"https://w3id.org/tractusx-trust/v0.8" | ||
], | ||
"@type": "Query", | ||
"scope": ["test-scope1"], | ||
"presentationDefinition": { | ||
"id": "first simple example", | ||
"input_descriptors": [ | ||
{ | ||
"id": "descriptor-id-1", | ||
"name": "A specific type of VC", | ||
"purpose": "We want a VC of this type", | ||
"constraints": { | ||
"fields": [ | ||
{ | ||
"path": [ | ||
"$.type" | ||
], | ||
"filter": { | ||
"type": "string", | ||
"pattern": "<the type of VC e.g. degree certificate>" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
"""; | ||
var jobj = mapper.readValue(json, JsonObject.class); | ||
|
||
var expansion = jsonLd.expand(jobj); | ||
assertThat(expansion.succeeded()).withFailMessage(expansion::getFailureDetail).isTrue(); | ||
|
||
var query = transformer.transform(expansion.getContent(), context); | ||
assertThat(query).isNotNull(); | ||
assertThat(query.getScopes()).isNotNull().containsExactly("test-scope1"); | ||
assertThat(query.getPresentationDefinition()).isNotNull(); | ||
assertThat(query.getPresentationDefinition().getInputDescriptors()).isNotEmpty() | ||
.allSatisfy(id -> assertThat(id.getId()).isEqualTo("descriptor-id-1")); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ommon/iam/identity-trust/identity-trust-transform/src/test/resources/presentation_ex.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"@context": { | ||
"@version": 1.1, | ||
"PresentationSubmission": { | ||
"@id": "https://identity.foundation/presentation-exchange/#presentation-submission", | ||
"@context": { | ||
"@version": 1.1, | ||
"presentation_submission": { | ||
"@id": "https://identity.foundation/presentation-exchange/#presentation-submission", | ||
"@type": "@json" | ||
} | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...on/iam/identity-trust/identity-trust-transform/src/test/resources/presentation_query.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"@context": { | ||
"scope": { | ||
"@id": "https://w3id.org/tractusx-trust/v0.8/scope", | ||
"@container": "@set" | ||
}, | ||
"presentationDefinition": { | ||
"@id": "https://w3id.org/tractusx-trust/v0.8/presentationDefinition", | ||
"@type": "@json" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.