-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Ssi): add credential issuer and credential subject id validation…
… rules
- Loading branch information
Showing
30 changed files
with
561 additions
and
108 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
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 |
---|---|---|
|
@@ -134,6 +134,7 @@ controlplane: | |
miw: | ||
url: "" | ||
authorityId: "" | ||
authorityIssuer: "" | ||
oauth: | ||
tokenurl: "" | ||
client: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,7 @@ runtime: | |
miw: | ||
url: "" | ||
authorityId: "" | ||
authorityIssuer: "" | ||
oauth: | ||
tokenurl: "" | ||
client: | ||
|
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
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 |
---|---|---|
|
@@ -135,6 +135,7 @@ controlplane: | |
miw: | ||
url: "" | ||
authorityId: "" | ||
authorityIssuer: "" | ||
oauth: | ||
tokenurl: "" | ||
client: | ||
|
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 |
---|---|---|
|
@@ -44,6 +44,7 @@ controlplane: | |
miw: | ||
url: "" | ||
authorityId: "" | ||
authorityIssuer: "" | ||
oauth: | ||
tokenurl: "" | ||
client: | ||
|
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 |
---|---|---|
|
@@ -43,6 +43,7 @@ controlplane: | |
miw: | ||
url: "" | ||
authorityId: "" | ||
authorityIssuer: "" | ||
oauth: | ||
tokenurl: "" | ||
client: | ||
|
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
97 changes: 0 additions & 97 deletions
97
...c/test/java/org/eclipse/tractusx/edc/iam/ssi/identity/extractor/fixtures/Credentials.java
This file was deleted.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
...ent/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/SsiMiwValidationRuleExtension.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,50 @@ | ||
/* | ||
* 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.tractusx.edc.iam.ssi.miw; | ||
|
||
import org.eclipse.edc.runtime.metamodel.annotation.Extension; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Inject; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Setting; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.system.ServiceExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
import org.eclipse.tractusx.edc.iam.ssi.miw.rule.SsiCredentialIssuerValidationRule; | ||
import org.eclipse.tractusx.edc.iam.ssi.miw.rule.SsiCredentialSubjectIdValidationRule; | ||
import org.eclipse.tractusx.edc.iam.ssi.spi.SsiValidationRuleRegistry; | ||
|
||
@Extension(SsiMiwValidationRuleExtension.EXTENSION_NAME) | ||
public class SsiMiwValidationRuleExtension implements ServiceExtension { | ||
|
||
@Setting(value = "MIW Authority Issuer") | ||
public static final String MIW_AUTHORITY_ISSUER = "tx.ssi.miw.authority.issuer"; | ||
protected static final String EXTENSION_NAME = "SSI MIW validation rules extension"; | ||
@Inject | ||
private SsiValidationRuleRegistry registry; | ||
|
||
@Inject | ||
private Monitor monitor; | ||
|
||
@Override | ||
public String name() { | ||
return EXTENSION_NAME; | ||
} | ||
|
||
@Override | ||
public void initialize(ServiceExtensionContext context) { | ||
var authorityIssuer = context.getConfig().getString(MIW_AUTHORITY_ISSUER); | ||
registry.addRule(new SsiCredentialSubjectIdValidationRule(monitor)); | ||
registry.addRule(new SsiCredentialIssuerValidationRule(authorityIssuer, monitor)); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...ain/java/org/eclipse/tractusx/edc/iam/ssi/miw/rule/SsiCredentialIssuerValidationRule.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,95 @@ | ||
/* | ||
* 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.tractusx.edc.iam.ssi.miw.rule; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.eclipse.edc.jwt.spi.TokenValidationRule; | ||
import org.eclipse.edc.spi.iam.ClaimToken; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.result.Result; | ||
import org.eclipse.tractusx.edc.iam.ssi.spi.jsonld.JsonLdFieldExtractor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import static java.lang.String.format; | ||
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; | ||
import static org.eclipse.tractusx.edc.iam.ssi.spi.jsonld.CredentialsNamespaces.CREDENTIAL_ISSUER; | ||
import static org.eclipse.tractusx.edc.iam.ssi.spi.jsonld.CredentialsNamespaces.SUMMARY_CREDENTIAL_TYPE; | ||
import static org.eclipse.tractusx.edc.iam.ssi.spi.jsonld.CredentialsNamespaces.VP_PROPERTY; | ||
import static org.eclipse.tractusx.edc.iam.ssi.spi.jsonld.JsonLdTypeFunctions.extractObjectsOfType; | ||
|
||
public class SsiCredentialIssuerValidationRule implements TokenValidationRule { | ||
|
||
private static final String SUBJECT_ISSUER_EXTRACTOR_PREFIX = "Credential issuer extractor:"; | ||
|
||
private static final String SUBJECT_ISSUER_FIELD_ALIAS = "issuer"; | ||
|
||
private final String credentialIssuer; | ||
|
||
private final Monitor monitor; | ||
|
||
private final JsonLdFieldExtractor credentialIssuerExtractor = JsonLdFieldExtractor.Builder.newInstance() | ||
.field(CREDENTIAL_ISSUER) | ||
.fieldAlias(SUBJECT_ISSUER_FIELD_ALIAS) | ||
.errorPrefix(SUBJECT_ISSUER_EXTRACTOR_PREFIX) | ||
.build(); | ||
|
||
public SsiCredentialIssuerValidationRule(String credentialIssuer, Monitor monitor) { | ||
this.credentialIssuer = credentialIssuer; | ||
this.monitor = monitor; | ||
} | ||
|
||
@Override | ||
public Result<Void> checkRule(@NotNull ClaimToken toVerify, @Nullable Map<String, Object> additional) { | ||
|
||
var vp = (JsonObject) toVerify.getClaim(VP_PROPERTY); | ||
|
||
return Optional.ofNullable(vp) | ||
.map(v -> extractObjectsOfType(SUMMARY_CREDENTIAL_TYPE, v)) | ||
.orElse(Stream.empty()) | ||
.map(this::extractIssuer) | ||
.findFirst() | ||
.orElseGet(() -> Result.failure("Failed to extract credential subject from the membership credential")) | ||
.compose(this::validateCredentialIssuer) | ||
.onFailure(failure -> monitor.severe(failure.getFailureDetail())); | ||
|
||
} | ||
|
||
private Result<Void> validateCredentialIssuer(String credentialSubjectId) { | ||
if (credentialIssuer.equals(credentialSubjectId)) { | ||
return Result.success(); | ||
} else { | ||
return Result.failure(format("Invalid credential issuer: expected %s, found %s", credentialIssuer, credentialSubjectId)); | ||
} | ||
} | ||
|
||
private Result<String> extractIssuer(JsonObject credential) { | ||
return this.credentialIssuerExtractor.extract(credential) | ||
.compose(this::extractIssuerValue); | ||
} | ||
|
||
private Result<String> extractIssuerValue(JsonObject issuer) { | ||
var issuerValue = issuer.getString(ID); | ||
if (issuerValue == null) { | ||
return Result.failure("Failed to find the issuer"); | ||
} else { | ||
return Result.success(issuerValue); | ||
} | ||
} | ||
} |
Oops, something went wrong.