|
| 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 org.springframework.security.oauth2.server.authorization.web; |
| 17 | + |
| 18 | +import com.nimbusds.jose.JWSAlgorithm; |
| 19 | +import com.nimbusds.jose.jwk.ECKey; |
| 20 | +import com.nimbusds.jose.jwk.JWKSet; |
| 21 | +import com.nimbusds.jose.jwk.KeyUse; |
| 22 | +import com.nimbusds.jose.jwk.RSAKey; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.springframework.http.MediaType; |
| 26 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 27 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 28 | +import org.springframework.security.crypto.keys.KeyManager; |
| 29 | +import org.springframework.security.crypto.keys.ManagedKey; |
| 30 | +import org.springframework.security.crypto.keys.TestManagedKeys; |
| 31 | + |
| 32 | +import javax.servlet.FilterChain; |
| 33 | +import javax.servlet.http.HttpServletRequest; |
| 34 | +import javax.servlet.http.HttpServletResponse; |
| 35 | +import java.time.Instant; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.stream.Collectors; |
| 39 | +import java.util.stream.Stream; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 43 | +import static org.mockito.ArgumentMatchers.any; |
| 44 | +import static org.mockito.Mockito.mock; |
| 45 | +import static org.mockito.Mockito.verify; |
| 46 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 47 | +import static org.mockito.Mockito.when; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests for {@link JwkSetEndpointFilter}. |
| 51 | + * |
| 52 | + * @author Joe Grandja |
| 53 | + */ |
| 54 | +public class JwkSetEndpointFilterTests { |
| 55 | + private KeyManager keyManager; |
| 56 | + private JwkSetEndpointFilter filter; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setUp() { |
| 60 | + this.keyManager = mock(KeyManager.class); |
| 61 | + this.filter = new JwkSetEndpointFilter(this.keyManager); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void constructorWhenKeyManagerNullThenThrowIllegalArgumentException() { |
| 66 | + assertThatThrownBy(() -> new JwkSetEndpointFilter(null)) |
| 67 | + .isInstanceOf(IllegalArgumentException.class) |
| 68 | + .hasMessage("keyManager cannot be null"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void constructorWhenJwkSetEndpointUriNullThenThrowIllegalArgumentException() { |
| 73 | + assertThatThrownBy(() -> new JwkSetEndpointFilter(this.keyManager, null)) |
| 74 | + .isInstanceOf(IllegalArgumentException.class) |
| 75 | + .hasMessage("jwkSetEndpointUri cannot be empty"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void doFilterWhenNotJwkSetRequestThenNotProcessed() throws Exception { |
| 80 | + String requestUri = "/path"; |
| 81 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 82 | + request.setServletPath(requestUri); |
| 83 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 84 | + FilterChain filterChain = mock(FilterChain.class); |
| 85 | + |
| 86 | + this.filter.doFilter(request, response, filterChain); |
| 87 | + |
| 88 | + verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void doFilterWhenJwkSetRequestPostThenNotProcessed() throws Exception { |
| 93 | + String requestUri = JwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI; |
| 94 | + MockHttpServletRequest request = new MockHttpServletRequest("POST", requestUri); |
| 95 | + request.setServletPath(requestUri); |
| 96 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 97 | + FilterChain filterChain = mock(FilterChain.class); |
| 98 | + |
| 99 | + this.filter.doFilter(request, response, filterChain); |
| 100 | + |
| 101 | + verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void doFilterWhenAsymmetricKeysThenJwkSetResponse() throws Exception { |
| 106 | + ManagedKey rsaManagedKey = TestManagedKeys.rsaManagedKey().build(); |
| 107 | + ManagedKey ecManagedKey = TestManagedKeys.ecManagedKey().build(); |
| 108 | + when(this.keyManager.getKeys()).thenReturn( |
| 109 | + Stream.of(rsaManagedKey, ecManagedKey).collect(Collectors.toSet())); |
| 110 | + |
| 111 | + String requestUri = JwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI; |
| 112 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 113 | + request.setServletPath(requestUri); |
| 114 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 115 | + FilterChain filterChain = mock(FilterChain.class); |
| 116 | + |
| 117 | + this.filter.doFilter(request, response, filterChain); |
| 118 | + |
| 119 | + verifyNoInteractions(filterChain); |
| 120 | + |
| 121 | + assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); |
| 122 | + |
| 123 | + JWKSet jwkSet = JWKSet.parse(response.getContentAsString()); |
| 124 | + assertThat(jwkSet.getKeys()).hasSize(2); |
| 125 | + |
| 126 | + RSAKey rsaJwk = (RSAKey) jwkSet.getKeyByKeyId(rsaManagedKey.getKeyId()); |
| 127 | + assertThat(rsaJwk).isNotNull(); |
| 128 | + assertThat(rsaJwk.toRSAPublicKey()).isEqualTo(rsaManagedKey.getPublicKey()); |
| 129 | + assertThat(rsaJwk.toRSAPrivateKey()).isNull(); |
| 130 | + assertThat(rsaJwk.getKeyUse()).isEqualTo(KeyUse.SIGNATURE); |
| 131 | + assertThat(rsaJwk.getAlgorithm()).isEqualTo(JWSAlgorithm.RS256); |
| 132 | + |
| 133 | + ECKey ecJwk = (ECKey) jwkSet.getKeyByKeyId(ecManagedKey.getKeyId()); |
| 134 | + assertThat(ecJwk).isNotNull(); |
| 135 | + assertThat(ecJwk.toECPublicKey()).isEqualTo(ecManagedKey.getPublicKey()); |
| 136 | + assertThat(ecJwk.toECPublicKey()).isEqualTo(ecManagedKey.getPublicKey()); |
| 137 | + assertThat(ecJwk.toECPrivateKey()).isNull(); |
| 138 | + assertThat(ecJwk.getKeyUse()).isEqualTo(KeyUse.SIGNATURE); |
| 139 | + assertThat(ecJwk.getAlgorithm()).isEqualTo(JWSAlgorithm.ES256); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void doFilterWhenSymmetricKeysThenJwkSetResponseEmpty() throws Exception { |
| 144 | + ManagedKey secretManagedKey = TestManagedKeys.secretManagedKey().build(); |
| 145 | + when(this.keyManager.getKeys()).thenReturn( |
| 146 | + new HashSet<>(Collections.singleton(secretManagedKey))); |
| 147 | + |
| 148 | + String requestUri = JwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI; |
| 149 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 150 | + request.setServletPath(requestUri); |
| 151 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 152 | + FilterChain filterChain = mock(FilterChain.class); |
| 153 | + |
| 154 | + this.filter.doFilter(request, response, filterChain); |
| 155 | + |
| 156 | + verifyNoInteractions(filterChain); |
| 157 | + |
| 158 | + assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); |
| 159 | + |
| 160 | + JWKSet jwkSet = JWKSet.parse(response.getContentAsString()); |
| 161 | + assertThat(jwkSet.getKeys()).isEmpty(); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void doFilterWhenNoActiveKeysThenJwkSetResponseEmpty() throws Exception { |
| 166 | + ManagedKey rsaManagedKey = TestManagedKeys.rsaManagedKey().deactivatedOn(Instant.now()).build(); |
| 167 | + ManagedKey ecManagedKey = TestManagedKeys.ecManagedKey().deactivatedOn(Instant.now()).build(); |
| 168 | + when(this.keyManager.getKeys()).thenReturn( |
| 169 | + Stream.of(rsaManagedKey, ecManagedKey).collect(Collectors.toSet())); |
| 170 | + |
| 171 | + String requestUri = JwkSetEndpointFilter.DEFAULT_JWK_SET_ENDPOINT_URI; |
| 172 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
| 173 | + request.setServletPath(requestUri); |
| 174 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 175 | + FilterChain filterChain = mock(FilterChain.class); |
| 176 | + |
| 177 | + this.filter.doFilter(request, response, filterChain); |
| 178 | + |
| 179 | + verifyNoInteractions(filterChain); |
| 180 | + |
| 181 | + assertThat(response.getContentType()).isEqualTo(MediaType.APPLICATION_JSON_VALUE); |
| 182 | + |
| 183 | + JWKSet jwkSet = JWKSet.parse(response.getContentAsString()); |
| 184 | + assertThat(jwkSet.getKeys()).isEmpty(); |
| 185 | + } |
| 186 | +} |
0 commit comments