|
25 | 25 | import org.springframework.mock.web.MockFilterChain;
|
26 | 26 | import org.springframework.mock.web.MockHttpServletRequest;
|
27 | 27 | import org.springframework.mock.web.MockHttpServletResponse;
|
| 28 | +import org.springframework.security.authentication.AuthenticationDetailsSource; |
28 | 29 | import org.springframework.security.authentication.AuthenticationManager;
|
29 | 30 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
30 | 31 | import org.springframework.security.core.Authentication;
|
31 | 32 | import org.springframework.security.saml2.core.Saml2ParameterNames;
|
32 | 33 | import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
|
33 | 34 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
|
| 35 | +import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken; |
34 | 36 | import org.springframework.security.saml2.provider.service.authentication.TestSaml2AuthenticationTokens;
|
35 | 37 | import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
|
36 | 38 | import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
|
|
40 | 42 | import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver;
|
41 | 43 | import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter;
|
42 | 44 | import org.springframework.security.web.authentication.AuthenticationConverter;
|
| 45 | +import org.springframework.security.web.authentication.WebAuthenticationDetails; |
43 | 46 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
44 | 47 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
45 | 48 |
|
46 | 49 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
47 | 50 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 51 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
48 | 52 | import static org.mockito.BDDMockito.given;
|
49 | 53 | import static org.mockito.Mockito.mock;
|
50 | 54 | import static org.mockito.Mockito.verify;
|
@@ -119,6 +123,36 @@ public void attemptAuthenticationWhenSavedAuthnRequestThenRemovesAuthnRequest()
|
119 | 123 | verify(authenticationRequestRepository).removeAuthenticationRequest(this.request, this.response);
|
120 | 124 | }
|
121 | 125 |
|
| 126 | + @Test |
| 127 | + public void attemptAuthenticationAddsDetails() { |
| 128 | + AuthenticationConverter authenticationConverter = mock(AuthenticationConverter.class); |
| 129 | + final Saml2AuthenticationToken token = TestSaml2AuthenticationTokens.token(); |
| 130 | + given(authenticationConverter.convert(this.request)).willReturn(token); |
| 131 | + final AuthenticationDetailsSource authenticationDetailsSource = mock(AuthenticationDetailsSource.class); |
| 132 | + final WebAuthenticationDetails details = mock(WebAuthenticationDetails.class); |
| 133 | + given(authenticationDetailsSource.buildDetails(this.request)).willReturn(details); |
| 134 | + this.filter = new Saml2WebSsoAuthenticationFilter(authenticationConverter, "/some/other/path/{registrationId}"); |
| 135 | + this.filter.setAuthenticationManager((authentication) -> null); |
| 136 | + this.filter.setAuthenticationDetailsSource(authenticationDetailsSource); |
| 137 | + this.request.setPathInfo("/some/other/path/idp-registration-id"); |
| 138 | + this.filter.attemptAuthentication(this.request, this.response); |
| 139 | + Assertions.assertEquals(details, token.getDetails()); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void attemptAuthenticationWhenAuthenticationNotAbstractAuthenticationTokenDoesNotAddDetails() { |
| 144 | + AuthenticationConverter authenticationConverter = mock(AuthenticationConverter.class); |
| 145 | + final Authentication authenticationWithoutDetails = mock(Authentication.class); |
| 146 | + given(authenticationConverter.convert(this.request)).willReturn(authenticationWithoutDetails); |
| 147 | + final AuthenticationDetailsSource authenticationDetailsSource = mock(AuthenticationDetailsSource.class); |
| 148 | + this.filter = new Saml2WebSsoAuthenticationFilter(authenticationConverter, "/some/other/path/{registrationId}"); |
| 149 | + this.filter.setAuthenticationManager((authentication) -> null); |
| 150 | + this.filter.setAuthenticationDetailsSource(authenticationDetailsSource); |
| 151 | + this.request.setPathInfo("/some/other/path/idp-registration-id"); |
| 152 | + assertThatNoException().isThrownBy(() -> this.filter.attemptAuthentication(this.request, this.response)); |
| 153 | + verifyNoInteractions(authenticationDetailsSource); |
| 154 | + } |
| 155 | + |
122 | 156 | @Test
|
123 | 157 | public void setAuthenticationRequestRepositoryWhenNullThenThrowsIllegalArgument() {
|
124 | 158 | assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setAuthenticationRequestRepository(null))
|
|
0 commit comments