Skip to content

Commit

Permalink
fix: verify key type
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Nov 13, 2024
1 parent d96e62a commit feaf354
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.sphereon.oid.fed.client.fetch

import kotlin.js.JsExport

@JsExport.Ignore
interface IFetchService {
suspend fun fetchStatement(
endpoint: String
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean>
}

@JsExport
@JsName("IFetchService")
external interface IFetchServiceJS {
fun fetchStatement(endpoint: String): Promise<String>
}

@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)

Expand All @@ -27,11 +46,11 @@ class FederationClientJS(
fun resolveTrustChainJS(
entityIdentifier: String,
trustAnchors: Array<String>,
timeout: Int = 10
maxDepth: Int = 10
): Promise<Array<String>?> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,23 +15,14 @@ external object Jose {
fun jwtVerify(jwt: String, key: Any, options: dynamic = definedExternally): Promise<dynamic>
}

@JsExport
@JsName("CryptoService")
external interface ICryptoServiceJS {
fun verify(
jwt: String,
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()
return jsCryptoService.verify(jwt, key).await()
}
}

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

class FetchServiceAdapter(private val jsFetchService: IFetchServiceJS) : IFetchService {
override suspend fun fetchStatement(endpoint: String): String {
Expand Down

0 comments on commit feaf354

Please sign in to comment.