-
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
1 parent
d39a04b
commit f5283c0
Showing
22 changed files
with
522 additions
and
0 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
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" | ||
} | ||
} |
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,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
11
src/main/kotlin/no/nav/syfo/aareg/AntallTimerForTimeloennet.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,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 |
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,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 |
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.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 |
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,9 @@ | ||
package no.nav.syfo.aareg | ||
|
||
import java.io.Serializable | ||
|
||
@Suppress("SerialVersionUIDInSerializableClass") | ||
data class Bruksperiode( | ||
var fom: String, | ||
var tom: String | ||
) : Serializable |
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,9 @@ | ||
package no.nav.syfo.aareg | ||
|
||
import java.io.Serializable | ||
|
||
@Suppress("SerialVersionUIDInSerializableClass") | ||
data class Gyldighetsperiode( | ||
var fom: String, | ||
var tom: String | ||
) : Serializable |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/no/nav/syfo/aareg/OpplysningspliktigArbeidsgiver.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,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 | ||
} | ||
} |
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,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
12
src/main/kotlin/no/nav/syfo/aareg/PermisjonPermittering.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,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 |
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,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 | ||
} | ||
} |
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,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 |
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,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 |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/no/nav/syfo/aareg/exceptions/RestErrorFromAareg.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,5 @@ | ||
package no.nav.syfo.aareg.exceptions | ||
|
||
import org.springframework.web.client.RestClientException | ||
|
||
class RestErrorFromAareg(message: String, e: RestClientException) : RuntimeException(message, e) |
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,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" | ||
} | ||
} |
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,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()) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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" | ||
} |
Oops, something went wrong.