@@ -5,6 +5,7 @@ import com.nimbusds.oauth2.sdk.GrantType
5
5
import com.nimbusds.oauth2.sdk.TokenRequest
6
6
import no.nav.security.mock.oauth2.extensions.clientIdAsString
7
7
import no.nav.security.mock.oauth2.extensions.grantType
8
+ import no.nav.security.mock.oauth2.extensions.replaceValues
8
9
import no.nav.security.mock.oauth2.extensions.scopesWithoutOidcScopes
9
10
import no.nav.security.mock.oauth2.extensions.tokenExchangeGrantOrNull
10
11
import java.time.Duration
@@ -89,27 +90,14 @@ data class RequestMappingTokenCallback(
89
90
90
91
private fun List<RequestMapping>.getClaims (tokenRequest : TokenRequest ): Map <String , Any > {
91
92
val claims = firstOrNull { it.isMatch(tokenRequest) }?.claims ? : emptyMap()
92
- val customParameters = tokenRequest.customParameters.mapValues { (_, value) -> value.first() }
93
- val variables =
94
- if (tokenRequest.grantType() == GrantType .CLIENT_CREDENTIALS ) {
95
- customParameters + (" clientId" to tokenRequest.clientIdAsString())
96
- } else {
97
- customParameters
98
- }
99
- return claims.mapValues { (_, value) ->
100
- when (value) {
101
- is String -> replaceVariables(value, variables)
102
- is List <* > ->
103
- value.map { v ->
104
- if (v is String ) {
105
- replaceVariables(v, variables)
106
- } else {
107
- v
108
- }
109
- }
110
- else -> value
111
- }
112
- }
93
+ val templateParams = tokenRequest.toHTTPRequest().bodyAsFormParameters.mapValues { it.value.joinToString(separator = " " ) }
94
+
95
+ // in case client_id is not set as form param but as basic auth, we add it to the template params in two different formats for backwards compatibility
96
+ return claims.replaceValues(
97
+ templateParams +
98
+ mapOf (" clientId" to tokenRequest.clientIdAsString()) +
99
+ mapOf (" client_id" to tokenRequest.clientIdAsString()),
100
+ )
113
101
}
114
102
115
103
private inline fun <reified T > List<RequestMapping>.getClaimOrNull (
@@ -118,18 +106,6 @@ data class RequestMappingTokenCallback(
118
106
): T ? = getClaims(tokenRequest)[key] as ? T
119
107
120
108
private fun List<RequestMapping>.getTypeHeader (tokenRequest : TokenRequest ) = firstOrNull { it.isMatch(tokenRequest) }?.typeHeader ? : JOSEObjectType .JWT .type
121
-
122
- private fun replaceVariables (
123
- input : String ,
124
- replacements : Map <String , String >,
125
- ): String {
126
- val pattern = Regex (""" \$\{(\w+)}""" )
127
- return pattern.replace(input) { result ->
128
- val variableName = result.groupValues[1 ]
129
- val replacement = replacements[variableName]
130
- replacement ? : result.value
131
- }
132
- }
133
109
}
134
110
135
111
data class RequestMapping (
0 commit comments