1212 *
1313 */
1414
15- package org .eclipse .edc .heleade .policy . extension .claims .checker ;
15+ package org .eclipse .edc .heleade .commons . verification .claims .checker ;
1616
1717import com .fasterxml .jackson .core .type .TypeReference ;
1818import com .fasterxml .jackson .databind .ObjectMapper ;
19+ import org .eclipse .edc .heleade .commons .verification .claims .Claims ;
1920import org .eclipse .edc .spi .monitor .Monitor ;
2021
22+ import java .io .IOException ;
2123import java .net .URI ;
2224import java .net .http .HttpClient ;
2325import 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}
0 commit comments