Skip to content

Commit 89629b8

Browse files
committed
fix(introspect): exp, iat, nbf claims were always null
1 parent 2c7a773 commit 89629b8

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/main/kotlin/no/nav/security/mock/oauth2/introspect/Introspect.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,20 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
2626
}
2727

2828
request.verifyToken(tokenProvider)?.let {
29-
val claims = it.claims
3029
json(
3130
IntrospectResponse(
3231
true,
33-
claims["scope"].toString(),
34-
claims["client_id"].toString(),
35-
claims["username"].toString(),
36-
claims["token_type"].toString(),
37-
claims["exp"] as? Long,
38-
claims["iat"] as? Long,
39-
claims["nbf"] as? Long,
40-
claims["sub"].toString(),
41-
claims["aud"].toString(),
42-
claims["iss"].toString(),
43-
claims["jti"].toString(),
32+
it.getStringClaim("scope"),
33+
it.getStringClaim("client_id"),
34+
it.getStringClaim("username"),
35+
it.getStringClaim("token_type") ?: "Bearer",
36+
it.expirationTime?.time?.div(1000),
37+
it.issueTime?.time?.div(1000),
38+
it.notBeforeTime?.time?.div(1000),
39+
it.subject,
40+
it.audience,
41+
it.issuer,
42+
it.jwtid,
4443
),
4544
)
4645
} ?: json(IntrospectResponse(false))
@@ -91,7 +90,7 @@ data class IntrospectResponse(
9190
@JsonProperty("sub")
9291
val sub: String? = null,
9392
@JsonProperty("aud")
94-
val aud: String? = null,
93+
val aud: List<String>? = null,
9594
@JsonProperty("iss")
9695
val iss: String? = null,
9796
@JsonProperty("jti")

src/test/kotlin/no/nav/security/mock/oauth2/introspect/IntrospectTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ internal class IntrospectTest {
9191
}
9292
}
9393

94+
@Test
95+
fun `introspect should return iat and exp from claims when provider`() {
96+
val issuerUrl = "http://localhost/default"
97+
val tokenProvider = OAuth2TokenProvider()
98+
val claims =
99+
mapOf(
100+
"iss" to issuerUrl,
101+
"client_id" to "yolo",
102+
"token_type" to "token",
103+
"sub" to "foo",
104+
"iat" to Instant.now().epochSecond,
105+
"exp" to Instant.now().plus(1, ChronoUnit.DAYS).epochSecond,
106+
)
107+
108+
val token = tokenProvider.jwt(claims)
109+
val request = request("$issuerUrl$INTROSPECT", token.serialize())
110+
111+
routes { introspect(tokenProvider) }.invoke(request).asClue {
112+
it.status shouldBe 200
113+
val response = it.parse<IntrospectResponse>()
114+
response.active shouldBe true
115+
response.iat shouldBe claims["iat"]
116+
response.exp shouldBe claims["exp"]
117+
}
118+
}
119+
94120
@Test
95121
fun `introspect should return active false when token is missing`() {
96122
val url = "http://localhost/default$INTROSPECT"

0 commit comments

Comments
 (0)