diff --git a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Application.kt b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Application.kt index 49771c4..25cff06 100644 --- a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Application.kt +++ b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Application.kt @@ -10,12 +10,8 @@ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation import io.ktor.server.plugins.cors.routing.CORS import io.ktor.server.request.path import kotlinx.serialization.json.Json -import no.nav.helsearbeidsgiver.lps.LpsClient -import no.nav.helsearbeidsgiver.maskinporten.MaskinportenClient -import no.nav.helsearbeidsgiver.maskinporten.MaskinportenClientConfigPkey import no.nav.helsearbeidsgiver.maskinporten.MaskinportenService import no.nav.helsearbeidsgiver.plugins.configureRouting -import no.nav.helsearbeidsgiver.utils.logger import org.slf4j.event.Level fun main(args: Array) { @@ -39,7 +35,6 @@ fun Application.module() { ) } - configureRouting(MaskinportenService()) configureCORS() } diff --git a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Env.kt b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Env.kt index 69be816..94fd0af 100644 --- a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Env.kt +++ b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/Env.kt @@ -2,7 +2,6 @@ package no.nav.helsearbeidsgiver import io.github.cdimascio.dotenv.dotenv - private val dotenv = dotenv() val maskinportenClientId: String = dotenv["MASKINPORTEN_CLIENT_ID"] val maskinportenClientJwk: String = dotenv["MASKINPORTEN_CLIENT_JWK"] @@ -14,4 +13,3 @@ val maskinportenKid: String = dotenv["MASKINPORTEN_KID"] val maskinportenPrivateKey: String = dotenv["MASKINPORTEN_PKEY"] val systemId: String = dotenv["SYSTEM_ID"] val altinnSystemUserRequestUrl: String = dotenv["ALTINN_SYSTEM_USER_REQUEST_URL"] - diff --git a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/altinn/AltinnService.kt b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/altinn/AltinnService.kt index 9c8f146..fd5655b 100644 --- a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/altinn/AltinnService.kt +++ b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/altinn/AltinnService.kt @@ -7,9 +7,9 @@ import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.http.ContentType import io.ktor.http.contentType +import no.nav.helsearbeidsgiver.altinnSystemUserRequestUrl import no.nav.helsearbeidsgiver.maskinporten.createHttpClient import no.nav.helsearbeidsgiver.systemId -import no.nav.helsearbeidsgiver.altinnSystemUserRequestUrl class AltinnService { suspend fun lagSystembrukerForespoersel( diff --git a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/lps/LpsClient.kt b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/lps/LpsClient.kt index c0562c4..9bdf571 100644 --- a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/lps/LpsClient.kt +++ b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/lps/LpsClient.kt @@ -8,20 +8,15 @@ import io.ktor.client.request.setBody import io.ktor.client.request.url import io.ktor.http.ContentType import io.ktor.http.contentType -import no.nav.helsearbeidsgiver.maskinporten.MaskinportenClient -import no.nav.helsearbeidsgiver.maskinporten.MaskinportenClientConfigPkey import no.nav.helsearbeidsgiver.maskinporten.MaskinportenService import no.nav.helsearbeidsgiver.maskinporten.createHttpClient -import no.nav.helsearbeidsgiver.maskinporten.getSystemBrukerClaim private const val LPS_API_ENDPOINT = "https://sykepenger-im-lps-api.ekstern.dev.nav.no/" -class LpsClient(var maskinportenService: MaskinportenService) { - - - suspend fun hentInntektsmeldinger( - consumerOrgNr: String, - ): List { +class LpsClient( + var maskinportenService: MaskinportenService, +) { + suspend fun hentInntektsmeldinger(consumerOrgNr: String): List { val fetchNewAccessToken = maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr).fetchNewAccessToken() val response = @@ -33,15 +28,16 @@ class LpsClient(var maskinportenService: MaskinportenService) { return response.body>() } - suspend fun filtrerInntektsmeldinger( - consumerOrgNr: String, request: InntektsmeldingRequest, ): InntektsmeldingResponse { try { val accessToken = - maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr).fetchNewAccessToken().tokenResponse.accessToken + maskinportenService + .getMaskinportenTokenForOrgNr(consumerOrgNr) + .fetchNewAccessToken() + .tokenResponse.accessToken val response = createHttpClient().post { @@ -57,13 +53,15 @@ class LpsClient(var maskinportenService: MaskinportenService) { } suspend fun filtrerForespoersler( - consumerOrgNr: String, request: ForespoerselRequest, ): ForespoerselResponse { try { val accessToken = - maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr).fetchNewAccessToken().tokenResponse.accessToken + maskinportenService + .getMaskinportenTokenForOrgNr(consumerOrgNr) + .fetchNewAccessToken() + .tokenResponse.accessToken val response = createHttpClient().post { @@ -96,11 +94,12 @@ class LpsClient(var maskinportenService: MaskinportenService) { } } - suspend fun hentForespoersler( - consumerOrgNr: String, - ): List { + suspend fun hentForespoersler(consumerOrgNr: String): List { val accessToken = - maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr).fetchNewAccessToken().tokenResponse.accessToken + maskinportenService + .getMaskinportenTokenForOrgNr(consumerOrgNr) + .fetchNewAccessToken() + .tokenResponse.accessToken val response = createHttpClient().get { url("${LPS_API_ENDPOINT}forespoersler") diff --git a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/maskinporten/MaskinportenService.kt b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/maskinporten/MaskinportenService.kt index 6a8d31f..20f0854 100644 --- a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/maskinporten/MaskinportenService.kt +++ b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/maskinporten/MaskinportenService.kt @@ -1,55 +1,56 @@ package no.nav.helsearbeidsgiver.maskinporten -import no.nav.helsearbeidsgiver.maskinportenClientId import no.nav.helsearbeidsgiver.maskinportenClientIssuer import no.nav.helsearbeidsgiver.maskinportenIntegrasjonsId import no.nav.helsearbeidsgiver.maskinportenKid import no.nav.helsearbeidsgiver.maskinportenPrivateKey import no.nav.helsearbeidsgiver.maskinportenTokenEndpoint -import no.nav.helsearbeidsgiver.systemId class MaskinportenService { private val httpClient = createHttpClient() - - fun getMaskinportenTokenForSystembruker(orgNr:String,scope: String) = MaskinportenClient( + fun getMaskinportenTokenForSystembruker( + orgNr: String, + scope: String, + ) = MaskinportenClient( maskinportenClientConfig = - MaskinportenClientConfigPkey( - kid = maskinportenKid, - privateKey = maskinportenPrivateKey, - issuer = maskinportenClientIssuer, - scope = scope, - clientId = maskinportenIntegrasjonsId, - endpoint = maskinportenTokenEndpoint, - additionalClaims = getSystemBrukerClaim(orgNr), - ) + MaskinportenClientConfigPkey( + kid = maskinportenKid, + privateKey = maskinportenPrivateKey, + issuer = maskinportenClientIssuer, + scope = scope, + clientId = maskinportenIntegrasjonsId, + endpoint = maskinportenTokenEndpoint, + additionalClaims = getSystemBrukerClaim(orgNr), + ), ) - fun getSimpleMaskinportenTokenForScope(scope: String) = MaskinportenClient( - maskinportenClientConfig = - MaskinportenClientConfigPkey( - kid = maskinportenKid, - privateKey = maskinportenPrivateKey, - issuer = maskinportenClientIssuer, - scope = scope, - clientId = maskinportenIntegrasjonsId, - endpoint = maskinportenTokenEndpoint, + fun getSimpleMaskinportenTokenForScope(scope: String) = + MaskinportenClient( + maskinportenClientConfig = + MaskinportenClientConfigPkey( + kid = maskinportenKid, + privateKey = maskinportenPrivateKey, + issuer = maskinportenClientIssuer, + scope = scope, + clientId = maskinportenIntegrasjonsId, + endpoint = maskinportenTokenEndpoint, + ), ) - ) fun getMaskinportenTokenForOrgNr( consumerOrgNr: String, scope: String = "nav:inntektsmelding/lps.write", ) = MaskinportenClient( maskinportenClientConfig = - MaskinportenClientConfigPkey( - kid = maskinportenKid, - privateKey = maskinportenPrivateKey, - issuer = maskinportenClientIssuer, - scope = scope, - clientId = maskinportenIntegrasjonsId, - endpoint = maskinportenTokenEndpoint, - additionalClaims = getSystemBrukerClaim(consumerOrgNr), - ), + MaskinportenClientConfigPkey( + kid = maskinportenKid, + privateKey = maskinportenPrivateKey, + issuer = maskinportenClientIssuer, + scope = scope, + clientId = maskinportenIntegrasjonsId, + endpoint = maskinportenTokenEndpoint, + additionalClaims = getSystemBrukerClaim(consumerOrgNr), + ), ) } diff --git a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/plugins/Routing.kt b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/plugins/Routing.kt index 3869e45..c48acb1 100644 --- a/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/plugins/Routing.kt +++ b/lps-client-backend/src/main/kotlin/no/nav/helsearbeidsgiver/plugins/Routing.kt @@ -10,17 +10,15 @@ import io.ktor.server.response.respond import io.ktor.server.routing.Routing import io.ktor.server.routing.post import io.ktor.server.routing.routing -import no.nav.helsearbeidsgiver.altinn.RegistrerRespons import no.nav.helsearbeidsgiver.altinn.AltinnService +import no.nav.helsearbeidsgiver.altinn.RegistrerRespons import no.nav.helsearbeidsgiver.lps.ForespoerselRequest import no.nav.helsearbeidsgiver.lps.InntektsmeldingRequest import no.nav.helsearbeidsgiver.lps.LpsClient import no.nav.helsearbeidsgiver.lps.Status -import no.nav.helsearbeidsgiver.maskinporten.MaskinportenClient import no.nav.helsearbeidsgiver.maskinporten.MaskinportenService import no.nav.helsearbeidsgiver.utils.logger import java.time.LocalDateTime -import kotlin.math.log fun Application.configureRouting(maskinportenService: MaskinportenService) { val lpsClient = LpsClient(maskinportenService) @@ -41,13 +39,20 @@ fun Application.configureRouting(maskinportenService: MaskinportenService) { } } } + private fun Routing.hentSystembruker(maskinportenService: MaskinportenService) { post("/systembruker") { val params = call.receiveParameters() val orgnr = params["orgnr"] ?: return@post call.respond(HttpStatusCode.BadRequest, "Mangler 'orgnr' parameter") try { - val systemBrukerClaim = maskinportenService.getMaskinportenTokenForSystembruker(orgnr,"nav:helse/im.read").fetchNewAccessToken().tokenResponse.accessToken + val systemBrukerClaim = + maskinportenService + .getMaskinportenTokenForSystembruker( + orgnr, + "nav:helse/im.read", + ).fetchNewAccessToken() + .tokenResponse.accessToken logger().info("token: $systemBrukerClaim") call.respond(HttpStatusCode.OK, systemBrukerClaim) } catch (e: Exception) { @@ -55,6 +60,7 @@ private fun Routing.hentSystembruker(maskinportenService: MaskinportenService) { } } } + private fun Routing.registrerNyBedrift(maskinportenService: MaskinportenService) { post("/registrer-ny-bedrift") { val parametre = call.receiveParameters() @@ -67,12 +73,15 @@ private fun Routing.registrerNyBedrift(maskinportenService: MaskinportenService) logger().info("Prøver å registrere bedriften med orgnr: $kundeOrgnr som ny kunde.") try { val maskinportenToken = - maskinportenService.getSimpleMaskinportenTokenForScope("altinn:authentication/systemuser.request.write") + maskinportenService + .getSimpleMaskinportenTokenForScope("altinn:authentication/systemuser.request.write") .fetchNewAccessToken() .tokenResponse.accessToken val systemBrukerForespoerselRespons = AltinnService().lagSystembrukerForespoersel(kundeOrgnr, maskinportenToken) -logger().info("Registrerte bedriften med orgnr: ${systemBrukerForespoerselRespons.redirectUrl} and ${systemBrukerForespoerselRespons.status} and ${systemBrukerForespoerselRespons.confirmUrl}") + logger().info( + "Registrerte bedriften med orgnr: ${systemBrukerForespoerselRespons.redirectUrl} and ${systemBrukerForespoerselRespons.status} and ${systemBrukerForespoerselRespons.confirmUrl}", + ) logger().info("Registrerte bedriften med orgnr: $kundeOrgnr som ny kunde.") call.respond(HttpStatusCode.OK, RegistrerRespons(systemBrukerForespoerselRespons.confirmUrl)) } catch (e: Exception) { @@ -102,8 +111,6 @@ private fun Routing.inntektsmeldinger(lpsClient: LpsClient) { } private fun Routing.getToken(maskinportenService: MaskinportenService) { - - post("/getToken") { try { val params = call.receiveParameters() @@ -112,7 +119,13 @@ private fun Routing.getToken(maskinportenService: MaskinportenService) { val consumerOrgNr = params["consumerOrgNr"] ?: return@post call.respond(HttpStatusCode.BadRequest, "Mangler 'consumerOrgNr' parameter") - val message = maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr, scope).fetchNewAccessToken().tokenResponse.accessToken + val message = + maskinportenService + .getMaskinportenTokenForOrgNr( + consumerOrgNr, + scope, + ).fetchNewAccessToken() + .tokenResponse.accessToken call.respond( HttpStatusCode.OK, message,