-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add credentialSchema property to VerifiableCredential
- Loading branch information
1 parent
347e228
commit 6f8bee4
Showing
9 changed files
with
255 additions
and
16 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...g/eclipse/edc/iam/identitytrust/transform/to/JsonObjectToCredentialSchemaTransformer.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,53 @@ | ||
/* | ||
* 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 jakarta.json.JsonObject; | ||
import org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSchema; | ||
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; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import static org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSchema.CREDENTIAL_SCHEMA_ID_PROPERTY; | ||
import static org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSchema.CREDENTIAL_SCHEMA_TYPE_PROPERTY; | ||
|
||
|
||
public class JsonObjectToCredentialSchemaTransformer extends AbstractJsonLdTransformer<JsonObject, CredentialSchema> { | ||
public JsonObjectToCredentialSchemaTransformer() { | ||
super(JsonObject.class, CredentialSchema.class); | ||
} | ||
|
||
@Override | ||
public @Nullable CredentialSchema transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) { | ||
|
||
var id = nodeId(jsonObject); | ||
|
||
try { | ||
new URI(id); | ||
} catch (URISyntaxException ignored) { | ||
context.reportProblem("The '%s' property must be in URI format but was not: '%s'".formatted(CREDENTIAL_SCHEMA_ID_PROPERTY, id)); | ||
} | ||
|
||
var type = transformString(jsonObject.get(CREDENTIAL_SCHEMA_TYPE_PROPERTY), context); | ||
if (type == null) { | ||
context.reportProblem("The '%s' property is mandatory on credentialSchema objects".formatted(CREDENTIAL_SCHEMA_TYPE_PROPERTY)); | ||
} | ||
return new CredentialSchema(id, type); | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
...lipse/edc/iam/identitytrust/transform/to/JsonObjectToCredentialSchemaTransformerTest.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,72 @@ | ||
/* | ||
* Copyright (c) 2025 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 jakarta.json.Json; | ||
import org.eclipse.edc.transform.spi.TransformerContext; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSchema.CREDENTIAL_SCHEMA_ID_PROPERTY; | ||
import static org.eclipse.edc.iam.verifiablecredentials.spi.model.CredentialSchema.CREDENTIAL_SCHEMA_TYPE_PROPERTY; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class JsonObjectToCredentialSchemaTransformerTest { | ||
|
||
private final JsonObjectToCredentialSchemaTransformer transformer = new JsonObjectToCredentialSchemaTransformer(); | ||
private final @NotNull TransformerContext context = mock(); | ||
|
||
@Test | ||
void transform() { | ||
var jo = Json.createObjectBuilder() | ||
.add(CREDENTIAL_SCHEMA_ID_PROPERTY, "http://foo.bar/id") | ||
.add(CREDENTIAL_SCHEMA_TYPE_PROPERTY, "JsonSchemaValidator2018") | ||
.build(); | ||
var result = transformer.transform(jo, context); | ||
assertThat(result).isNotNull(); | ||
assertThat(result.id()).isEqualTo("http://foo.bar/id"); | ||
assertThat(result.type()).isEqualTo("JsonSchemaValidator2018"); | ||
verify(context, never()).reportProblem(any()); | ||
} | ||
|
||
@Test | ||
void transform_typeMissing() { | ||
var jo = Json.createObjectBuilder() | ||
.add(CREDENTIAL_SCHEMA_ID_PROPERTY, "http://foo.bar/id") | ||
.build(); | ||
var result = transformer.transform(jo, context); | ||
assertThat(result).isNotNull(); | ||
assertThat(result.id()).isEqualTo("http://foo.bar/id"); | ||
verify(context).reportProblem(anyString()); | ||
} | ||
|
||
@Test | ||
void transform_idNotUri() { | ||
var jo = Json.createObjectBuilder() | ||
.add(CREDENTIAL_SCHEMA_ID_PROPERTY, "not a uri") | ||
.add(CREDENTIAL_SCHEMA_TYPE_PROPERTY, "JsonSchemaValidator2018") | ||
.build(); | ||
var result = transformer.transform(jo, context); | ||
assertThat(result).isNotNull(); | ||
assertThat(result.type()).isEqualTo("JsonSchemaValidator2018"); | ||
verify(context).reportProblem(anyString()); | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
...i/src/main/java/org/eclipse/edc/iam/verifiablecredentials/spi/model/CredentialSchema.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,21 @@ | ||
/* | ||
* Copyright (c) 2025 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.verifiablecredentials.spi.model; | ||
|
||
public record CredentialSchema(String id, String type) { | ||
public static final String CREDENTIAL_SCHEMA_ID_PROPERTY = "@id"; | ||
public static final String CREDENTIAL_SCHEMA_TYPE_PROPERTY = "@type"; | ||
|
||
} |
Oops, something went wrong.