Skip to content

Commit

Permalink
Forenkler Jwt AzureAuthMethod 😌
Browse files Browse the repository at this point in the history
Trenger jo ikke å laste inn sertifikatet, tror jeg 🤔
  • Loading branch information
fraadsbrandth committed Jan 19, 2024
1 parent 493dd1a commit ef3c0f8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fun createAzureTokenClientFromEnvironment(env: Map<String, String> = System.gete
fun createJwkAzureTokenClientFromEnvironment(env: Map<String, String> = System.getenv()): AzureTokenProvider {
val objectMapper = jacksonObjectMapper().registerModule(JavaTimeModule())
val jwk: Map<String, Any?> = objectMapper.readValue(env.getValue("AZURE_APP_JWK"))

return createJwkAzureTokenClient(
tokenEndpoint = URI(env.getValue("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT")),
clientId = env.getValue("AZURE_APP_CLIENT_ID"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.net.URI
import java.security.KeyFactory
import java.security.Signature
import java.security.interfaces.RSAPrivateKey
import java.security.spec.RSAPrivateCrtKeySpec
import java.security.spec.RSAPrivateKeySpec
import java.time.Clock
import java.time.Instant
import java.util.*
Expand Down Expand Up @@ -70,13 +70,7 @@ sealed interface AzureAuthMethod {
check(keyType.uppercase() == "RSA") { "Key type (kty) må være RSA" }
val modulus = hent("n", "Modulus").decodeToBigInteger
val privateExponent = hent("d", "Private exponent").decodeToBigInteger
val publicExponent = hent("e", "Public exponent").decodeToBigInteger
val primeP = hent("p", "Prime P").decodeToBigInteger
val primeQ = hent("q", "Prime Q").decodeToBigInteger
val primeExponentP = hent("dp", "Prime Exponent P").decodeToBigInteger
val primeExponentQ = hent("dq", "Prime Exponent Q").decodeToBigInteger
val crtCoefficient = hent("qi", "Certificate Coefficient").decodeToBigInteger
val keySpec = RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient)
val keySpec = RSAPrivateKeySpec(modulus, privateExponent)
val factory = KeyFactory.getInstance("RSA")
return (factory.generatePrivate(keySpec) as RSAPrivateKey)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.navikt.tbd_libs.azure

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -56,24 +57,19 @@ class JwkTest {
private val base64Decoder = Base64.getUrlDecoder()
private val String.decodeToBigInteger get() = BigInteger(1, base64Decoder.decode(this))

@Language("JSON")
private const val TEST_JWK = """
{
"kid": "test-jwk",
"kty": "RSA",
"n": "pBjxqwjKaM4YG3Kf9dRugVjWGFLT6w4tdHRaQoipaTzl_891DMx6ccuUMMbjTxdxevsDbYOB0fjcKHHXQ9JW5yVaBxl2hk7FIre3uDeLOqNbbpr7mekwGzqz4YGAkTpNjDoljxS-5v3Dxo5Zr85FFXpdoed4Vs37p3U7FAlc91sZ0TJ0BV1q5k-kkG6UmsEsdp1qZxNsQ_5K1nWxREDxaBUOiIfDfPiHmmRXHEEEKY_AQ00-i97SC4vMu4cW9tCKxiBpKh743qF-GkctCePol5PCjFpy56PFC4PnZjRFgnn80kdSbFOwH8l07unzOiUzKlWkv0b5WXw8h3ydZFFexw",
"e": "AQAB",
"d": "gRm-x7iaxemevblob5c5eTnS9j_zybHVwRDpEf9CiTEIIkGs7OzSSETJybYvj0H6Xa6t-7LCp9cKHieyHAGXrTKNqZg2z2OZZL71I1FPkEqE3HfCCkyTNFjyvC-OXrNn3zK_6dmAd2qeY9AKb23wm_0xPPdGjcRwgEaSvCjBozgd8dKgrn8bnALb1V1mGPZt5X648723uW6zBqO94ue73gqp7WrE2AMTG4SaiX-CzO4dSzLI6AUZGnBfF6umyxrZBFR6g2m1zATBa5i0YXrIHXM3RREnF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kyTNFjyvC-OXrNn3zK_6dmAd2qeY9AKb23wm_0xPPdGjcRwgEaSvCjBozgd8dKgrn8bnALb1V1mGPZt5X648723uW6zBqO94ue73gqp7WrE2AMTG4SaiX-CzO4dSzLI6AUZGnBfF6umyxrZBFR6g2m1zATBa5i0YXrIHXM3RREnFNmcOrcNO3borzNtZCiMW7ZrXIqO8AVnDjNmVzbg5v3f9Ol1U6t1TT0XAQ"
}
"""

private fun publicKey(jwk: Map<String, Any?>): RSAPublicKey {
val modulus = jwk.getValue("n").toString().decodeToBigInteger
val exponent = jwk.getValue("e").toString().decodeToBigInteger
val exponent = "AQAB".decodeToBigInteger
val spec = RSAPublicKeySpec(modulus, exponent)
val factory = KeyFactory.getInstance("RSA")
return factory.generatePublic(spec) as RSAPublicKey
Expand Down

0 comments on commit ef3c0f8

Please sign in to comment.