| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2002-2025 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.web.authentication.ott;  | 
 | 18 | + | 
 | 19 | +import java.io.IOException;  | 
 | 20 | + | 
 | 21 | +import jakarta.servlet.FilterChain;  | 
 | 22 | +import jakarta.servlet.ServletException;  | 
 | 23 | +import jakarta.servlet.http.HttpServletResponse;  | 
 | 24 | +import org.junit.jupiter.api.BeforeEach;  | 
 | 25 | +import org.junit.jupiter.api.Test;  | 
 | 26 | +import org.junit.jupiter.api.extension.ExtendWith;  | 
 | 27 | +import org.mockito.Mock;  | 
 | 28 | +import org.mockito.junit.jupiter.MockitoExtension;  | 
 | 29 | + | 
 | 30 | +import org.springframework.http.HttpStatus;  | 
 | 31 | +import org.springframework.mock.web.MockHttpServletResponse;  | 
 | 32 | +import org.springframework.security.authentication.AuthenticationManager;  | 
 | 33 | +import org.springframework.security.authentication.BadCredentialsException;  | 
 | 34 | +import org.springframework.security.authentication.ott.OneTimeTokenAuthenticationToken;  | 
 | 35 | +import org.springframework.security.core.authority.AuthorityUtils;  | 
 | 36 | +import org.springframework.security.web.servlet.MockServletContext;  | 
 | 37 | + | 
 | 38 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 39 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;  | 
 | 40 | +import static org.mockito.ArgumentMatchers.any;  | 
 | 41 | +import static org.mockito.BDDMockito.given;  | 
 | 42 | +import static org.mockito.Mockito.mock;  | 
 | 43 | +import static org.mockito.Mockito.verify;  | 
 | 44 | +import static org.mockito.Mockito.verifyNoInteractions;  | 
 | 45 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;  | 
 | 46 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;  | 
 | 47 | + | 
 | 48 | +/**  | 
 | 49 | + * Tests for {@link OneTimeTokenAuthenticationFilter}.  | 
 | 50 | + *  | 
 | 51 | + * @author Daniel Garnier-Moiroux  | 
 | 52 | + * @since 6.5  | 
 | 53 | + */  | 
 | 54 | +@ExtendWith(MockitoExtension.class)  | 
 | 55 | +class OneTimeTokenAuthenticationFilterTests {  | 
 | 56 | + | 
 | 57 | +	@Mock  | 
 | 58 | +	private FilterChain chain;  | 
 | 59 | + | 
 | 60 | +	@Mock  | 
 | 61 | +	private AuthenticationManager authenticationManager;  | 
 | 62 | + | 
 | 63 | +	private final OneTimeTokenAuthenticationFilter filter = new OneTimeTokenAuthenticationFilter();  | 
 | 64 | + | 
 | 65 | +	private final HttpServletResponse response = new MockHttpServletResponse();  | 
 | 66 | + | 
 | 67 | +	@BeforeEach  | 
 | 68 | +	void setUp() {  | 
 | 69 | +		this.filter.setAuthenticationManager(this.authenticationManager);  | 
 | 70 | +	}  | 
 | 71 | + | 
 | 72 | +	@Test  | 
 | 73 | +	void setAuthenticationConverterWhenNullThenIllegalArgumentException() {  | 
 | 74 | +		assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setAuthenticationConverter(null));  | 
 | 75 | +	}  | 
 | 76 | + | 
 | 77 | +	@Test  | 
 | 78 | +	void doFilterWhenUrlDoesNotMatchThenContinues() throws ServletException, IOException {  | 
 | 79 | +		OneTimeTokenAuthenticationConverter converter = mock(OneTimeTokenAuthenticationConverter.class);  | 
 | 80 | +		HttpServletResponse response = mock(HttpServletResponse.class);  | 
 | 81 | +		this.filter.setAuthenticationConverter(converter);  | 
 | 82 | +		this.filter.doFilter(post("/nomatch").buildRequest(new MockServletContext()), response, this.chain);  | 
 | 83 | +		verifyNoInteractions(converter, response);  | 
 | 84 | +		verify(this.chain).doFilter(any(), any());  | 
 | 85 | +	}  | 
 | 86 | + | 
 | 87 | +	@Test  | 
 | 88 | +	void doFilterWhenMethodDoesNotMatchThenContinues() throws ServletException, IOException {  | 
 | 89 | +		OneTimeTokenAuthenticationConverter converter = mock(OneTimeTokenAuthenticationConverter.class);  | 
 | 90 | +		HttpServletResponse response = mock(HttpServletResponse.class);  | 
 | 91 | +		this.filter.setAuthenticationConverter(converter);  | 
 | 92 | +		this.filter.doFilter(get("/login/ott").buildRequest(new MockServletContext()), response, this.chain);  | 
 | 93 | +		verifyNoInteractions(converter, response);  | 
 | 94 | +		verify(this.chain).doFilter(any(), any());  | 
 | 95 | +	}  | 
 | 96 | + | 
 | 97 | +	@Test  | 
 | 98 | +	void doFilterWhenMissingTokenThenUnauthorized() throws ServletException, IOException {  | 
 | 99 | +		this.filter.doFilter(post("/login/ott").buildRequest(new MockServletContext()), this.response, this.chain);  | 
 | 100 | +		assertThat(this.response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());  | 
 | 101 | +		verifyNoInteractions(this.chain);  | 
 | 102 | +	}  | 
 | 103 | + | 
 | 104 | +	@Test  | 
 | 105 | +	void doFilterWhenInvalidTokenThenUnauthorized() throws ServletException, IOException {  | 
 | 106 | +		given(this.authenticationManager.authenticate(any())).willThrow(new BadCredentialsException("invalid token"));  | 
 | 107 | +		this.filter.doFilter(  | 
 | 108 | +				post("/login/ott").param("token", "some-token-value").buildRequest(new MockServletContext()),  | 
 | 109 | +				this.response, this.chain);  | 
 | 110 | +		assertThat(this.response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());  | 
 | 111 | +		verifyNoInteractions(this.chain);  | 
 | 112 | +	}  | 
 | 113 | + | 
 | 114 | +	@Test  | 
 | 115 | +	void doFilterWhenValidThenRedirectsToSavedRequest() throws ServletException, IOException {  | 
 | 116 | +		given(this.authenticationManager.authenticate(any()))  | 
 | 117 | +			.willReturn(OneTimeTokenAuthenticationToken.authenticated("username", AuthorityUtils.NO_AUTHORITIES));  | 
 | 118 | +		this.filter.doFilter(  | 
 | 119 | +				post("/login/ott").param("token", "some-token-value").buildRequest(new MockServletContext()),  | 
 | 120 | +				this.response, this.chain);  | 
 | 121 | +		assertThat(this.response.getStatus()).isEqualTo(HttpStatus.FOUND.value());  | 
 | 122 | +		assertThat(this.response.getHeader("location")).endsWith("/");  | 
 | 123 | +	}  | 
 | 124 | + | 
 | 125 | +}  | 
0 commit comments