-
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.
refactor: Extracted JWT service to its own file
- Loading branch information
Zoe Maas
committed
Oct 11, 2024
1 parent
0da2041
commit 432d15a
Showing
2 changed files
with
58 additions
and
51 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...ration-client/src/jvmTest/kotlin/com/sphereon/oid/fed/client/validation/MockJwtService.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,57 @@ | ||
package com.sphereon.oid.fed.client.validation | ||
|
||
import com.nimbusds.jose.JOSEObjectType | ||
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.ECDSASigner | ||
import com.nimbusds.jose.crypto.ECDSAVerifier | ||
import com.nimbusds.jose.jwk.ECKey | ||
import com.nimbusds.jwt.JWTClaimsSet | ||
import com.nimbusds.jwt.SignedJWT | ||
import com.sphereon.oid.fed.common.jwt.IJwtService | ||
import com.sphereon.oid.fed.common.jwt.JwtSignInput | ||
import com.sphereon.oid.fed.common.jwt.JwtVerifyInput | ||
import com.sphereon.oid.fed.openapi.models.JWTHeader | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
|
||
class MockJwtService : IJwtService { | ||
|
||
override suspend fun sign(input: JwtSignInput): String { | ||
val jwkJsonString = Json.encodeToString(input.key) | ||
val ecJWK = ECKey.parse(jwkJsonString) | ||
val signer: JWSSigner = ECDSASigner(ecJWK) | ||
val jwsHeader = input.header.toJWSHeader() | ||
|
||
val signedJWT = SignedJWT( | ||
jwsHeader, JWTClaimsSet.parse(JsonObject(input.payload).toString()) | ||
) | ||
|
||
signedJWT.sign(signer) | ||
return signedJWT.serialize() | ||
} | ||
|
||
override suspend fun verify(input: JwtVerifyInput): Boolean { | ||
try { | ||
val jwkJsonString = Json.encodeToString(input.key) | ||
val ecKey = ECKey.parse(jwkJsonString) | ||
val verifier: JWSVerifier = ECDSAVerifier(ecKey) | ||
val signedJWT = SignedJWT.parse(input.jwt) | ||
val verified = signedJWT.verify(verifier) | ||
return verified | ||
} catch (e: Exception) { | ||
throw Exception("Couldn't verify the JWT Signature: ${e.message}", e) | ||
} | ||
} | ||
|
||
private fun JWTHeader.toJWSHeader(): JWSHeader { | ||
val type = typ | ||
return JWSHeader.Builder(JWSAlgorithm.parse(alg)).apply { | ||
type(JOSEObjectType(type)) | ||
keyID(kid) | ||
}.build() | ||
} | ||
} |
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