Skip to content

Commit

Permalink
Fikse feil med ugyldig token
Browse files Browse the repository at this point in the history
  • Loading branch information
mettok committed Jul 5, 2024
1 parent 23534db commit 6f308c1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id("maven-publish")
}
group = "no.nav.helsearbeidsgiver"
version = "0.1.8"
version = "0.1.9"

kotlin {
compilerOptions {
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/MaskinportenClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/MaskinportenClientConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/TokenResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6f308c1

Please sign in to comment.