-
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.
Merge branch 'refs/heads/feature/OIDF-42' into feature/OIDF-53
# Conflicts: # modules/openid-federation-common/build.gradle.kts
- Loading branch information
Showing
7 changed files
with
260 additions
and
66 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
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
7 changes: 7 additions & 0 deletions
7
...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,7 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
expect class JwtHeader | ||
expect class JwtPayload | ||
|
||
expect fun sign(payload: JwtPayload, header: JwtHeader, opts: Map<String, Any>): String | ||
expect fun verify(jwt: String, key: Any, opts: Map<String, Any>): Boolean |
51 changes: 51 additions & 0 deletions
51
.../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,51 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
import com.sphereon.oid.fed.openapi.models.EntityStatement | ||
import com.sphereon.oid.fed.openapi.models.JWTHeader | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
@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 | ||
} | ||
|
||
actual typealias JwtPayload = EntityStatement | ||
actual typealias JwtHeader = JWTHeader | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual fun sign( | ||
payload: JwtPayload, | ||
header: JwtHeader, | ||
opts: Map<String, Any> | ||
): String { | ||
val privateKey = opts["privateKey"] ?: throw IllegalArgumentException("JWK private key is required") | ||
|
||
return Jose.SignJWT(JSON.parse<Any>(Json.encodeToString(payload))) | ||
.setProtectedHeader(JSON.parse<Any>(Json.encodeToString(header))) | ||
.sign(key = privateKey, signOptions = opts) | ||
} | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
actual fun verify( | ||
jwt: String, | ||
key: Any, | ||
opts: Map<String, Any> | ||
): Boolean { | ||
return Jose.jwtVerify(jwt, key, opts) | ||
} |
35 changes: 35 additions & 0 deletions
35
...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,35 @@ | ||
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( | ||
JwtPayload(iss="test"), | ||
JwtHeader(typ="JWT",alg="RS256",kid="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( | ||
JwtPayload(iss="test"), | ||
JwtHeader(typ="JWT",alg="RS256",kid="test"), | ||
mutableMapOf("privateKey" to keyPair.privateKey)) as Promise<dynamic>).await() | ||
val result = async { verify(signed, keyPair.publicKey, emptyMap()) } | ||
assertTrue((result.await())) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...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,47 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
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.jwt.JWTClaimsSet | ||
import com.nimbusds.jwt.SignedJWT | ||
|
||
actual typealias JwtPayload = JWTClaimsSet | ||
actual typealias JwtHeader = JWSHeader | ||
|
||
actual fun sign( | ||
payload: JwtPayload, | ||
header: JwtHeader, | ||
opts: Map<String, Any> | ||
): String { | ||
val rsaJWK = opts["key"] as RSAKey? ?: throw IllegalArgumentException("The RSA key pair is required") | ||
|
||
val signer: JWSSigner = RSASSASigner(rsaJWK) | ||
|
||
val signedJWT = SignedJWT( | ||
header, | ||
payload | ||
) | ||
|
||
signedJWT.sign(signer) | ||
return signedJWT.serialize() | ||
} | ||
|
||
actual fun verify( | ||
jwt: String, | ||
key: Any, | ||
opts: Map<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}", e) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...d-federation-common/src/jvmTest/kotlin/com/sphereon/oid/fed/common/jwt/JoseJwtTest.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,44 @@ | ||
package com.sphereon.oid.fed.common.jwt | ||
|
||
import com.nimbusds.jose.jwk.RSAKey | ||
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class JoseJwtTest { | ||
|
||
@Test | ||
fun signTest() { | ||
val key = RSAKeyGenerator(2048).keyID("key1").generate() | ||
val signature = sign( | ||
JwtPayload.parse( | ||
mutableMapOf<String, Any>( | ||
"iss" to "test" | ||
) | ||
), | ||
JwtHeader.parse(mutableMapOf<String, Any>( | ||
"typ" to "JWT", | ||
"alg" to "RS256", | ||
"kid" to 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 signature = sign( | ||
JwtPayload.parse( | ||
mutableMapOf<String, Any>("iss" to "test") | ||
), | ||
JwtHeader.parse(mutableMapOf<String, Any>( | ||
"typ" to "JWT", | ||
"alg" to "RS256", | ||
"kid" to key.keyID)), | ||
mutableMapOf("key" to key) | ||
) | ||
assertTrue { verify(signature, key, emptyMap()) } | ||
} | ||
} |