-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add credential subject validation (#4776)
* feat: add rule to validate credential subjects * improve fluent statement
- Loading branch information
1 parent
b78e5ab
commit 893f6ca
Showing
11 changed files
with
407 additions
and
23 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
63 changes: 63 additions & 0 deletions
63
.../src/main/java/org/eclipse/edc/iam/verifiablecredentials/rules/HasValidSubjectSchema.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,63 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* 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: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.iam.verifiablecredentials.rules; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SpecVersion; | ||
import org.eclipse.edc.iam.verifiablecredentials.spi.model.VerifiableCredential; | ||
import org.eclipse.edc.iam.verifiablecredentials.spi.validation.CredentialValidationRule; | ||
import org.eclipse.edc.spi.result.Result; | ||
|
||
import java.net.URI; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Performs JSON Schema Validation of the credential subjects. Every credential subject must be validated against all | ||
* credential schemas, and all validations must succeed in order for this rule to pass. | ||
*/ | ||
public class HasValidSubjectSchema implements CredentialValidationRule { | ||
private final ObjectMapper jsonMapper; | ||
private final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012, builder -> builder.enableSchemaCache(true)); | ||
|
||
public HasValidSubjectSchema(ObjectMapper jsonMapper) { | ||
this.jsonMapper = jsonMapper; | ||
} | ||
|
||
|
||
@Override | ||
public Result<Void> apply(VerifiableCredential verifiableCredential) { | ||
if (verifiableCredential.getCredentialSchema() == null || verifiableCredential.getCredentialSchema().isEmpty()) { | ||
return Result.success(); | ||
} | ||
return verifiableCredential.getCredentialSchema().stream().filter(Objects::nonNull).map(schema -> { | ||
var schemaUrl = schema.id(); | ||
// returns the schema using the JsonSchemaFactory. The factory does some caching internally, so there is no need to cache again | ||
var jsonSchema = factory.getSchema(URI.create(schemaUrl)); | ||
|
||
// validate all subjects against the current schema | ||
var validationMessages = verifiableCredential.getCredentialSubject().stream() | ||
.map(subject -> jsonMapper.convertValue(subject, JsonNode.class)) | ||
.flatMap(jsonNode -> jsonSchema.validate(jsonNode).stream()) | ||
.toList(); | ||
return validationMessages.isEmpty() | ||
? Result.success() | ||
: Result.<Void>failure("Error validating CredentialSubject against schema: " + validationMessages); //ValidationMessage overwrites toString() | ||
|
||
}).reduce(Result::merge).orElseGet(Result::success); | ||
} | ||
|
||
} |
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.