Skip to content

Commit 6a47ad8

Browse files
committed
refactor: removed references to bearer token to avoid confusion
* removed unnecessary null check on form parameters * changed data type for exp, iat, nbf to long
1 parent 2f3b4d6 commit 6a47ad8

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

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

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
2929
throw OAuth2Exception(OAuth2Error.INVALID_CLIENT.setDescription(msg), msg)
3030
}
3131

32-
request.verifyBearerToken(tokenProvider)?.let {
32+
request.verifyToken(tokenProvider)?.let {
3333
val claims = it.claims
3434
json(
3535
IntrospectResponse(
@@ -38,9 +38,9 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
3838
claims["client_id"].toString(),
3939
claims["username"].toString(),
4040
claims["token_type"].toString(),
41-
claims["exp"].toString(),
42-
claims["iat"].toString(),
43-
claims["nbf"].toString(),
41+
claims["exp"] as? Long,
42+
claims["iat"] as? Long,
43+
claims["nbf"] as? Long,
4444
claims["sub"].toString(),
4545
claims["aud"].toString(),
4646
claims["iss"].toString(),
@@ -50,18 +50,14 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
5050
} ?: json(IntrospectResponse(false))
5151
}
5252

53-
private fun OAuth2HttpRequest.verifyBearerToken(tokenProvider: OAuth2TokenProvider): JWTClaimsSet? {
54-
val tokenString = this.getToken()
55-
if (tokenString.isNullOrEmpty()) {
56-
return null
57-
}
58-
53+
private fun OAuth2HttpRequest.verifyToken(tokenProvider: OAuth2TokenProvider): JWTClaimsSet? {
54+
val tokenString = this.formParameters.get("token")
5955
val issuer = url.toIssuerUrl()
6056
val jwkSet = tokenProvider.publicJwkSet(issuer.issuerId())
61-
6257
return try {
6358
SignedJWT.parse(tokenString).verifySignatureAndIssuer(Issuer(issuer.toString()), jwkSet)
6459
} catch (e: Exception) {
60+
log.debug("token_introspection: failed signature validation")
6561
return null
6662
}
6763
}
@@ -80,14 +76,6 @@ private fun String.auth(method: String): String? {
8076
?.last()
8177
}
8278

83-
private fun OAuth2HttpRequest.getToken(): String? {
84-
val tokenParams = this.formParameters
85-
if (tokenParams.map.isEmpty()) {
86-
return null
87-
}
88-
return tokenParams.get("token")
89-
}
90-
9179
@JsonInclude(JsonInclude.Include.NON_NULL)
9280
data class IntrospectResponse(
9381
@JsonProperty("active")
@@ -101,11 +89,11 @@ data class IntrospectResponse(
10189
@JsonProperty("token_type")
10290
val tokenType: String? = null,
10391
@JsonProperty("exp")
104-
val exp: String? = null,
92+
val exp: Long? = null,
10593
@JsonProperty("iat")
106-
val iat: String? = null,
94+
val iat: Long? = null,
10795
@JsonProperty("nbf")
108-
val nbf: String? = null,
96+
val nbf: Long? = null,
10997
@JsonProperty("sub")
11098
val sub: String? = null,
11199
@JsonProperty("aud")

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
44
import com.fasterxml.jackson.module.kotlin.readValue
55
import io.kotest.assertions.asClue
66
import io.kotest.assertions.throwables.shouldThrow
7+
import io.kotest.matchers.maps.shouldContain
78
import io.kotest.matchers.maps.shouldContainAll
89
import io.kotest.matchers.maps.shouldContainExactly
910
import io.kotest.matchers.shouldBe
@@ -24,24 +25,25 @@ internal class IntrospectTest {
2425
val issuerUrl = "http://localhost/default"
2526
val tokenProvider = OAuth2TokenProvider()
2627
val claims = mapOf(
27-
"active" to true,
2828
"iss" to issuerUrl,
2929
"client_id" to "yolo",
3030
"token_type" to "token",
3131
"sub" to "foo"
3232
)
33-
val bearerToken = tokenProvider.jwt(claims)
34-
val request = request("$issuerUrl$INTROSPECT", bearerToken.serialize())
33+
val token = tokenProvider.jwt(claims)
34+
println("token: " + token.jwtClaimsSet.toJSONObject())
35+
val request = request("$issuerUrl$INTROSPECT", token.serialize())
3536

3637
routes { introspect(tokenProvider) }.invoke(request).asClue {
37-
println(it.parse<Map<String, Any>>())
3838
it.status shouldBe 200
39-
it.parse<Map<String, Any>>() shouldContainAll claims
39+
val response = it.parse<Map<String, Any>>()
40+
response shouldContainAll claims
41+
response shouldContain ("active" to true)
4042
}
4143
}
4244

4345
@Test
44-
fun `introspect should return active false when bearer token is missing`() {
46+
fun `introspect should return active false when token is missing`() {
4547
val url = "http://localhost/default$INTROSPECT"
4648

4749
routes {
@@ -53,7 +55,7 @@ internal class IntrospectTest {
5355
}
5456

5557
@Test
56-
fun `introspect should return active false when bearer token is invalid`() {
58+
fun `introspect should return active false when token is invalid`() {
5759
val url = "http://localhost/default$INTROSPECT"
5860

5961
routes {
@@ -81,7 +83,7 @@ internal class IntrospectTest {
8183

8284
private inline fun <reified T> OAuth2HttpResponse.parse(): T = jacksonObjectMapper().readValue(checkNotNull(body))
8385

84-
private fun request(url: String, bearerToken: String?, auth: String = "Basic user=password"): OAuth2HttpRequest {
86+
private fun request(url: String, token: String?, auth: String = "Basic user=password"): OAuth2HttpRequest {
8587
return OAuth2HttpRequest(
8688
Headers.headersOf(
8789
"Authorization", auth,
@@ -90,7 +92,7 @@ internal class IntrospectTest {
9092
),
9193
method = "POST",
9294
url.toHttpUrl(),
93-
body = bearerToken?.let { "token=$it&token_type_hint=access_token" }
95+
body = token?.let { "token=$it&token_type_hint=access_token" }
9496
)
9597
}
9698
}

0 commit comments

Comments
 (0)