From 6c4b686bbc33207d127be039da95750c403b66ea Mon Sep 17 00:00:00 2001 From: Thomas Diesler Date: Wed, 29 Mar 2023 21:17:51 +0200 Subject: [PATCH] [#275] VCTemplateService does not respect configured location --- .../w3c/templates/VcTemplateService.kt | 3 ++- .../id/walt/signatory/WaltIdSignatory.kt | 21 ++++++++++--------- .../id/walt/signatory/SignatoryApiTest.kt | 9 ++++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/id/walt/credentials/w3c/templates/VcTemplateService.kt b/src/main/kotlin/id/walt/credentials/w3c/templates/VcTemplateService.kt index 95c2e227b..e01db5b10 100644 --- a/src/main/kotlin/id/walt/credentials/w3c/templates/VcTemplateService.kt +++ b/src/main/kotlin/id/walt/credentials/w3c/templates/VcTemplateService.kt @@ -5,6 +5,7 @@ import id.walt.credentials.w3c.VerifiableCredential import id.walt.credentials.w3c.W3CIssuer import id.walt.credentials.w3c.toVerifiableCredential import id.walt.servicematrix.ServiceProvider +import id.walt.servicematrix.ServiceRegistry import id.walt.services.WaltIdService import id.walt.services.context.ContextManager import id.walt.services.hkvstore.HKVKey @@ -23,7 +24,7 @@ open class VcTemplateService(private val resourcePath: String = "/vc-templates") private val log = KotlinLogging.logger {} companion object : ServiceProvider { - override fun getService() = object : VcTemplateService() {} + override fun getService() = ServiceRegistry.getService(VcTemplateService::class) override fun defaultImplementation() = VcTemplateService() const val SAVED_VC_TEMPLATES_KEY = "vc-templates" } diff --git a/src/main/kotlin/id/walt/signatory/WaltIdSignatory.kt b/src/main/kotlin/id/walt/signatory/WaltIdSignatory.kt index ee5fc9663..405d754bf 100644 --- a/src/main/kotlin/id/walt/signatory/WaltIdSignatory.kt +++ b/src/main/kotlin/id/walt/signatory/WaltIdSignatory.kt @@ -5,7 +5,7 @@ import id.walt.credentials.w3c.W3CIssuer import id.walt.credentials.w3c.builder.AbstractW3CCredentialBuilder import id.walt.credentials.w3c.builder.W3CCredentialBuilder import id.walt.credentials.w3c.templates.VcTemplate -import id.walt.credentials.w3c.templates.VcTemplateManager +import id.walt.credentials.w3c.templates.VcTemplateService import id.walt.credentials.w3c.toVerifiableCredential import id.walt.crypto.LdSignatureType import id.walt.model.DidMethod @@ -19,7 +19,6 @@ import java.nio.file.Files import java.nio.file.Path import java.time.Instant import java.util.* -import kotlin.NoSuchElementException private val log = KotlinLogging.logger {} @@ -28,6 +27,8 @@ class WaltIdSignatory(configurationPath: String) : Signatory() { private val VC_GROUP = "signatory" override val configuration: SignatoryConfig = fromConfiguration(configurationPath) + private val templateService get () = VcTemplateService.getService() + private fun defaultLdSignatureByDidMethod(did: String): LdSignatureType? { val didUrl = DidUrl.from(did) return when (didUrl.method) { @@ -79,7 +80,7 @@ class WaltIdSignatory(configurationPath: String) : Signatory() { val credentialBuilder = when (Files.exists(Path.of(templateIdOrFilename))) { true -> Files.readString(Path.of(templateIdOrFilename)).toVerifiableCredential() - else -> VcTemplateManager.getTemplate(templateIdOrFilename, true, configuration.templatesFolder).template + else -> templateService.getTemplate(templateIdOrFilename, true, configuration.templatesFolder).template }?.let { W3CCredentialBuilder.fromPartial(it) } ?: throw NoSuchElementException("Template could not be loaded: $templateIdOrFilename") @@ -121,24 +122,24 @@ class WaltIdSignatory(configurationPath: String) : Signatory() { } override fun hasTemplateId(templateId: String) = - runCatching { VcTemplateManager.getTemplate(templateId, false) }.getOrNull() != null + runCatching { templateService.getTemplate(templateId, false) }.getOrNull() != null - override fun listTemplates(): List = VcTemplateManager.listTemplates(configuration.templatesFolder) - override fun listTemplateIds() = VcTemplateManager.listTemplates(configuration.templatesFolder).map { it.name } + override fun listTemplates(): List = templateService.listTemplates(configuration.templatesFolder) + override fun listTemplateIds() = templateService.listTemplates(configuration.templatesFolder).map { it.name } override fun loadTemplate(templateId: String): VerifiableCredential = - VcTemplateManager.getTemplate(templateId, true, configuration.templatesFolder).template + templateService.getTemplate(templateId, true, configuration.templatesFolder).template ?: throw IllegalArgumentException("Could not load template \"$templateId\" into WaltSignatory") override fun importTemplate(templateId: String, template: String) { val vc = VerifiableCredential.fromJson(template) // serialize parsed credential template and save - VcTemplateManager.register(templateId, vc) + templateService.register(templateId, vc) } override fun removeTemplate(templateId: String) { - val template = VcTemplateManager.getTemplate(templateId, true, configuration.templatesFolder) + val template = templateService.getTemplate(templateId, true, configuration.templatesFolder) if (template.mutable) { - VcTemplateManager.unregisterTemplate(templateId) + templateService.unregisterTemplate(templateId) } else { throw IllegalArgumentException("Template is immutable and cannot be removed. Use import to override existing templates.") } diff --git a/src/test/kotlin/id/walt/signatory/SignatoryApiTest.kt b/src/test/kotlin/id/walt/signatory/SignatoryApiTest.kt index 38523d3a5..8b4176a7b 100644 --- a/src/test/kotlin/id/walt/signatory/SignatoryApiTest.kt +++ b/src/test/kotlin/id/walt/signatory/SignatoryApiTest.kt @@ -3,7 +3,7 @@ package id.walt.signatory import com.beust.klaxon.Klaxon import id.walt.common.KlaxonWithConverters import id.walt.credentials.w3c.templates.VcTemplate -import id.walt.credentials.w3c.templates.VcTemplateManager +import id.walt.credentials.w3c.templates.VcTemplateService import id.walt.credentials.w3c.toVerifiableCredential import id.walt.model.DidMethod import id.walt.servicematrix.ServiceMatrix @@ -13,7 +13,6 @@ import id.walt.signatory.rest.IssueCredentialRequest import id.walt.signatory.rest.SignatoryRestAPI import id.walt.test.RESOURCES_PATH import io.kotest.core.spec.style.AnnotationSpec -import io.kotest.inspectors.shouldForAll import io.kotest.matchers.collections.shouldContain import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe @@ -55,6 +54,8 @@ class SignatoryApiTest : AnnotationSpec() { SignatoryRestAPI.stop() } + private val templateService get () = VcTemplateService.getService() + @Test fun testHealth() = runBlocking { val response = client.get("$SIGNATORY_API_URL/health").bodyAsText() @@ -76,7 +77,7 @@ class SignatoryApiTest : AnnotationSpec() { templateNames shouldContain "Europass" templateNames shouldContain "VerifiablePresentation" - VcTemplateManager.listTemplates().map { it.name }.forEach { templateName -> templateNames shouldContain templateName } + templateService.listTemplates().map { it.name }.forEach { templateName -> templateNames shouldContain templateName } println(templates) } @@ -85,7 +86,7 @@ class SignatoryApiTest : AnnotationSpec() { @Test fun testLoadVcTemplates() = runBlocking { - VcTemplateManager.listTemplates().forEach { template -> + templateService.listTemplates().forEach { template -> val templateJson = client.get("$SIGNATORY_API_URL/v1/templates/${template.name}") { contentType(ContentType.Application.Json)