Skip to content

Commit

Permalink
Fix: Updated filtering logic for SD-JWT with nested structure
Browse files Browse the repository at this point in the history
  • Loading branch information
josmilan committed Oct 10, 2024
1 parent 69847ed commit 91fafb8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,37 @@ class SDJWTService : SDJWTServiceInterface {
return Gson().toJson(jsonObject)
}

fun updateIssuerJwtWithDisclosuresForFiltering(credential: String?): String? {
val split = credential?.split(".")

val jsonString = Base64.decode(
split?.get(1) ?: "",
Base64.URL_SAFE
).toString(charset("UTF-8"))

val jsonObject = Gson().fromJson(jsonString, JsonObject::class.java)

val hashList: MutableList<String> = mutableListOf()
var disclosures = getDisclosuresFromSDJWT(credential)
disclosures = disclosures?.filter { it != null && it.isNotBlank() }
disclosures?.forEach { encodedString ->
try {
val hash = calculateSHA256Hash(encodedString)
if (hash != null) {
hashList.add(hash)
}
} catch (e: IllegalArgumentException) {
}
}

addDisclosuresToCredentialForFiltering(
jsonObject,
disclosures ?: listOf(),
hashList
)
return Gson().toJson(jsonObject)
}

private fun addDisclosuresToCredential(
jsonElement: JsonElement,
disclosures: List<String>,
Expand Down Expand Up @@ -294,6 +325,45 @@ class SDJWTService : SDJWTServiceInterface {
}
}

private fun addDisclosuresToCredentialForFiltering(
jsonElement: JsonElement,
disclosures: List<String>,
hashList: MutableList<String>
) {
if (jsonElement.isJsonObject) {
val jsonObject = jsonElement.asJsonObject
if (jsonObject.has("_sd")) {
val sdList = jsonObject.getAsJsonArray("_sd")

hashList.forEachIndexed { index, hash ->

if (isStringPresentInJSONArray(sdList, hash)) {
try {
val disclosure = Base64.decode(
disclosures[index],
Base64.URL_SAFE
).toString(charset("UTF-8"))
// Extract key-value pair from the encodedString
val (decodedKey, decodedValue) = extractKeyValue(disclosure)

jsonObject.addProperty(decodedKey, disclosure)

} catch (e: IllegalArgumentException) {
// Handle invalid base64-encoded strings
}
}
}
}
jsonObject.entrySet().forEach { (_, value) ->
addDisclosuresToCredentialForFiltering(value, disclosures, hashList)
}
} else if (jsonElement.isJsonArray) {
jsonElement.asJsonArray.forEach { arrayElement ->
addDisclosuresToCredentialForFiltering(arrayElement, disclosures, hashList)
}
}
}

private fun isStringPresentInJSONArray(jsonArray: JsonArray, searchString: String): Boolean {
for (i in 0 until jsonArray.size()) {
val element = jsonArray.elementAt(i).asString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class VerificationService : VerificationServiceInterface {


val jsonString = if ((cred?.split("~")?.size ?: 0) > 0)
SDJWTService().updateIssuerJwtWithDisclosures(cred)
SDJWTService().updateIssuerJwtWithDisclosuresForFiltering(cred)
else
Base64.decode(
split?.get(1) ?: "",
Expand Down

0 comments on commit 91fafb8

Please sign in to comment.