Skip to content

Commit

Permalink
Skriver om restTemplateConfiguration til kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
simhos committed Dec 20, 2023
1 parent bdfe57d commit 1e967ac
Showing 1 changed file with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
package no.nav.bidrag.dokument.security;

import static no.nav.bidrag.dokument.BidragDokumentTest.TEST_PROFILE;

import com.nimbusds.jose.JOSEObjectType;
import java.util.List;
import java.util.Map;
import no.nav.security.mock.oauth2.MockOAuth2Server;
import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;
package no.nav.bidrag.dokument.security

import com.nimbusds.jose.JOSEObjectType
import no.nav.bidrag.dokument.BidragDokumentTest
import no.nav.security.mock.oauth2.MockOAuth2Server
import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.http.HttpHeaders

@Configuration
@Profile(TEST_PROFILE)
public class HttpHeaderTestRestTemplateConfiguration {

@Autowired
private MockOAuth2Server mockOAuth2Server;

@Bean
HttpHeaderTestRestTemplate securedTestRestTemplate(TestRestTemplate testRestTemplate) {
HttpHeaderTestRestTemplate httpHeaderTestRestTemplate = new HttpHeaderTestRestTemplate(testRestTemplate);
httpHeaderTestRestTemplate.add(HttpHeaders.AUTHORIZATION, this::generateTestToken);

return httpHeaderTestRestTemplate;
}

private String generateTestToken() {
var iss = mockOAuth2Server.issuerUrl("aad");
var newIssuer = iss.newBuilder().host("localhost").build();
// var token = mockOAuth2Server.issueToken("aad", "aud-localhost", "aud-localhost");

var token = mockOAuth2Server.issueToken("aad", "aud-localhost", new DefaultOAuth2TokenCallback("aad", "aud-localhost", JOSEObjectType.JWT.getType(), List.of("aud-localhost"), Map.of("iss", newIssuer.toString()), 3600));
return "Bearer " + token.serialize();
}
@Profile(BidragDokumentTest.TEST_PROFILE)
class HttpHeaderTestRestTemplateConfiguration {

@Autowired
private lateinit var mockOAuth2Server: MockOAuth2Server

@Bean
fun securedTestRestTemplate(testRestTemplate: TestRestTemplate): HttpHeaderTestRestTemplate {
val httpHeaderTestRestTemplate = HttpHeaderTestRestTemplate(testRestTemplate)
httpHeaderTestRestTemplate.add(HttpHeaders.AUTHORIZATION) {
generateTestToken()
}
return httpHeaderTestRestTemplate
}

private fun generateTestToken(): String {
val iss = mockOAuth2Server.issuerUrl("aad")
val newIssuer = iss.newBuilder().host("localhost").build()

val token = mockOAuth2Server.issueToken(
"aad",
"aud-localhost",
DefaultOAuth2TokenCallback(
"aad",
"aud-localhost",
JOSEObjectType.JWT.type,
listOf("aud-localhost"),
mapOf(Pair("iss", newIssuer.toString())),
3600,
),
)
return "Bearer " + token.serialize()
}
}

0 comments on commit 1e967ac

Please sign in to comment.