Skip to content

Commit

Permalink
refactor: made the second paramenter of functions a Map without defau…
Browse files Browse the repository at this point in the history
…lt value and refactored the key generation
  • Loading branch information
Zoe Maas committed Jul 23, 2024
1 parent d5c34e9 commit 440300c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sphereon.oid.fed.common.jwt

expect fun sign(payload: String, opts: MutableMap<String, Any>?): String
expect fun verify(jwt: String, key: Any, opts: MutableMap<String, Any>? = mutableMapOf()): Boolean
expect fun sign(payload: String, opts: Map<String, Any>): String
expect fun verify(jwt: String, key: Any, opts: Map<String, Any>): Boolean
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ external object Uuid {
@JsExport
actual fun sign(
payload: String,
opts: MutableMap<String, Any>?
opts: Map<String, Any>
): String {
val privateKey = opts?.get("privateKey") ?: throw IllegalArgumentException("JWK private key is required")
val privateKey = opts["privateKey"] ?: throw IllegalArgumentException("JWK private key is required")
val header = opts["jwtHeader"] as String? ?: "{\"typ\":\"JWT\",\"alg\":\"RS256\",\"kid\":\"${Uuid.v4()}\"}"
return Jose.SignJWT(JSON.parse<Any>(payload).asDynamic())
.setProtectedHeader(JSON.parse<Any>(header).asDynamic())
Expand All @@ -42,7 +42,7 @@ actual fun sign(
actual fun verify(
jwt: String,
key: Any,
opts: MutableMap<String, Any>?
opts: Map<String, Any>
): Boolean {
return Jose.jwtVerify(jwt, key, opts)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class JoseJwtTest {
fun verifyTest() = runTest {
val keyPair = (generateKeyPair("RS256") as Promise<dynamic>).await()
val signed = (sign("{ \"iss\": \"test\" }", mutableMapOf("privateKey" to keyPair.privateKey)) as Promise<dynamic>).await()
val result = async { verify(signed, keyPair.publicKey) }
val result = async { verify(signed, keyPair.publicKey, emptyMap()) }
assertTrue((result.await() as Promise<Boolean>).await())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ import java.util.*

actual fun sign(
payload: String,
opts: MutableMap<String, Any>?
opts: Map<String, Any>
): String {
var rsaJWK = opts?.get("key") as RSAKey?
val kid = rsaJWK?.keyID ?: UUID.randomUUID().toString()
val rsaJWK = opts["key"] as RSAKey? ?: RSAKeyGenerator(2048)
.keyID(UUID.randomUUID().toString())
.generate()

val kid = rsaJWK?.keyID

val header: JWSHeader?
if (opts?.get("jwtHeader") != null) {
if (opts["jwtHeader"] != null) {
header = JWSHeader.parse(opts["jwtHeader"] as String?)
} else {
header = JWSHeader.Builder(JWSAlgorithm.RS256).keyID(kid).build()
}

if (rsaJWK == null) {
rsaJWK = RSAKeyGenerator(2048)
.keyID(kid)
.generate()
}

val signer: JWSSigner = RSASSASigner(rsaJWK)

val claimsSet = JWTClaimsSet.parse(payload)
Expand All @@ -47,7 +45,7 @@ actual fun sign(
actual fun verify(
jwt: String,
key: Any,
opts: MutableMap<String, Any>?
opts: Map<String, Any>
): Boolean {
try {
val rsaKey = key as RSAKey
Expand Down

0 comments on commit 440300c

Please sign in to comment.