From 63b8363da9886677cc529d37c251e6d4f1ccaa54 Mon Sep 17 00:00:00 2001 From: mikeplotean Date: Tue, 14 Jan 2025 17:20:42 +0200 Subject: [PATCH] refactor: revocation policy --- .../policies/policies/RevocationPolicy.jvm.kt | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/waltid-libraries/credentials/waltid-verification-policies/src/jvmMain/kotlin/id/walt/policies/policies/RevocationPolicy.jvm.kt b/waltid-libraries/credentials/waltid-verification-policies/src/jvmMain/kotlin/id/walt/policies/policies/RevocationPolicy.jvm.kt index 8e59be4535..c333c4880c 100644 --- a/waltid-libraries/credentials/waltid-verification-policies/src/jvmMain/kotlin/id/walt/policies/policies/RevocationPolicy.jvm.kt +++ b/waltid-libraries/credentials/waltid-verification-policies/src/jvmMain/kotlin/id/walt/policies/policies/RevocationPolicy.jvm.kt @@ -33,31 +33,37 @@ actual class RevocationPolicy : RevocationPolicyMp() { } } - val response = runCatching { httpClient.get(statusListCredentialUrl!!).bodyAsText() } - - if (response.isFailure) { + val response = runCatching { httpClient.get(statusListCredentialUrl!!).bodyAsText() }.getOrElse { return Result.failure(Throwable("Error when getting Status List Credential from $statusListCredentialUrl")) } - - return try { - // response is a jwt - val payload = response.getOrThrow().substringAfter(".").substringBefore(".") - .let { Json.decodeFromString(Base64Utils.decode(it).decodeToString()) } - - val credentialSubject = payload["vc"]!!.jsonObject["credentialSubject"]?.jsonObject!! - val encodedList = credentialSubject["encodedList"]?.jsonPrimitive?.content ?: "" - val bitValue = get(encodedList, statusListIndex) - // ensure bitValue always consists of valid binary characters (0,1) - require(!bitValue.isNullOrEmpty()) { "Null or empty bit value" } - require(isBinaryValue(bitValue)) { "Invalid bit value" } - if (StreamUtils.binToInt(bitValue.joinToString("")) == 0) { - Result.success(statusListCredentialUrl!!) - } else { - Result.failure(Throwable("Credential has been revoked")) - } - } catch (e: NumberFormatException) { - throw IllegalArgumentException() + // response is a jwt + val bitValue = getRevocationStatusValue(response, statusListIndex).getOrElse { + return Result.failure(Throwable(it.cause)) + } + checkStatus(bitValue).getOrElse { + return Result.failure(Throwable("Credential has been revoked")) } + return Result.success(statusListCredentialUrl!!) + } + + private fun checkStatus(it: List) = runCatching { + require(StreamUtils.binToInt(it.joinToString("")) == 0) + } + + private fun getRevocationStatusValue( + response: String, + statusListIndex: ULong? + ) = runCatching { + val payload = response.substringAfter(".").substringBefore(".") + .let { Json.decodeFromString(Base64Utils.decode(it).decodeToString()) } + + val credentialSubject = payload["vc"]!!.jsonObject["credentialSubject"]?.jsonObject!! + val encodedList = credentialSubject["encodedList"]?.jsonPrimitive?.content ?: "" + val bitValue = get(encodedList, statusListIndex) + // ensure bitValue always consists of valid binary characters (0,1) + require(!bitValue.isNullOrEmpty()) { "Null or empty bit value" } + require(isBinaryValue(bitValue)) { "Invalid bit value: $bitValue" } + bitValue } }