Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TB-118 Legg til IDporten autentisering ved egenregistrering #184

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class TestApplicationWithDb {
companion object {
private val postgresSQLContainer = PostgreSQLContainer("postgres:15-alpine")
@JvmStatic
protected lateinit var mockOAuthServer : MockOAuth2Server
internal lateinit var mockOAuthServer : MockOAuth2Server

@BeforeAll
@JvmStatic
Expand Down Expand Up @@ -69,8 +69,11 @@ abstract class TestApplicationWithDb {
"matrikkel.useStub" to "true",
"maskinporten.issuer" to mockOAuthServer.issuerUrl("testIssuer").toString(),
"maskinporten.jwksUri" to mockOAuthServer.jwksUrl("testIssuer").toString(),
"maskinporten.scopes" to "kartverket:riktig:scope",
"maskinporten.scopes" to "kartverk:riktig:scope",
"maskinporten.disabled" to "false",
"idporten.issuer" to mockOAuthServer.issuerUrl("testIssuer").toString(),
"idporten.jwksUri" to mockOAuthServer.jwksUrl("testIssuer").toString(),
"idporten.disabled" to "false"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import java.time.Instant

internal fun EgenregistreringRequest.Companion.validEgenregistrering() = EgenregistreringRequest(
bygningId = 1L,
eier = "31129956715",
bruksenhetRegistreringer = listOf(
BruksenhetRegistreringRequest(
bruksenhetId = 1L,
Expand Down Expand Up @@ -55,7 +54,6 @@ internal fun EgenregistreringRequest.Companion.validEgenregistrering() = Egenreg

internal fun EgenregistreringRequest.Companion.ugyldigEgenregistreringMedKunBruksarealPerEtasje() = EgenregistreringRequest(
bygningId = 1L,
eier = "31129956715",
bruksenhetRegistreringer = listOf(
BruksenhetRegistreringRequest(
bruksenhetId = 1L,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package no.kartverket.matrikkel.bygning.v1.common

import no.kartverket.matrikkel.bygning.TestApplicationWithDb.Companion.mockOAuthServer

class JWTUtil {
companion object {
private const val DEFAULT_SUBJECT = "123456789"
private const val DEFAULT_ISSUER = "testIssuer"
private const val DEFAULT_ORG = "123456789"

fun getJWTWithScope(scope: String) = mockOAuthServer.issueToken(
issuerId = DEFAULT_ISSUER,
subject = DEFAULT_SUBJECT,
claims = mapOf(
"orgno" to DEFAULT_ORG,
"scope" to scope,
),
)

fun getDefaultMaskinportenJWT() = getJWTWithScope("kartverk:riktig:scope")

fun getDefaultIDPortenJWT() = mockOAuthServer.issueToken(
issuerId = DEFAULT_ISSUER,
subject = DEFAULT_SUBJECT,
claims = mapOf(
"orgno" to DEFAULT_ORG,
"pid" to "31129956715",
),
)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import assertk.assertions.prop
import com.nimbusds.jwt.SignedJWT
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.testing.*
import no.kartverket.matrikkel.bygning.TestApplicationWithDb
import no.kartverket.matrikkel.bygning.routes.v1.ekstern.bygning.BygningEksternResponse
import no.kartverket.matrikkel.bygning.v1.common.JWTUtil
import org.junit.jupiter.api.Test

class BygningEksternTest : TestApplicationWithDb() {

@Test
fun `gitt en gyldig token med riktig scope skal tilgang gis`() = testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = signedJWTTokenWithScope()
val token = JWTUtil.getDefaultMaskinportenJWT()

val response = client.get("/v1/ekstern/bygninger/1") {
headers {
Expand All @@ -37,7 +36,7 @@ class BygningEksternTest : TestApplicationWithDb() {
@Test
fun `gitt et token med feil scope skal tilgang nektes`() = testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = signedJWTTokenWithScope("feil:scope")
val token = JWTUtil.getJWTWithScope("feil:scope")

val response = client.get("/v1/ekstern/bygninger/1") {
headers {
Expand All @@ -57,15 +56,4 @@ class BygningEksternTest : TestApplicationWithDb() {
assertThat(response.status).isEqualTo(HttpStatusCode.Unauthorized)
}

private fun signedJWTTokenWithScope(scope: String = "kartverket:riktig:scope"): SignedJWT {
val token: SignedJWT = mockOAuthServer.issueToken(
issuerId = "testIssuer",
subject = "123456789",
claims = mapOf(
"scope" to scope,
"orgno" to "123456789",
),
)
return token
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import no.kartverket.matrikkel.bygning.routes.v1.intern.bygning.EnergikildeRespo
import no.kartverket.matrikkel.bygning.routes.v1.intern.bygning.OppvarmingResponse
import no.kartverket.matrikkel.bygning.routes.v1.intern.bygning.VannforsyningKodeResponse
import no.kartverket.matrikkel.bygning.routes.v1.intern.egenregistrering.EgenregistreringRequest
import no.kartverket.matrikkel.bygning.v1.common.JWTUtil
import no.kartverket.matrikkel.bygning.v1.common.hasRegistreringstidspunktWithinThreshold
import no.kartverket.matrikkel.bygning.v1.common.validEgenregistrering
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -80,12 +81,16 @@ class BygningRouteTest : TestApplicationWithDb() {
@Test
fun `gitt at en bygning eksisterer med noe egenregistrert data feltene vaere satt`() = testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}

val response = client.get("/v1/bygninger/1/egenregistrert")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import no.kartverket.matrikkel.bygning.routes.v1.intern.egenregistrering.Bruksar
import no.kartverket.matrikkel.bygning.routes.v1.intern.egenregistrering.BruksenhetRegistreringRequest
import no.kartverket.matrikkel.bygning.routes.v1.intern.egenregistrering.ByggeaarRegistreringRequest
import no.kartverket.matrikkel.bygning.routes.v1.intern.egenregistrering.EgenregistreringRequest
import no.kartverket.matrikkel.bygning.v1.common.JWTUtil
import no.kartverket.matrikkel.bygning.v1.common.hasRegistreringstidspunktWithinThreshold
import no.kartverket.matrikkel.bygning.v1.common.ugyldigEgenregistreringMedKunBruksarealPerEtasje
import no.kartverket.matrikkel.bygning.v1.common.validEgenregistrering
Expand All @@ -47,12 +48,16 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
@Test
fun `gitt at en bygning eksisterer og request er gyldig svarer egenregistrering route ok`() = testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

val response = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}

assertThat(response.status).isEqualTo(HttpStatusCode.Created)
Expand All @@ -62,12 +67,16 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
fun `gitt en gyldig egenregistrering paa bygning og bruksenhet kan bygningen hentes ut med de egenregistrerte dataene`() =
testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

val response = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}

assertThat(response.status).isEqualTo(HttpStatusCode.Created)
Expand Down Expand Up @@ -141,12 +150,16 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
@Test
fun `gitt en gyldig egenregistrering paa bruksenhet kan bruksenheten hentes ut med de egenregistrerte dataene`() = testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

val response = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}

assertThat(response.status).isEqualTo(HttpStatusCode.Created)
Expand Down Expand Up @@ -187,12 +200,16 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
fun `gitt to gyldige egenregistreringer paa bygning og bruksenhet returneres dataene med den nyeste registreringen`() =
testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

val egenregistrering1 = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}
assertThat(egenregistrering1.status).isEqualTo(HttpStatusCode.Created)

Expand Down Expand Up @@ -220,6 +237,9 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
),
),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}
assertThat(egenregistrering2.status).isEqualTo(HttpStatusCode.Created)

Expand Down Expand Up @@ -255,14 +275,16 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
fun `gitt at egenregistrering sender info om hvem har registrert blir dette lagret og sendt ut igjen`() =
testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

val response = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering().copy(
eier = "31129956715",
),
EgenregistreringRequest.validEgenregistrering(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}

assertThat(response.status).isEqualTo(HttpStatusCode.Created)
Expand All @@ -289,12 +311,16 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
fun `gitt at egenregistrering inneholder kun BRA per etasje og ikke totalt BRA skal man få en bad request i respons`() =
testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()
val token = JWTUtil.getDefaultIDPortenJWT()

val response = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.ugyldigEgenregistreringMedKunBruksarealPerEtasje(),
EgenregistreringRequest.ugyldigEgenregistreringMedKunBruksarealPerEtasje(),
)
headers {
append("Authorization", "Bearer ${token.serialize()}")
}
}

assertThat(response.status).isEqualTo(HttpStatusCode.BadRequest)
Expand All @@ -306,6 +332,21 @@ class EgenregistreringRouteTest : TestApplicationWithDb() {
}
}

@Test
fun `gitt manglende idporten token skal man ikke få lov til å kalle endepunktet`() =
testApplication {
val client = mainModuleWithDatabaseEnvironmentAndClient()

val response = client.post("/v1/egenregistreringer") {
contentType(ContentType.Application.Json)
setBody(
EgenregistreringRequest.validEgenregistrering(),
)
}

assertThat(response.status).isEqualTo(HttpStatusCode.Unauthorized)
}

private fun Assert<List<BruksenhetResponse>>.withBruksenhetId(bruksenhetId: Long) =
transform(appendName("[bruksenhetId=$bruksenhetId]")) { it.find { br -> br.bruksenhetId == bruksenhetId }!! }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import no.kartverket.matrikkel.bygning.infrastructure.database.repositories.Heal
import no.kartverket.matrikkel.bygning.infrastructure.database.runFlywayMigrations
import no.kartverket.matrikkel.bygning.infrastructure.matrikkel.MatrikkelApiConfig
import no.kartverket.matrikkel.bygning.infrastructure.matrikkel.createBygningClient
import no.kartverket.matrikkel.bygning.plugins.configureAuthentication
import no.kartverket.matrikkel.bygning.plugins.configureHTTP
import no.kartverket.matrikkel.bygning.plugins.configureMaskinportenAuthentication
import no.kartverket.matrikkel.bygning.plugins.configureMonitoring
import no.kartverket.matrikkel.bygning.plugins.configureOpenAPI
import no.kartverket.matrikkel.bygning.plugins.configureStatusPages
import no.kartverket.matrikkel.bygning.routes.v1.intern.internRouting
import no.kartverket.matrikkel.bygning.routes.internalRouting
import no.kartverket.matrikkel.bygning.routes.v1.ekstern.eksternRouting
import no.kartverket.matrikkel.bygning.routes.v1.intern.internRouting

fun main() {
val internalPort = System.getenv("INTERNAL_PORT")?.toIntOrNull() ?: 8081
Expand Down Expand Up @@ -56,7 +56,7 @@ fun Application.mainModule() {
configureMonitoring()
configureOpenAPI()
configureStatusPages()
configureMaskinportenAuthentication(config)
configureAuthentication(config)

val dataSource = createDataSource(
DatabaseConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ private fun configLocation(): String {
LOCAL -> "application-local.conf"
}
}


Loading
Loading