Skip to content

Commit c1e3872

Browse files
tommytroenybelMekk
andcommitted
feat: support scope as dynamic variable in requestmapping
Co-authored-by: ybelmekk <[email protected]>
1 parent f662d5f commit c1e3872

File tree

2 files changed

+77
-53
lines changed

2 files changed

+77
-53
lines changed

src/main/kotlin/no/nav/security/mock/oauth2/token/OAuth2TokenCallback.kt

+8-9
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,19 @@ data class RequestMappingTokenCallback(
8989

9090
private fun List<RequestMapping>.getClaims(tokenRequest: TokenRequest): Map<String, Any> {
9191
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-
}
92+
93+
// TODO: hack choose first element. Rewrite to support multiple elements and custom objects
94+
val params = (tokenRequest.toHTTPRequest().bodyAsFormParameters.map {
95+
it.key to it.value.first()
96+
}).toMap() + mapOf("clientId" to tokenRequest.clientIdAsString())
97+
9998
return claims.mapValues { (_, value) ->
10099
when (value) {
101-
is String -> replaceVariables(value, variables)
100+
is String -> replaceVariables(value, params)
102101
is List<*> ->
103102
value.map { v ->
104103
if (v is String) {
105-
replaceVariables(v, variables)
104+
replaceVariables(v, params)
106105
} else {
107106
v
108107
}

src/test/kotlin/no/nav/security/mock/oauth2/token/OAuth2TokenCallbackTest.kt

+69-44
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,56 @@ internal class OAuth2TokenCallbackTest {
1717
RequestMappingTokenCallback(
1818
issuerId = "issuer1",
1919
requestMappings =
20-
listOf(
21-
RequestMapping(
22-
requestParam = "scope",
23-
match = "scope1",
24-
claims =
25-
mapOf(
26-
"sub" to "subByScope1",
27-
"aud" to listOf("audByScope1"),
28-
"custom" to "custom1",
29-
),
20+
listOf(
21+
RequestMapping(
22+
requestParam = "scope",
23+
match = "scope1",
24+
claims =
25+
mapOf(
26+
"sub" to "subByScope1",
27+
"aud" to listOf("audByScope1"),
28+
"custom" to "custom1",
3029
),
31-
RequestMapping(
32-
requestParam = "scope",
33-
match = "scope2",
34-
typeHeader = "JWT2",
35-
claims =
36-
mapOf(
37-
"sub" to "subByScope2",
38-
"aud" to listOf("audByScope2"),
39-
"custom" to "custom2",
40-
),
30+
),
31+
RequestMapping(
32+
requestParam = "scope",
33+
match = "scope2",
34+
typeHeader = "JWT2",
35+
claims =
36+
mapOf(
37+
"sub" to "subByScope2",
38+
"aud" to listOf("audByScope2"),
39+
"custom" to "custom2",
4140
),
42-
RequestMapping(
43-
requestParam = "audience",
44-
match = "https://myapp.com/jwt/aud/.*",
45-
claims =
46-
mapOf(
47-
"sub" to "\${clientId}",
48-
"aud" to listOf("\${audience}"),
49-
),
41+
),
42+
RequestMapping(
43+
requestParam = "audience",
44+
match = "https://myapp.com/jwt/aud/.*",
45+
claims =
46+
mapOf(
47+
"sub" to "\${clientId}",
48+
"aud" to listOf("\${audience}"),
5049
),
51-
RequestMapping(
52-
requestParam = "grant_type",
53-
match = "authorization_code",
54-
claims =
55-
mapOf(
56-
"sub" to "defaultSub",
57-
"aud" to listOf("defaultAud"),
58-
),
50+
),
51+
RequestMapping(
52+
requestParam = "grant_type",
53+
match = "authorization_code",
54+
claims =
55+
mapOf(
56+
"sub" to "defaultSub",
57+
"aud" to listOf("defaultAud"),
5958
),
60-
RequestMapping(
61-
requestParam = "grant_type",
62-
match = "*",
63-
claims =
64-
mapOf(
65-
"sub" to "\${clientId}",
66-
"aud" to listOf("defaultAud"),
67-
),
59+
),
60+
RequestMapping(
61+
requestParam = "grant_type",
62+
match = "*",
63+
claims =
64+
mapOf(
65+
"sub" to "\${clientId}",
66+
"aud" to listOf("defaultAud"),
6867
),
6968
),
69+
),
7070
tokenExpiry = 120,
7171
)
7272

@@ -124,6 +124,31 @@ internal class OAuth2TokenCallbackTest {
124124
issuer1.typeHeader(grantTypeShouldMatch) shouldBe "JWT"
125125
}
126126
}
127+
128+
@Test
129+
fun `token request with custom parameters in token request should include claims with placeholder names`() {
130+
val request = clientCredentialsRequest(
131+
"scope" to "testscope:something",
132+
"mock_token_type" to "custom",
133+
)
134+
RequestMappingTokenCallback(
135+
issuerId = "issuer1",
136+
requestMappings =
137+
listOf(
138+
RequestMapping(
139+
requestParam = "scope",
140+
match = "testscope:.*",
141+
claims = mapOf(
142+
"sub" to "\${clientId}",
143+
"scope" to "\${scope}",
144+
"mock_token_type" to "\${mock_token_type}",
145+
),
146+
),
147+
)
148+
).addClaims(request).asClue {
149+
it shouldContainAll mapOf("sub" to clientId, "scope" to "testscope:something", "mock_token_type" to "custom")
150+
}
151+
}
127152
}
128153

129154
@Nested

0 commit comments

Comments
 (0)