From 80941c178ca52e8bb5e2493f245fe92845e79332 Mon Sep 17 00:00:00 2001 From: John Melati Date: Sat, 19 Oct 2024 04:11:48 +0200 Subject: [PATCH] fix: refactor --- .../sphereon/oid/fed/client/crypto/Crypto.kt | 25 +++++++++++++++++++ .../oid/fed/client/helpers/Helpers.kt | 2 +- .../oid/fed/client/trustchain/TrustChain.kt | 6 ++--- .../oid/fed/client/crypto/CryptoTest.js.kt | 25 ------------------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.kt b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.kt index e1b0c259..ff88500b 100644 --- a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.kt +++ b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/crypto/Crypto.kt @@ -1,7 +1,13 @@ package com.sphereon.oid.fed.client.crypto +import com.sphereon.oid.fed.client.mapper.decodeJWTComponents import com.sphereon.oid.fed.client.types.ICallbackService import com.sphereon.oid.fed.openapi.models.Jwk +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive interface ICryptoService { suspend fun verify( @@ -37,3 +43,22 @@ object CryptoServiceObject : ICryptoCallbackService { expect fun cryptoService(): ICryptoCallbackService expect suspend fun verifyImpl(jwt: String, key: Jwk): Boolean + +private fun findKeyInJwks(keys: JsonArray, kid: String): Jwk? { + val key = keys.firstOrNull { it.jsonObject["kid"]?.jsonPrimitive?.content?.trim() == kid.trim() } + + if (key == null) return null + + return Json.decodeFromJsonElement(Jwk.serializer(), key) +} + +fun getKeyFromJwt(jwt: String): Jwk { + val decodedJwt = decodeJWTComponents(jwt) + + val key = findKeyInJwks( + decodedJwt.payload["jwks"]?.jsonObject?.get("keys")?.jsonArray ?: JsonArray(emptyList()), + decodedJwt.header.kid + ) ?: throw IllegalStateException("Key not found") + + return key +} diff --git a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/helpers/Helpers.kt b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/helpers/Helpers.kt index 98a521b8..e82daf80 100644 --- a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/helpers/Helpers.kt +++ b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/helpers/Helpers.kt @@ -5,5 +5,5 @@ fun getEntityConfigurationEndpoint(iss: String): String { } fun getSubordinateStatementEndpoint(fetchEndpoint: String, sub: String): String { - return "${fetchEndpoint.trim('"')}?sub=$sub" + return "${fetchEndpoint}?sub=$sub" } diff --git a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChain.kt b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChain.kt index b43709e3..b1d3d677 100644 --- a/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChain.kt +++ b/modules/openid-federation-client/src/commonMain/kotlin/com/sphereon/oid/fed/client/trustchain/TrustChain.kt @@ -42,7 +42,7 @@ class TrustChain(private val fetchService: IFetchCallbackService, private val cr } private fun findKeyInJwks(keys: JsonArray, kid: String): Jwk? { - val key = keys.firstOrNull { it.jsonObject["kid"]?.jsonPrimitive?.content?.trim() == kid.trim() } + val key = keys.firstOrNull { it.jsonObject["kid"]?.jsonPrimitive?.content == kid } if (key == null) return null @@ -140,7 +140,7 @@ class TrustChain(private val fetchService: IFetchCallbackService, private val cr if (federationEntityMetadata == null || !federationEntityMetadata.containsKey("federation_fetch_endpoint")) return null val authorityEntityFetchEndpoint = - federationEntityMetadata["federation_fetch_endpoint"]?.toString()?.trim('"') ?: return null + federationEntityMetadata["federation_fetch_endpoint"]?.jsonPrimitive?.content ?: return null val subordinateStatementEndpoint = getSubordinateStatementEndpoint(authorityEntityFetchEndpoint, entityIdentifier) @@ -152,7 +152,7 @@ class TrustChain(private val fetchService: IFetchCallbackService, private val cr val subordinateStatementKey = findKeyInJwks( decodedJwt.payload["jwks"]?.jsonObject?.get("keys")?.jsonArray ?: return null, - decodedSubordinateStatement.header.kid.trim() + decodedSubordinateStatement.header.kid ) if (subordinateStatementKey == null) return null diff --git a/modules/openid-federation-client/src/jsTest/kotlin/com/sphereon/oid/fed/client/crypto/CryptoTest.js.kt b/modules/openid-federation-client/src/jsTest/kotlin/com/sphereon/oid/fed/client/crypto/CryptoTest.js.kt index b6507531..16dbdba2 100644 --- a/modules/openid-federation-client/src/jsTest/kotlin/com/sphereon/oid/fed/client/crypto/CryptoTest.js.kt +++ b/modules/openid-federation-client/src/jsTest/kotlin/com/sphereon/oid/fed/client/crypto/CryptoTest.js.kt @@ -1,14 +1,8 @@ package com.sphereon.oid.fed.client.crypto -import com.sphereon.oid.fed.client.mapper.decodeJWTComponents import com.sphereon.oid.fed.openapi.models.Jwk import kotlinx.coroutines.await import kotlinx.coroutines.test.runTest -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonArray -import kotlinx.serialization.json.jsonArray -import kotlinx.serialization.json.jsonObject -import kotlinx.serialization.json.jsonPrimitive import kotlin.js.Promise import kotlin.test.Test import kotlin.test.assertEquals @@ -23,25 +17,6 @@ external object Jose { class CryptoTest { private val cryptoService = CryptoServiceJS.register(CryptoPlatformCallback()) - private fun findKeyInJwks(keys: JsonArray, kid: String): Jwk? { - val key = keys.firstOrNull { it.jsonObject["kid"]?.jsonPrimitive?.content?.trim() == kid.trim() } - - if (key == null) return null - - return Json.decodeFromJsonElement(Jwk.serializer(), key) - } - - private fun getKeyFromJwt(jwt: String): Jwk { - val decodedJwt = decodeJWTComponents(jwt) - - val key = findKeyInJwks( - decodedJwt.payload["jwks"]?.jsonObject?.get("keys")?.jsonArray ?: JsonArray(emptyList()), - decodedJwt.header.kid - ) ?: throw IllegalStateException("Key not found") - - return key - } - @Test fun testVerifyValidJwt() = runTest { val jwt =