diff --git a/modules/openapi/src/commonMain/kotlin/com/sphereon/oid/fed/openapi/openapi.yaml b/modules/openapi/src/commonMain/kotlin/com/sphereon/oid/fed/openapi/openapi.yaml index c8ba18d..e910b38 100644 --- a/modules/openapi/src/commonMain/kotlin/com/sphereon/oid/fed/openapi/openapi.yaml +++ b/modules/openapi/src/commonMain/kotlin/com/sphereon/oid/fed/openapi/openapi.yaml @@ -1530,7 +1530,6 @@ components: type: string description: The key ID. example: 12345 - nullable: true x: type: string description: The X coordinate for EC keys (optional). diff --git a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.kt b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.kt index 57fddaa..ddc7e1d 100644 --- a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.kt +++ b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/fetch/Fetch.kt @@ -1,8 +1,5 @@ package com.sphereon.oid.fed.client.fetch -import kotlin.js.JsExport - -@JsExport.Ignore interface IFetchService { suspend fun fetchStatement( endpoint: String 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 41e7307..4e34f02 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,23 +1,42 @@ 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 kotlinx.coroutines.* +import com.sphereon.oid.fed.openapi.models.Jwk +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.promise import kotlin.js.Promise +@JsExport +@JsName("ICryptoService") +external interface ICryptoServiceJS { + fun verify( + jwt: String, + key: Jwk + ): Promise +} + +@JsExport +@JsName("IFetchService") +external interface IFetchServiceJS { + fun fetchStatement(endpoint: String): Promise +} + @JsExport @JsName("FederationClient") class FederationClientJS( fetchServiceCallback: IFetchServiceJS?, cryptoServiceCallback: ICryptoServiceJS?, ) { - private val fetchService: IFetchService = if (fetchServiceCallback != null) FetchServiceAdapter(fetchServiceCallback) else fetchService() - private val cryptoService: ICryptoService = if (cryptoServiceCallback != null) CryptoServiceAdapter(cryptoServiceCallback) else cryptoService() + private val fetchService: IFetchService = + if (fetchServiceCallback != null) FetchServiceAdapter(fetchServiceCallback) else fetchService() + private val cryptoService: ICryptoService = + if (cryptoServiceCallback != null) CryptoServiceAdapter(cryptoServiceCallback) else cryptoService() private val trustChainService: TrustChain = TrustChain(fetchService, cryptoService) @@ -27,11 +46,11 @@ class FederationClientJS( fun resolveTrustChainJS( entityIdentifier: String, trustAnchors: Array, - timeout: Int = 10 + maxDepth: Int = 10 ): Promise?> { return scope.promise { try { - trustChainService.resolve(entityIdentifier, trustAnchors, timeout)?.toTypedArray() + trustChainService.resolve(entityIdentifier, trustAnchors, maxDepth)?.toTypedArray() } catch (e: Exception) { throw RuntimeException("Failed to resolve trust chain: ${e.message}", e) } 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 ea73ef0..c0c1f1c 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 @@ -1,5 +1,6 @@ package com.sphereon.oid.fed.client.crypto +import ICryptoServiceJS import com.sphereon.oid.fed.client.mapper.decodeJWTComponents import com.sphereon.oid.fed.openapi.models.Jwk import kotlinx.coroutines.await @@ -14,23 +15,14 @@ external object Jose { fun jwtVerify(jwt: String, key: Any, options: dynamic = definedExternally): Promise } -@JsExport -@JsName("CryptoService") -external interface ICryptoServiceJS { - fun verify( - jwt: String, - 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() + return jsCryptoService.verify(jwt, key).await() } } object CryptoServiceJS : ICryptoServiceJS { - override fun verify(jwt: String, key: String): Promise { + override fun verify(jwt: String, key: Jwk): Promise { return Promise { resolve, reject -> try { val decodedJwt = decodeJWTComponents(jwt) @@ -58,7 +50,7 @@ object CryptoServiceJS : ICryptoServiceJS { actual fun cryptoService(): ICryptoService { return object : ICryptoService { override suspend fun verify(jwt: String, key: Jwk): Boolean { - return CryptoServiceJS.verify(jwt, Json.encodeToString(key)).await() + return CryptoServiceJS.verify(jwt, 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 d3c48f3..b1cf1ea 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 @@ -1,18 +1,12 @@ package com.sphereon.oid.fed.client.fetch +import IFetchServiceJS import io.ktor.client.* 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 {