Skip to content

Commit

Permalink
Update credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
nodh committed Oct 2, 2024
1 parent 055131e commit 4048167
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 85 deletions.
4 changes: 2 additions & 2 deletions conventions-vclib/src/main/resources/vcLibVersions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ uuid=0.8.1
jsonpath=2.3.0
jvm.json=20230618
jvm.cbor=1.18
eupid=2.1.3
mdl=1.0.3
eupid=2.2.0-SNAPSHOT
mdl=1.1.0-SNAPSHOT

android.compileSDK=34
android.minSDK=33
Expand Down
2 changes: 1 addition & 1 deletion mobile-driving-licence-credential
132 changes: 53 additions & 79 deletions vck/src/commonMain/kotlin/at/asitplus/wallet/lib/LibraryInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import at.asitplus.wallet.lib.data.ConstantIndex.supportsVcJwt
import at.asitplus.wallet.lib.data.JsonCredentialSerializer
import at.asitplus.wallet.lib.iso.CborCredentialSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.CompositeEncoder
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.modules.SerializersModule

Expand All @@ -20,41 +17,22 @@ import kotlinx.serialization.modules.SerializersModule
*/
object LibraryInitializer {

@Deprecated(message = "Please use methods that do not use this data class")
data class ExtensionLibraryInfo(
/**
* Implementation of [at.asitplus.wallet.lib.data.ConstantIndex.CredentialScheme].
*/
val credentialScheme: ConstantIndex.CredentialScheme,
/**
* Definition of a polymorphic serializers module in this form:
* ```
* kotlinx.serialization.modules.SerializersModule {
* kotlinx.serialization.modules.polymorphic(CredentialSubject::class) {
* kotlinx.serialization.modules.subclass(YourCredential::class)
* }
* }
* ```
*/
val serializersModule: SerializersModule,
)

/**
* Register the extension library with information from [data].
*/
@Deprecated(
message = "Please use methods not using the data class",
replaceWith = ReplaceWith("registerExtensionLibrary(credentialScheme, serializersModule)")
)
fun registerExtensionLibrary(@Suppress("DEPRECATION") data: ExtensionLibraryInfo) {
registerExtensionLibrary(data.credentialScheme, data.serializersModule)
}

/**
* Register [credentialScheme] to be used with this library, e.g. in OpenID protocol implementations.
*
* Specify [serializersModule] if the credential scheme supports [ConstantIndex.CredentialRepresentation.PLAIN_JWT],
* i.e. it implements a subclass of [at.asitplus.wallet.lib.data.CredentialSubject] that needs to be de/serialized.
*
* Implement `serializersModule` in this form:
* ```
* kotlinx.serialization.modules.SerializersModule {
* kotlinx.serialization.modules.polymorphic(CredentialSubject::class) {
* kotlinx.serialization.modules.subclass(YourCredential::class)
* }
* }
* ```
*
* @param serializersModule Definition of a polymorphic serializers module, see example in function doc.
*/
fun registerExtensionLibrary(
credentialScheme: ConstantIndex.CredentialScheme,
Expand All @@ -70,10 +48,42 @@ object LibraryInitializer {
* Register [credentialScheme] to be used with this library, e.g. in OpenID protocol implementations.
* Used for credentials supporting [at.asitplus.wallet.lib.data.ConstantIndex.CredentialRepresentation.ISO_MDOC],
* which need to specify several functions to allow encoding any values
* in [at.asitplus.wallet.lib.iso.IssuerSignedItem]. See the function typealiases for implementation notes.
* in [at.asitplus.wallet.lib.iso.IssuerSignedItem].
* See the function typealiases in [JsonValueEncoder] and [ElementIdentifierToItemValueSerializerMap]
* for implementation notes.
*
*
* Example for [serializersModule]:
* ```
* kotlinx.serialization.modules.SerializersModule {
* kotlinx.serialization.modules.polymorphic(CredentialSubject::class) {
* kotlinx.serialization.modules.subclass(YourCredential::class)
* }
* }
* ```
*
* Example for [jsonValueEncoder]:
* ```
* when (it) {
* is DrivingPrivilege -> vckJsonSerializer.encodeToJsonElement(it)
* is LocalDate -> vckJsonSerializer.encodeToJsonElement(it)
* is UInt -> vckJsonSerializer.encodeToJsonElement(it)
* else -> null
* }
* ```
*
* Example for [itemValueSerializerMap]:
* ```
* mapOf(
* MobileDrivingLicenceDataElements.BIRTH_DATE to LocalDate.serializer(),
* MobileDrivingLicenceDataElements.PORTRAIT to ByteArraySerializer(),
* )
* ```
*
* @param serializersModule needed if supporting [ConstantIndex.CredentialRepresentation.PLAIN_JWT],
* i.e. it implements a subclass of [at.asitplus.wallet.lib.data.CredentialSubject] that needs to be de/serialized.
* @param jsonValueEncoder used to describe the credential in input descriptors used in verifiable presentations,
* e.g. when used in SIOPv2
* e.g. when used in SIOPv2
* @param itemValueSerializerMap used to actually serialize and deserialize `Any` object in
* [at.asitplus.wallet.lib.iso.IssuerSignedItemSerializer], with `elementIdentifier` as the key
*/
Expand All @@ -91,58 +101,22 @@ object LibraryInitializer {
}

/**
* Implementation may be
* Used to encode any value into a [JsonElement], implementation may be
* ```
* if (value is Array<*> && value.isNotEmpty() && value.all { it is DrivingPrivilege }) {
* true.also {
* compositeEncoder.encodeSerializableElement(
* descriptor,
* index,
* ArraySerializer<DrivingPrivilege, DrivingPrivilege>(DrivingPrivilege.serializer()),
* value as Array<DrivingPrivilege>
* )
* }
* } else {
* false
* when (it) {
* is DrivingPrivilege -> vckJsonSerializer.encodeToJsonElement(it)
* is LocalDate -> vckJsonSerializer.encodeToJsonElement(it)
* is UInt -> vckJsonSerializer.encodeToJsonElement(it)
* else -> null
* }
* ```
*/
typealias ItemValueEncoder
= (descriptor: SerialDescriptor, index: Int, compositeEncoder: CompositeEncoder, value: Any) -> Unit

/**
* Implementation may be
* ```
* compositeDecoder.decodeSerializableElement(
* descriptor,
* index,
* ArraySerializer(DrivingPrivilege.serializer())
* )
* ```
*/
typealias ItemValueDecoder
= (descriptor: SerialDescriptor, index: Int, compositeDecoder: CompositeDecoder) -> Any

/**
* Implementation may be
* ```
* if (it is Array<*>) ArraySerializer(DrivingPrivilege.serializer()) else null
* ```
*/
typealias SerializerLookup
= (element: Any) -> KSerializer<*>?

/**
* Implementation may be
* ```
* if (it is DrivingPrivilege) jsonSerializer.encodeToJsonElement(it) else null
* ```
*/
typealias JsonValueEncoder
= (value: Any) -> JsonElement?

/**
* Maps from `IssuerSignedItem.elementIdentifier` to its corresponding [KSerializer]
* Maps from [at.asitplus.wallet.lib.iso.IssuerSignedItem.elementIdentifier] (the claim name) to its corresponding
* [KSerializer].
*/
typealias ElementIdentifierToItemValueSerializerMap
= Map<String, KSerializer<*>>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package at.asitplus.wallet.lib.iso

import at.asitplus.wallet.lib.ItemValueDecoder
import at.asitplus.wallet.lib.ItemValueEncoder
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.cbor.Cbor
Expand Down Expand Up @@ -86,3 +84,10 @@ fun ByteArray.stripCborTag(tag: Byte): ByteArray {
fun ByteArray.wrapInCborTag(tag: Byte) = byteArrayOf(0xd8.toByte()) + byteArrayOf(tag) + this

fun ByteArray.sha256(): ByteArray = toByteString().sha256().toByteArray()


private typealias ItemValueEncoder
= (descriptor: SerialDescriptor, index: Int, compositeEncoder: CompositeEncoder, value: Any) -> Unit

private typealias ItemValueDecoder
= (descriptor: SerialDescriptor, index: Int, compositeDecoder: CompositeDecoder) -> Any

0 comments on commit 4048167

Please sign in to comment.