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

WAL-775: Verifier presentation definition policy bug fixes #861

Open
wants to merge 13 commits 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
Expand Up @@ -8,6 +8,7 @@ import id.walt.definitionparser.PresentationSubmission
import id.walt.policies.CredentialWrapperValidatorPolicy
import id.walt.sdjwt.SDJwt
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toList
import kotlinx.serialization.Serializable
Expand All @@ -28,36 +29,40 @@ class PresentationDefinitionPolicy : CredentialWrapperValidatorPolicy(
val presentationDefinition = context["presentationDefinition"]?.toJsonElement()
?.let { Json.decodeFromJsonElement<PresentationDefinition>(it) }
?: throw IllegalArgumentException("No presentationDefinition in context!")
require(presentationDefinition.inputDescriptors.isNotEmpty()) {
"No input descriptors were found. " +
"At least one is required in the context of the presentation definition policy."
}
val presentationSubmission = context["presentationSubmission"]?.toJsonElement()
?.let { Json.decodeFromJsonElement<PresentationSubmission>(it) }
?: throw IllegalArgumentException("No presentationSubmission in context!")
val format = presentationSubmission.descriptorMap.firstOrNull()?.format?.let { Json.decodeFromJsonElement<VCFormat>(it) }

//val requestedTypes = presentationDefinition.primitiveVerificationGetTypeList()
val format =
presentationSubmission.descriptorMap.firstOrNull()?.format?.let { Json.decodeFromJsonElement<VCFormat>(it) }

val presentedTypes = when(format) {
val presentedTypes = when (format) {
VCFormat.sd_jwt_vc -> listOf(data["vct"]!!.jsonPrimitive.content)
else -> data["vp"]!!.jsonObject["verifiableCredential"]?.jsonArray?.mapNotNull {
it.jsonPrimitive.contentOrNull?.let { SDJwt.parse(it) }?.fullPayload
?.get("vc")?.jsonObject?.get("type")?.jsonArray?.last()?.jsonPrimitive?.contentOrNull
} ?: emptyList()
}

val presentationDefinitionMatch = when(format) {
VCFormat.sd_jwt_vc -> PresentationDefinitionParser.matchCredentialsForInputDescriptor(
flowOf(data), presentationDefinition.inputDescriptors.first()
).toList().isNotEmpty()
else -> data["vp"]!!.jsonObject["verifiableCredential"]?.jsonArray?.mapIndexedNotNull { idx,cred ->
val payload = cred.jsonPrimitive.contentOrNull?.let { SDJwt.parse(it) }?.fullPayload ?: throw IllegalArgumentException("Credential $idx is not a valid JWT string")
val credentialsFlow = when (format) {
VCFormat.sd_jwt_vc -> flowOf(data)

else -> (data["vp"]!!.jsonObject["verifiableCredential"]?.jsonArray?.mapNotNull { vc ->
vc.jsonPrimitive.contentOrNull?.let { SDJwt.parse(it) }?.fullPayload
?: throw IllegalArgumentException("Credential $vc is not a valid (SD-)JWT string")
} ?: emptyList()).asFlow()

PresentationDefinitionParser.matchCredentialsForInputDescriptor(
flowOf(payload["vc"]?.jsonObject ?: payload),
presentationDefinition.inputDescriptors[idx]
).toList().isNotEmpty()
}!!.all { it }
}

val success = /*presentedTypes.containsAll(requestedTypes) &&*/ presentationDefinitionMatch
val success = presentationDefinition.inputDescriptors.map { inputDescriptor ->
PresentationDefinitionParser.matchCredentialsForInputDescriptor(
credentialsFlow,
inputDescriptor,
).toList().isNotEmpty()
}.all { it }

return if (success)
Result.success(presentedTypes)
Expand All @@ -68,11 +73,7 @@ class PresentationDefinitionPolicy : CredentialWrapperValidatorPolicy(
log.debug { "Presented data: $data" }

Result.failure(
id.walt.policies.PresentationDefinitionException(
/*missingCredentialTypes = requestedTypes.minus(
presentedTypes.toSet()
),*/ presentationDefinitionMatch
)
id.walt.policies.PresentationDefinitionException(success)
)
}
}
Expand Down
Loading
Loading