Skip to content

Commit

Permalink
dataklasser for å håndtere respons fra Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsteinsland committed Jan 9, 2024
1 parent ec6a00c commit 1175339
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
3 changes: 3 additions & 0 deletions azure-token-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
val slf4jApiVersion = "2.0.7"
val jacksonVersion = "2.16.1"
dependencies {
implementation("org.slf4j:slf4j-api:$slf4jApiVersion")
api("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
api("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.navikt.tbd_libs.azure

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
internal data class AzureErrorResponse(
@JsonProperty("error")
val error: String,
@JsonProperty("error_description")
val description: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.navikt.tbd_libs.azure

import com.fasterxml.jackson.annotation.JacksonInject
import com.fasterxml.jackson.annotation.JsonProperty
import java.time.Duration
import java.time.LocalDateTime

internal data class AzureTokenResponse(
@JsonProperty("token_type")
val tokenType: String,
@JsonProperty("access_token")
val token: String,
@JsonProperty("expires_in")
val expiresIn: Long,
@JacksonInject
private val utstedtTidspunkt: LocalDateTime
) {
val expirationTime: LocalDateTime = utstedtTidspunkt.plusSeconds(expiresIn)

init {
check(tokenType.lowercase() == "bearer") {
"Forventer kun token av typen Bearer. Fikk $tokenType"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.navikt.tbd_libs.azure

import com.fasterxml.jackson.databind.InjectableValues
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import java.time.LocalDateTime

class AzureTokenResponseTest {
private val objectMapper = jacksonObjectMapper()

@Test
fun deserializeTokenResponse() {
@Language("JSON")
val json = """{
"token_type": "Bearer",
"expires_in": 3599,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWmNBVGZNNXBP..."
}"""
val utstedtTidspunkt = LocalDateTime.of(2018, 1, 1, 13, 37, 0, 123)
val reader = objectMapper.reader(InjectableValues.Std()
.addValue(LocalDateTime::class.java, utstedtTidspunkt)
).forType(AzureTokenResponse::class.java)

val token = assertDoesNotThrow { reader.readValue<AzureTokenResponse>(json) }
assertEquals(utstedtTidspunkt.plusSeconds(3599), token.expirationTime)
}

@Test
fun deserializeErrorResponse() {
@Language("JSON")
val json = """{
"error": "invalid_scope",
"error_description": "AADSTS70011: The provided value for the input parameter 'scope' is not valid. The scope https://foo.microsoft.com/.default is not valid.\r\nTrace ID: 255d1aef-8c98-452f-ac51-23d051240864\r\nCorrelation ID: fb3d2015-bc17-4bb9-bb85-30c5cf1aaaa7\r\nTimestamp: 2016-01-09 02:02:12Z",
"error_codes": [
70011
],
"timestamp": "YYYY-MM-DD HH:MM:SSZ",
"trace_id": "255d1aef-8c98-452f-ac51-23d051240864",
"correlation_id": "fb3d2015-bc17-4bb9-bb85-30c5cf1aaaa7"
}"""
assertDoesNotThrow { objectMapper.readValue<AzureErrorResponse>(json) }
}
}

0 comments on commit 1175339

Please sign in to comment.