-
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.
- Loading branch information
1 parent
77cd3f8
commit 27fce72
Showing
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
val jvmMajorVersion: String by project | ||
val jvmVersion = JavaVersion.valueOf("VERSION_$jvmMajorVersion") | ||
|
||
dependencies { | ||
implementation(project(":lib:hoplite-config")) | ||
implementation(libs.ktor.serialization.jackson) | ||
implementation(libs.jackson.datatypeJsr310) | ||
implementation(libs.jackson.kotlin) | ||
implementation(libs.ktor.client.contentNegotiation) | ||
implementation(libs.ktor.client.cio) | ||
api(libs.nav.common.tokenClient) | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(jvmVersion.majorVersion) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
lib/http-client-utils/src/main/kotlin/no/nav/paw/client/config/AzureAdM2MConfig.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,8 @@ | ||
package no.nav.paw.client.config | ||
|
||
const val AZURE_M2M_CONFIG = "azure_m2m_config.toml" | ||
|
||
data class AzureAdM2MConfig( | ||
val tokenEndpointUrl: String, | ||
val clientId: String | ||
) |
44 changes: 44 additions & 0 deletions
44
...tp-client-utils/src/main/kotlin/no/nav/paw/client/factory/AzureAdM2MTokenClientFactory.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 no.nav.paw.client.factory | ||
|
||
import com.nimbusds.jose.jwk.KeyUse | ||
import com.nimbusds.jose.jwk.RSAKey | ||
import no.nav.common.token_client.builder.AzureAdTokenClientBuilder | ||
import no.nav.common.token_client.cache.CaffeineTokenCache | ||
import no.nav.common.token_client.client.AzureAdMachineToMachineTokenClient | ||
import no.nav.paw.client.config.AzureAdM2MConfig | ||
import no.nav.paw.config.env.Local | ||
import no.nav.paw.config.env.RuntimeEnvironment | ||
import no.nav.paw.config.env.currentRuntimeEnvironment | ||
import java.security.KeyPairGenerator | ||
import java.security.interfaces.RSAPrivateKey | ||
import java.security.interfaces.RSAPublicKey | ||
|
||
fun createAzureAdM2MTokenClient( | ||
runtimeEnvironment: RuntimeEnvironment = currentRuntimeEnvironment, | ||
azureProviderConfig: AzureAdM2MConfig | ||
): AzureAdMachineToMachineTokenClient = | ||
when (runtimeEnvironment) { | ||
is Local -> AzureAdTokenClientBuilder.builder() | ||
.withClientId(azureProviderConfig.clientId) | ||
.withPrivateJwk(createMockRSAKey("azure")) | ||
.withTokenEndpointUrl(azureProviderConfig.tokenEndpointUrl) | ||
.buildMachineToMachineTokenClient() | ||
|
||
else -> AzureAdTokenClientBuilder.builder() | ||
.withNaisDefaults() | ||
.withCache(CaffeineTokenCache()) | ||
.buildMachineToMachineTokenClient() | ||
} | ||
|
||
private fun createMockRSAKey(keyID: String): String? = KeyPairGenerator | ||
.getInstance("RSA").let { | ||
it.initialize(2048) | ||
it.generateKeyPair() | ||
}.let { | ||
RSAKey.Builder(it.public as RSAPublicKey) | ||
.privateKey(it.private as RSAPrivateKey) | ||
.keyUse(KeyUse.SIGNATURE) | ||
.keyID(keyID) | ||
.build() | ||
.toJSONString() | ||
} |
36 changes: 36 additions & 0 deletions
36
lib/http-client-utils/src/main/kotlin/no/nav/paw/client/factory/HttpClientFactory.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,36 @@ | ||
package no.nav.paw.client.factory | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.HttpClientConfig | ||
import io.ktor.client.engine.HttpClientEngine | ||
import io.ktor.client.engine.HttpClientEngineConfig | ||
import io.ktor.client.engine.HttpClientEngineFactory | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.serialization.jackson.jackson | ||
|
||
fun createHttpClient( | ||
engineFactory: HttpClientEngineFactory<HttpClientEngineConfig> = CIO, | ||
block: HttpClientConfig<*>.() -> Unit = { | ||
install(ContentNegotiation) { | ||
jackson { | ||
configureJackson() | ||
} | ||
} | ||
} | ||
): HttpClient { | ||
return HttpClient(engineFactory, block) | ||
} | ||
|
||
fun createHttpClient( | ||
engine: HttpClientEngine, | ||
block: HttpClientConfig<*>.() -> Unit = { | ||
install(ContentNegotiation) { | ||
jackson { | ||
configureJackson() | ||
} | ||
} | ||
} | ||
): HttpClient { | ||
return HttpClient(engine, block) | ||
} |
32 changes: 32 additions & 0 deletions
32
lib/http-client-utils/src/main/kotlin/no/nav/paw/client/factory/JacksonFactory.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,32 @@ | ||
package no.nav.paw.client.factory | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.KotlinFeature | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.kotlinModule | ||
|
||
fun createObjectMapper(): ObjectMapper { | ||
return jacksonObjectMapper().apply { | ||
configureJackson() | ||
} | ||
} | ||
|
||
fun ObjectMapper.configureJackson() { | ||
setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL) | ||
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) | ||
registerModule(JavaTimeModule()) | ||
kotlinModule { | ||
withReflectionCacheSize(512) | ||
disable(KotlinFeature.NullIsSameAsDefault) | ||
disable(KotlinFeature.SingletonSupport) | ||
disable(KotlinFeature.StrictNullChecks) | ||
enable(KotlinFeature.NullToEmptyCollection) | ||
enable(KotlinFeature.NullToEmptyMap) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
lib/http-client-utils/src/main/resources/nais/azure_m2m_config.toml
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,2 @@ | ||
clientId = "${AZURE_APP_CLIENT_ID}" | ||
tokenEndpointUrl = "${AZURE_OPENID_CONFIG_TOKEN_ENDPOINT}" |
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