-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
17 changes: 15 additions & 2 deletions
17
...tilgangskontroll/src/main/kotlin/no/nav/paw/tilgangskontroll/api/validering/Validering.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
20 changes: 20 additions & 0 deletions
20
...troll/src/main/kotlin/no/nav/paw/tilgangskontroll/ktorserver/InstallContentNegotiation.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package no.nav.paw.tilgangskontroll.ktorserver | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import io.ktor.serialization.jackson.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.contentnegotiation.* | ||
|
||
fun Application.installContentNegotiation() { | ||
install(ContentNegotiation) { | ||
jackson { | ||
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
registerModule(JavaTimeModule()) | ||
registerKotlinModule() | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...gangskontroll/src/main/kotlin/no/nav/paw/tilgangskontroll/ktorserver/InstallStatusPage.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package no.nav.paw.tilgangskontroll.ktorserver | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.statuspages.* | ||
import no.nav.paw.error.handler.handleException | ||
|
||
fun Application.installStatusPage() { | ||
install(StatusPages) { | ||
exception<Throwable> { call: ApplicationCall, cause: Throwable -> | ||
call.handleException(cause) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
apps/tilgangskontroll/src/main/kotlin/no/nav/paw/tilgangskontroll/routes/ApiV1Tilgang.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package no.nav.paw.tilgangskontroll.routes | ||
|
||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import no.nav.paw.tilgangskontroll.TilgangsTjenesteForAnsatte | ||
import no.nav.paw.tilgangskontroll.api.models.TilgangskontrollRequestV1 | ||
import no.nav.paw.tilgangskontroll.api.models.TilgangskontrollResponseV1 | ||
import no.nav.paw.tilgangskontroll.api.validering.valider | ||
|
||
fun Route.apiV1Tilgang(service: TilgangsTjenesteForAnsatte) { | ||
post("/api/v1/tilgang") { | ||
val request: TilgangskontrollRequestV1 = call.receive<TilgangskontrollRequestV1>() | ||
val validertTilgangskontrollRequest = request.valider() | ||
val harTilgang = service.harAnsattTilgangTilPerson( | ||
navIdent = validertTilgangskontrollRequest.navAnsatt, | ||
identitetsnummer = validertTilgangskontrollRequest.person, | ||
tilgang = validertTilgangskontrollRequest.tilgang | ||
) | ||
call.respond(TilgangskontrollResponseV1(harTilgang = harTilgang)) | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
apps/tilgangskontroll/src/test/kotlin/no/nav/paw/tilgangskontroll/TilgangskontrollTest.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 |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package no.nav.paw.tilgangskontroll | ||
|
||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import com.nimbusds.jwt.SignedJWT | ||
import io.kotest.common.runBlocking | ||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.should | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.client.plugins.defaultRequest | ||
import io.ktor.client.request.bearerAuth | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.setBody | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.serialization.jackson.jackson | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.auth.authenticate | ||
import io.ktor.server.routing.routing | ||
import io.ktor.server.testing.testApplication | ||
import no.nav.paw.config.hoplite.loadNaisOrLocalConfiguration | ||
import no.nav.paw.tilgangskontroll.api.models.TilgangskontrollRequestV1 | ||
import no.nav.paw.tilgangskontroll.api.models.TilgangskontrollResponseV1 | ||
import no.nav.paw.tilgangskontroll.ktorserver.AuthProvider | ||
import no.nav.paw.tilgangskontroll.ktorserver.AuthProviderConfig | ||
import no.nav.paw.tilgangskontroll.ktorserver.AuthProviders | ||
import no.nav.paw.tilgangskontroll.ktorserver.configureAuthentication | ||
import no.nav.paw.tilgangskontroll.ktorserver.installContentNegotiation | ||
import no.nav.paw.tilgangskontroll.ktorserver.installStatusPage | ||
import no.nav.paw.tilgangskontroll.routes.apiV1Tilgang | ||
import no.nav.paw.tilgangskontroll.vo.EntraId | ||
import no.nav.paw.tilgangskontroll.vo.Identitetsnummer | ||
import no.nav.paw.tilgangskontroll.vo.Tilgang | ||
import no.nav.security.mock.oauth2.MockOAuth2Server | ||
import java.util.* | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class TilgangskontrollTest: FreeSpec({ | ||
val mockOAuthServer = MockOAuth2Server() | ||
beforeSpec { | ||
mockOAuthServer.start() | ||
} | ||
afterSpec { | ||
mockOAuthServer.shutdown() | ||
} | ||
val map = ConcurrentHashMap<Triple<EntraId, Identitetsnummer, Tilgang>, Boolean>() | ||
val service = object: TilgangsTjenesteForAnsatte { | ||
override suspend fun harAnsattTilgangTilPerson( | ||
navIdent: EntraId, | ||
identitetsnummer: Identitetsnummer, | ||
tilgang: Tilgang | ||
): Boolean { | ||
return map[Triple(navIdent, identitetsnummer, tilgang)] ?: false | ||
} | ||
} | ||
|
||
"Verifiser applikasjonsflyt".config(enabled = false) { | ||
val ansatt = NavAnsatt(UUID.randomUUID(), "Z123") | ||
val person = Identitetsnummer("12345678901") | ||
val token = mockOAuthServer.ansattToken(ansatt) | ||
map[Triple(EntraId(ansatt.azureId), person, Tilgang.LESE)] = true | ||
map[Triple(EntraId(ansatt.azureId), person, Tilgang.SKRIVE)] = true | ||
testApplication { | ||
application { | ||
configureAuthentication(mockOAuthServer, AuthProvider.EntraId) | ||
installStatusPage() | ||
installContentNegotiation() | ||
routing { | ||
authenticate(AuthProvider.EntraId.name) { | ||
apiV1Tilgang(service) | ||
} | ||
} | ||
} | ||
val client = createClient { | ||
defaultRequest { | ||
bearerAuth(token.serialize()) | ||
} | ||
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) { | ||
jackson { | ||
registerKotlinModule() | ||
registerModule(JavaTimeModule()) | ||
} | ||
} | ||
} | ||
client.post("/api/v1/tilgang") { | ||
setBody(TilgangskontrollRequestV1( | ||
identitetsnummer = person.value, | ||
navAnsattId = ansatt.azureId, | ||
tilgang = TilgangskontrollRequestV1.Tilgang.LESE | ||
)) | ||
} should { response -> | ||
response.status shouldBe HttpStatusCode.OK | ||
val body = runBlocking { response.body<TilgangskontrollResponseV1>() } | ||
body.harTilgang shouldBe true | ||
} | ||
client.post("/api/v1/tilgang") { | ||
setBody(TilgangskontrollRequestV1( | ||
identitetsnummer = person.value, | ||
navAnsattId = ansatt.azureId, | ||
tilgang = TilgangskontrollRequestV1.Tilgang.SKRIVE | ||
)) | ||
} should { response -> | ||
response.status shouldBe HttpStatusCode.OK | ||
val body = runBlocking { response.body<TilgangskontrollResponseV1>() } | ||
body.harTilgang shouldBe false | ||
} | ||
} | ||
} | ||
|
||
}) | ||
|
||
|
||
fun Application.configureAuthentication( | ||
oAuth2Server: MockOAuth2Server, | ||
vararg authProvider: AuthProvider | ||
) { | ||
val authProviders = authProvider.map { provider -> | ||
loadNaisOrLocalConfiguration<AuthProviderConfig>(provider.config) | ||
.copy( | ||
discoveryUrl = oAuth2Server.wellKnownUrl("default").toString(), | ||
clientId = "default" | ||
) | ||
} .let(::AuthProviders) | ||
configureAuthentication(authProviders) | ||
} | ||
|
||
fun MockOAuth2Server.ansattToken(navAnsatt: NavAnsatt): SignedJWT = issueToken( | ||
claims = mapOf( | ||
"oid" to navAnsatt.azureId, | ||
"NAVident" to navAnsatt.ident | ||
) | ||
) | ||
|
||
data class NavAnsatt( | ||
val azureId: UUID, | ||
val ident: String | ||
) |