From 5d785eebfe19fbe6aa2984f5fcdbbf20c97943ea Mon Sep 17 00:00:00 2001 From: Thomas Johansen Date: Thu, 5 Dec 2024 07:40:53 +0100 Subject: [PATCH] =?UTF-8?q?Oppdaterte=20feilh=C3=A5ndtering=20til=20=C3=A5?= =?UTF-8?q?=20stemme=20med=20RCF,=20endret=20bekreftelse-api=20til=20?= =?UTF-8?q?=C3=A5=20benytte=20generert=20API=20modell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/bekreftelse-api/build.gradle.kts | 45 +++++++++- .../api/context/JsonbColumnType.kt | 2 +- .../exception/DataIkkeFunnetForIdException.kt | 7 +- .../DataTilhoererIkkeBrukerException.kt | 7 +- .../api/model/MottaBekreftelseRequest.kt | 10 --- .../api/model/TilgjengeligBekreftelse.kt | 11 --- .../model/TilgjengeligeBekreftelserRequest.kt | 5 -- .../api/{model => models}/BekreftelseRow.kt | 2 +- .../{model => models}/BekreftelserTable.kt | 2 +- .../api/{model => models}/Converters.kt | 2 +- .../TilgjengeligBekreftelserResponse.kt} | 4 +- .../api/policy/SluttbrukerAccessPolicy.kt | 2 +- .../api/repository/BekreftelseRepository.kt | 6 +- .../api/routes/BekreftelseRoutes.kt | 4 +- .../bekreftelse/api/routes/SwaggerRoutes.kt | 2 +- .../api/services/BekreftelseService.kt | 12 +-- .../openapi/{api.yaml => documentation.yaml} | 47 ++++++---- .../bekreftelse/api/plugin/TestDataPlugin.kt | 2 +- .../api/routes/BekreftelseRoutesTest.kt | 20 +++-- .../nav/paw/bekreftelse/api/test/TestData.kt | 6 +- .../exception/AuthenticationException.kt | 9 -- .../error/exception/AuthorizationException.kt | 9 -- .../exception/ClientResponseException.kt | 5 +- .../exception/ErrorCodeAwareException.kt | 7 -- .../exception/ErrorTypeAwareException.kt | 11 +++ .../exception/ServerResponseException.kt | 5 +- .../paw/error/handler/HttpExceptionHandler.kt | 85 ++++++++++--------- .../no/nav/paw/error/model/ErrorType.kt | 27 ++++++ .../no/nav/paw/error/model/ProblemDetails.kt | 44 +++++----- .../error/handler/HttpExceptionHandlerTest.kt | 12 ++- .../exception/AuthenticationException.kt | 11 +++ .../exception/BearerTokenManglerException.kt | 4 +- .../exception/UgyldigBearerTokenException.kt | 6 -- .../authentication/token/AccessToken.kt | 2 +- .../authorization/context/SecurityContext.kt | 2 +- .../exception/AuthorizationException.kt | 11 +++ .../exception/IngenTilgangException.kt | 4 +- .../exception/UgyldigBearerTokenException.kt | 6 ++ .../exception/UgyldigBrukerException.kt | 4 +- 39 files changed, 279 insertions(+), 183 deletions(-) delete mode 100644 apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/MottaBekreftelseRequest.kt delete mode 100644 apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligBekreftelse.kt delete mode 100644 apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserRequest.kt rename apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/{model => models}/BekreftelseRow.kt (97%) rename apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/{model => models}/BekreftelserTable.kt (93%) rename apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/{model => models}/Converters.kt (98%) rename apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/{model/TilgjengeligeBekreftelserResponse.kt => models/TilgjengeligBekreftelserResponse.kt} (55%) rename apps/bekreftelse-api/src/main/resources/openapi/{api.yaml => documentation.yaml} (86%) delete mode 100644 lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthenticationException.kt delete mode 100644 lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthorizationException.kt delete mode 100644 lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorCodeAwareException.kt create mode 100644 lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorTypeAwareException.kt create mode 100644 lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ErrorType.kt create mode 100644 lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/AuthenticationException.kt delete mode 100644 lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/UgyldigBearerTokenException.kt create mode 100644 lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/AuthorizationException.kt create mode 100644 lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBearerTokenException.kt diff --git a/apps/bekreftelse-api/build.gradle.kts b/apps/bekreftelse-api/build.gradle.kts index 86930d94..8c8e3236 100644 --- a/apps/bekreftelse-api/build.gradle.kts +++ b/apps/bekreftelse-api/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - plugins { kotlin("jvm") id("org.openapi.generator") @@ -90,6 +88,22 @@ application { mainClass.set("no.nav.paw.bekreftelse.api.ApplicationKt") } +sourceSets { + main { + kotlin { + srcDir("${layout.buildDirectory.get()}/generated/src/main/kotlin") + } + } +} + +tasks.named("compileKotlin") { + dependsOn("openApiValidate", "openApiGenerate") +} + +tasks.named("compileTestKotlin") { + dependsOn("openApiValidate", "openApiGenerate") +} + tasks.withType().configureEach { useJUnitPlatform() } @@ -102,6 +116,33 @@ tasks.withType(Jar::class) { } } +val openApiDocFile = "${layout.projectDirectory}/src/main/resources/openapi/documentation.yaml" + +openApiValidate { + inputSpec = openApiDocFile +} + +openApiGenerate { + generatorName = "kotlin" + inputSpec = openApiDocFile + outputDir = "${layout.buildDirectory.get()}/generated/" + packageName = "no.nav.paw.bekreftelse.api" + configOptions = mapOf( + "serializationLibrary" to "jackson", + "enumPropertyNaming" to "original", + ) + globalProperties = mapOf( + "apis" to "none", + "models" to "" + ) + typeMappings = mapOf( + "DateTime" to "Instant" + ) + importMappings = mapOf( + "Instant" to "java.time.Instant" + ) +} + jib { from.image = "$baseImage:$jvmMajorVersion" to.image = "${image ?: project.name}:${project.version}" diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/context/JsonbColumnType.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/context/JsonbColumnType.kt index 25be83e4..8cfe767d 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/context/JsonbColumnType.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/context/JsonbColumnType.kt @@ -1,6 +1,6 @@ package no.nav.paw.bekreftelse.api.context -import no.nav.paw.bekreftelse.api.model.BekreftelserTable.registerColumn +import no.nav.paw.bekreftelse.api.models.BekreftelserTable.registerColumn import org.jetbrains.exposed.sql.IColumnType import org.postgresql.util.PGobject diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataIkkeFunnetForIdException.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataIkkeFunnetForIdException.kt index ac6a9c62..421cf35d 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataIkkeFunnetForIdException.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataIkkeFunnetForIdException.kt @@ -2,6 +2,11 @@ package no.nav.paw.bekreftelse.api.exception import io.ktor.http.HttpStatusCode import no.nav.paw.error.exception.ServerResponseException +import no.nav.paw.error.model.ErrorType class DataIkkeFunnetForIdException(message: String) : - ServerResponseException(HttpStatusCode.BadRequest, "PAW_DATA_IKKE_FUNNET_FOR_ID", message) \ No newline at end of file + ServerResponseException( + HttpStatusCode.BadRequest, + ErrorType.domain("bekreftelse").error("ikke-funnet-for-id").build(), + message + ) \ No newline at end of file diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataTilhoererIkkeBrukerException.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataTilhoererIkkeBrukerException.kt index 091076a5..64fcc564 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataTilhoererIkkeBrukerException.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/exception/DataTilhoererIkkeBrukerException.kt @@ -2,6 +2,11 @@ package no.nav.paw.bekreftelse.api.exception import io.ktor.http.HttpStatusCode import no.nav.paw.error.exception.ServerResponseException +import no.nav.paw.error.model.ErrorType class DataTilhoererIkkeBrukerException(message: String) : - ServerResponseException(HttpStatusCode.BadRequest, "PAW_DATA_TILHOERER_IKKE_BRUKER", message) \ No newline at end of file + ServerResponseException( + HttpStatusCode.BadRequest, + ErrorType.domain("bekreftelse").error("tilhører-ikke-bruker").build(), + message + ) \ No newline at end of file diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/MottaBekreftelseRequest.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/MottaBekreftelseRequest.kt deleted file mode 100644 index d5b9b11c..00000000 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/MottaBekreftelseRequest.kt +++ /dev/null @@ -1,10 +0,0 @@ -package no.nav.paw.bekreftelse.api.model - -import java.util.* - -data class MottaBekreftelseRequest( - val identitetsnummer: String?, - val bekreftelseId: UUID, - val harJobbetIDennePerioden: Boolean, - val vilFortsetteSomArbeidssoeker: Boolean -) diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligBekreftelse.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligBekreftelse.kt deleted file mode 100644 index fa7012c5..00000000 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligBekreftelse.kt +++ /dev/null @@ -1,11 +0,0 @@ -package no.nav.paw.bekreftelse.api.model - -import java.time.Instant -import java.util.* - -data class TilgjengeligBekreftelse( - val periodeId: UUID, - val bekreftelseId: UUID, - val gjelderFra: Instant, - val gjelderTil: Instant, -) diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserRequest.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserRequest.kt deleted file mode 100644 index e7e02f29..00000000 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserRequest.kt +++ /dev/null @@ -1,5 +0,0 @@ -package no.nav.paw.bekreftelse.api.model - -data class TilgjengeligeBekreftelserRequest( - val identitetsnummer: String? = null -) \ No newline at end of file diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/BekreftelseRow.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/BekreftelseRow.kt similarity index 97% rename from apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/BekreftelseRow.kt rename to apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/BekreftelseRow.kt index aedd4a94..aaf9a9a7 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/BekreftelseRow.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/BekreftelseRow.kt @@ -1,4 +1,4 @@ -package no.nav.paw.bekreftelse.api.model +package no.nav.paw.bekreftelse.api.models import no.nav.paw.bekreftelse.internehendelser.BekreftelseTilgjengelig import org.jetbrains.exposed.sql.ResultRow diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/BekreftelserTable.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/BekreftelserTable.kt similarity index 93% rename from apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/BekreftelserTable.kt rename to apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/BekreftelserTable.kt index 00ecb034..e429ed08 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/BekreftelserTable.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/BekreftelserTable.kt @@ -1,4 +1,4 @@ -package no.nav.paw.bekreftelse.api.model +package no.nav.paw.bekreftelse.api.models import no.nav.paw.bekreftelse.api.utils.JsonbSerde import org.jetbrains.exposed.sql.Table diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/Converters.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/Converters.kt similarity index 98% rename from apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/Converters.kt rename to apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/Converters.kt index 7c45bf86..025e60cf 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/Converters.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/Converters.kt @@ -1,4 +1,4 @@ -package no.nav.paw.bekreftelse.api.model +package no.nav.paw.bekreftelse.api.models import no.nav.paw.bekreftelse.internehendelser.BekreftelseTilgjengelig import no.nav.paw.bekreftelse.melding.v1.Bekreftelse diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserResponse.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/TilgjengeligBekreftelserResponse.kt similarity index 55% rename from apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserResponse.kt rename to apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/TilgjengeligBekreftelserResponse.kt index 6d752878..381fb6bd 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/model/TilgjengeligeBekreftelserResponse.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/models/TilgjengeligBekreftelserResponse.kt @@ -1,3 +1,3 @@ -package no.nav.paw.bekreftelse.api.model +package no.nav.paw.bekreftelse.api.models -typealias TilgjengeligBekreftelserResponse = List +typealias TilgjengeligBekreftelserResponse = List \ No newline at end of file diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/policy/SluttbrukerAccessPolicy.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/policy/SluttbrukerAccessPolicy.kt index ae882839..9c95e074 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/policy/SluttbrukerAccessPolicy.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/policy/SluttbrukerAccessPolicy.kt @@ -20,7 +20,7 @@ class SluttbrukerAccessPolicy( is Sluttbruker -> { // TODO Håndtere verge if (identitetsnummer != null && identitetsnummer != bruker.ident) { - return Deny("Sluttbruke kan ikke hente data for annen bruker") + return Deny("Sluttbruker har ikke tilgang til data for annen bruker") } return Permit("Sluttbruker har tilgang") } diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/repository/BekreftelseRepository.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/repository/BekreftelseRepository.kt index 01c2a76d..072016b5 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/repository/BekreftelseRepository.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/repository/BekreftelseRepository.kt @@ -1,8 +1,8 @@ package no.nav.paw.bekreftelse.api.repository -import no.nav.paw.bekreftelse.api.model.BekreftelseRow -import no.nav.paw.bekreftelse.api.model.BekreftelserTable -import no.nav.paw.bekreftelse.api.model.asBekreftelseRow +import no.nav.paw.bekreftelse.api.models.BekreftelseRow +import no.nav.paw.bekreftelse.api.models.BekreftelserTable +import no.nav.paw.bekreftelse.api.models.asBekreftelseRow import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.deleteWhere diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutes.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutes.kt index e861f25c..4349b5a5 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutes.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutes.kt @@ -9,8 +9,8 @@ import io.ktor.server.routing.get import io.ktor.server.routing.post import io.ktor.server.routing.route import no.nav.paw.bekreftelse.api.context.ApplicationContext -import no.nav.paw.bekreftelse.api.model.MottaBekreftelseRequest -import no.nav.paw.bekreftelse.api.model.TilgjengeligeBekreftelserRequest +import no.nav.paw.bekreftelse.api.models.MottaBekreftelseRequest +import no.nav.paw.bekreftelse.api.models.TilgjengeligeBekreftelserRequest import no.nav.paw.bekreftelse.api.utils.hentSluttbrukerIdentitet import no.nav.paw.security.authentication.model.asIdentitetsnummer import no.nav.paw.security.authentication.token.AzureAd diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/SwaggerRoutes.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/SwaggerRoutes.kt index 27229ea5..addb05ea 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/SwaggerRoutes.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/routes/SwaggerRoutes.kt @@ -4,5 +4,5 @@ import io.ktor.server.plugins.swagger.swaggerUI import io.ktor.server.routing.Route fun Route.swaggerRoutes() { - swaggerUI(path = "docs", swaggerFile = "openapi/api.yaml") + swaggerUI(path = "docs", swaggerFile = "openapi/documentation.yaml") } \ No newline at end of file diff --git a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/services/BekreftelseService.kt b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/services/BekreftelseService.kt index 98138152..df9a42e7 100644 --- a/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/services/BekreftelseService.kt +++ b/apps/bekreftelse-api/src/main/kotlin/no/nav/paw/bekreftelse/api/services/BekreftelseService.kt @@ -8,12 +8,12 @@ import no.nav.paw.bekreftelse.api.config.ApplicationConfig import no.nav.paw.bekreftelse.api.config.ServerConfig import no.nav.paw.bekreftelse.api.exception.DataIkkeFunnetForIdException import no.nav.paw.bekreftelse.api.exception.DataTilhoererIkkeBrukerException -import no.nav.paw.bekreftelse.api.model.MottaBekreftelseRequest -import no.nav.paw.bekreftelse.api.model.TilgjengeligBekreftelserResponse -import no.nav.paw.bekreftelse.api.model.asBekreftelse -import no.nav.paw.bekreftelse.api.model.asBekreftelseBruker -import no.nav.paw.bekreftelse.api.model.asBekreftelseRow -import no.nav.paw.bekreftelse.api.model.asTilgjengeligBekreftelse +import no.nav.paw.bekreftelse.api.models.MottaBekreftelseRequest +import no.nav.paw.bekreftelse.api.models.TilgjengeligBekreftelserResponse +import no.nav.paw.bekreftelse.api.models.asBekreftelse +import no.nav.paw.bekreftelse.api.models.asBekreftelseBruker +import no.nav.paw.bekreftelse.api.models.asBekreftelseRow +import no.nav.paw.bekreftelse.api.models.asTilgjengeligBekreftelse import no.nav.paw.bekreftelse.api.producer.BekreftelseKafkaProducer import no.nav.paw.bekreftelse.api.repository.BekreftelseRepository import no.nav.paw.bekreftelse.api.utils.buildLogger diff --git a/apps/bekreftelse-api/src/main/resources/openapi/api.yaml b/apps/bekreftelse-api/src/main/resources/openapi/documentation.yaml similarity index 86% rename from apps/bekreftelse-api/src/main/resources/openapi/api.yaml rename to apps/bekreftelse-api/src/main/resources/openapi/documentation.yaml index ea28c933..d3f1fb73 100644 --- a/apps/bekreftelse-api/src/main/resources/openapi/api.yaml +++ b/apps/bekreftelse-api/src/main/resources/openapi/documentation.yaml @@ -11,6 +11,7 @@ servers: paths: /api/v1/tilgjengelige-bekreftelser: get: + operationId: getTilgjengeligBekreftelser description: "Endepunkt for å hende tilgjengelige bekreftelser for innlogget sluttbruker" parameters: - name: "traceparent" @@ -34,6 +35,7 @@ paths: schema: $ref: "#/components/schemas/ProblemDetails" post: + operationId: postTilgjengeligBekreftelser description: "Endepunkt for å hende tilgjengelige bekreftelser for en arbeidssøker" parameters: - name: "traceparent" @@ -56,6 +58,12 @@ paths: application/json: schema: $ref: "#/components/schemas/TilgjengeligBekreftelserResponse" + "400": + description: "Bad Request" + content: + application/json: + schema: + $ref: "#/components/schemas/ProblemDetails" "403": description: "Forbidden" content: @@ -64,6 +72,7 @@ paths: $ref: "#/components/schemas/ProblemDetails" /api/v1/bekreftelse: post: + operationId: postMottaBekreftelse description: "Endepunkt for å registrere en bekreftelse for en arbeidssøker" parameters: - name: "traceparent" @@ -77,11 +86,17 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/BekreftelseRequest" + $ref: "#/components/schemas/MottaBekreftelseRequest" required: true responses: "200": description: "OK" + "400": + description: "Bad Request" + content: + application/json: + schema: + $ref: "#/components/schemas/ProblemDetails" "403": description: "Forbidden" content: @@ -90,6 +105,7 @@ paths: $ref: "#/components/schemas/ProblemDetails" /internal/isAlive: get: + operationId: getIsAlive description: "Service is alive probe" responses: "503": @@ -106,6 +122,7 @@ paths: $ref: "#/components/schemas/HealthStatus" /internal/isReady: get: + operationId: getIsReady description: "Service is ready probe" responses: "503": @@ -122,6 +139,7 @@ paths: $ref: "#/components/schemas/HealthStatus" /internal/metrics: get: + operationId: getMetrics description: "Prometheus metrics" responses: "200": @@ -137,8 +155,6 @@ components: properties: identitetsnummer: type: "string" - required: - - "identitetsnummer" example: identitetsnummer: "01017012345" TilgjengeligBekreftelserResponse: @@ -170,7 +186,7 @@ components: bekreftelseId: "ec6b5a10-b67c-42c1-b6e7-a642c36bd78e" gjelderFra: "2020-01-01T11:22:33.444Z" gjelderTil: "2020-02-02T11:22:33.444Z" - BekreftelseRequest: + MottaBekreftelseRequest: type: "object" properties: identitetsnummer: @@ -183,7 +199,6 @@ components: vilFortsetteSomArbeidssoeker: type: "boolean" required: - - "identitetsnummer" - "bekreftelseId" - "harJobbetIDennePerioden" - "vilFortsetteSomArbeidssoeker" @@ -195,31 +210,33 @@ components: ProblemDetails: type: object properties: - type: + id: type: "string" - code: + format: "uuid" + type: type: "string" + format: "uri" + status: + type: "integer" title: type: "string" - status: - type: "number" detail: type: "string" instance: type: "string" required: + - id - type - - code - - title - status + - title - detail - instance example: - type: "about:blank" - code: "BRUKER_HAR_IKKE_TILGANG" - title: "Forbidden" + id: "3cd944fb-6187-41a8-91b2-b172f2baf890" + type: "urn:paw:sikkerhet:bruker-har-ikke-tilgang" status: 403 - detail: "Access denied" + title: "Forbidden" + detail: "Bruker har ikke tilgang" instance: "/api/endpoint" HealthStatus: type: "string" diff --git a/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/plugin/TestDataPlugin.kt b/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/plugin/TestDataPlugin.kt index a3619ab8..8b9fd07f 100644 --- a/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/plugin/TestDataPlugin.kt +++ b/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/plugin/TestDataPlugin.kt @@ -7,7 +7,7 @@ import io.ktor.server.application.hooks.MonitoringEvent import io.ktor.server.application.install import io.ktor.server.application.log import io.ktor.util.KtorDsl -import no.nav.paw.bekreftelse.api.model.BekreftelseRow +import no.nav.paw.bekreftelse.api.models.BekreftelseRow import no.nav.paw.bekreftelse.api.plugins.custom.FlywayMigrationCompleted import no.nav.paw.bekreftelse.api.repository.BekreftelseRepository import no.nav.paw.bekreftelse.api.test.TestData diff --git a/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutesTest.kt b/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutesTest.kt index 97999fd8..6af9e770 100644 --- a/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutesTest.kt +++ b/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/routes/BekreftelseRoutesTest.kt @@ -16,8 +16,8 @@ import io.mockk.every import io.mockk.just import io.mockk.runs import io.mockk.verify -import no.nav.paw.bekreftelse.api.model.TilgjengeligBekreftelserResponse -import no.nav.paw.bekreftelse.api.model.TilgjengeligeBekreftelserRequest +import no.nav.paw.bekreftelse.api.models.TilgjengeligBekreftelserResponse +import no.nav.paw.bekreftelse.api.models.TilgjengeligeBekreftelserRequest import no.nav.paw.bekreftelse.api.test.ApplicationTestContext import no.nav.paw.bekreftelse.api.test.TestData import no.nav.paw.bekreftelse.api.test.issueAzureToken @@ -26,6 +26,8 @@ import no.nav.paw.bekreftelse.api.test.setJsonBody import no.nav.paw.bekreftelse.melding.v1.Bekreftelse import no.nav.paw.error.model.ProblemDetails import no.nav.paw.kafkakeygenerator.client.KafkaKeysResponse +import no.nav.paw.security.authorization.exception.IngenTilgangException +import no.nav.paw.security.authorization.exception.UgyldigBearerTokenException import no.nav.poao_tilgang.client.Decision import no.nav.poao_tilgang.client.NavAnsattTilgangTilEksternBrukerPolicyInput import no.nav.poao_tilgang.client.api.ApiResult @@ -99,7 +101,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_UGYLDIG_BEARER_TOKEN" + body.type shouldBe UgyldigBearerTokenException("").type } } } @@ -126,7 +128,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_UGYLDIG_BEARER_TOKEN" + body.type shouldBe UgyldigBearerTokenException("").type } } @@ -147,7 +149,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_INGEN_TILGANG" + body.type shouldBe IngenTilgangException("").type } } @@ -243,7 +245,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_INGEN_TILGANG" + body.type shouldBe IngenTilgangException("").type } } @@ -329,7 +331,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_INGEN_TILGANG" + body.type shouldBe IngenTilgangException("").type } } @@ -348,7 +350,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_INGEN_TILGANG" + body.type shouldBe IngenTilgangException("").type } } @@ -373,7 +375,7 @@ class BekreftelseRoutesTest : FreeSpec({ response.status shouldBe HttpStatusCode.Forbidden val body = response.body() body.status shouldBe HttpStatusCode.Forbidden - body.code shouldBe "PAW_INGEN_TILGANG" + body.type shouldBe IngenTilgangException("").type } coVerify { kafkaKeysClientMock.getIdAndKey(any()) } diff --git a/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/test/TestData.kt b/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/test/TestData.kt index cd5d76a1..17f85f56 100644 --- a/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/test/TestData.kt +++ b/apps/bekreftelse-api/src/test/kotlin/no/nav/paw/bekreftelse/api/test/TestData.kt @@ -6,9 +6,9 @@ import io.ktor.client.request.setBody import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.append -import no.nav.paw.bekreftelse.api.model.BekreftelseRow -import no.nav.paw.bekreftelse.api.model.MottaBekreftelseRequest -import no.nav.paw.bekreftelse.api.model.TilgjengeligeBekreftelserRequest +import no.nav.paw.bekreftelse.api.models.BekreftelseRow +import no.nav.paw.bekreftelse.api.models.MottaBekreftelseRequest +import no.nav.paw.bekreftelse.api.models.TilgjengeligeBekreftelserRequest import no.nav.paw.bekreftelse.internehendelser.BekreftelseTilgjengelig import java.time.Duration import java.time.Instant diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthenticationException.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthenticationException.kt deleted file mode 100644 index 6d973ecc..00000000 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthenticationException.kt +++ /dev/null @@ -1,9 +0,0 @@ -package no.nav.paw.error.exception - -import io.ktor.http.HttpStatusCode - -open class AuthenticationException( - override val code: String, - override val message: String, - override val cause: Throwable? = null -) : ServerResponseException(HttpStatusCode.Unauthorized, code, message, cause) \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthorizationException.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthorizationException.kt deleted file mode 100644 index c11f9ee3..00000000 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/AuthorizationException.kt +++ /dev/null @@ -1,9 +0,0 @@ -package no.nav.paw.error.exception - -import io.ktor.http.HttpStatusCode - -open class AuthorizationException( - override val code: String, - override val message: String, - override val cause: Throwable? = null -) : ServerResponseException(HttpStatusCode.Forbidden, code, message, cause) \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ClientResponseException.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ClientResponseException.kt index 117bf524..2610ecab 100644 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ClientResponseException.kt +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ClientResponseException.kt @@ -1,10 +1,11 @@ package no.nav.paw.error.exception import io.ktor.http.HttpStatusCode +import java.net.URI open class ClientResponseException( val status: HttpStatusCode, - override val code: String, + override val type: URI, override val message: String, override val cause: Throwable? = null -) : ErrorCodeAwareException(code, message, cause) \ No newline at end of file +) : ErrorTypeAwareException(type, message, cause) \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorCodeAwareException.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorCodeAwareException.kt deleted file mode 100644 index b8b24a96..00000000 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorCodeAwareException.kt +++ /dev/null @@ -1,7 +0,0 @@ -package no.nav.paw.error.exception - -open class ErrorCodeAwareException( - open val code: String, - override val message: String, - override val cause: Throwable? = null -) : Exception(message, cause) \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorTypeAwareException.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorTypeAwareException.kt new file mode 100644 index 00000000..2651f85b --- /dev/null +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ErrorTypeAwareException.kt @@ -0,0 +1,11 @@ +package no.nav.paw.error.exception + +import java.net.URI + +open class ErrorTypeAwareException( + open val type: URI, + override val message: String, + override val cause: Throwable? = null +) : + Exception(message, cause) { +} \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ServerResponseException.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ServerResponseException.kt index 7231e258..99925df0 100644 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ServerResponseException.kt +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/error/exception/ServerResponseException.kt @@ -1,10 +1,11 @@ package no.nav.paw.error.exception import io.ktor.http.HttpStatusCode +import java.net.URI open class ServerResponseException( val status: HttpStatusCode, - override val code: String, + override val type: URI, override val message: String, override val cause: Throwable? = null -) : ErrorCodeAwareException(code, message, cause) \ No newline at end of file +) : ErrorTypeAwareException(type, message, cause) \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/handler/HttpExceptionHandler.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/handler/HttpExceptionHandler.kt index 37b59f94..60b98710 100644 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/handler/HttpExceptionHandler.kt +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/error/handler/HttpExceptionHandler.kt @@ -1,5 +1,6 @@ package no.nav.paw.error.handler +import io.ktor.http.HttpStatusCode import io.ktor.server.application.ApplicationCall import io.ktor.server.plugins.BadRequestException import io.ktor.server.plugins.ContentTransformationException @@ -9,28 +10,34 @@ import io.ktor.server.request.uri import io.ktor.server.response.respond import no.nav.paw.error.exception.ClientResponseException import no.nav.paw.error.exception.ServerResponseException +import no.nav.paw.error.model.ErrorType import no.nav.paw.error.model.ProblemDetails -import no.nav.paw.error.model.build400Error -import no.nav.paw.error.model.build500Error -import no.nav.paw.error.model.buildError +import no.nav.paw.error.model.ProblemDetailsBuilder import org.slf4j.Logger import org.slf4j.LoggerFactory import org.slf4j.MDC private val logger: Logger = LoggerFactory.getLogger("no.nav.paw.logger.error.http") private const val MDC_ERROR_ID_KEY = "x_error_id" -private const val MDC_ERROR_CODE_KEY = "x_error_code" +private const val MDC_ERROR_TYPE_KEY = "x_error_type" +private const val MDC_EXCEPTION_KEY = "exception" suspend fun ApplicationCall.handleException( throwable: Throwable, resolver: (throwable: Throwable) -> ProblemDetails? = { null } ) { val problemDetails = resolveProblemDetails(request, throwable, resolver) + MDC.put(MDC_ERROR_ID_KEY, problemDetails.id.toString()) - MDC.put(MDC_ERROR_CODE_KEY, problemDetails.code) + MDC.put(MDC_ERROR_TYPE_KEY, problemDetails.type.toString()) + MDC.put(MDC_EXCEPTION_KEY, throwable.javaClass.canonicalName) + logger.error(problemDetails.detail, throwable) + MDC.remove(MDC_ERROR_ID_KEY) - MDC.remove(MDC_ERROR_CODE_KEY) + MDC.remove(MDC_ERROR_TYPE_KEY) + MDC.remove(MDC_EXCEPTION_KEY) + respond(problemDetails.status, problemDetails) } @@ -46,53 +53,55 @@ fun resolveProblemDetails( when (throwable) { is BadRequestException -> { - return build400Error( - code = "PAW_KUNNE_IKKE_TOLKE_FORESPOERSEL", - detail = "Kunne ikke tolke forespørsel", - instance = request.uri - ) + return ProblemDetailsBuilder.builder() + .type(ErrorType.domain("http").error("kunne-ikke-tolke-forespoersel").build()) + .status(HttpStatusCode.BadRequest) + .detail("Kunne ikke tolke forespørsel") + .instance(request.uri) + .build() } is ContentTransformationException -> { - return build400Error( - code = "PAW_KUNNE_IKKE_TOLKE_INNHOLD", - detail = "Kunne ikke tolke innhold i forespørsel", - instance = request.uri - ) + return ProblemDetailsBuilder.builder() + .type(ErrorType.domain("http").error("kunne-ikke-tolke-innhold").build()) + .status(HttpStatusCode.BadRequest) + .detail("Kunne ikke tolke innhold i forespørsel") + .instance(request.uri) + .build() } is RequestAlreadyConsumedException -> { - return build500Error( - code = "PAW_FORESPOERSEL_ALLEREDE_MOTTATT", - detail = "Forespørsel er allerede mottatt. Dette er en kodefeil", - instance = request.uri - ) + return ProblemDetailsBuilder.builder() + .type(ErrorType.domain("http").error("forespoersel-allerede-mottatt").build()) + .status(HttpStatusCode.InternalServerError) + .detail("Forespørsel er allerede mottatt. Dette er en kodefeil") + .instance(request.uri) + .build() } is ServerResponseException -> { - return buildError( - code = throwable.code, - detail = throwable.message, - status = throwable.status, - instance = request.uri - ) + return ProblemDetailsBuilder.builder() + .type(throwable.type) + .status(throwable.status) + .detail(throwable.message) + .instance(request.uri) + .build() } is ClientResponseException -> { - return buildError( - code = throwable.code, - detail = throwable.message, - status = throwable.status, - instance = request.uri - ) + return ProblemDetailsBuilder.builder() + .type(throwable.type) + .status(throwable.status) + .detail(throwable.message) + .instance(request.uri) + .build() } else -> { - return build500Error( - code = "PAW_UKJENT_FEIL", - detail = "Forespørsel feilet med ukjent feil", - instance = request.uri - ) + return ProblemDetailsBuilder.builder() + .detail("Forespørsel feilet med ukjent feil") + .instance(request.uri) + .build() } } } \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ErrorType.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ErrorType.kt new file mode 100644 index 00000000..9b13333a --- /dev/null +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ErrorType.kt @@ -0,0 +1,27 @@ +package no.nav.paw.error.model + +import java.net.URI + +object ErrorTypeDefaults { + const val TEAM = "paw" + const val DOMAIN = "default" + const val ERROR = "ukjent-feil" +} + +class ErrorType( + var team: String = ErrorTypeDefaults.TEAM, + var domain: String = ErrorTypeDefaults.DOMAIN, + var error: String = ErrorTypeDefaults.ERROR, +) { + fun team(team: String) = apply { this.team = team } + fun domain(domain: String) = apply { this.domain = domain } + fun error(error: String) = apply { this.error = error } + fun build(): URI = URI.create("urn:${team.lowercase()}:${domain.lowercase()}:${error.lowercase()}") + + companion object { + fun team(team: String): ErrorType = ErrorType(team = team) + fun domain(domain: String): ErrorType = ErrorType(domain = domain) + fun error(error: String): ErrorType = ErrorType(error = error) + fun default(): ErrorType = ErrorType() + } +} \ No newline at end of file diff --git a/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ProblemDetails.kt b/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ProblemDetails.kt index 41b97977..10e4a6cb 100644 --- a/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ProblemDetails.kt +++ b/lib/error-handling/src/main/kotlin/no/nav/paw/error/model/ProblemDetails.kt @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize import io.ktor.http.HttpStatusCode import no.nav.paw.error.serialize.HttpStatusCodeDeserializer import no.nav.paw.error.serialize.HttpStatusCodeSerializer +import java.net.URI import java.time.Instant import java.util.* @@ -14,30 +15,31 @@ import java.util.* */ data class ProblemDetails( val id: UUID = UUID.randomUUID(), - val code: String, // Maskinelt lesbar feilkode - val title: String, + val type: URI = ErrorType.default().build(), @JsonSerialize(using = HttpStatusCodeSerializer::class) @JsonDeserialize(using = HttpStatusCodeDeserializer::class) val status: HttpStatusCode, - val detail: String, + val title: String, + val detail: String? = null, val instance: String, - val type: String = "about:blank", val timestamp: Instant = Instant.now() ) -fun build400Error(code: String, detail: String, instance: String, type: String = "about:blank") = - buildError(code, detail, HttpStatusCode.BadRequest, instance, type) - -fun build403Error(code: String, detail: String, instance: String, type: String = "about:blank") = - buildError(code, detail, HttpStatusCode.Forbidden, instance, type) - -fun build500Error(code: String, detail: String, instance: String, type: String = "about:blank") = - buildError(code, detail, HttpStatusCode.InternalServerError, instance, type) +class ProblemDetailsBuilder private constructor( + var type: URI = ErrorType.default().build(), + var status: HttpStatusCode = HttpStatusCode.InternalServerError, + var title: String? = null, + var detail: String? = null, + var instance: String = "/" +) { + fun type(type: URI) = apply { this.type = type } + fun status(status: HttpStatusCode) = apply { this.status = status } + fun title(title: String) = apply { this.title = title } + fun detail(detail: String) = apply { this.detail = detail } + fun instance(instance: String) = apply { this.instance = instance } + fun build(): ProblemDetails = ProblemDetails( + type = type, status = status, title = title ?: status.description, detail = detail, instance = instance + ) -fun buildError(code: String, detail: String, status: HttpStatusCode, instance: String, type: String = "about:blank") = - ProblemDetails( - code = code, - title = status.description, - status = status, - detail = detail, - instance = instance, - type = type - ) \ No newline at end of file + companion object { + fun builder(): ProblemDetailsBuilder = ProblemDetailsBuilder() + } +} diff --git a/lib/error-handling/src/test/kotlin/no/nav/paw/error/handler/HttpExceptionHandlerTest.kt b/lib/error-handling/src/test/kotlin/no/nav/paw/error/handler/HttpExceptionHandlerTest.kt index b46d97e2..0ea10119 100644 --- a/lib/error-handling/src/test/kotlin/no/nav/paw/error/handler/HttpExceptionHandlerTest.kt +++ b/lib/error-handling/src/test/kotlin/no/nav/paw/error/handler/HttpExceptionHandlerTest.kt @@ -15,6 +15,7 @@ import io.ktor.server.routing.IgnoreTrailingSlash import io.ktor.server.routing.get import io.ktor.server.routing.routing import io.ktor.server.testing.testApplication +import no.nav.paw.error.model.ErrorType import no.nav.paw.error.model.ProblemDetails import io.ktor.client.plugins.contentnegotiation.ContentNegotiation as ClientContentNegotiation import io.ktor.server.application.install as serverInstall @@ -53,11 +54,14 @@ class HttpExceptionHandlerTest : FreeSpec({ } val response400 = client.get("/api/400") - val responseBody404 = response400.body() + val responseBody400 = response400.body() response400.status shouldBe HttpStatusCode.BadRequest - responseBody404.status shouldBe HttpStatusCode.BadRequest - responseBody404.code shouldBe "PAW_KUNNE_IKKE_TOLKE_FORESPOERSEL" - responseBody404.title shouldBe HttpStatusCode.BadRequest.description + responseBody400.type shouldBe ErrorType + .domain("http") + .error("kunne-ikke-tolke-forespoersel") + .build() + responseBody400.status shouldBe HttpStatusCode.BadRequest + responseBody400.title shouldBe HttpStatusCode.BadRequest.description } } }) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/AuthenticationException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/AuthenticationException.kt new file mode 100644 index 00000000..4e4cb156 --- /dev/null +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/AuthenticationException.kt @@ -0,0 +1,11 @@ +package no.nav.paw.security.authentication.exception + +import io.ktor.http.HttpStatusCode +import no.nav.paw.error.exception.ServerResponseException +import java.net.URI + +open class AuthenticationException( + override val type: URI, + override val message: String, + override val cause: Throwable? = null +) : ServerResponseException(HttpStatusCode.Unauthorized, type, message, cause) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/BearerTokenManglerException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/BearerTokenManglerException.kt index f69672e9..8802c27e 100644 --- a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/BearerTokenManglerException.kt +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/BearerTokenManglerException.kt @@ -1,6 +1,6 @@ package no.nav.paw.security.authentication.exception -import no.nav.paw.error.exception.AuthenticationException +import no.nav.paw.error.model.ErrorType class BearerTokenManglerException(message: String) : - AuthenticationException("PAW_BEARER_TOKEN_MANGLER", message) \ No newline at end of file + AuthenticationException(ErrorType.domain("security").error("bearer-token-mangler").build(), message) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/UgyldigBearerTokenException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/UgyldigBearerTokenException.kt deleted file mode 100644 index 5f64f323..00000000 --- a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/exception/UgyldigBearerTokenException.kt +++ /dev/null @@ -1,6 +0,0 @@ -package no.nav.paw.security.authentication.exception - -import no.nav.paw.error.exception.AuthorizationException - -class UgyldigBearerTokenException(message: String) : - AuthorizationException("PAW_UGYLDIG_BEARER_TOKEN", message) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/token/AccessToken.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/token/AccessToken.kt index 113a8dc0..c5fd2aa4 100644 --- a/lib/security/src/main/kotlin/no/nav/paw/security/authentication/token/AccessToken.kt +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authentication/token/AccessToken.kt @@ -1,6 +1,6 @@ package no.nav.paw.security.authentication.token -import no.nav.paw.security.authentication.exception.UgyldigBearerTokenException +import no.nav.paw.security.authorization.exception.UgyldigBearerTokenException import no.nav.paw.security.authentication.model.Identitetsnummer import no.nav.security.token.support.core.context.TokenValidationContext import no.nav.security.token.support.core.jwt.JwtToken diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/context/SecurityContext.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/context/SecurityContext.kt index 087b5efd..613ba8a0 100644 --- a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/context/SecurityContext.kt +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/context/SecurityContext.kt @@ -1,7 +1,7 @@ package no.nav.paw.security.authorization.context import no.nav.paw.security.authentication.exception.BearerTokenManglerException -import no.nav.paw.security.authentication.exception.UgyldigBearerTokenException +import no.nav.paw.security.authorization.exception.UgyldigBearerTokenException import no.nav.paw.security.authentication.model.Bruker import no.nav.paw.security.authentication.model.M2MToken import no.nav.paw.security.authentication.model.NavAnsatt diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/AuthorizationException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/AuthorizationException.kt new file mode 100644 index 00000000..296bb558 --- /dev/null +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/AuthorizationException.kt @@ -0,0 +1,11 @@ +package no.nav.paw.security.authorization.exception + +import io.ktor.http.HttpStatusCode +import no.nav.paw.error.exception.ServerResponseException +import java.net.URI + +open class AuthorizationException( + override val type: URI, + override val message: String, + override val cause: Throwable? = null +) : ServerResponseException(HttpStatusCode.Forbidden, type, message, cause) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/IngenTilgangException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/IngenTilgangException.kt index 01780351..4e2512ef 100644 --- a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/IngenTilgangException.kt +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/IngenTilgangException.kt @@ -1,6 +1,6 @@ package no.nav.paw.security.authorization.exception -import no.nav.paw.error.exception.AuthorizationException +import no.nav.paw.error.model.ErrorType class IngenTilgangException(message: String) : - AuthorizationException("PAW_INGEN_TILGANG", message) \ No newline at end of file + AuthorizationException(ErrorType.domain("security").error("ingen-tilgang").build(), message) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBearerTokenException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBearerTokenException.kt new file mode 100644 index 00000000..d221f9e9 --- /dev/null +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBearerTokenException.kt @@ -0,0 +1,6 @@ +package no.nav.paw.security.authorization.exception + +import no.nav.paw.error.model.ErrorType + +class UgyldigBearerTokenException(message: String) : + AuthorizationException(ErrorType.domain("security").error("ugyldig-bearer-token").build(), message) \ No newline at end of file diff --git a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBrukerException.kt b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBrukerException.kt index eb3c329b..881213cf 100644 --- a/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBrukerException.kt +++ b/lib/security/src/main/kotlin/no/nav/paw/security/authorization/exception/UgyldigBrukerException.kt @@ -1,6 +1,6 @@ package no.nav.paw.security.authorization.exception -import no.nav.paw.error.exception.AuthorizationException +import no.nav.paw.error.model.ErrorType class UgyldigBrukerException(message: String) : - AuthorizationException("PAW_UGYLDIG_BRUKER", message) \ No newline at end of file + AuthorizationException(ErrorType.domain("security").error("ugyldig-bruker").build(), message) \ No newline at end of file