-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dataklasser for å håndtere respons fra Azure
- Loading branch information
1 parent
ec6a00c
commit 1175339
Showing
4 changed files
with
87 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
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") | ||
} |
12 changes: 12 additions & 0 deletions
12
azure-token-client/src/main/kotlin/com/github/navikt/tbd_libs/azure/AzureErrorResponse.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 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 | ||
) |
25 changes: 25 additions & 0 deletions
25
azure-token-client/src/main/kotlin/com/github/navikt/tbd_libs/azure/AzureTokenResponse.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,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" | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...e-token-client/src/test/kotlin/com/github/navikt/tbd_libs/azure/AzureTokenResponseTest.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,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) } | ||
} | ||
} |