diff --git a/modules/local-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/LocalKms.kt b/modules/local-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/LocalKms.kt index 00a14f7e..dc88b2c3 100644 --- a/modules/local-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/LocalKms.kt +++ b/modules/local-kms/src/commonMain/kotlin/com/sphereon/oid/fed/kms/local/LocalKms.kt @@ -2,12 +2,11 @@ package com.sphereon.oid.fed.kms.local import com.sphereon.oid.fed.kms.local.database.LocalKmsDatabase import com.sphereon.oid.fed.kms.local.jwk.generateKeyPair -import com.sphereon.oid.fed.openapi.models.JWTHeader import com.sphereon.oid.fed.kms.local.jwt.sign import com.sphereon.oid.fed.kms.local.jwt.verify +import com.sphereon.oid.fed.openapi.models.JWTHeader import com.sphereon.oid.fed.openapi.models.Jwk -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.* class LocalKms { @@ -20,8 +19,30 @@ class LocalKms { fun sign(header: JWTHeader, payload: JsonObject, keyId: String): String { val jwk = database.getKey(keyId) + val jwkString: String = Json.decodeFromString(jwk.private_key) + val jwkObject: Jwk = Json.decodeFromString(jwkString) + + // Adding necessary parameter is header + val mHeader = header.copy(alg = jwkObject.alg, kid = jwkObject.kid) + + // Adding JWKs object in payload + val mutablePayload = payload.toMutableMap() + mutablePayload["kid"] = JsonPrimitive(jwkObject.kid) + val keyArrayOfJwks = buildJsonObject { + putJsonArray("keys") { + addJsonObject { + put("kty", jwkObject.kty) + put("n", jwkObject.n) + put("e", jwkObject.e) + put("kid", jwkObject.kid) + put("use", jwkObject.use) + } + } + } + mutablePayload["jwks"] = keyArrayOfJwks + val mPayload = JsonObject(mutablePayload) - return sign(header = header, payload = payload, key = Json.decodeFromString(jwk.private_key)) + return sign(header = mHeader, payload = mPayload, key = jwkObject) } fun verify(token: String, jwk: Jwk): Boolean {