Skip to content

Commit

Permalink
reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Nov 12, 2024
1 parent fc25995 commit d96e62a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>
}

@JsExport
@JsName("ICryptoService")
interface ICryptoServiceJS {
fun verify(data: String, key: String): Promise<Boolean>
}

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ external object Jose {
external interface ICryptoServiceJS {
fun verify(
jwt: String,
key: Jwk
key: String
): Promise<Boolean>
}

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<Boolean> {
override fun verify(jwt: String, key: String): Promise<Boolean> {
return Promise { resolve, reject ->
try {
val decodedJwt = decodeJWTComponents(jwt)
Expand Down Expand Up @@ -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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
}

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 {
Expand Down

0 comments on commit d96e62a

Please sign in to comment.