@@ -74,9 +74,11 @@ cJSON* MatchCredential(cJSON* credential, cJSON* credential_store) {
7474 cJSON_AddItemReferenceToObject (matched_credential , "subtitle" , cJSON_GetObjectItemCaseSensitive (candidate , "subtitle" ));
7575 cJSON_AddItemReferenceToObject (matched_credential , "icon" , cJSON_GetObjectItemCaseSensitive (candidate , "icon" ));
7676 cJSON * matched_claim_names = cJSON_CreateArray ();
77+ cJSON * matched_claim_metadata = cJSON_CreateArray ();
7778 //printf("candidate %s\n", cJSON_Print(candidate));
7879 AddAllClaims (matched_claim_names , cJSON_GetObjectItemCaseSensitive (candidate , "paths" ));
7980 cJSON_AddItemReferenceToObject (matched_credential , "matched_claim_names" , matched_claim_names );
81+ cJSON_AddItemReferenceToObject (matched_credential , "matched_claim_metadata" , matched_claim_metadata ); // Empty array represents all matched
8082 cJSON_AddItemReferenceToArray (matched_credentials , matched_credential );
8183 }
8284 } else {
@@ -89,6 +91,7 @@ cJSON* MatchCredential(cJSON* credential, cJSON* credential_store) {
8991 cJSON_AddItemReferenceToObject (matched_credential , "subtitle" , cJSON_GetObjectItemCaseSensitive (candidate , "subtitle" ));
9092 cJSON_AddItemReferenceToObject (matched_credential , "icon" , cJSON_GetObjectItemCaseSensitive (candidate , "icon" ));
9193 cJSON * matched_claim_names = cJSON_CreateArray ();
94+ cJSON * matched_claim_metadata = cJSON_CreateArray ();
9295
9396 cJSON * claim ;
9497 cJSON * candidate_claims = cJSON_GetObjectItemCaseSensitive (candidate , "paths" );
@@ -112,16 +115,19 @@ cJSON* MatchCredential(cJSON* credential, cJSON* credential_store) {
112115 cJSON * v ;
113116 cJSON_ArrayForEach (v , claim_values ) {
114117 if (cJSON_Compare (v , cJSON_GetObjectItemCaseSensitive (curr_claim , "value" ), cJSON_True )) {
118+ cJSON_AddItemReferenceToArray (matched_claim_metadata , paths );
115119 cJSON_AddItemReferenceToArray (matched_claim_names , cJSON_GetObjectItem (curr_claim , "display" ));
116120 break ;
117121 }
118122 }
119123 } else {
124+ cJSON_AddItemReferenceToArray (matched_claim_metadata , paths );
120125 cJSON_AddItemReferenceToArray (matched_claim_names , cJSON_GetObjectItem (curr_claim , "display" ));
121126 }
122127 }
123128 }
124129 cJSON_AddItemReferenceToObject (matched_credential , "matched_claim_names" , matched_claim_names );
130+ cJSON_AddItemReferenceToObject (matched_credential , "matched_claim_metadata" , matched_claim_metadata );
125131 if (cJSON_GetArraySize (matched_claim_names ) == cJSON_GetArraySize (claims )) {
126132 cJSON_AddItemReferenceToArray (matched_credentials , matched_credential );
127133 }
@@ -159,7 +165,10 @@ cJSON* MatchCredential(cJSON* credential, cJSON* credential_store) {
159165 cJSON * v ;
160166 cJSON_ArrayForEach (v , claim_values ) {
161167 if (cJSON_Compare (v , cJSON_GetObjectItemCaseSensitive (curr_claim , "value" ), cJSON_True )) {
162- cJSON_AddItemReferenceToObject (matched_claim_ids , claim_id , cJSON_GetObjectItem (curr_claim , "display" ));
168+ cJSON * matched_claim_info = cJSON_CreateObject ();
169+ cJSON_AddItemReferenceToObject (matched_claim_info , "claim_display" , cJSON_GetObjectItem (curr_claim , "display" ));
170+ cJSON_AddItemReferenceToObject (matched_claim_info , "claim_path" , paths );
171+ cJSON_AddItemReferenceToObject (matched_claim_ids , claim_id , matched_claim_info );
163172 break ;
164173 }
165174 }
@@ -171,14 +180,18 @@ cJSON* MatchCredential(cJSON* credential, cJSON* credential_store) {
171180 cJSON * claim_set ;
172181 cJSON_ArrayForEach (claim_set , claim_sets ) {
173182 cJSON * matched_claim_names = cJSON_CreateArray ();
183+ cJSON * matched_claim_metadata = cJSON_CreateArray ();
174184 cJSON * c ;
175185 cJSON_ArrayForEach (c , claim_set ) {
176186 if (cJSON_HasObjectItem (matched_claim_ids , cJSON_GetStringValue (c ))) {
177- cJSON_AddItemReferenceToArray (matched_claim_names , cJSON_GetObjectItemCaseSensitive (matched_claim_ids , cJSON_GetStringValue (c )));
187+ cJSON * matched_claim_info = cJSON_GetObjectItemCaseSensitive (matched_claim_ids , cJSON_GetStringValue (c ));
188+ cJSON_AddItemReferenceToArray (matched_claim_metadata , cJSON_GetObjectItemCaseSensitive (matched_claim_info , "claim_path" ));
189+ cJSON_AddItemReferenceToArray (matched_claim_names , cJSON_GetObjectItemCaseSensitive (matched_claim_info , "claim_display" ));
178190 }
179191 }
180192 if (cJSON_GetArraySize (matched_claim_names ) == cJSON_GetArraySize (claim_set )) {
181193 cJSON_AddItemReferenceToObject (matched_credential , "matched_claim_names" , matched_claim_names );
194+ cJSON_AddItemReferenceToObject (matched_credential , "matched_claim_metadata" , matched_claim_metadata );
182195 cJSON_AddItemReferenceToArray (matched_credentials , matched_credential );
183196 break ;
184197 }
@@ -190,10 +203,12 @@ cJSON* MatchCredential(cJSON* credential, cJSON* credential_store) {
190203 return matched_credentials ;
191204}
192205
193- cJSON * dcql_query (cJSON * query , cJSON * credential_store ) {
194- cJSON * matched_credentials = cJSON_CreateObject ();
206+ cJSON * dcql_query (const int request_id , cJSON * query , cJSON * credential_store ) {
207+ cJSON * match_result = cJSON_CreateObject ();
208+ cJSON * matched_credential_sets = cJSON_CreateArray ();
195209 cJSON * candidate_matched_credentials = cJSON_CreateObject ();
196210 cJSON * credentials = cJSON_GetObjectItemCaseSensitive (query , "credentials" );
211+ cJSON * credential_sets = cJSON_GetObjectItemCaseSensitive (query , "credential_sets" );
197212
198213 cJSON * credential ;
199214 cJSON_ArrayForEach (credential , credentials ) {
@@ -205,11 +220,67 @@ cJSON* dcql_query(cJSON* query, cJSON* credential_store) {
205220 cJSON_AddItemReferenceToObject (m , "matched" , matched );
206221 cJSON_AddItemReferenceToObject (candidate_matched_credentials , id , m );
207222 }
223+ }
208224
209- // Only support matching 1 credential for now
210- if (cJSON_GetArraySize (candidate_matched_credentials ) > 0 ) {
211- matched_credentials = candidate_matched_credentials ;
225+ if (credential_sets == NULL ) {
226+ cJSON * single_matched_credential_set = cJSON_CreateObject ();
227+ cJSON * matched_cred_ids = cJSON_CreateArray ();
228+ cJSON * matched_credential ;
229+ cJSON_ArrayForEach (matched_credential , credentials ) {
230+ cJSON_AddItemReferenceToArray (matched_cred_ids , cJSON_GetObjectItemCaseSensitive (matched_credential , "id" ));
231+ }
232+ char set_id_buffer [16 ];
233+ int chars_written = sprintf (set_id_buffer , "req:%d;null" , request_id );
234+ cJSON_AddStringToObject (single_matched_credential_set , "set_id" , set_id_buffer );
235+ cJSON_AddItemReferenceToObject (single_matched_credential_set , "matched_credential_ids" , matched_cred_ids );
236+ cJSON_AddItemReferenceToArray (matched_credential_sets , single_matched_credential_set );
237+ cJSON_AddItemReferenceToObject (match_result , "matched_credential_sets" , matched_credential_sets );
238+ cJSON_AddItemReferenceToObject (match_result , "matched_credentials" , candidate_matched_credentials );
239+ } else {
240+ cJSON * credential_set ;
241+ int matched = 1 ;
242+ int set_idx = 0 ;
243+ cJSON_ArrayForEach (credential_set , credential_sets ) {
244+ if (cJSON_IsFalse (cJSON_GetObjectItemCaseSensitive (credential_set , "required" ))) {
245+ ++ set_idx ;
246+ continue ;
247+ }
248+ cJSON * options = cJSON_GetObjectItemCaseSensitive (credential_set , "options" );
249+ cJSON * option ;
250+ int credential_set_matched = 0 ;
251+ int option_idx = 0 ;
252+ cJSON_ArrayForEach (option , options ) {
253+ cJSON * matched_cred_ids = cJSON_CreateArray ();
254+ cJSON * cred_id ;
255+ credential_set_matched = 1 ;
256+ cJSON_ArrayForEach (cred_id , option ) {
257+ if (cJSON_GetObjectItemCaseSensitive (candidate_matched_credentials , cJSON_GetStringValue (cred_id )) == NULL ) {
258+ credential_set_matched = 0 ;
259+ break ;
260+ } // Remove for multi-provider support
261+ cJSON_AddItemReferenceToArray (matched_cred_ids , cred_id );
262+ }
263+ if (credential_set_matched != 0 ) {
264+ cJSON * cred_set_info = cJSON_CreateObject ();
265+ char set_id_buffer [26 ];
266+ int chars_written = sprintf (set_id_buffer , "req:%d;set:%d;option:%d" , request_id , set_idx , option_idx );
267+ cJSON_AddStringToObject (cred_set_info , "set_id" , set_id_buffer );
268+ cJSON_AddItemReferenceToObject (cred_set_info , "matched_credential_ids" , matched_cred_ids );
269+ cJSON_AddItemReferenceToArray (matched_credential_sets , cred_set_info );
270+ }
271+ ++ option_idx ;
272+ }
273+ if (credential_set_matched == 0 ) {
274+ matched = 0 ;
275+ break ;
276+ }
277+ ++ set_idx ;
278+ }
279+ if (matched != 0 ) {
280+ cJSON_AddItemReferenceToObject (match_result , "matched_credential_sets" , matched_credential_sets );
281+ cJSON_AddItemReferenceToObject (match_result , "matched_credentials" , candidate_matched_credentials );
212282 }
213283 }
214- return matched_credentials ;
284+
285+ return match_result ;
215286}
0 commit comments