|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.web.server |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test |
| 20 | +import org.junit.jupiter.api.extension.ExtendWith |
| 21 | +import reactor.core.publisher.Mono |
| 22 | + |
| 23 | +import org.springframework.beans.factory.annotation.Autowired |
| 24 | +import org.springframework.context.annotation.Bean |
| 25 | +import org.springframework.context.annotation.Configuration |
| 26 | +import org.springframework.context.annotation.Import |
| 27 | +import org.springframework.context.ApplicationContext |
| 28 | +import org.springframework.http.MediaType |
| 29 | +import org.springframework.security.authentication.ott.OneTimeToken |
| 30 | +import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity |
| 31 | +import org.springframework.security.config.test.SpringTestContext |
| 32 | +import org.springframework.security.config.test.SpringTestContextExtension |
| 33 | +import org.springframework.security.core.userdetails.MapReactiveUserDetailsService |
| 34 | +import org.springframework.security.core.userdetails.ReactiveUserDetailsService |
| 35 | +import org.springframework.security.core.userdetails.User |
| 36 | +import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers |
| 37 | +import org.springframework.security.web.server.SecurityWebFilterChain |
| 38 | +import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler |
| 39 | +import org.springframework.security.web.server.authentication.ott.ServerOneTimeTokenGenerationSuccessHandler |
| 40 | +import org.springframework.security.web.server.authentication.ott.ServerRedirectOneTimeTokenGenerationSuccessHandler |
| 41 | +import org.springframework.test.web.reactive.server.WebTestClient |
| 42 | +import org.springframework.web.reactive.config.EnableWebFlux |
| 43 | +import org.springframework.web.reactive.function.BodyInserters |
| 44 | +import org.springframework.web.server.ServerWebExchange |
| 45 | +import org.springframework.web.util.UriBuilder |
| 46 | + |
| 47 | +/** |
| 48 | + * Tests for [ServerOneTimeTokenLoginDsl] |
| 49 | + * |
| 50 | + * @author Max Batischev |
| 51 | + */ |
| 52 | +@ExtendWith(SpringTestContextExtension::class) |
| 53 | +class ServerOneTimeTokenLoginDslTests { |
| 54 | + @JvmField |
| 55 | + val spring = SpringTestContext(this) |
| 56 | + |
| 57 | + private lateinit var client: WebTestClient |
| 58 | + |
| 59 | + @Autowired |
| 60 | + fun setup(context: ApplicationContext) { |
| 61 | + this.client = WebTestClient |
| 62 | + .bindToApplicationContext(context) |
| 63 | + .configureClient() |
| 64 | + .build() |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `oneTimeToken when correct token then can authenticate`() { |
| 69 | + spring.register(OneTimeTokenConfig::class.java).autowire() |
| 70 | + |
| 71 | + // @formatter:off |
| 72 | + client.mutateWith(SecurityMockServerConfigurers.csrf()) |
| 73 | + .post() |
| 74 | + .uri{ uriBuilder: UriBuilder -> uriBuilder |
| 75 | + .path("/ott/generate") |
| 76 | + .build() |
| 77 | + } |
| 78 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 79 | + .body(BodyInserters.fromFormData("username", "user")) |
| 80 | + .exchange() |
| 81 | + .expectStatus() |
| 82 | + .is3xxRedirection() |
| 83 | + .expectHeader().valueEquals("Location", "/login/ott") |
| 84 | + |
| 85 | + client.mutateWith(SecurityMockServerConfigurers.csrf()) |
| 86 | + .post() |
| 87 | + .uri{ uriBuilder:UriBuilder -> uriBuilder |
| 88 | + .path("/ott/generate") |
| 89 | + .build() |
| 90 | + } |
| 91 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 92 | + .body(BodyInserters.fromFormData("username", "user")) |
| 93 | + .exchange() |
| 94 | + .expectStatus() |
| 95 | + .is3xxRedirection() |
| 96 | + .expectHeader().valueEquals("Location", "/login/ott") |
| 97 | + |
| 98 | + val token = TestServerOneTimeTokenGenerationSuccessHandler.lastToken?.tokenValue |
| 99 | + |
| 100 | + client.mutateWith(SecurityMockServerConfigurers.csrf()) |
| 101 | + .post() |
| 102 | + .uri{ uriBuilder:UriBuilder -> uriBuilder |
| 103 | + .path("/login/ott") |
| 104 | + .queryParam("token", token) |
| 105 | + .build() |
| 106 | + } |
| 107 | + .exchange() |
| 108 | + .expectStatus() |
| 109 | + .is3xxRedirection() |
| 110 | + .expectHeader().valueEquals("Location", "/") |
| 111 | + // @formatter:on |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun `oneTimeToken when different authentication urls then can authenticate`() { |
| 116 | + spring.register(OneTimeTokenDifferentUrlsConfig::class.java).autowire() |
| 117 | + |
| 118 | + // @formatter:off |
| 119 | + client.mutateWith(SecurityMockServerConfigurers.csrf()) |
| 120 | + .post() |
| 121 | + .uri{ uriBuilder: UriBuilder -> uriBuilder |
| 122 | + .path("/generateurl") |
| 123 | + .build() |
| 124 | + } |
| 125 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 126 | + .body(BodyInserters.fromFormData("username", "user")) |
| 127 | + .exchange() |
| 128 | + .expectStatus() |
| 129 | + .is3xxRedirection() |
| 130 | + .expectHeader().valueEquals("Location", "/redirected") |
| 131 | + |
| 132 | + val token = TestServerOneTimeTokenGenerationSuccessHandler.lastToken?.tokenValue |
| 133 | + |
| 134 | + client.mutateWith(SecurityMockServerConfigurers.csrf()) |
| 135 | + .post() |
| 136 | + .uri{ uriBuilder: UriBuilder -> uriBuilder |
| 137 | + .path("/loginprocessingurl") |
| 138 | + .build() |
| 139 | + } |
| 140 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 141 | + .body(BodyInserters.fromFormData("token", token!!)) |
| 142 | + .exchange() |
| 143 | + .expectStatus() |
| 144 | + .is3xxRedirection() |
| 145 | + .expectHeader().valueEquals("Location", "/authenticated") |
| 146 | + // @formatter:on |
| 147 | + } |
| 148 | + |
| 149 | + @Configuration |
| 150 | + @EnableWebFlux |
| 151 | + @EnableWebFluxSecurity |
| 152 | + @Import(UserDetailsServiceConfig::class) |
| 153 | + open class OneTimeTokenConfig { |
| 154 | + |
| 155 | + @Bean |
| 156 | + open fun springWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { |
| 157 | + // @formatter:off |
| 158 | + return http { |
| 159 | + authorizeExchange { |
| 160 | + authorize(anyExchange, authenticated) |
| 161 | + } |
| 162 | + oneTimeTokenLogin { |
| 163 | + tokenGenerationSuccessHandler = TestServerOneTimeTokenGenerationSuccessHandler() |
| 164 | + } |
| 165 | + } |
| 166 | + // @formatter:on |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Configuration |
| 171 | + @EnableWebFlux |
| 172 | + @EnableWebFluxSecurity |
| 173 | + @Import(UserDetailsServiceConfig::class) |
| 174 | + open class OneTimeTokenDifferentUrlsConfig { |
| 175 | + |
| 176 | + @Bean |
| 177 | + open fun springWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { |
| 178 | + // @formatter:off |
| 179 | + return http { |
| 180 | + authorizeExchange { |
| 181 | + authorize(anyExchange, authenticated) |
| 182 | + } |
| 183 | + oneTimeTokenLogin { |
| 184 | + tokenGeneratingUrl = "/generateurl" |
| 185 | + tokenGenerationSuccessHandler = TestServerOneTimeTokenGenerationSuccessHandler("/redirected") |
| 186 | + loginProcessingUrl = "/loginprocessingurl" |
| 187 | + authenticationSuccessHandler = RedirectServerAuthenticationSuccessHandler("/authenticated") |
| 188 | + } |
| 189 | + } |
| 190 | + // @formatter:on |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @Configuration(proxyBeanMethods = false) |
| 195 | + open class UserDetailsServiceConfig { |
| 196 | + |
| 197 | + @Bean |
| 198 | + open fun userDetailsService(): ReactiveUserDetailsService = |
| 199 | + MapReactiveUserDetailsService(User("user", "password", listOf())) |
| 200 | + } |
| 201 | + |
| 202 | + private class TestServerOneTimeTokenGenerationSuccessHandler: ServerOneTimeTokenGenerationSuccessHandler { |
| 203 | + private var delegate: ServerRedirectOneTimeTokenGenerationSuccessHandler? = null |
| 204 | + |
| 205 | + companion object { |
| 206 | + var lastToken: OneTimeToken? = null |
| 207 | + } |
| 208 | + |
| 209 | + constructor() { |
| 210 | + this.delegate = ServerRedirectOneTimeTokenGenerationSuccessHandler("/login/ott") |
| 211 | + } |
| 212 | + |
| 213 | + constructor(redirectUrl: String?) { |
| 214 | + this.delegate = ServerRedirectOneTimeTokenGenerationSuccessHandler(redirectUrl) |
| 215 | + } |
| 216 | + |
| 217 | + override fun handle(exchange: ServerWebExchange?, oneTimeToken: OneTimeToken?): Mono<Void> { |
| 218 | + lastToken = oneTimeToken |
| 219 | + return delegate!!.handle(exchange, oneTimeToken) |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments