|
38 | 38 | import org.springframework.security.authentication.AuthenticationManagerResolver;
|
39 | 39 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
40 | 40 | import org.springframework.security.oauth2.jose.TestKeys;
|
| 41 | +import org.springframework.security.oauth2.jwt.JwtClaimNames; |
41 | 42 |
|
42 | 43 | import static org.assertj.core.api.Assertions.assertThat;
|
43 | 44 | import static org.assertj.core.api.Assertions.assertThatCode;
|
| 45 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
44 | 46 | import static org.mockito.Mockito.mock;
|
45 | 47 | import static org.springframework.security.oauth2.jwt.JwtClaimNames.ISS;
|
46 | 48 |
|
@@ -85,6 +87,35 @@ public void resolveWhenUsingTrustedIssuerThenReturnsAuthenticationManager() thro
|
85 | 87 | }
|
86 | 88 | }
|
87 | 89 |
|
| 90 | + @Test |
| 91 | + public void resolveWhenIssuerFailsThenErrorNotCached() throws Exception { |
| 92 | + try (MockWebServer server = new MockWebServer()) { |
| 93 | + server.start(); |
| 94 | + String issuer = server.url("").toString(); |
| 95 | + // @formatter:off |
| 96 | + server.enqueue(new MockResponse().setResponseCode(500) |
| 97 | + .setHeader("Content-Type", "application/json") |
| 98 | + .setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)) |
| 99 | + ); |
| 100 | + server.enqueue(new MockResponse().setResponseCode(200) |
| 101 | + .setHeader("Content-Type", "application/json") |
| 102 | + .setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)) |
| 103 | + ); |
| 104 | + // @formatter:on |
| 105 | + JWSObject jws = new JWSObject(new JWSHeader(JWSAlgorithm.RS256), |
| 106 | + new Payload(new JSONObject(Collections.singletonMap(JwtClaimNames.ISS, issuer)))); |
| 107 | + jws.sign(new RSASSASigner(TestKeys.DEFAULT_PRIVATE_KEY)); |
| 108 | + JwtIssuerAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerAuthenticationManagerResolver( |
| 109 | + issuer); |
| 110 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 111 | + request.addHeader("Authorization", "Bearer " + jws.serialize()); |
| 112 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 113 | + .isThrownBy(() -> authenticationManagerResolver.resolve(request)); |
| 114 | + AuthenticationManager authenticationManager = authenticationManagerResolver.resolve(request); |
| 115 | + assertThat(authenticationManager).isNotNull(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
88 | 119 | @Test
|
89 | 120 | public void resolveWhenUsingUntrustedIssuerThenException() {
|
90 | 121 | JwtIssuerAuthenticationManagerResolver authenticationManagerResolver =
|
|
0 commit comments