Skip to content

Commit

Permalink
fix: Fixed jvm test for sign and verify
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmathew committed Sep 1, 2024
1 parent c22d139 commit 5228299
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,32 @@ import com.nimbusds.jwt.JWTClaimsSet
import com.nimbusds.jwt.SignedJWT
import com.sphereon.oid.fed.openapi.models.JWTHeader
import com.sphereon.oid.fed.openapi.models.Jwk
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject


actual fun sign(
payload: JsonObject,
header: JWTHeader,
key: Jwk
payload: JsonObject, header: JWTHeader, key: Jwk
): String {
val ecJWK = ECKey.parse(key.toString())

val jwkJsonString = Json.encodeToString(key)
val ecJWK = ECKey.parse(jwkJsonString)
val signer: JWSSigner = ECDSASigner(ecJWK)
val jwsHeader = header.toJWSHeader()

val signedJWT = SignedJWT(
header.toJWSHeader(),
JWTClaimsSet.parse(payload.toString())
jwsHeader, JWTClaimsSet.parse(payload.toString())
)

signedJWT.sign(signer)
return signedJWT.serialize()
}

actual fun verify(
jwt: String,
key: Jwk
jwt: String, key: Jwk
): Boolean {
try {
val ecKey = ECKey.parse(key.toString()) // Parse JWK into ECKey
val jwkJsonString = Json.encodeToString(key)
val ecKey = ECKey.parse(jwkJsonString)
val verifier: JWSVerifier = ECDSAVerifier(ecKey)
val signedJWT = SignedJWT.parse(jwt)
val verified = signedJWT.verify(verifier)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.sphereon.oid.fed.kms.local.jwt

import com.nimbusds.jose.jwk.RSAKey
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator
import com.nimbusds.jose.Algorithm
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.jwk.Curve
import com.nimbusds.jose.jwk.gen.ECKeyGenerator
import com.sphereon.oid.fed.openapi.models.EntityConfigurationStatement
import com.sphereon.oid.fed.openapi.models.JWKS
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.encodeToJsonElement
Expand All @@ -13,29 +15,35 @@ import kotlin.test.assertTrue

class JoseJwtTest {

//TODO Fix it
// @Test
// fun signTest() {
// val key = RSAKeyGenerator(2048).keyID("key1").generate()
// val entityStatement =
// EntityConfigurationStatement(iss = "test", sub = "test", exp = 111111, iat = 111111, jwks = JWKS())
// val payload: JsonObject = Json.encodeToJsonElement(entityStatement) as JsonObject
// val signature = sign(
// payload, JWTHeader(alg = "RS256", typ = "JWT", kid = key.keyID), mutableMapOf("key" to key)
// )
// assertTrue { signature.startsWith("ey") }
// }
//
// @Test
// fun verifyTest() {
// val kid = "key1"
// val key: RSAKey = RSAKeyGenerator(2048).keyID(kid).generate()
// val entityStatement =
// EntityConfigurationStatement(iss = "test", sub = "test", exp = 111111, iat = 111111, jwks = JWKS())
// val payload: JsonObject = Json.encodeToJsonElement(entityStatement) as JsonObject
// val signature = sign(
// payload, JWTHeader(alg = "RS256", typ = "JWT", kid = key.keyID), mutableMapOf("key" to key)
// )
// assertTrue { verify(signature, key, emptyMap()) }
// }
@Test
fun signTest() {
val key = ECKeyGenerator(Curve.P_256).keyID("key1").algorithm(Algorithm("ES256")).generate()
val jwk = key.toString()
val entityStatement = EntityConfigurationStatement(
iss = "test", sub = "test", exp = 111111, iat = 111111, jwks = JsonObject(mapOf())
)
val payload: JsonObject = Json.encodeToJsonElement(entityStatement) as JsonObject
val signature = sign(
payload,
JWTHeader(alg = JWSAlgorithm.ES256.toString(), typ = "JWT", kid = key.keyID),
Json.decodeFromString<Jwk>(jwk)
)
assertTrue { signature.startsWith("ey") }
}

@Test
fun verifyTest() {
val key = ECKeyGenerator(Curve.P_256).keyID("key1").algorithm(Algorithm("ES256")).generate()
val jwk = key.toString()
val entityStatement = EntityConfigurationStatement(
iss = "test", sub = "test", exp = 111111, iat = 111111, jwks = JsonObject(mapOf())
)
val payload: JsonObject = Json.encodeToJsonElement(entityStatement) as JsonObject
val signature = sign(
payload,
JWTHeader(alg = JWSAlgorithm.ES256.toString(), typ = "JWT", kid = key.keyID),
Json.decodeFromString<Jwk>(jwk)
)
assertTrue { verify(signature, Json.decodeFromString<Jwk>(jwk)) }
}
}

0 comments on commit 5228299

Please sign in to comment.