|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | +package sample; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.mock.web.MockFilterChain; |
| 21 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 22 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 23 | +import org.springframework.mock.web.MockServletContext; |
| 24 | +import org.springframework.security.authentication.AuthenticationManager; |
| 25 | +import org.springframework.security.authentication.BadCredentialsException; |
| 26 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 27 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +import static java.net.URI.create; |
| 31 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 32 | +import static java.util.Base64.getEncoder; |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 35 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 36 | + |
| 37 | +public class ClientCredentialsAuthenticationFilterTests { |
| 38 | + private static final String CLIENT_ID = "myclientid"; |
| 39 | + private static final String CLIENT_SECRET = "myclientsecret"; |
| 40 | + private final AuthenticationManager authenticationManager = authentication -> { |
| 41 | + Assert.isInstanceOf(OAuth2ClientAuthenticationToken.class, authentication); |
| 42 | + OAuth2ClientAuthenticationToken token = (OAuth2ClientAuthenticationToken) authentication; |
| 43 | + if (CLIENT_ID.equals(token.getPrincipal()) && CLIENT_SECRET.equals(token.getCredentials())) { |
| 44 | + authentication.setAuthenticated(true); |
| 45 | + return authentication; |
| 46 | + } |
| 47 | + throw new BadCredentialsException("Bad credentials"); |
| 48 | + }; |
| 49 | + private final ClientCredentialsAuthenticationFilter filter = new ClientCredentialsAuthenticationFilter(this.authenticationManager); |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + public void setup() { |
| 53 | + SecurityContextHolder.clearContext(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void doFilterWhenUrlDoesNotMatchThenDontAuthenticate() throws Exception { |
| 58 | + MockHttpServletRequest request = post(create("/someotherendpoint")).buildRequest(new MockServletContext()); |
| 59 | + request.addHeader("Authorization", basicAuthHeader(CLIENT_ID, CLIENT_SECRET)); |
| 60 | + |
| 61 | + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); |
| 62 | + |
| 63 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void doFilterWhenRequestMatchesThenAuthenticate() throws Exception { |
| 68 | + MockHttpServletRequest request = post(create("/oauth2/token")).buildRequest(new MockServletContext()); |
| 69 | + request.addHeader("Authorization", basicAuthHeader(CLIENT_ID, CLIENT_SECRET)); |
| 70 | + |
| 71 | + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain()); |
| 72 | + |
| 73 | + assertThat(SecurityContextHolder.getContext().getAuthentication().isAuthenticated()).isTrue(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void doFilterWhenBasicAuthenticationHeaderIsMissingThenThrowBadCredentialsException() { |
| 78 | + MockHttpServletRequest request = post(create("/oauth2/token")).buildRequest(new MockServletContext()); |
| 79 | + assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> |
| 80 | + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain())); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void doFilterWhenBasicAuthenticationHeaderHasInvalidSyntaxThenThrowBadCredentialsException() { |
| 85 | + MockHttpServletRequest request = post(create("/oauth2/token")).buildRequest(new MockServletContext()); |
| 86 | + request.addHeader("Authorization", "Basic invalid"); |
| 87 | + |
| 88 | + assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> |
| 89 | + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain())); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void doFilterWhenBasicAuthenticationProvidesIncorrectSecretThenThrowBadCredentialsException() { |
| 94 | + MockHttpServletRequest request = post(create("/oauth2/token")).buildRequest(new MockServletContext()); |
| 95 | + request.addHeader("Authorization", basicAuthHeader(CLIENT_ID, "incorrectsecret")); |
| 96 | + |
| 97 | + assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> |
| 98 | + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain())); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void doFilterWhenBasicAuthenticationProvidesIncorrectClientIdThenThrowBadCredentialsException() { |
| 103 | + MockHttpServletRequest request = post(create("/oauth2/token")).buildRequest(new MockServletContext()); |
| 104 | + request.addHeader("Authorization", basicAuthHeader("anotherclientid", CLIENT_SECRET)); |
| 105 | + |
| 106 | + assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> |
| 107 | + filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain())); |
| 108 | + } |
| 109 | + |
| 110 | + private static String basicAuthHeader(String clientId, String clientSecret) { |
| 111 | + return "Basic " + getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes(UTF_8)); |
| 112 | + } |
| 113 | +} |
0 commit comments