Skip to content

Commit

Permalink
refactor: blockchain token validation reworked (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianLegler authored Sep 9, 2024
1 parent c345593 commit 38635e7
Show file tree
Hide file tree
Showing 66 changed files with 3,176 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
implementation(libs.edc.http)

implementation(libs.edc.configuration.filesystem)
implementation(libs.edc.iam.mock)
//implementation(libs.edc.iam.mock)
implementation(libs.edc.auth.tokenbased)
implementation(libs.edc.management.api)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,31 @@ public String getCatalog(FederatedCatalogCacheQuery federatedCatalogCacheQuery)

// iterate over all sources of contractDefinitionResponseDtoGroupedBySource and fetch all contracts for each source

var extendedDebugging = false;


monitor.debug("-------------------------------------------------");
if (extendedDebugging) monitor.debug("-------------------------------------------------");
for (Asset asset : assetList) {
monitor.debug("Asset: " + asset.getId());
monitor.debug("AssetName: " + asset.getName());
monitor.debug("AssetProperties: " + asset.getProperties().toString());
monitor.debug("AssetDataAddress: " + asset.getDataAddress().toString());
if (extendedDebugging) monitor.debug("Asset: " + asset.getId());
if (extendedDebugging) monitor.debug("AssetName: " + asset.getName());
if (extendedDebugging) monitor.debug("AssetProperties: " + asset.getProperties().toString());
if (extendedDebugging) monitor.debug("AssetDataAddress: " + asset.getDataAddress().toString());
}
for (PolicyDefinition policyDefinition : policyDefinitionList) {
monitor.debug("Policy: " + policyDefinition.getId());
monitor.debug("PolicyTarget: " + policyDefinition.getPolicy().getTarget());
if (extendedDebugging) monitor.debug("Policy: " + policyDefinition.getId());
if (extendedDebugging) monitor.debug("PolicyTarget: " + policyDefinition.getPolicy().getTarget());
}
for (ContractDefinition contractDefinition : contractDefinitionList) {
monitor.debug("Contract: " + contractDefinition.getId());
monitor.debug("ContractPolicyId: " + contractDefinition.getContractPolicyId());
monitor.debug("ContractCriteria: " + contractDefinition.getAccessPolicyId());
if (extendedDebugging) monitor.debug("Contract: " + contractDefinition.getId());
if (extendedDebugging) monitor.debug("ContractPolicyId: " + contractDefinition.getContractPolicyId());
if (extendedDebugging) monitor.debug("ContractCriteria: " + contractDefinition.getAccessPolicyId());
}


// iterate over all contracts for a source
assert contractDefinitionList != null;
for (ContractDefinition contract : contractDefinitionList) {

monitor.debug(format("[%s] fetching contract %s", this.getClass().getSimpleName(), contract.getId()));
if (extendedDebugging) monitor.debug(format("[%s] fetching contract %s", this.getClass().getSimpleName(), contract.getId()));


// TODO: Refactor - connect everything together
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.policy.spi.PolicyDefinition;
import org.eclipse.edc.jsonld.spi.JsonLd;

public class TokenizedPolicyDefinition {
@SuppressWarnings("CheckStyle")
Expand Down Expand Up @@ -37,8 +38,8 @@ public void setDecimals(String decimals) {
this.decimals = decimals;
}

public JsonObject getTokenData() {
return tokenData;
public JsonObject getTokenData(JsonLd jsonLd) {
return jsonLd.expand(tokenData).getContent();
}

public void setTokenData(JsonObject tokenData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@


import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.spi.types.domain.HttpDataAddress;
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.eclipse.edc.jsonld.spi.JsonLd;

public class TokenziedAsset {
@SuppressWarnings("CheckStyle")
Expand Down Expand Up @@ -42,111 +37,8 @@ public void setDecimals(String decimals) {
this.decimals = decimals;
}

/*
Strangely there is some mismatch where the transformer is creating JSON representations of Assets
where dataAddress and properties are objects containing objects but when trying to deserialize them back
into an Asset the dataAddress and properties are exprected to be arrays
*/
public JsonObject getTokenData() {
JsonObject returnTokenData = tokenData;
/*
JsonValue dataAddressNode = returnTokenData.get("https://w3id.org/edc/v0.0.1/ns/dataAddress");
if (dataAddressNode != null && dataAddressNode.getValueType() == JsonValue.ValueType.OBJECT) {
JsonArray dataAddressArray = Json.createArrayBuilder().add(dataAddressNode).build();
returnTokenData = Json.createObjectBuilder(returnTokenData)
.remove("https://w3id.org/edc/v0.0.1/ns/dataAddress")
.add("https://w3id.org/edc/v0.0.1/ns/dataAddress", dataAddressArray)
.build();
}
*/
JsonValue dataAddressTypeNode = returnTokenData.getJsonObject("edc:dataAddress").get("@type");
if (dataAddressTypeNode != null && dataAddressTypeNode.getValueType() == JsonValue.ValueType.STRING) {
JsonArray dataAddressTypeArray = Json.createArrayBuilder().add(dataAddressTypeNode).build();
var changes = Json.createObjectBuilder(returnTokenData.getJsonObject("edc:dataAddress"))
.remove("@type")
.add("@type", dataAddressTypeArray)
.build();
returnTokenData = Json.createObjectBuilder(returnTokenData)
.remove("edc:dataAddress")
.add("edc:dataAddress", changes)
.build();
}



JsonValue propertiesNode = returnTokenData.get("edc:properties");
if (propertiesNode != null && propertiesNode.getValueType() == JsonValue.ValueType.OBJECT) {
JsonArray propertiesArray = Json.createArrayBuilder().add(propertiesNode).build();
returnTokenData = Json.createObjectBuilder(returnTokenData)
.remove("edc:properties")
.add("edc:properties", propertiesArray)
.build();
}
return returnTokenData;
public JsonObject getTokenData(JsonLd jsonLd) {
return jsonLd.expand(tokenData).getContent();
}

public Asset getTokenDataAsAsset() throws IllegalArgumentException {
var jsonDataAddressTypeObject = tokenData.getJsonObject("edc:dataAddress"); // typo DataAddress vs dataAdress - DataAddress.EDC_DATA_ADDRESS_TYPE);
if (jsonDataAddressTypeObject == null) {
throw new IllegalArgumentException("The token data does not contain a data address type");
}
if (!jsonDataAddressTypeObject.containsKey(DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY)) {
throw new IllegalArgumentException("The token data does not contain a data address type property");
}
DataAddress transformedDataAddress;
if (jsonDataAddressTypeObject.containsKey("baseUrl")) {
HttpDataAddress.Builder builder = HttpDataAddress.Builder.newInstance()
.baseUrl(jsonDataAddressTypeObject.getString("baseUrl"));
if (jsonDataAddressTypeObject.containsKey("name")) {
builder.name(jsonDataAddressTypeObject.getString("name"));
} else {
builder.name("default");
}

transformedDataAddress = builder
.type(jsonDataAddressTypeObject.getString(DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY))
.build();
} else {
transformedDataAddress = DataAddress.Builder.newInstance()
.type(jsonDataAddressTypeObject.getString(DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY))
.build();
}
/*
if (!jsonDataAddressTypeObject.containsKey("path")) {
throw new IllegalArgumentException("The token data does not contain a data address type path");
}*/

/*
var transformedDataAddress = HttpDataAddress.Builder.newInstance()
//.type(tokenData.getJsonObject(DataAddress.EDC_DATA_ADDRESS_TYPE).getString("@type"))
.baseUrl(jsonDataAddressTypeObject.getString("baseUrl"))
.path(jsonDataAddressTypeObject.getString("path"))
.type(jsonDataAddressTypeObject.getString(DataAddress.EDC_DATA_ADDRESS_TYPE_PROPERTY))
.build();
*/
var assetBuilder = Asset.Builder.newInstance()
.id(tokenData.getString("@id"))
.contentType(tokenData.getString("@type"))
.dataAddress(transformedDataAddress);

var jsonAssetPropertiesObject = tokenData.getJsonObject(Asset.EDC_ASSET_PROPERTIES);
if (jsonAssetPropertiesObject == null) {
throw new IllegalArgumentException("The token data does not contain an asset properties object");
}
if (!jsonAssetPropertiesObject.containsKey(Asset.PROPERTY_ID)) {
throw new IllegalArgumentException("The token data does not contain an asset properties id");
}
if (!jsonAssetPropertiesObject.containsKey(Asset.PROPERTY_NAME)) {
throw new IllegalArgumentException("The token data does not contain an asset properties name");
}
if (jsonAssetPropertiesObject.containsKey(Asset.PROPERTY_DESCRIPTION)) {
assetBuilder.property(Asset.PROPERTY_DESCRIPTION, jsonAssetPropertiesObject.getString(Asset.PROPERTY_DESCRIPTION));
}

var transformedAsset = assetBuilder
.property(Asset.PROPERTY_ID, jsonAssetPropertiesObject.getString(Asset.PROPERTY_ID))
.property(Asset.PROPERTY_NAME, jsonAssetPropertiesObject.getString(Asset.PROPERTY_NAME))
.build();
return transformedAsset;
}
}
25 changes: 25 additions & 0 deletions extensions/helper/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

plugins {
`java-library`
id("application")
id("com.github.johnrengelman.shadow") version "7.1.2"
id("io.swagger.core.v3.swagger-gradle-plugin")
}

val groupId: String by project
val edcVersion: String by project

dependencies {
implementation(libs.edc.vault.filesystem)
}


application {
mainClass.set("org.eclipse.edc.boot.system.runtime.BaseRuntime")
}

tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
exclude("**/pom.properties", "**/pom.xml")
mergeServiceFiles()
archiveFileName.set("consumer.jar")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package berlin.tu.ise.extension.helper;


import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

@Extension(value = "TU Berlin Helper Extensions")
public class HelperExtension implements ServiceExtension {
//@Inject
//private FsVaultExtension fsVaultExtension;


private ServiceExtensionContext context;


@Override
public void initialize(ServiceExtensionContext context) {

this.context = context;

var monitor = context.getMonitor();

monitor.info("HelperExtensions initialized");


}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
berlin.tu.ise.extension.helper.HelperExtension
24 changes: 24 additions & 0 deletions external/oauth2/oauth2-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2022 Amadeus
*
* 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:
* Amadeus - initial API and implementation
*
*/

plugins {
`java-library`
}

dependencies {
api(libs.edc.spi.http)
api(libs.edc.spi.oauth2)
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022 Amadeus
*
* 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:
* Amadeus - initial API and implementation
*
*/

package org.eclipse.edc.iam.oauth2;

import org.eclipse.edc.iam.oauth2.client.Oauth2ClientImpl;
import org.eclipse.edc.iam.oauth2.spi.client.Oauth2Client;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.spi.http.EdcHttpClient;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;

@Extension(value = Oauth2ClientExtension.NAME)
public class Oauth2ClientExtension implements ServiceExtension {

public static final String NAME = "OAuth2 Client";

@Inject
private EdcHttpClient httpClient;

@Inject
private TypeManager typeManager;

@Override
public String name() {
return NAME;
}

@Provider
public Oauth2Client oauth2Client(ServiceExtensionContext context) {
return new Oauth2ClientImpl(httpClient, typeManager);
}
}
Loading

0 comments on commit 38635e7

Please sign in to comment.