Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: credential-status endpoint #829

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package id.walt.webwallet.service.credentials.status

import id.walt.crypto.utils.Base64Utils.base64toBase64Url
import id.walt.webwallet.service.BitStringValueParser
import id.walt.webwallet.service.credentials.CredentialValidator
import id.walt.webwallet.service.credentials.status.fetch.StatusListCredentialFetchFactory
Expand Down Expand Up @@ -29,7 +30,7 @@ class StatusListCredentialStatusService(
extractCredentialSubject(credential) ?: error("STATUS_RETRIEVAL_ERROR (-128)")
credentialValidator.validate(entry.statusPurpose, subject.statusPurpose, subject.type, credential)
.takeIf { it }?.let {
getStatusBit(subject.encodedList, entry.statusListIndex, subject.statusSize)?.let {
getStatusBit(subject.encodedList.base64toBase64Url(), entry.statusListIndex, subject.statusSize)?.let {
val bit = it.joinToString("")
CredentialStatusResult(
type = entry.statusPurpose,
Expand All @@ -40,10 +41,13 @@ class StatusListCredentialStatusService(
} ?: error("STATUS_VERIFICATION_ERROR (-129)")
} ?: error("Error parsing status list entry")

private fun extractCredentialSubject(credential: JsonObject): StatusListCredentialSubject? =
JsonUtils.tryGetData(credential, "credentialSubject")?.let {
json.decodeFromJsonElement(it)
}
private fun extractCredentialSubject(credential: JsonObject): StatusListCredentialSubject? = let {
JsonUtils.tryGetData(credential, "credentialSubject") ?: JsonUtils.tryGetData(
credential, "vc.credentialSubject"
)
}?.let {
json.decodeFromJsonElement(it)
}

private fun getStatusBit(bitstring: String, idx: ULong, bitSize: Int) =
bitStringValueParser.get(bitstring, idx, bitSize)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package id.walt.webwallet.service.credentials.status.fetch

import id.walt.crypto.utils.JwsUtils.decodeJws
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
Expand All @@ -11,8 +12,9 @@ class DefaultStatusListCredentialFetchStrategy(
) : StatusListCredentialFetchStrategy {

private val json = Json { ignoreUnknownKeys = true }
override suspend fun fetch(url: String): JsonObject = http.get(url).bodyAsText().let {
json.decodeFromString(it)
override suspend fun fetch(url: String): JsonObject = http.get(url).bodyAsText().let { content ->
runCatching { content.decodeJws().payload }.fold(onSuccess = { it }, onFailure = {
json.decodeFromString(content)
})
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StatusListCredentialFetchFactory(
) {
private val didPattern = "^did:([^:]+):(.+)"
private val urlPattern =
"^((https?:)(\\/\\/\\/?)([\\w]*(?::[\\w]*)?@)?([\\d\\w\\.-]+)(?::(\\d+))?)?([\\/\\\\w\\.()-]*)?(?:([?][^#]*)?(#.*)?)*"
"^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,}\\.[a-zA-Z0-9()]{1,}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)\$"

fun new(url: String) = when {
didPattern.toRegex().matches(url) -> entraStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class CredentialStatusUseCase(
}
} ?: error("Credential not found or invalid document for id: $credentialId")

private val jsonModule = Json { ignoreUnknownKeys = true }

private fun getStatusEntry(json: JsonObject) = json.jsonObject["credentialStatus"]?.let {
when (it) {
is JsonArray -> Json.decodeFromJsonElement<List<StatusListEntry>>(it)
is JsonObject -> listOf(Json.decodeFromJsonElement<StatusListEntry>(it))
is JsonArray -> jsonModule.decodeFromJsonElement<List<StatusListEntry>>(it)
is JsonObject -> listOf(jsonModule.decodeFromJsonElement<StatusListEntry>(it))
else -> null
}
} ?: emptyList()
Expand Down
Loading