Skip to content

Commit

Permalink
fix: Added JWK object into payload body
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmathew committed Sep 2, 2024
1 parent 282aadf commit 623d2be
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

0 comments on commit 623d2be

Please sign in to comment.