Skip to content

Commit

Permalink
Create Aareg and relevant classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
MalazAlkoj committed Jul 3, 2024
1 parent d39a04b commit f5283c0
Show file tree
Hide file tree
Showing 22 changed files with 522 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nais/nais-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ spec:
value: dev-gcp.teamsykefravr.istilgangskontroll
- name: ISTILGANGSKONTROLL_URL
value: http://istilgangskontroll.teamsykefravr
- name: AAREG_SERVICES_URL
value: https://aareg-services-q1.dev.intern.nav.no/api
redis:
- instance: oppfolgingsplan
access: readwrite
2 changes: 2 additions & 0 deletions nais/nais-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ spec:
value: prod-gcp.teamsykefravr.istilgangskontroll
- name: ISTILGANGSKONTROLL_URL
value: http://istilgangskontroll.teamsykefravr
- name: AAREG_SERVICES_URL
value: https://aareg-services.intern.nav.no/api
redis:
- instance: oppfolgingsplan
access: readwrite
63 changes: 63 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/AaregClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package no.nav.syfo.aareg

import no.nav.syfo.aareg.exceptions.RestErrorFromAareg
import no.nav.syfo.metric.Metrikk
import no.nav.syfo.sts.StsClient
import no.nav.syfo.util.bearerHeader
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.cache.annotation.Cacheable
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod.GET
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClientException
import org.springframework.web.client.RestTemplate

@Service
class AaregClient(
private val metrikk: Metrikk,
private val stsClient: StsClient,
@Value("\${aareg.services.url}") private val url: String
) {
companion object {
private val LOG = LoggerFactory.getLogger(AaregClient::class.java)
const val NAV_CONSUMER_TOKEN_HEADER = "Nav-Consumer-Token"
const val NAV_PERSONIDENT_HEADER = "Nav-Personident"
}

@Cacheable(cacheNames = ["arbeidsforholdAT"], key = "#fnr", condition = "#fnr != null")
fun arbeidsforholdArbeidstaker(fnr: String): List<Arbeidsforhold> {
metrikk.tellHendelse("call_aareg")
val token = stsClient.token()

return try {
val response: ResponseEntity<List<Arbeidsforhold>> = RestTemplate().exchange(
arbeidstakerUrl(),
GET,
entity(fnr, token),
object : ParameterizedTypeReference<List<Arbeidsforhold>>() {}
)
metrikk.tellHendelse("call_aareg_success")
response.body ?: emptyList()
} catch (e: RestClientException) {
metrikk.tellHendelse("call_aareg_fail")
LOG.error("Error from AAREG with request-url: $url", e)
throw RestErrorFromAareg("Tried to get arbeidsforhold for arbeidstaker", e)
}
}

private fun entity(fnr: String, token: String): HttpEntity<*> {
val headers = HttpHeaders()
headers.add(HttpHeaders.AUTHORIZATION, bearerHeader(token))
headers.add(NAV_CONSUMER_TOKEN_HEADER, bearerHeader(token))
headers.add(NAV_PERSONIDENT_HEADER, fnr)
return HttpEntity<Any>(headers)
}

private fun arbeidstakerUrl(): String {
return "$url/v1/arbeidstaker/arbeidsforhold"
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Ansettelsesperiode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Ansettelsesperiode(
var bruksperiode: Bruksperiode? = null,
var periode: Periode,
var sporingsinformasjon: Sporingsinformasjon? = null,
var varslingskode: String? = null
) : Serializable
11 changes: 11 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/AntallTimerForTimeloennet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class AntallTimerForTimeloennet(
var antallTimer: Double,
var periode: Periode,
var rapporteringsperiode: String,
var sporingsinformasjon: Sporingsinformasjon
) : Serializable
17 changes: 17 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Arbeidsavtale.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Arbeidsavtale(
var antallTimerPrUke: Double? = null,
var arbeidstidsordning: String? = null,
var beregnetAntallTimerPrUke: Double? = null,
var bruksperiode: Bruksperiode? = null,
var gyldighetsperiode: Gyldighetsperiode? = null,
var sistLoennsendring: String? = null,
var sistStillingsendring: String? = null,
var sporingsinformasjon: Sporingsinformasjon? = null,
var stillingsprosent: Double,
var yrke: String
) : Serializable
22 changes: 22 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Arbeidsforhold.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Arbeidsforhold(
var ansettelsesperiode: Ansettelsesperiode? = null,
var antallTimerForTimeloennet: List<AntallTimerForTimeloennet>? = null,
var arbeidsavtaler: List<Arbeidsavtale>? = null,
var arbeidsforholdId: String? = null,
var arbeidsgiver: OpplysningspliktigArbeidsgiver? = null,
var arbeidstaker: Person? = null,
var innrapportertEtterAOrdningen: Boolean = false,
var navArbeidsforholdId: Int = 0,
var opplysningspliktig: OpplysningspliktigArbeidsgiver? = null,
var permisjonPermitteringer: List<PermisjonPermittering>? = null,
var registrert: String? = null,
var sistBekreftet: String? = null,
var sporingsinformasjon: Sporingsinformasjon? = null,
var type: String? = null,
var utenlandsopphold: List<Utenlandsopphold>? = null
) : Serializable
9 changes: 9 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Bruksperiode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Bruksperiode(
var fom: String,
var tom: String
) : Serializable
9 changes: 9 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Gyldighetsperiode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Gyldighetsperiode(
var fom: String,
var tom: String
) : Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class OpplysningspliktigArbeidsgiver(
var organisasjonsnummer: String,
var type: Type
) : Serializable {
enum class Type {
Organisasjon,
Person
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Periode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Periode(
var fom: String,
var tom: String
) : Serializable
12 changes: 12 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/PermisjonPermittering.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class PermisjonPermittering(
var periode: Periode,
var permisjonPermitteringId: String,
var prosent: Double,
var sporingsinformasjon: Sporingsinformasjon,
var type: String
) : Serializable
15 changes: 15 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Person(
var type: Type,
var aktoerId: String,
var offentligIdent: String
) : Serializable {
enum class Type {
Person
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Sporingsinformasjon.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Sporingsinformasjon(
var endretAv: String,
var endretKilde: String,
var endretKildereferanse: String,
var endretTidspunkt: String,
var opprettetAv: String,
var opprettetKilde: String,
var opprettetKildereferanse: String,
var opprettetTidspunkt: String
) : Serializable
11 changes: 11 additions & 0 deletions src/main/kotlin/no/nav/syfo/aareg/Utenlandsopphold.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.nav.syfo.aareg

import java.io.Serializable

@Suppress("SerialVersionUIDInSerializableClass")
data class Utenlandsopphold(
var landkode: String,
var periode: Periode,
var rapporteringsperiode: String,
var sporingsinformasjon: Sporingsinformasjon
) : Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package no.nav.syfo.aareg.exceptions

import org.springframework.web.client.RestClientException

class RestErrorFromAareg(message: String, e: RestClientException) : RuntimeException(message, e)
62 changes: 62 additions & 0 deletions src/main/kotlin/no/nav/syfo/sts/StsClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package no.nav.syfo.sts

import no.nav.syfo.metric.Metrikk
import no.nav.syfo.util.basicCredentials
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClientResponseException
import org.springframework.web.client.RestTemplate

@Service
class StsClient @Autowired constructor(
private val metrikk: Metrikk,
@Value("\${srv.password}") private val password: String,
@Value("\${security.token.service.rest.url}") private val url: String,
@Value("\${srv.username}") private val username: String
) {
private var cachedOidcToken: StsToken? = null
fun token(): String {
if (StsToken.shouldRenew(cachedOidcToken)) {
val request = HttpEntity<Any>(authorizationHeader())
try {
val response = RestTemplate().exchange(
stsTokenUrl,
HttpMethod.GET,
request,
StsToken::class.java
)
cachedOidcToken = response.body
metrikk.tellHendelse(METRIC_CALL_STS_SUCCESS)
} catch (e: RestClientResponseException) {
LOG.error(
"Request to get STS failed with status: ${e.statusCode.value()} and message: ${e.responseBodyAsString}"
)
metrikk.tellHendelse(METRIC_CALL_STS_FAIL)
throw e
}
}
return cachedOidcToken!!.accessToken
}

private val stsTokenUrl: String
get() = "$url/rest/v1/sts/token?grant_type=client_credentials&scope=openid"

private fun authorizationHeader(): HttpHeaders {
val credentials = basicCredentials(username, password)
val headers = HttpHeaders()
headers.add(HttpHeaders.AUTHORIZATION, credentials)
return headers
}

companion object {
private val LOG = LoggerFactory.getLogger(StsClient::class.java)

const val METRIC_CALL_STS_SUCCESS = "call_sts_success"
const val METRIC_CALL_STS_FAIL = "call_sts_fail"
}
}
27 changes: 27 additions & 0 deletions src/main/kotlin/no/nav/syfo/sts/StsToken.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package no.nav.syfo.sts

import java.time.LocalDateTime

data class StsToken(
val accessToken: String,
val tokenType: String,
val expiresIn: Int
) {
// Expire 10 seconds before token expiration

val expirationTime: LocalDateTime = LocalDateTime.now().plusSeconds(expiresIn - 10L)

companion object {
fun shouldRenew(token: StsToken?): Boolean {
if (token == null) {
return true
}

return isExpired(token)
}

private fun isExpired(token: StsToken): Boolean {
return token.expirationTime.isBefore(LocalDateTime.now())
}
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/no/nav/syfo/util/CredentialUtil.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package no.nav.syfo.util

import java.util.Base64

fun basicCredentials(credentialUsername: String?, credentialPassword: String?): String {
return "Basic ${Base64.getEncoder().encodeToString("$credentialUsername:$credentialPassword".toByteArray())}"
}

fun bearerHeader(token: String?): String {
return "Bearer $token"
}
Loading

0 comments on commit f5283c0

Please sign in to comment.