11package no.nav.security.mock.oauth2.introspect
22
3+ import com.fasterxml.jackson.annotation.JsonFormat
34import com.fasterxml.jackson.annotation.JsonInclude
45import com.fasterxml.jackson.annotation.JsonProperty
56import com.nimbusds.jwt.JWTClaimsSet
7+ import com.nimbusds.jwt.util.DateUtils
68import com.nimbusds.oauth2.sdk.OAuth2Error
79import mu.KotlinLogging
810import no.nav.security.mock.oauth2.OAuth2Exception
@@ -13,6 +15,7 @@ import no.nav.security.mock.oauth2.http.Route
1315import no.nav.security.mock.oauth2.http.json
1416import no.nav.security.mock.oauth2.token.OAuth2TokenProvider
1517import okhttp3.Headers
18+ import java.util.Date
1619
1720private val log = KotlinLogging .logger { }
1821
@@ -26,21 +29,20 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
2629 }
2730
2831 request.verifyToken(tokenProvider)?.let {
29- val claims = it.claims
3032 json(
3133 IntrospectResponse (
32- 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() ,
34+ active = true ,
35+ scope = it.getStringClaim( " scope " ),
36+ clientId = it.getStringClaim( " client_id" ),
37+ username = it.getStringClaim( " username " ),
38+ tokenType = it.getStringClaim( " token_type" ) ? : " Bearer " ,
39+ exp = it.expirationTime.epochSeconds() ,
40+ iat = it.issueTime.epochSeconds() ,
41+ nbf = it.notBeforeTime.epochSeconds() ,
42+ sub = it.subject ,
43+ aud = it.audience ,
44+ iss = it.issuer ,
45+ jti = it.jwtid ,
4446 ),
4547 )
4648 } ? : json(IntrospectResponse (false ))
@@ -70,6 +72,8 @@ private fun String.auth(method: String): String? =
7072 .takeIf { it.size == 2 }
7173 ?.last()
7274
75+ private fun Date?.epochSeconds (): Long? = this ?.let (DateUtils ::toSecondsSinceEpoch)
76+
7377@JsonInclude(JsonInclude .Include .NON_NULL )
7478data class IntrospectResponse (
7579 @JsonProperty(" active" )
@@ -91,7 +95,8 @@ data class IntrospectResponse(
9195 @JsonProperty(" sub" )
9296 val sub : String? = null ,
9397 @JsonProperty(" aud" )
94- val aud : String? = null ,
98+ @JsonFormat(with = [JsonFormat .Feature .WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED ])
99+ val aud : List <String >? = null ,
95100 @JsonProperty(" iss" )
96101 val iss : String? = null ,
97102 @JsonProperty(" jti" )
0 commit comments