-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dff616a
commit f4490a5
Showing
2 changed files
with
145 additions
and
0 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
82 changes: 82 additions & 0 deletions
82
azure-token-client/src/test/kotlin/com/github/navikt/tbd_libs/azure/JwkTest.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,82 @@ | ||
package com.github.navikt.tbd_libs.azure | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
import java.math.BigInteger | ||
import java.net.URI | ||
import java.security.KeyFactory | ||
import java.security.Signature | ||
import java.security.interfaces.RSAPublicKey | ||
import java.security.spec.RSAPublicKeySpec | ||
import java.util.* | ||
|
||
class JwkTest { | ||
|
||
@Test | ||
fun `Generere client assertion fra JWK`() { | ||
val jwk: Map<String, String> = om.readValue(TEST_JWK) | ||
val authMethod = AzureAuthMethod.Jwk(jwk, "test-client-id", URI("http://localhost:8080/token")) | ||
val assertion = authMethod.requestParameters().getValue("client_assertion") | ||
val parts = assertion.split(".") | ||
assertEquals(3, parts.size) | ||
val header = parts[0] | ||
val claims = parts[1] | ||
val signature = parts[2] | ||
|
||
// Header | ||
assertEquals("""{"kid":"test-jwk","typ":"JWT","alg":"RS256"}""", String(base64Decoder.decode(header))) | ||
|
||
// Claims | ||
val claimsMap: Map<String, String> = om.readValue(String(base64Decoder.decode(claims))) | ||
assertEquals(setOf("aud", "sub", "nbf", "iss", "exp", "iat", "jti"), claimsMap.keys) | ||
assertEquals("test-client-id", claimsMap.getValue("sub")) | ||
assertEquals("test-client-id", claimsMap.getValue("iss")) | ||
assertEquals("http://localhost:8080/token", claimsMap.getValue("aud")) | ||
assertDoesNotThrow { UUID.fromString(claimsMap.getValue("jti")) } | ||
val expiry = assertDoesNotThrow { claimsMap.getValue("exp").toLong() } | ||
val issuedAt = assertDoesNotThrow { claimsMap.getValue("iat").toLong() } | ||
val notBefore = assertDoesNotThrow { claimsMap.getValue("nbf").toLong() } | ||
assertEquals(issuedAt, notBefore) | ||
assertTrue(expiry > issuedAt) | ||
|
||
// Signature | ||
val verifyer = Signature.getInstance("SHA256withRSA") | ||
verifyer.initVerify(publicKey(jwk)) | ||
verifyer.update("$header.$claims".toByteArray()) | ||
assertTrue(verifyer.verify(base64Decoder.decode(signature))) | ||
} | ||
|
||
private companion object { | ||
|
||
private val om = jacksonObjectMapper() | ||
private val base64Decoder = Base64.getUrlDecoder() | ||
private val String.decodeToBigInteger get() = BigInteger(1, base64Decoder.decode(this)) | ||
|
||
private const val TEST_JWK = """ | ||
{ | ||
"kid": "test-jwk", | ||
"kty": "RSA", | ||
"n": "pBjxqwjKaM4YG3Kf9dRugVjWGFLT6w4tdHRaQoipaTzl_891DMx6ccuUMMbjTxdxevsDbYOB0fjcKHHXQ9JW5yVaBxl2hk7FIre3uDeLOqNbbpr7mekwGzqz4YGAkTpNjDoljxS-5v3Dxo5Zr85FFXpdoed4Vs37p3U7FAlc91sZ0TJ0BV1q5k-kkG6UmsEsdp1qZxNsQ_5K1nWxREDxaBUOiIfDfPiHmmRXHEEEKY_AQ00-i97SC4vMu4cW9tCKxiBpKh743qF-GkctCePol5PCjFpy56PFC4PnZjRFgnn80kdSbFOwH8l07unzOiUzKlWkv0b5WXw8h3ydZFFexw", | ||
"e": "AQAB", | ||
"d": "gRm-x7iaxemevblob5c5eTnS9j_zybHVwRDpEf9CiTE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XU0PxwEwmMYMoF5Sc64dRs5chats1tVM35fjmEhAthxeQv19x_SV5NB0hX8sE_jqIfAKx5nsZbv-3-FQaI68GKlpxdsGqaK0CqAJYAkGtEUeeWi6HAV8Xl68GSzBYMYDtOM8PPJWO4" | ||
} | ||
""" | ||
|
||
private fun publicKey(jwk: Map<String, String>): RSAPublicKey { | ||
val modulus = jwk.getValue("n").decodeToBigInteger | ||
val exponent = jwk.getValue("e").decodeToBigInteger | ||
val spec = RSAPublicKeySpec(modulus, exponent) | ||
val factory = KeyFactory.getInstance("RSA") | ||
return factory.generatePublic(spec) as RSAPublicKey | ||
} | ||
} | ||
} |