From 15dfc607f8aa8bd62e346be47230d74aa93eff8e Mon Sep 17 00:00:00 2001 From: AudunSorheim Date: Thu, 12 Dec 2024 14:45:23 +0100 Subject: [PATCH] wip --- .../ArbeidsgiverNotifikasjonProdusent.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/kotlin/no/nav/syfo/producer/arbeidsgivernotifikasjon/ArbeidsgiverNotifikasjonProdusent.kt b/src/main/kotlin/no/nav/syfo/producer/arbeidsgivernotifikasjon/ArbeidsgiverNotifikasjonProdusent.kt index 2bb038732..aa92fa2d2 100644 --- a/src/main/kotlin/no/nav/syfo/producer/arbeidsgivernotifikasjon/ArbeidsgiverNotifikasjonProdusent.kt +++ b/src/main/kotlin/no/nav/syfo/producer/arbeidsgivernotifikasjon/ArbeidsgiverNotifikasjonProdusent.kt @@ -9,12 +9,21 @@ import com.apollo.graphql.type.MottakerInput import com.apollo.graphql.type.NaermesteLederMottakerInput import com.apollo.graphql.type.NyTidStrategi import com.apollographql.apollo.ApolloClient +import com.apollographql.apollo.api.ApolloRequest import com.apollographql.apollo.api.ApolloResponse +import com.apollographql.apollo.api.Operation import com.apollographql.apollo.api.Optional +import com.apollographql.apollo.interceptor.ApolloInterceptor +import com.apollographql.apollo.interceptor.ApolloInterceptorChain import io.ktor.client.call.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.withContext import no.nav.syfo.UrlEnv import no.nav.syfo.auth.AzureAdTokenConsumer import no.nav.syfo.producer.arbeidsgivernotifikasjon.domain.ArbeidsgiverDeleteNotifikasjon @@ -85,6 +94,7 @@ open class ArbeidsgiverNotifikasjonProdusent(urlEnv: UrlEnv, private val azureAd log.info("Forsøker å opprette ny kalenderavtale") val apolloClient = ApolloClient.Builder() .serverUrl(arbeidsgiverNotifikasjonProdusentBasepath) + .addInterceptor(BearerTokenInterceptor { azureAdTokenConsumer.getToken(scope) }) .build() val mutation = NyKalenderavtaleMutation( @@ -285,3 +295,18 @@ open class ArbeidsgiverNotifikasjonProdusent(urlEnv: UrlEnv, private val azureAd } } } + +class BearerTokenInterceptor(private val tokenProvider: suspend () -> String) : ApolloInterceptor { + override fun intercept( + request: ApolloRequest, + chain: ApolloInterceptorChain + ): Flow> = flow { + val token = withContext(Dispatchers.IO) { tokenProvider() } + val newRequest = request.newBuilder() + .addHttpHeader("Authorization", "Bearer $token") + .addHttpHeader("Content-Type", "application/json") + .addHttpHeader("Accept", "application/json") + .build() + emitAll(chain.proceed(newRequest)) + } +} \ No newline at end of file