Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Oppretter egen resttemplate for maskinporten som legger til bearer au…
Browse files Browse the repository at this point in the history
…tomatisk
  • Loading branch information
simhos committed Oct 24, 2023
1 parent 052dee9 commit cfecb89
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fasterxml.jackson.module.kotlin.readValue
import com.github.benmanes.caffeine.cache.Caffeine
import com.github.benmanes.caffeine.cache.LoadingCache
import com.nimbusds.jwt.SignedJWT
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Service
import java.net.URI
import java.net.http.HttpClient
Expand Down Expand Up @@ -33,7 +34,9 @@ import java.net.http.HttpResponse.BodyHandlers.ofString
* maskinporten:
* enabled: true
*/
@Service

@EnableConfigurationProperties(MaskinportenConfig::class)
@Service("maskinportenClient")
class MaskinportenClient(
private val maskinportenConfig: MaskinportenConfig
) {
Expand Down Expand Up @@ -62,6 +65,15 @@ class MaskinportenClient(
}
}

fun hentMaskinportenToken(): SignedJWT {
val cache = maskinportenTokenCache.get(maskinportenConfig.scope) { nyttScope: String ->
MaskinportenTokenCache(hentNyttJwtToken(nyttScope))
} ?: error("Feil ved henting eller opprettelse av cached scope for maskinporten-token! Scope: ${maskinportenConfig.scope}, cache content: $maskinportenTokenCache")
return cache.run {
maskinportenToken ?: renew(hentNyttJwtToken(maskinportenConfig.scope))
}
}

private fun hentNyttJwtToken(scope: String): String =
httpClient.send(opprettMaskinportenTokenRequest(scope), ofString()).run {
if (statusCode() != 200) throw MaskinportenClientException("Feil ved henting av token: Status: ${statusCode()} , Body: ${body()}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package no.nav.bidrag.commons.web.config

import no.nav.bidrag.commons.web.interceptor.MaskinportenBearerTokenClientInterceptor
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.Scope

@Configuration
@Import(RestTemplateBuilderBean::class,
MaskinportenBearerTokenClientInterceptor::class)
class RestOperationsMaskinporten {

@Bean("maskinporten")
@Scope("prototype")
fun restOperationsMaskinporten(
restTemplateBuilder: RestTemplateBuilder,
maskinportenBearerTokenClientInterceptor: MaskinportenBearerTokenClientInterceptor
) = restTemplateBuilder.additionalInterceptors(maskinportenBearerTokenClientInterceptor).build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package no.nav.bidrag.commons.web.interceptor

import no.nav.bidrag.commons.security.maskinporten.MaskinportenClient
import org.springframework.context.annotation.Import
import org.springframework.http.HttpRequest
import org.springframework.http.MediaType
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse
import org.springframework.stereotype.Component

@Component
@Import(MaskinportenClient::class)
class MaskinportenBearerTokenClientInterceptor(private val maskinportenClient: MaskinportenClient): ClientHttpRequestInterceptor {

override fun intercept(
request: HttpRequest,
body: ByteArray,
execution: ClientHttpRequestExecution
): ClientHttpResponse {
request.headers.setBearerAuth(maskinportenClient.hentMaskinportenToken().parsedString)
request.headers.accept = listOf(MediaType.APPLICATION_JSON)
request.headers.contentType = MediaType.APPLICATION_JSON
return execution.execute(request, body)
}
}

0 comments on commit cfecb89

Please sign in to comment.