1
1
package no.nav.security.mock.oauth2.introspect
2
2
3
+ import com.fasterxml.jackson.annotation.JsonFormat
3
4
import com.fasterxml.jackson.annotation.JsonInclude
4
5
import com.fasterxml.jackson.annotation.JsonProperty
5
6
import com.nimbusds.jwt.JWTClaimsSet
7
+ import com.nimbusds.jwt.util.DateUtils
6
8
import com.nimbusds.oauth2.sdk.OAuth2Error
7
9
import mu.KotlinLogging
8
10
import no.nav.security.mock.oauth2.OAuth2Exception
@@ -13,6 +15,7 @@ import no.nav.security.mock.oauth2.http.Route
13
15
import no.nav.security.mock.oauth2.http.json
14
16
import no.nav.security.mock.oauth2.token.OAuth2TokenProvider
15
17
import okhttp3.Headers
18
+ import java.util.Date
16
19
17
20
private val log = KotlinLogging .logger { }
18
21
@@ -26,21 +29,20 @@ internal fun Route.Builder.introspect(tokenProvider: OAuth2TokenProvider) =
26
29
}
27
30
28
31
request.verifyToken(tokenProvider)?.let {
29
- val claims = it.claims
30
32
json(
31
33
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 ,
44
46
),
45
47
)
46
48
} ? : json(IntrospectResponse (false ))
@@ -70,6 +72,8 @@ private fun String.auth(method: String): String? =
70
72
.takeIf { it.size == 2 }
71
73
?.last()
72
74
75
+ private fun Date?.epochSeconds (): Long? = this ?.let (DateUtils ::toSecondsSinceEpoch)
76
+
73
77
@JsonInclude(JsonInclude .Include .NON_NULL )
74
78
data class IntrospectResponse (
75
79
@JsonProperty(" active" )
@@ -91,7 +95,8 @@ data class IntrospectResponse(
91
95
@JsonProperty(" sub" )
92
96
val sub : String? = null ,
93
97
@JsonProperty(" aud" )
94
- val aud : String? = null ,
98
+ @JsonFormat(with = [JsonFormat .Feature .WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED ])
99
+ val aud : List <String >? = null ,
95
100
@JsonProperty(" iss" )
96
101
val iss : String? = null ,
97
102
@JsonProperty(" jti" )
0 commit comments