-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Created sign and verify jwt functions
- Loading branch information
Zoe Maas
committed
Jul 17, 2024
1 parent
b3f03c1
commit 14c6a80
Showing
12 changed files
with
191 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
...federation-common/src/commonMain/kotlin/com/sphereon/oid/fed/common/jwks/JWKSGenerator.kt
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
...openid-federation-common/src/commonMain/kotlin/com/sphereon/oid/fed/common/jwt/JoseJwt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 |
11 changes: 0 additions & 11 deletions
11
...deration-common/src/commonMain/kotlin/com/sphereon/oid/fed/common/kms/AbstractKeyStore.kt
This file was deleted.
Oops, something went wrong.
50 changes: 0 additions & 50 deletions
50
...federation-common/src/commonMain/kotlin/com/sphereon/oid/fed/common/kms/MemoryKeyStore.kt
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
...ration-common/src/commonTest/kotlin/com/sphereon/oid/fed/common/jwks/JWKSGeneratorTest.kt
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
...ration-common/src/commonTest/kotlin/com/sphereon/oid/fed/common/kms/MemoryKeyStoreTest.kt
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
...penid-federation-common/src/iosMain/kotlin/com/sphereon/oid/fed/common/jwt/JoseJwt.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
actual fun sign( | ||
payload: String, | ||
opts: MutableMap<String, Any>? | ||
): String { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
actual fun verify( | ||
jwt: String, | ||
key: Any, | ||
opts: MutableMap<String, Any>? | ||
): Boolean { | ||
TODO("Not yet implemented") | ||
} |
48 changes: 48 additions & 0 deletions
48
.../openid-federation-common/src/jsMain/kotlin/com/sphereon/oid/fed/common/jwt/JoseJwt.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
@JsModule("jose") | ||
@JsNonModule | ||
external object Jose { | ||
class SignJWT { | ||
constructor(payload: dynamic) { | ||
definedExternally | ||
} | ||
fun setProtectedHeader(protectedHeader: dynamic): SignJWT { | ||
definedExternally | ||
} | ||
fun sign(key: Any?, signOptions: Any?): String { | ||
definedExternally | ||
} | ||
} | ||
fun generateKeyPair(alg: String, options: dynamic = definedExternally): dynamic | ||
fun jwtVerify(jwt: String, key: Any, options: dynamic = definedExternally): dynamic | ||
} | ||
|
||
@JsModule("uuid") | ||
@JsNonModule | ||
external object Uuid { | ||
fun v4(): String | ||
} | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual fun sign( | ||
payload: String, | ||
opts: MutableMap<String, Any>? | ||
): String { | ||
val privateKey = opts?.get("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()) | ||
.sign(key = privateKey, signOptions = opts) | ||
} | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual fun verify( | ||
jwt: String, | ||
key: Any, | ||
opts: MutableMap<String, Any>? | ||
): Boolean { | ||
return Jose.jwtVerify(jwt, key, opts) | ||
} |
28 changes: 28 additions & 0 deletions
28
...nid-federation-common/src/jsTest/kotlin/com/sphereon/oid/fed/common/jwt/JoseJwtTest.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
import com.sphereon.oid.fed.common.jwt.Jose.generateKeyPair | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.await | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.js.Promise | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class JoseJwtTest { | ||
@OptIn(ExperimentalJsExport::class) | ||
@Test | ||
fun signTest() = runTest { | ||
val keyPair = (generateKeyPair("RS256") as Promise<dynamic>).await() | ||
val result = async { sign("{ \"iss\": \"test\" }", mutableMapOf("privateKey" to keyPair.privateKey)) } | ||
assertTrue((result.await() as Promise<String>).await().startsWith("ey")) | ||
} | ||
|
||
@OptIn(ExperimentalJsExport::class) | ||
@Test | ||
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) } | ||
assertTrue((result.await() as Promise<Boolean>).await()) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...penid-federation-common/src/jvmMain/kotlin/com/sphereon/oid/fed/common/jwt/JoseJwt.jvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
import com.nimbusds.jose.JWSAlgorithm | ||
import com.nimbusds.jose.JWSHeader | ||
import com.nimbusds.jose.JWSSigner | ||
import com.nimbusds.jose.JWSVerifier | ||
import com.nimbusds.jose.crypto.RSASSASigner | ||
import com.nimbusds.jose.crypto.RSASSAVerifier | ||
import com.nimbusds.jose.jwk.RSAKey | ||
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator | ||
import com.nimbusds.jwt.JWTClaimsSet | ||
import com.nimbusds.jwt.SignedJWT | ||
import java.util.* | ||
|
||
actual fun sign( | ||
payload: String, | ||
opts: MutableMap<String, Any>? | ||
): String { | ||
var rsaJWK = opts?.get("key") as RSAKey? | ||
val kid = rsaJWK?.keyID ?: UUID.randomUUID().toString() | ||
val header: JWSHeader? | ||
if (opts?.get("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) | ||
|
||
val signedJWT = SignedJWT( | ||
header, | ||
claimsSet | ||
) | ||
|
||
signedJWT.sign(signer) | ||
return signedJWT.serialize() | ||
} | ||
|
||
actual fun verify( | ||
jwt: String, | ||
key: Any, | ||
opts: MutableMap<String, Any>? | ||
): Boolean { | ||
try { | ||
val rsaKey = key as RSAKey | ||
val verifier: JWSVerifier = RSASSAVerifier(rsaKey) | ||
val signedJWT = SignedJWT.parse(jwt) | ||
val verified = signedJWT.verify(verifier) | ||
return verified | ||
} catch (e: Exception) { | ||
throw Exception("Couldn't verify the JWT Signature: ${e.message}") | ||
} | ||
} |
Oops, something went wrong.