Skip to content

Commit 307d8f2

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

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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import no.nav.security.mock.oauth2.http.Route
1313
import no.nav.security.mock.oauth2.http.json
1414
import no.nav.security.mock.oauth2.token.OAuth2TokenProvider
1515
import okhttp3.Headers
16+
import java.util.Date
1617

1718
private val log = KotlinLogging.logger { }
1819

@@ -26,21 +27,20 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
2627
}
2728

2829
request.verifyToken(tokenProvider)?.let {
29-
val claims = it.claims
3030
json(
3131
IntrospectResponse(
3232
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(),
33+
it.getStringClaim("scope"),
34+
it.getStringClaim("client_id"),
35+
it.getStringClaim("username"),
36+
it.getStringClaim("token_type") ?: "Bearer",
37+
it.expirationTime?.time?.div(1000),
38+
it.issueTime?.time?.div(1000),
39+
it.notBeforeTime?.time?.div(1000),
40+
it.subject,
41+
it.audience,
42+
it.issuer,
43+
it.jwtid,
4444
),
4545
)
4646
} ?: json(IntrospectResponse(false))
@@ -91,7 +91,7 @@ data class IntrospectResponse(
9191
@JsonProperty("sub")
9292
val sub: String? = null,
9393
@JsonProperty("aud")
94-
val aud: String? = null,
94+
val aud: List<String>? = null,
9595
@JsonProperty("iss")
9696
val iss: String? = null,
9797
@JsonProperty("jti")

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ 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 = mapOf(
99+
"iss" to issuerUrl,
100+
"client_id" to "yolo",
101+
"token_type" to "token",
102+
"sub" to "foo",
103+
"iat" to Instant.now().epochSecond,
104+
"exp" to Instant.now().plus(1, ChronoUnit.DAYS).epochSecond,
105+
)
106+
107+
val token = tokenProvider.jwt(claims)
108+
val request = request("$issuerUrl$INTROSPECT", token.serialize())
109+
110+
routes { introspect(tokenProvider) }.invoke(request).asClue {
111+
it.status shouldBe 200
112+
val response = it.parse<IntrospectResponse>()
113+
response.active shouldBe true
114+
response.iat shouldBe claims["iat"]
115+
response.exp shouldBe claims["exp"]
116+
}
117+
}
118+
94119
@Test
95120
fun `introspect should return active false when token is missing`() {
96121
val url = "http://localhost/default$INTROSPECT"

0 commit comments

Comments
 (0)