-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support objects and lists in request mapping claims (#699)
* fixes #683 and #674 Co-authored-by: ybelmekk <[email protected]>
- Loading branch information
1 parent
f662d5f
commit 4aab3a5
Showing
5 changed files
with
136 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/no/nav/security/mock/oauth2/extensions/Template.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package no.nav.security.mock.oauth2.extensions | ||
|
||
/** | ||
* Replaces all template values denoted with ${key} in a map with the corresponding values from the templates map. | ||
* | ||
* @param templates a map of template values | ||
* @return a new map with all template values replaced | ||
*/ | ||
fun Map<String, Any>.replaceValues(templates: Map<String, Any>): Map<String, Any> { | ||
fun replaceTemplateString( | ||
value: String, | ||
templates: Map<String, Any>, | ||
): String { | ||
val regex = Regex("""\$\{(\w+)\}""") | ||
return regex.replace(value) { matchResult -> | ||
val key = matchResult.groupValues[1] | ||
templates[key]?.toString() ?: matchResult.value | ||
} | ||
} | ||
|
||
fun replaceValue(value: Any): Any { | ||
return when (value) { | ||
is String -> replaceTemplateString(value, templates) | ||
is List<*> -> value.map { it?.let { replaceValue(it) } } | ||
is Map<*, *> -> value.mapValues { v -> v.value?.let { replaceValue(it) } } | ||
else -> value | ||
} | ||
} | ||
|
||
return this.mapValues { replaceValue(it.value) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/kotlin/no/nav/security/mock/oauth2/extensions/TemplateTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package no.nav.security.mock.oauth2.extensions | ||
|
||
import io.kotest.assertions.asClue | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class TemplateTest { | ||
@Test | ||
fun `template values in map should be replaced`() { | ||
val templates = | ||
mapOf( | ||
"templateVal1" to "val1", | ||
"templateVal2" to "val2", | ||
"templateListVal" to "listVal1", | ||
) | ||
|
||
mapOf( | ||
"object1" to mapOf("key1" to "\${templateVal1}"), | ||
"object2" to "\${templateVal2}", | ||
"nestedObject" to mapOf("nestedKey" to mapOf("nestedKeyAgain" to "\${templateVal2}")), | ||
"list1" to listOf("\${templateListVal}"), | ||
).replaceValues(templates).asClue { | ||
it["object1"] shouldBe mapOf("key1" to "val1") | ||
it["list1"] shouldBe listOf("listVal1") | ||
println(it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters