Skip to content

Commit

Permalink
fix: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Oct 19, 2024
1 parent ae0d0f3 commit 80941c1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 =
Expand Down

0 comments on commit 80941c1

Please sign in to comment.