|
|
|
@ -13,31 +13,17 @@ |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* limitations under the License. |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
package sample; |
|
|
|
package sample; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
import com.nimbusds.jose.JOSEException; |
|
|
|
import static org.hamcrest.Matchers.is; |
|
|
|
import com.nimbusds.jose.jwk.JWK; |
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
import com.nimbusds.jose.jwk.JWKSet; |
|
|
|
import static org.mockito.Mockito.never; |
|
|
|
import com.nimbusds.jose.jwk.KeyUse; |
|
|
|
import static org.mockito.Mockito.only; |
|
|
|
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
|
|
|
|
|
|
import static sample.JwkSetEndpointFilter.WELL_KNOWN_JWK_URIS; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.FilterChain; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
|
|
import org.junit.jupiter.api.BeforeAll; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.TestInstance; |
|
|
|
import org.junit.jupiter.api.TestInstance; |
|
|
|
import org.junit.jupiter.api.TestInstance.Lifecycle; |
|
|
|
import org.junit.jupiter.api.TestInstance.Lifecycle; |
|
|
|
import org.mockito.Mockito; |
|
|
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.MockHttpServletResponse; |
|
|
|
import org.springframework.mock.web.MockHttpServletResponse; |
|
|
|
import org.springframework.test.web.servlet.MockMvc; |
|
|
|
import org.springframework.test.web.servlet.MockMvc; |
|
|
|
@ -45,36 +31,48 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
|
|
|
|
|
|
|
import com.nimbusds.jose.JOSEException; |
|
|
|
import javax.servlet.FilterChain; |
|
|
|
import com.nimbusds.jose.jwk.JWK; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import com.nimbusds.jose.jwk.JWKSet; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import com.nimbusds.jose.jwk.KeyUse; |
|
|
|
|
|
|
|
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@TestInstance(Lifecycle.PER_CLASS) |
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
public class JwkSetEndpointFilterTest { |
|
|
|
import static org.hamcrest.Matchers.is; |
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.only; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verifyNoInteractions; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
|
|
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
|
|
|
|
|
|
import static sample.JwkSetEndpointFilter.DEFAULT_JWK_SET_URI; |
|
|
|
|
|
|
|
|
|
|
|
private MockMvc mvc; |
|
|
|
@TestInstance(Lifecycle.PER_CLASS) |
|
|
|
private JWKSet jwkSet; |
|
|
|
public class JwkSetEndpointFilterTests { |
|
|
|
private JWK jwk; |
|
|
|
private JWK jwk; |
|
|
|
|
|
|
|
private JWKSet jwkSet; |
|
|
|
private JwkSetEndpointFilter filter; |
|
|
|
private JwkSetEndpointFilter filter; |
|
|
|
|
|
|
|
private MockMvc mvc; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeAll |
|
|
|
@BeforeAll |
|
|
|
void setup() throws JOSEException { |
|
|
|
void setup() throws JOSEException { |
|
|
|
this.jwk = new RSAKeyGenerator(2048).keyID("endpoint-test").keyUse(KeyUse.SIGNATURE).generate(); |
|
|
|
this.jwk = new RSAKeyGenerator(2048).keyID("endpoint-test").keyUse(KeyUse.SIGNATURE).generate(); |
|
|
|
this.jwkSet = new JWKSet(jwk); |
|
|
|
this.jwkSet = new JWKSet(this.jwk); |
|
|
|
this.filter = new JwkSetEndpointFilter(jwkSet); |
|
|
|
this.filter = new JwkSetEndpointFilter(this.jwkSet); |
|
|
|
this.mvc = MockMvcBuilders.standaloneSetup(new FakeController()).addFilters(filter).alwaysDo(print()).build(); |
|
|
|
this.mvc = MockMvcBuilders.standaloneSetup(new HelloController()).addFilters(this.filter).alwaysDo(print()).build(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void constructorWhenJsonWebKeySetIsNullThrowIllegalArgumentException() { |
|
|
|
void constructorWhenJWKSetNullThenThrowIllegalArgumentException() { |
|
|
|
assertThatThrownBy(() -> new JwkSetEndpointFilter(null)).isInstanceOf(IllegalArgumentException.class); |
|
|
|
assertThatThrownBy(() -> new JwkSetEndpointFilter(null)) |
|
|
|
|
|
|
|
.isInstanceOf(IllegalArgumentException.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void doFilterWhenPathMatches() throws Exception { |
|
|
|
void doFilterWhenRequestMatchesThenProcess() throws Exception { |
|
|
|
String requestUri = WELL_KNOWN_JWK_URIS; |
|
|
|
String requestUri = DEFAULT_JWK_SET_URI; |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
|
|
|
request.setServletPath(requestUri); |
|
|
|
request.setServletPath(requestUri); |
|
|
|
|
|
|
|
|
|
|
|
@ -83,13 +81,12 @@ public class JwkSetEndpointFilterTest { |
|
|
|
|
|
|
|
|
|
|
|
this.filter.doFilter(request, response, filterChain); |
|
|
|
this.filter.doFilter(request, response, filterChain); |
|
|
|
|
|
|
|
|
|
|
|
verify(filterChain, never()).doFilter(Mockito.any(HttpServletRequest.class), |
|
|
|
verifyNoInteractions(filterChain); |
|
|
|
Mockito.any(HttpServletResponse.class)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void doFilterWhenPathDoesNotMatch() throws Exception { |
|
|
|
void doFilterWhenRequestDoesNotMatchThenContinueChain() throws Exception { |
|
|
|
String requestUri = "/stuff/" + WELL_KNOWN_JWK_URIS; |
|
|
|
String requestUri = "/path"; |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); |
|
|
|
request.setServletPath(requestUri); |
|
|
|
request.setServletPath(requestUri); |
|
|
|
|
|
|
|
|
|
|
|
@ -98,30 +95,34 @@ public class JwkSetEndpointFilterTest { |
|
|
|
|
|
|
|
|
|
|
|
this.filter.doFilter(request, response, filterChain); |
|
|
|
this.filter.doFilter(request, response, filterChain); |
|
|
|
|
|
|
|
|
|
|
|
verify(filterChain, only()).doFilter(Mockito.any(HttpServletRequest.class), |
|
|
|
verify(filterChain, only()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); |
|
|
|
Mockito.any(HttpServletResponse.class)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testResponseIfRequestMatches() throws Exception { |
|
|
|
void requestWhenMatchesThenResponseContainsKeys() throws Exception { |
|
|
|
mvc.perform(get(WELL_KNOWN_JWK_URIS)).andDo(print()).andExpect(status().isOk()) |
|
|
|
this.mvc.perform(get(DEFAULT_JWK_SET_URI)) |
|
|
|
.andExpect(jsonPath("$.keys").isArray()).andExpect(jsonPath("$.keys").isNotEmpty()) |
|
|
|
.andDo(print()) |
|
|
|
.andExpect(jsonPath("$.keys[0].kid").value(jwk.getKeyID())) |
|
|
|
.andExpect(status().isOk()) |
|
|
|
.andExpect(jsonPath("$.keys[0].kty").value(jwk.getKeyType().toString())); |
|
|
|
.andExpect(jsonPath("$.keys").isArray()) |
|
|
|
|
|
|
|
.andExpect(jsonPath("$.keys").isNotEmpty()) |
|
|
|
|
|
|
|
.andExpect(jsonPath("$.keys[0].kid").value(this.jwk.getKeyID())) |
|
|
|
|
|
|
|
.andExpect(jsonPath("$.keys[0].kty").value(this.jwk.getKeyType().toString())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void testResponseIfNotRequestMatches() throws Exception { |
|
|
|
void requestWhenDoesNotMatchThenResponseContainsOther() throws Exception { |
|
|
|
mvc.perform(get("/fake")).andDo(print()).andExpect(status().isOk()) |
|
|
|
this.mvc.perform(get("/hello")) |
|
|
|
.andExpect(content().string(is("fake"))); |
|
|
|
.andDo(print()) |
|
|
|
|
|
|
|
.andExpect(status().isOk()) |
|
|
|
|
|
|
|
.andExpect(content().string(is("hello"))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@RestController |
|
|
|
@RestController |
|
|
|
class FakeController { |
|
|
|
static class HelloController { |
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/fake") |
|
|
|
@RequestMapping("/hello") |
|
|
|
public String hello() { |
|
|
|
String hello() { |
|
|
|
return "fake"; |
|
|
|
return "hello"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |