From 0cb44c179dae1b760ea6edf59eb63e89126224c9 Mon Sep 17 00:00:00 2001 From: mikeplotean Date: Fri, 10 Nov 2023 19:02:51 +0200 Subject: [PATCH] chore: jsonld document loader service --- service-matrix.properties | 1 + .../jsonld/JsonLdDocumentLoaderService.kt | 19 +++++++++++++++++++ .../LocalJsonLdDocumentLoaderService.kt | 8 ++++++++ .../RemoteJsonLdDocumentLoaderService.kt | 9 +++++++++ .../services/vc/JsonLdCredentialService.kt | 2 +- .../vc/WaltIdJsonLdCredentialService.kt | 17 ++++++++--------- 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/id/walt/credentials/jsonld/JsonLdDocumentLoaderService.kt create mode 100644 src/main/kotlin/id/walt/credentials/jsonld/LocalJsonLdDocumentLoaderService.kt create mode 100644 src/main/kotlin/id/walt/credentials/jsonld/RemoteJsonLdDocumentLoaderService.kt diff --git a/service-matrix.properties b/service-matrix.properties index 56ddac7ec..5af5d726c 100644 --- a/service-matrix.properties +++ b/service-matrix.properties @@ -1,6 +1,7 @@ id.walt.services.ecosystems.essif.didebsi.DidEbsiService=id.walt.services.ecosystems.essif.didebsi.WaltIdDidEbsiService id.walt.services.ecosystems.essif.jsonrpc.JsonRpcService=id.walt.services.ecosystems.essif.jsonrpc.WaltIdJsonRpcService id.walt.services.vc.JsonLdCredentialService=id.walt.services.vc.WaltIdJsonLdCredentialService +id.walt.credentials.jsonld.JsonLdDocumentLoaderService=id.walt.credentials.jsonld.LocalJsonLdDocumentLoaderService id.walt.services.vc.JwtCredentialService=id.walt.services.vc.WaltIdJwtCredentialService id.walt.services.crypto.CryptoService=id.walt.services.crypto.SunCryptoService id.walt.services.keystore.KeyStoreService=id.walt.services.keystore.SqlKeyStoreService diff --git a/src/main/kotlin/id/walt/credentials/jsonld/JsonLdDocumentLoaderService.kt b/src/main/kotlin/id/walt/credentials/jsonld/JsonLdDocumentLoaderService.kt new file mode 100644 index 000000000..81d06fc49 --- /dev/null +++ b/src/main/kotlin/id/walt/credentials/jsonld/JsonLdDocumentLoaderService.kt @@ -0,0 +1,19 @@ +package id.walt.credentials.jsonld + +import com.apicatalog.jsonld.loader.DocumentLoader +import id.walt.servicematrix.ServiceProvider +import id.walt.servicematrix.ServiceRegistry +import id.walt.services.WaltIdService + +abstract class JsonLdDocumentLoaderService: WaltIdService() { + + override val implementation: JsonLdDocumentLoaderService get() = serviceImplementation() + + abstract val documentLoader: DocumentLoader + + companion object : ServiceProvider { + override fun getService() = ServiceRegistry.getService(JsonLdDocumentLoaderService::class) + override fun defaultImplementation() = LocalJsonLdDocumentLoaderService() + + } +} diff --git a/src/main/kotlin/id/walt/credentials/jsonld/LocalJsonLdDocumentLoaderService.kt b/src/main/kotlin/id/walt/credentials/jsonld/LocalJsonLdDocumentLoaderService.kt new file mode 100644 index 000000000..7156215f8 --- /dev/null +++ b/src/main/kotlin/id/walt/credentials/jsonld/LocalJsonLdDocumentLoaderService.kt @@ -0,0 +1,8 @@ +package id.walt.credentials.jsonld + +import com.apicatalog.jsonld.loader.DocumentLoader + +class LocalJsonLdDocumentLoaderService : JsonLdDocumentLoaderService() { + override val documentLoader: DocumentLoader + get() = VerifiableCredentialContexts.DOCUMENT_LOADER +} diff --git a/src/main/kotlin/id/walt/credentials/jsonld/RemoteJsonLdDocumentLoaderService.kt b/src/main/kotlin/id/walt/credentials/jsonld/RemoteJsonLdDocumentLoaderService.kt new file mode 100644 index 000000000..1c36b5522 --- /dev/null +++ b/src/main/kotlin/id/walt/credentials/jsonld/RemoteJsonLdDocumentLoaderService.kt @@ -0,0 +1,9 @@ +package id.walt.credentials.jsonld + +import com.apicatalog.jsonld.loader.DocumentLoader +import info.weboftrust.ldsignatures.jsonld.LDSecurityContexts + +class RemoteJsonLdDocumentLoaderService : JsonLdDocumentLoaderService() { + override val documentLoader: DocumentLoader + get() = LDSecurityContexts.DOCUMENT_LOADER +} diff --git a/src/main/kotlin/id/walt/services/vc/JsonLdCredentialService.kt b/src/main/kotlin/id/walt/services/vc/JsonLdCredentialService.kt index b0665b422..01ddafa96 100644 --- a/src/main/kotlin/id/walt/services/vc/JsonLdCredentialService.kt +++ b/src/main/kotlin/id/walt/services/vc/JsonLdCredentialService.kt @@ -19,7 +19,7 @@ enum class VerificationType { @Serializable data class VerificationResult(val verified: Boolean, val verificationType: VerificationType) -abstract class JsonLdCredentialService : WaltIdService() { +abstract class JsonLdCredentialService() : WaltIdService() { override val implementation get() = serviceImplementation() open fun sign(jsonCred: String, config: ProofConfig): String = implementation.sign(jsonCred, config) diff --git a/src/main/kotlin/id/walt/services/vc/WaltIdJsonLdCredentialService.kt b/src/main/kotlin/id/walt/services/vc/WaltIdJsonLdCredentialService.kt index 0f0b50936..51315e4a3 100644 --- a/src/main/kotlin/id/walt/services/vc/WaltIdJsonLdCredentialService.kt +++ b/src/main/kotlin/id/walt/services/vc/WaltIdJsonLdCredentialService.kt @@ -7,7 +7,7 @@ import foundation.identity.jsonld.ConfigurableDocumentLoader import foundation.identity.jsonld.JsonLDException import foundation.identity.jsonld.JsonLDObject import id.walt.auditor.VerificationPolicyResult -import id.walt.credentials.jsonld.VerifiableCredentialContexts +import id.walt.credentials.jsonld.JsonLdDocumentLoaderService import id.walt.credentials.w3c.* import id.walt.credentials.w3c.schema.SchemaValidatorFactory import id.walt.crypto.Key @@ -20,7 +20,6 @@ import id.walt.services.keystore.KeyStoreService import id.walt.signatory.ProofConfig import id.walt.signatory.ProofType import info.weboftrust.ldsignatures.LdProof -import info.weboftrust.ldsignatures.jsonld.LDSecurityContexts import info.weboftrust.ldsignatures.verifier.LdVerifier import mu.KotlinLogging import org.json.JSONObject @@ -32,10 +31,10 @@ import java.util.* private val log = KotlinLogging.logger {} -open class WaltIdJsonLdCredentialService : JsonLdCredentialService() { +open class WaltIdJsonLdCredentialService() : JsonLdCredentialService() { - private val keyStore: KeyStoreService - get() = ContextManager.keyStore + private val keyStore: KeyStoreService get() = ContextManager.keyStore + private val documentLoaderService: JsonLdDocumentLoaderService get() = JsonLdDocumentLoaderService.getService() init { Ed25519Provider.set(TinkEd25519Provider()) @@ -110,13 +109,13 @@ open class WaltIdJsonLdCredentialService : JsonLdCredentialService() { log.debug { "Signing jsonLd object with: issuerDid (${config.issuerDid}), domain (${config.domain}), nonce (${config.nonce}" } val jsonLdObject: JsonLDObject = JsonLDObject.fromJson(jsonCred) - val confLoader = VerifiableCredentialContexts.DOCUMENT_LOADER as ConfigurableDocumentLoader + val confLoader = documentLoaderService.documentLoader as ConfigurableDocumentLoader confLoader.isEnableHttp = true confLoader.isEnableHttps = true confLoader.isEnableFile = true confLoader.isEnableLocalCache = true - jsonLdObject.documentLoader = VerifiableCredentialContexts.DOCUMENT_LOADER as ConfigurableDocumentLoader + jsonLdObject.documentLoader = documentLoaderService.documentLoader val vm = config.issuerVerificationMethod ?: config.issuerDid val key = keyStore.load(vm) @@ -183,7 +182,7 @@ open class WaltIdJsonLdCredentialService : JsonLdCredentialService() { log.debug { "Verification key for: $vm is: $publicKey" } - val confLoader = LDSecurityContexts.DOCUMENT_LOADER as ConfigurableDocumentLoader + val confLoader = documentLoaderService.documentLoader as ConfigurableDocumentLoader confLoader.isEnableHttp = true confLoader.isEnableHttps = true @@ -193,7 +192,7 @@ open class WaltIdJsonLdCredentialService : JsonLdCredentialService() { log.debug { "Document loader config: isEnableHttp (${confLoader.isEnableHttp}), isEnableHttps (${confLoader.isEnableHttps}), isEnableFile (${confLoader.isEnableFile}), isEnableLocalCache (${confLoader.isEnableLocalCache})" } val jsonLdObject = JsonLDObject.fromJson(vcOrVp) - jsonLdObject.documentLoader = LDSecurityContexts.DOCUMENT_LOADER + jsonLdObject.documentLoader = documentLoaderService.documentLoader log.debug { "Decoded Json LD object: $jsonLdObject" } val ldProof = LdProof.getFromJsonLDObject(jsonLdObject)