From 6f308c15444f261550b0b0aae001797c7204506d Mon Sep 17 00:00:00 2001 From: Mehdi Zare Date: Fri, 5 Jul 2024 07:56:08 +0200 Subject: [PATCH] Fikse feil med ugyldig token --- build.gradle.kts | 2 +- src/main/kotlin/MaskinportenClient.kt | 11 ++++++----- src/main/kotlin/MaskinportenClientConfig.kt | 6 +++--- src/main/kotlin/TokenResponse.kt | 6 +++++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c671719..cea254c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { id("maven-publish") } group = "no.nav.helsearbeidsgiver" -version = "0.1.8" +version = "0.1.9" kotlin { compilerOptions { diff --git a/src/main/kotlin/MaskinportenClient.kt b/src/main/kotlin/MaskinportenClient.kt index 08c1772..3801796 100644 --- a/src/main/kotlin/MaskinportenClient.kt +++ b/src/main/kotlin/MaskinportenClient.kt @@ -18,7 +18,7 @@ class MaskinportenClient(private val maskinportenClientConfig: MaskinportenClien private val httpClient = createHttpClient() suspend fun fetchNewAccessToken(): TokenResponseWrapper { - sikkerLogger().info("Henter ny access token fra Maskinporten") + sikkerLogger().info("Maskinporten: Henter ny access token fra Maskinporten") val result = runCatching { val response: HttpResponse = httpClient.post(maskinportenClientConfig.endpoint) { @@ -35,21 +35,22 @@ class MaskinportenClient(private val maskinportenClientConfig: MaskinportenClien return result.fold( onSuccess = { tokenResponse -> TokenResponseWrapper(tokenResponse).also { - sikkerLogger().info("Hentet ny access token. Expires in ${it.remainingTimeInSeconds} seconds.") + sikkerLogger().info("Maskinporten: Hentet ny access token. ${it.tokenResponse} ") + sikkerLogger().info("Maskinporten: Hentet ny access token. Expires in ${it.remainingTimeInSeconds} seconds.") } }, onFailure = { e -> when (e) { is ClientRequestException -> { - sikkerLogger().error("ClientRequestException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") + sikkerLogger().error("Maskinporten:: ClientRequestException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") } is ServerResponseException -> { - sikkerLogger().error("ServerResponseException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") + sikkerLogger().error("Maskinporten: ServerResponseException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") } else -> { - sikkerLogger().error("Feilet å hente ny access token fra Maskinporten: $e") + sikkerLogger().error("Maskinporten: Feilet å hente ny access token fra Maskinporten: $e") } } throw e diff --git a/src/main/kotlin/MaskinportenClientConfig.kt b/src/main/kotlin/MaskinportenClientConfig.kt index 5f64023..9fe80c8 100644 --- a/src/main/kotlin/MaskinportenClientConfig.kt +++ b/src/main/kotlin/MaskinportenClientConfig.kt @@ -38,9 +38,9 @@ data class MaskinportenClientConfig( private fun currentTime(): Date = Date.from(Instant.now()) - private val claims: JWTClaimsSet by lazy { + private fun claims(): JWTClaimsSet { val now = currentTime() - JWTClaimsSet.Builder() + return JWTClaimsSet.Builder() .issuer(clientId) .audience(issuer) .issueTime(now) @@ -51,6 +51,6 @@ data class MaskinportenClientConfig( } fun getJwtAssertion(): String { - return SignedJWT(header, claims).apply { sign(signer) }.serialize() + return SignedJWT(header, claims()).apply { sign(signer) }.serialize() } } diff --git a/src/main/kotlin/TokenResponse.kt b/src/main/kotlin/TokenResponse.kt index b3b2b9a..e43a0e8 100644 --- a/src/main/kotlin/TokenResponse.kt +++ b/src/main/kotlin/TokenResponse.kt @@ -17,7 +17,11 @@ data class TokenResponse( @SerialName("token_type") val tokenType: String, @SerialName("expires_in") val expiresInSeconds: Long, val scope: String -) +) { + override fun toString(): String { + return "TokenResponse(accessToken='${accessToken.take(3)}', tokenType='$tokenType', expiresInSeconds=$expiresInSeconds, scope='$scope')" + } +} class TokenResponseWrapper(val tokenResponse: TokenResponse) { private val issueTime = System.currentTimeMillis() / 1000