Skip to content

Commit

Permalink
Ktlint
Browse files Browse the repository at this point in the history
Co-authored-by: Magnus Sælensminde <[email protected]>
  • Loading branch information
mettok and magnusae committed Dec 10, 2024
1 parent 468a913 commit 9b5a131
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
Expand All @@ -39,7 +35,6 @@ fun Application.module() {
)
}


configureRouting(MaskinportenService())
configureCORS()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"]

Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Inntektsmelding> {
class LpsClient(
var maskinportenService: MaskinportenService,
) {
suspend fun hentInntektsmeldinger(consumerOrgNr: String): List<Inntektsmelding> {
val fetchNewAccessToken =
maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr).fetchNewAccessToken()
val response =
Expand All @@ -33,15 +28,16 @@ class LpsClient(var maskinportenService: MaskinportenService) {
return response.body<List<Inntektsmelding>>()
}


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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -96,11 +94,12 @@ class LpsClient(var maskinportenService: MaskinportenService) {
}
}

suspend fun hentForespoersler(
consumerOrgNr: String,
): List<Forespoersel> {
suspend fun hentForespoersler(consumerOrgNr: String): List<Forespoersel> {
val accessToken =
maskinportenService.getMaskinportenTokenForOrgNr(consumerOrgNr).fetchNewAccessToken().tokenResponse.accessToken
maskinportenService
.getMaskinportenTokenForOrgNr(consumerOrgNr)
.fetchNewAccessToken()
.tokenResponse.accessToken
val response =
createHttpClient().get {
url("${LPS_API_ENDPOINT}forespoersler")
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,20 +39,28 @@ 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) {
call.respond(HttpStatusCode.InternalServerError, "Feilet å hente systembruker: ${e.message}")
}
}
}

private fun Routing.registrerNyBedrift(maskinportenService: MaskinportenService) {
post("/registrer-ny-bedrift") {
val parametre = call.receiveParameters()
Expand All @@ -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) {
Expand Down Expand Up @@ -102,8 +111,6 @@ private fun Routing.inntektsmeldinger(lpsClient: LpsClient) {
}

private fun Routing.getToken(maskinportenService: MaskinportenService) {


post("/getToken") {
try {
val params = call.receiveParameters()
Expand All @@ -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,
Expand Down

0 comments on commit 9b5a131

Please sign in to comment.