Skip to content

Commit

Permalink
Merge pull request #277 from tdiesler/ghi275
Browse files Browse the repository at this point in the history
[#275] VCTemplateService does not respect configured location
  • Loading branch information
mikeplotean authored Apr 10, 2023
2 parents d836237 + 6c4b686 commit 0c7d120
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/kotlin/id/walt/signatory/WaltIdSignatory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {}

Expand All @@ -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) {
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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<VcTemplate> = VcTemplateManager.listTemplates(configuration.templatesFolder)
override fun listTemplateIds() = VcTemplateManager.listTemplates(configuration.templatesFolder).map { it.name }
override fun listTemplates(): List<VcTemplate> = 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.")
}
Expand Down
9 changes: 5 additions & 4 deletions src/test/kotlin/id/walt/signatory/SignatoryApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit 0c7d120

Please sign in to comment.