Skip to content

Commit

Permalink
Wrong display of address in OIDC4VCI test 10 #3143
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkbee1 committed Dec 3, 2024
1 parent e6fe189 commit f0fd8f6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class SelectiveDisclosureDisplayMap {
builtMap[title] = nestedMap;
} else {
builtMap.addAll(
claimData(mapKey, title, type),
claimData(mapKey, title, type, parentKeyId),
);
}
} else {
builtMap.addAll(claimData(mapKey, title, type));
builtMap.addAll(claimData(mapKey, title, type, parentKeyId));
}
});
final Map<String, dynamic> mapFromJwtEntries = fileterdMapFromJwt(
Expand Down Expand Up @@ -168,6 +168,7 @@ class SelectiveDisclosureDisplayMap {
element.key.toString(),
element.key.toString(),
null,
parentKeyId,
),
);
}
Expand Down Expand Up @@ -248,11 +249,13 @@ class SelectiveDisclosureDisplayMap {
String mapKey,
String? title,
String? type,
String? parentKeyId,
) {
final claimDataMap = <String, dynamic>{};
final List<ClaimsData> claimsData =
SelectiveDisclosure(credentialModel).getClaimsData(
key: mapKey,
parentKeyId: parentKeyId,
);

if (claimsData.isEmpty) return claimDataMap;
Expand Down
105 changes: 83 additions & 22 deletions lib/selective_disclosure/selective_disclosure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,7 @@ class SelectiveDisclosure {
try {
final lisString = jsonDecode(element.value.toString());
if (lisString is List) {
if (lisString.length == 3) {
/// '["Qg_O64zqAxe412a108iroA", "phone_number", "+81-80-1234-5678"]'
extractedValues[lisString[1].toString()] = lisString[2];
} else if (lisString.length == 2) {
/// '["Qg_O64zqAxe412a108iroA", "DE']
extractedValues[lisString[0].toString()] = lisString[1];
} else {
throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description':
'The disclosure content should contain 2 or 3 elements.',
},
);
}
extractedValues.addAll(getMapFromList(lisString));
}
} catch (e) {
throw ResponseMessage(
Expand All @@ -97,6 +82,8 @@ class SelectiveDisclosure {
);
}
}
replaceSdValues(extractedValues);

return extractedValues;
}

Expand Down Expand Up @@ -187,7 +174,10 @@ class SelectiveDisclosure {
if (valueType == null) return null;

if (valueType == 'image/jpeg') {
final List<ClaimsData> claimsData = getClaimsData(key: 'picture');
final List<ClaimsData> claimsData = getClaimsData(
key: 'picture',
parentKeyId: null,
);

if (claimsData.isEmpty) return null;
return claimsData[0].data;
Expand All @@ -196,13 +186,21 @@ class SelectiveDisclosure {
}
}

List<ClaimsData> getClaimsData({required String key}) {
List<ClaimsData> getClaimsData({
required String key,
required String? parentKeyId,
}) {
dynamic data;
final value = <ClaimsData>[];
final JsonPath dataPath = JsonPath(
// ignore: prefer_interpolation_to_compose_strings
r'$..["' + key + '"]',
);
final JsonPath dataPath = parentKeyId == null
? JsonPath(
// ignore: prefer_interpolation_to_compose_strings
r'$..["' + key + '"]',
)
: JsonPath(
// ignore: prefer_interpolation_to_compose_strings
r'$..["' + parentKeyId + '"]["' + key + '"]',
);

try {
final uncryptedDataPath = dataPath.read(extractedValuesFromJwt).first;
Expand Down Expand Up @@ -290,4 +288,67 @@ class SelectiveDisclosure {
//
}
}

Map<String, dynamic> replaceSdValues(Map<String, dynamic> extractedValues) {
// Look for _sd in values of extractedValues
// For each value which is a map and contains _sd, replace _sd value with
// the value from disclosureListToContent which has the same sh256 hash
final newExtractedValue = Map<String, dynamic>.from(extractedValues);
extractedValues.forEach((key, value) {
if (value is Map) {
if (value['_sd'] != null && value['_sd'] is List) {
// For each element in the list replace it with element
// from disclosureListToContent which has the same sh256 hash
final sdList = value['_sd'] as List;
for (final sdElement in sdList) {
for (final element in disclosureListToContent.entries.toList()) {
final digest =
OIDC4VC().sh256HashOfContent(element.value.toString());
if (digest == sdElement) {
final toto = getMapFromList(
jsonDecode(element.value.toString()) as List,
).entries.first;
if (toto.value is Map<String, dynamic> &&
toto.value['_sd'] != null) {
newExtractedValue[key] = replaceSdValues(
toto.value as Map<String, dynamic>,
);
} else {
if (toto.value is List || toto.value is Map) {
newExtractedValue[key][toto.key] = replaceSdValues(
toto.value as Map<String, dynamic>,
);
} else {
newExtractedValue[key][toto.key] = toto.value;
}
}
}
}
}
}
// add recursive call if value is a list or a map.
}
// add recursive call if value is a list or a map.
});
return newExtractedValue;
}

Map<String, dynamic> getMapFromList(List lisString) {
if (lisString.length == 3) {
/// '["Qg_O64zqAxe412a108iroA", "phone_number", "+81-80-1234-5678"]'
return {lisString[1].toString(): lisString[2]};
} else if (lisString.length == 2) {
/// '["Qg_O64zqAxe412a108iroA", "DE']
return {lisString[0].toString(): lisString[1]};
} else {
throw ResponseMessage(
data: {
'error': 'invalid_format',
'error_description':
'The disclosure content should contain 2 or 3 elements.',
},
);
}
}
}

0 comments on commit f0fd8f6

Please sign in to comment.