-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skriver om restTemplateConfiguration til kotlin
- Loading branch information
Showing
1 changed file
with
45 additions
and
37 deletions.
There are no files selected for viewing
82 changes: 45 additions & 37 deletions
82
src/test/java/no/nav/bidrag/dokument/security/HttpHeaderTestRestTemplateConfiguration.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 |
---|---|---|
@@ -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() | ||
} | ||
} |