Skip to content

Commit d2cafdd

Browse files
committed
Refactoring of the full identity checks. First working version of auto checker of identity against fc.
1 parent 0a984e8 commit d2cafdd

File tree

28 files changed

+166
-129
lines changed

28 files changed

+166
-129
lines changed

.idea/gradle.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

commons/src/main/java/org/eclipse/edc/heleade/commons/verify/claims/Claims.java renamed to commons/src/main/java/org/eclipse/edc/heleade/commons/verification/claims/Claims.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.heleade.commons.verify.claims;
15+
package org.eclipse.edc.heleade.commons.verification.claims;
1616

1717
import com.fasterxml.jackson.databind.ObjectMapper;
1818

@@ -90,5 +90,33 @@ public static boolean verifyClaim(String verifiedClaim, Map<String, Object> part
9090
public static boolean verifyClaims(Map<String, Object> participantClaims, Map<String, Object> participantClaimsFromFc) {
9191
return Objects.equals(participantClaims, participantClaimsFromFc);
9292
}
93+
94+
/**
95+
* Constructs a JSON string representation of the provided participant information and claims.
96+
*
97+
* @param participantId the unique identifier of the participant
98+
* @param signedClaims the signed claims associated with the participant
99+
* @param participantClaims a map containing participant-specific claims as key-value pairs
100+
* @return a JSON string representation of the provided input data
101+
* @throws RuntimeException if an error occurs while building the JSON string
102+
*/
103+
public static String getJsonBody(String participantId, String signedClaims, Map<String, Object> participantClaims) {
104+
try {
105+
Map<String, Object> body = Map.of(
106+
"participantId", participantId,
107+
"signedClaims", signedClaims,
108+
"claims", participantClaims,
109+
"@context", Map.of(
110+
"@vocab", "https://w3id.org/edc/v0.0.1/ns/"
111+
)
112+
);
113+
114+
ObjectMapper mapper = new ObjectMapper();
115+
return mapper.writeValueAsString(body);
116+
117+
} catch (Exception e) {
118+
throw new RuntimeException("Failed to build JSON body", e);
119+
}
120+
}
93121
}
94122

providers/policy/claims-checker/src/main/java/org/eclipse/edc/heleade/policy/extension/claims/checker/FcParticipantClaimChecker.java renamed to commons/src/main/java/org/eclipse/edc/heleade/commons/verification/claims/checker/FcParticipantClaimChecker.java

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.heleade.policy.extension.claims.checker;
15+
package org.eclipse.edc.heleade.commons.verification.claims.checker;
1616

1717
import com.fasterxml.jackson.core.type.TypeReference;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import org.eclipse.edc.heleade.commons.verification.claims.Claims;
1920
import org.eclipse.edc.spi.monitor.Monitor;
2021

22+
import java.io.IOException;
2123
import java.net.URI;
2224
import java.net.http.HttpClient;
2325
import java.net.http.HttpRequest;
@@ -45,80 +47,34 @@ public FcParticipantClaimChecker(Monitor monitor, String baseUrl) {
4547
this.httpClient = HttpClient.newHttpClient();
4648
}
4749

48-
4950
/**
5051
* Verifies the claims of a participant against the provided signed claims and participant claims data.
5152
* This method sends an HTTP POST request to the verification endpoint and processes the response
5253
* to determine the validity of the participant's signature and claims.
5354
*
55+
* @param baseUrl the ur of the FC catalog
5456
* @param participantId the unique identifier of the participant
5557
* @param signedClaims the signed claims associated with the participant
5658
* @param participantClaims a map containing the participant's specific claims as key-value pairs
57-
* @return {@code true} if both the signature verification and claims verification are successful,
58-
* {@code false} otherwise
59+
* @param httpClient an http client to perform the requests
60+
* @return a {@link VerificationResult} object indicating the success or failure of
61+
* signature and claims verification
62+
* @throws IOException failure during http request
63+
* @throws InterruptedException failure during http request
5964
*/
60-
@Override
61-
public boolean verifyClaims(String participantId, String signedClaims, Map<String, Object> participantClaims) {
62-
63-
try {
64-
65-
String url = baseUrl + "verification";
66-
monitor.info("Verifying participant node: " + participantId);
67-
68-
String json = getJsonBody(participantId, signedClaims, participantClaims);
69-
var request = HttpRequest.newBuilder()
70-
.uri(URI.create(url))
71-
.header("Content-Type", "application/json")
72-
.POST(HttpRequest.BodyPublishers.ofString(json))
73-
.build();
74-
75-
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
76-
77-
VerificationResult result = parseVerificationResponse(response);
78-
79-
if (!result.signatureResult()) {
80-
monitor.warning("Signature verification failed");
81-
}
65+
public static VerificationResult verifyClaims(String baseUrl, String participantId, String signedClaims, Map<String, Object> participantClaims, HttpClient httpClient) throws IOException, InterruptedException {
66+
String url = baseUrl + "verification";
8267

83-
if (!result.claimsResult()) {
84-
monitor.warning("Claims verification failed");
85-
}
86-
87-
return result.signatureResult() && result.claimsResult();
68+
String json = Claims.getJsonBody(participantId, signedClaims, participantClaims);
69+
var request = HttpRequest.newBuilder()
70+
.uri(URI.create(url))
71+
.header("Content-Type", "application/json")
72+
.POST(HttpRequest.BodyPublishers.ofString(json))
73+
.build();
8874

89-
} catch (Exception e) {
90-
monitor.warning("Failed to verify claims" + e.getMessage());
91-
return false;
92-
}
75+
var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
9376

94-
}
95-
96-
/**
97-
* Constructs a JSON string representation of the provided participant information and claims.
98-
*
99-
* @param participantId the unique identifier of the participant
100-
* @param signedClaims the signed claims associated with the participant
101-
* @param participantClaims a map containing participant-specific claims as key-value pairs
102-
* @return a JSON string representation of the provided input data
103-
* @throws RuntimeException if an error occurs while building the JSON string
104-
*/
105-
public String getJsonBody(String participantId, String signedClaims, Map<String, Object> participantClaims) {
106-
try {
107-
Map<String, Object> body = Map.of(
108-
"participantId", participantId,
109-
"signedClaims", signedClaims,
110-
"claims", participantClaims,
111-
"@context", Map.of(
112-
"@vocab", "https://w3id.org/edc/v0.0.1/ns/"
113-
)
114-
);
115-
116-
ObjectMapper mapper = new ObjectMapper();
117-
return mapper.writeValueAsString(body);
118-
119-
} catch (Exception e) {
120-
throw new RuntimeException("Failed to build JSON body", e);
121-
}
77+
return parseVerificationResponse(response);
12278
}
12379

12480
/**
@@ -129,7 +85,7 @@ public String getJsonBody(String participantId, String signedClaims, Map<String,
12985
* @return a {@link VerificationResult} object indicating the success or failure of
13086
* signature and claims verification
13187
*/
132-
private VerificationResult parseVerificationResponse(HttpResponse<String> response) {
88+
public static VerificationResult parseVerificationResponse(HttpResponse<String> response) {
13389

13490
try {
13591
ObjectMapper mapper = new ObjectMapper();
@@ -155,9 +111,38 @@ private VerificationResult parseVerificationResponse(HttpResponse<String> respon
155111

156112
}
157113

114+
/**
115+
* Verifies the claims of a participant against the provided signed claims and participant claims data.
116+
* This method sends an HTTP POST request to the verification endpoint and processes the response
117+
* to determine the validity of the participant's signature and claims.
118+
*
119+
* @param participantId the unique identifier of the participant
120+
* @param signedClaims the signed claims associated with the participant
121+
* @param participantClaims a map containing the participant's specific claims as key-value pairs
122+
* @return {@code true} if both the signature verification and claims verification are successful,
123+
* {@code false} otherwise
124+
*/
125+
@Override
126+
public boolean verifyClaims(String participantId, String signedClaims, Map<String, Object> participantClaims) {
127+
try {
128+
129+
VerificationResult result = verifyClaims(this.baseUrl, participantId, signedClaims, participantClaims, this.httpClient);
130+
131+
if (!result.signatureResult()) {
132+
monitor.warning("Signature verification failed");
133+
}
158134

135+
if (!result.claimsResult()) {
136+
monitor.warning("Claims verification failed");
137+
}
159138

139+
return result.signatureResult() && result.claimsResult();
160140

141+
} catch (Exception e) {
142+
monitor.warning("Failed to verify claims" + e.getMessage());
143+
return false;
144+
}
161145

146+
}
162147

163148
}

providers/policy/claims-checker/src/main/java/org/eclipse/edc/heleade/policy/extension/claims/checker/ParticipantClaimChecker.java renamed to commons/src/main/java/org/eclipse/edc/heleade/commons/verification/claims/checker/ParticipantClaimChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.heleade.policy.extension.claims.checker;
15+
package org.eclipse.edc.heleade.commons.verification.claims.checker;
1616

1717

1818
import java.util.Map;

providers/policy/claims-checker/src/main/java/org/eclipse/edc/heleade/policy/extension/claims/checker/VerificationResult.java renamed to commons/src/main/java/org/eclipse/edc/heleade/commons/verification/claims/checker/VerificationResult.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
*
1313
*/
1414

15-
package org.eclipse.edc.heleade.policy.extension.claims.checker;
15+
package org.eclipse.edc.heleade.commons.verification.claims.checker;
16+
17+
import jakarta.json.Json;
18+
import jakarta.json.JsonObject;
19+
import jakarta.json.JsonObjectBuilder;
20+
21+
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
1622

1723
/**
1824
* * Represents the result of a verification process including signature and claim validation.
@@ -38,4 +44,26 @@ public record VerificationResult(boolean signatureResult, boolean claimsResult)
3844
*/
3945
public VerificationResult {
4046
}
47+
48+
/**
49+
* Converts the current instance into a JsonObject representation containing its properties.
50+
*
51+
* @return a JsonObject containing the attributes of the instance
52+
*/
53+
public JsonObject asJsonObject() {
54+
try {
55+
// Create a JSON object with the TargetNode properties
56+
JsonObjectBuilder builder = Json.createObjectBuilder()
57+
.add(EDC_NAMESPACE + "signatureResult", this.signatureResult())
58+
.add(EDC_NAMESPACE + "claimsResult", this.claimsResult());
59+
60+
// Build and return the JSON object
61+
JsonObject jsonObject = builder.build();
62+
63+
return jsonObject;
64+
65+
} catch (Exception e) {
66+
throw new RuntimeException("Error converting VerificationResult to JsonObject", e);
67+
}
68+
}
4169
}

federated-catalog/src/main/java/org/eclipse/edc/heleade/federated/catalog/extension/api/verification/VerificationApiController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import java.util.HashMap;
3737
import java.util.Map;
3838

39-
import static org.eclipse.edc.heleade.commons.verify.claims.Claims.verifyClaims;
40-
import static org.eclipse.edc.heleade.commons.verify.claims.Claims.verifySignature;
39+
import static org.eclipse.edc.heleade.commons.verification.claims.Claims.verifyClaims;
40+
import static org.eclipse.edc.heleade.commons.verification.claims.Claims.verifySignature;
4141

4242

4343
/**

iam-identity/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ dependencies {
2222
implementation(libs.jakarta.rsApi)
2323
implementation(libs.edc.control.plane.core)
2424
implementation(libs.edc.http)
25-
25+
implementation(project(":commons"))
2626
}

iam-identity/src/main/java/org/eclipse/edc/heleade/identity/IamIdentityExtension.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public void initialize(ServiceExtensionContext context) {
112112
IamIdentityService iamIdentityService = new IamIdentityService(typeManager, claims, participantId, signedClaims);
113113
context.registerService(IdentityService.class, iamIdentityService);
114114

115-
webService.registerResource(new IamIdentityApiController(iamIdentityService, context.getMonitor()));
115+
webService.registerResource(
116+
new IamIdentityApiController(iamIdentityService,
117+
participantRegistryUrl,
118+
context.getMonitor())
119+
);
116120

117121
}
118122

iam-identity/src/main/java/org/eclipse/edc/heleade/identity/api/IamIdentityApiController.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
import jakarta.ws.rs.Path;
2020
import jakarta.ws.rs.Produces;
2121
import jakarta.ws.rs.core.MediaType;
22+
import org.eclipse.edc.heleade.commons.verification.claims.checker.FcParticipantClaimChecker;
23+
import org.eclipse.edc.heleade.commons.verification.claims.checker.VerificationResult;
2224
import org.eclipse.edc.heleade.identity.IamIdentityService;
2325
import org.eclipse.edc.spi.monitor.Monitor;
2426

27+
import java.net.http.HttpClient;
28+
2529
/**
2630
* Endpoint to validate the participant identity
2731
*/
@@ -32,18 +36,23 @@ public class IamIdentityApiController {
3236

3337
private final Monitor monitor;
3438
private final IamIdentityService iamIdentityService;
39+
private final HttpClient httpClient;
40+
private final String participantRegistryUrl;
3541

3642
/**
3743
* Instantiates the controller for the verify identity endpoint
3844
*
3945
* @param iamIdentityService identity service
46+
* @param participantRegistryUrl federated catalog url
4047
* @param monitor logger object
4148
*/
42-
public IamIdentityApiController(IamIdentityService iamIdentityService, Monitor monitor) {
49+
public IamIdentityApiController(IamIdentityService iamIdentityService, String participantRegistryUrl, Monitor monitor) {
4350
this.iamIdentityService = iamIdentityService;
4451
this.monitor = monitor;
52+
this.participantRegistryUrl = participantRegistryUrl;
53+
this.httpClient = HttpClient.newHttpClient();
4554
}
46-
55+
4756
/**
4857
* Defines the verify identity endpoint
4958
*
@@ -54,6 +63,24 @@ public IamIdentityApiController(IamIdentityService iamIdentityService, Monitor m
5463
public String verify() {
5564
monitor.debug("Verify identity received a request");
5665

57-
return "{\"response\":\"IdentityProvider: I'm alive!\"}";
66+
try {
67+
68+
monitor.info("Auto Verification participant node: " + iamIdentityService.getClientId());
69+
70+
VerificationResult result = FcParticipantClaimChecker.verifyClaims(
71+
this.participantRegistryUrl,
72+
iamIdentityService.getClientId(),
73+
iamIdentityService.getSignedClaims(),
74+
iamIdentityService.getClaims(),
75+
httpClient
76+
);
77+
78+
return result.asJsonObject().toString();
79+
80+
} catch (Exception e) {
81+
monitor.warning("Failed to verify claims" + e.getMessage());
82+
return "{\"error\": \"Failed to verify claims:" + e.getMessage() + "\"}";
83+
}
84+
5885
}
5986
}

providers/policy/claims-checker/README.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)