Skip to content

Commit 8c0b754

Browse files
committed
Fix credentials precedence over introspector in Kotlin
Fixes: gh-7878
1 parent 1fed688 commit 8c0b754

File tree

2 files changed

+53
-3
lines changed
  • config/src
    • main/kotlin/org/springframework/security/config/web/servlet/oauth2/resourceserver
    • test/kotlin/org/springframework/security/config/web/servlet/oauth2/resourceserver

2 files changed

+53
-3
lines changed

config/src/main/kotlin/org/springframework/security/config/web/servlet/oauth2/resourceserver/OpaqueTokenDsl.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,25 @@ import org.springframework.security.oauth2.server.resource.introspection.OpaqueT
2929
* @property introspector the [OpaqueTokenIntrospector] to use.
3030
*/
3131
class OpaqueTokenDsl {
32-
var introspectionUri: String? = null
33-
var introspector: OpaqueTokenIntrospector? = null
34-
32+
private var _introspectionUri: String? = null
33+
private var _introspector: OpaqueTokenIntrospector? = null
3534
private var clientCredentials: Pair<String, String>? = null
3635

36+
var introspectionUri: String?
37+
get() = _introspectionUri
38+
set(value) {
39+
_introspectionUri = value
40+
_introspector = null
41+
}
42+
var introspector: OpaqueTokenIntrospector?
43+
get() = _introspector
44+
set(value) {
45+
_introspector = value
46+
_introspectionUri = null
47+
clientCredentials = null
48+
}
49+
50+
3751
/**
3852
* Configures the credentials for Introspection endpoint.
3953
*
@@ -42,6 +56,7 @@ class OpaqueTokenDsl {
4256
*/
4357
fun introspectionClientCredentials(clientId: String, clientSecret: String) {
4458
clientCredentials = Pair(clientId, clientSecret)
59+
_introspector = null
4560
}
4661

4762
internal fun get(): (OAuth2ResourceServerConfigurer<HttpSecurity>.OpaqueTokenConfigurer) -> Unit {

config/src/test/kotlin/org/springframework/security/config/web/servlet/oauth2/resourceserver/OpaqueTokenDslTests.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,41 @@ class OpaqueTokenDslTests {
138138
}
139139
}
140140

141+
@Test
142+
fun `opaque token when custom introspector set after client credentials then introspector used`() {
143+
this.spring.register(IntrospectorAfterClientCredentialsConfig::class.java, AuthenticationController::class.java).autowire()
144+
`when`(IntrospectorAfterClientCredentialsConfig.INTROSPECTOR.introspect(ArgumentMatchers.anyString()))
145+
.thenReturn(DefaultOAuth2AuthenticatedPrincipal(mapOf(Pair(JwtClaimNames.SUB, "mock-subject")), emptyList()))
146+
147+
this.mockMvc.get("/authenticated") {
148+
header("Authorization", "Bearer token")
149+
}
150+
151+
verify(IntrospectorAfterClientCredentialsConfig.INTROSPECTOR).introspect("token")
152+
}
153+
154+
@EnableWebSecurity
155+
open class IntrospectorAfterClientCredentialsConfig : WebSecurityConfigurerAdapter() {
156+
companion object {
157+
var INTROSPECTOR: OpaqueTokenIntrospector = mock(OpaqueTokenIntrospector::class.java)
158+
}
159+
160+
override fun configure(http: HttpSecurity) {
161+
http {
162+
authorizeRequests {
163+
authorize(anyRequest, authenticated)
164+
}
165+
oauth2ResourceServer {
166+
opaqueToken {
167+
introspectionUri = "/introspect"
168+
introspectionClientCredentials("clientId", "clientSecret")
169+
introspector = INTROSPECTOR
170+
}
171+
}
172+
}
173+
}
174+
}
175+
141176
@RestController
142177
class AuthenticationController {
143178
@GetMapping("/authenticated")

0 commit comments

Comments
 (0)