From 4a12c673b5133105d12ca2ad6fe4b9c29430b1ae Mon Sep 17 00:00:00 2001 From: mikeplotean Date: Sun, 17 Sep 2023 20:56:21 +0300 Subject: [PATCH] fix: 328-setting-the-expirydate-when-issuing-credential-via-rest-v1credentialsissue --- .../id/walt/common/SerializationUtils.kt | 19 +++++++++++++++++++ .../kotlin/id/walt/signatory/Signatory.kt | 8 ++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/id/walt/common/SerializationUtils.kt b/src/main/kotlin/id/walt/common/SerializationUtils.kt index c38d26a6..9db0fcb5 100644 --- a/src/main/kotlin/id/walt/common/SerializationUtils.kt +++ b/src/main/kotlin/id/walt/common/SerializationUtils.kt @@ -7,6 +7,7 @@ import id.walt.model.VerificationMethod import id.walt.sdjwt.SDMap import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject +import java.time.Instant @Target(AnnotationTarget.FIELD) annotation class VCList @@ -42,6 +43,9 @@ annotation class DidVerificationRelationships @Target(AnnotationTarget.FIELD) annotation class SDMapProperty +@Target(AnnotationTarget.FIELD) +annotation class InstantValue + class VcConverter(private val singleVC: Boolean, private val singleIfOne: Boolean, private val toVcObject: Boolean) : Converter { override fun canConvert(cls: Class<*>) = singleVC && cls == VerifiableCredential::class.java || cls == List::class.java @@ -162,6 +166,20 @@ val sdMapConverter = object : Converter { } +val klaxonInstantValueConverter = object : Converter { + override fun canConvert(cls: Class<*>) = cls == InstantValue::class.java + + override fun fromJson(jv: JsonValue): Any? = jv.longValue ?: jv.int?.toLong()?.let { + Instant.ofEpochSecond(it) + } + + override fun toJson(value: Any): String { + return (value as Instant).epochSecond.toString() + } + + +} + fun KlaxonWithConverters() = Klaxon() .fieldConverter(VCList::class, VcConverter(singleVC = false, singleIfOne = false, toVcObject = false)) .fieldConverter(VCObjectList::class, VcConverter(singleVC = false, singleIfOne = false, toVcObject = true)) @@ -174,6 +192,7 @@ fun KlaxonWithConverters() = Klaxon() .fieldConverter(DidVerificationRelationships::class, didVerificationRelationshipsConverter) .fieldConverter(SDMapProperty::class, sdMapConverter) .fieldConverter(KotlinxJsonObjectField::class, kotlinxJsonObjectFieldConverter) + .fieldConverter(InstantValue::class, klaxonInstantValueConverter) @Deprecated("Use KlaxonWithConverters()") val KlaxonWithConverters = KlaxonWithConverters() diff --git a/src/main/kotlin/id/walt/signatory/Signatory.kt b/src/main/kotlin/id/walt/signatory/Signatory.kt index 99b7b22e..d0bd6aef 100644 --- a/src/main/kotlin/id/walt/signatory/Signatory.kt +++ b/src/main/kotlin/id/walt/signatory/Signatory.kt @@ -1,6 +1,7 @@ package id.walt.signatory import com.beust.klaxon.Json +import id.walt.common.InstantValue import id.walt.common.SDMapProperty import id.walt.credentials.w3c.VerifiableCredential import id.walt.credentials.w3c.W3CIssuer @@ -39,9 +40,9 @@ data class ProofConfig( @Json(serializeNull = false) val nonce: String? = null, @Json(serializeNull = false) val proofPurpose: String? = null, @Json(serializeNull = false) val credentialId: String? = null, - @Json(serializeNull = false) val issueDate: Instant? = null, // issue date from json-input or current system time if null - @Json(serializeNull = false) val validDate: Instant? = null, // valid date from json-input or current system time if null - @Json(serializeNull = false) val expirationDate: Instant? = null, + @Json(serializeNull = false) @InstantValue val issueDate: Instant? = null, // issue date from json-input or current system time if null + @Json(serializeNull = false) @InstantValue val validDate: Instant? = null, // valid date from json-input or current system time if null + @Json(serializeNull = false) @InstantValue val expirationDate: Instant? = null, @Json(serializeNull = false) val dataProviderIdentifier: String? = null, // may be used for mapping data-sets from a custom data-provider @Json(serializeNull = false) val ldSignatureType: LdSignatureType? = null, @Json(serializeNull = false) val creator: String? = issuerDid, @@ -90,4 +91,3 @@ abstract class Signatory : WaltIdService() { open fun removeTemplate(templateId: String): Unit = implementation.removeTemplate(templateId) open fun hasTemplateId(templateId: String): Boolean = implementation.hasTemplateId(templateId) } -