From d96e62ad3ce7b276bdf6fa858e66d069335be653 Mon Sep 17 00:00:00 2001 From: John Melati Date: Tue, 12 Nov 2024 21:39:29 +0100 Subject: [PATCH] reorganize code --- .../fed/client/trustchain/TrustChainTest.kt | 10 ++++++- .../com/sphereon/oid/fed/client/Client.js.kt | 30 +++---------------- .../oid/fed/client/crypto/Crypto.js.kt | 12 ++++++-- .../sphereon/oid/fed/client/fetch/Fetch.js.kt | 14 +++++++++ 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/modules/openid-federation-client/src/commonTest/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChainTest.kt b/modules/openid-federation-client/src/commonTest/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChainTest.kt index 37cd66b..218c525 100644 --- a/modules/openid-federation-client/src/commonTest/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChainTest.kt +++ b/modules/openid-federation-client/src/commonTest/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChainTest.kt @@ -1,7 +1,9 @@ package com.sphereon.oid.fed.client.trustchain import com.sphereon.oid.fed.client.FederationClient +import com.sphereon.oid.fed.client.crypto.ICryptoService import com.sphereon.oid.fed.client.fetch.IFetchService +import com.sphereon.oid.fed.openapi.models.Jwk import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals @@ -13,10 +15,16 @@ object FetchService : IFetchService { } } +object CryptoService : ICryptoService { + override suspend fun verify(jwt: String, key: Jwk): Boolean { + return true + } +} + class TrustChainTest { @Test fun buildTrustChain() = runTest { - val client = FederationClient(FetchService) + val client = FederationClient(FetchService, CryptoService) val trustChain = client.resolveTrustChain( "https://spid.wbss.it/Spid/oidc/rp/ipasv_lt", diff --git a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/Client.js.kt b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/Client.js.kt index 8577478..41e7307 100644 --- a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/Client.js.kt +++ b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/Client.js.kt @@ -1,37 +1,15 @@ +import com.sphereon.oid.fed.client.crypto.CryptoServiceAdapter import com.sphereon.oid.fed.client.crypto.ICryptoService +import com.sphereon.oid.fed.client.crypto.ICryptoServiceJS import com.sphereon.oid.fed.client.crypto.cryptoService +import com.sphereon.oid.fed.client.fetch.FetchServiceAdapter import com.sphereon.oid.fed.client.fetch.IFetchService +import com.sphereon.oid.fed.client.fetch.IFetchServiceJS import com.sphereon.oid.fed.client.fetch.fetchService import com.sphereon.oid.fed.client.trustchain.TrustChain -import com.sphereon.oid.fed.openapi.models.Jwk import kotlinx.coroutines.* -import kotlinx.serialization.json.Json import kotlin.js.Promise -@JsExport -@JsName("IFetchService") -interface IFetchServiceJS { - fun fetchStatement(endpoint: String): Promise -} - -@JsExport -@JsName("ICryptoService") -interface ICryptoServiceJS { - fun verify(data: String, key: String): Promise -} - -class FetchServiceAdapter(private val jsFetchService: IFetchServiceJS) : IFetchService { - override suspend fun fetchStatement(endpoint: String): String { - return jsFetchService.fetchStatement(endpoint).await() - } -} - -class CryptoServiceAdapter(private val jsCryptoService: ICryptoServiceJS) : ICryptoService { - override suspend fun verify(jwt: String, key: Jwk): Boolean { - return jsCryptoService.verify(jwt, Json.encodeToString(Jwk.serializer(), key)).await() - } -} - @JsExport @JsName("FederationClient") class FederationClientJS( diff --git a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.js.kt b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.js.kt index 834aed3..ea73ef0 100644 --- a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.js.kt +++ b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.js.kt @@ -19,12 +19,18 @@ external object Jose { external interface ICryptoServiceJS { fun verify( jwt: String, - key: Jwk + key: String ): Promise } +class CryptoServiceAdapter(private val jsCryptoService: ICryptoServiceJS) : ICryptoService { + override suspend fun verify(jwt: String, key: Jwk): Boolean { + return jsCryptoService.verify(jwt, Json.encodeToString(Jwk.serializer(), key)).await() + } +} + object CryptoServiceJS : ICryptoServiceJS { - override fun verify(jwt: String, key: Jwk): Promise { + override fun verify(jwt: String, key: String): Promise { return Promise { resolve, reject -> try { val decodedJwt = decodeJWTComponents(jwt) @@ -52,7 +58,7 @@ object CryptoServiceJS : ICryptoServiceJS { actual fun cryptoService(): ICryptoService { return object : ICryptoService { override suspend fun verify(jwt: String, key: Jwk): Boolean { - return CryptoServiceJS.verify(jwt, key).await() + return CryptoServiceJS.verify(jwt, Json.encodeToString(key)).await() } } } diff --git a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt index 8b0addd..d3c48f3 100644 --- a/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt +++ b/modules/openid-federation-client/src/jsMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.js.kt @@ -5,6 +5,20 @@ import io.ktor.client.call.* import io.ktor.client.engine.js.* import io.ktor.client.request.* import io.ktor.http.* +import kotlinx.coroutines.await +import kotlin.js.Promise + +@JsExport +@JsName("IFetchService") +interface IFetchServiceJS { + fun fetchStatement(endpoint: String): Promise +} + +class FetchServiceAdapter(private val jsFetchService: IFetchServiceJS) : IFetchService { + override suspend fun fetchStatement(endpoint: String): String { + return jsFetchService.fetchStatement(endpoint).await() + } +} actual fun fetchService(): IFetchService { return object : IFetchService {