16
16
17
17
package org .springframework .boot .autoconfigure .security .saml2 ;
18
18
19
+ import java .io .InputStream ;
19
20
import java .util .List ;
20
21
21
22
import javax .servlet .Filter ;
22
23
23
24
import okhttp3 .mockwebserver .MockResponse ;
24
25
import okhttp3 .mockwebserver .MockWebServer ;
25
- import org . junit . jupiter . api . AfterEach ;
26
+ import okio . Buffer ;
26
27
import org .junit .jupiter .api .Test ;
27
28
28
29
import org .springframework .boot .autoconfigure .AutoConfigurations ;
33
34
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
34
35
import org .springframework .context .annotation .Bean ;
35
36
import org .springframework .context .annotation .Configuration ;
37
+ import org .springframework .core .io .ClassPathResource ;
36
38
import org .springframework .security .config .BeanIds ;
37
39
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
38
40
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
@@ -58,15 +60,6 @@ class Saml2RelyingPartyAutoConfigurationTests {
58
60
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner ().withConfiguration (
59
61
AutoConfigurations .of (Saml2RelyingPartyAutoConfiguration .class , SecurityAutoConfiguration .class ));
60
62
61
- private MockWebServer server ;
62
-
63
- @ AfterEach
64
- void cleanup () throws Exception {
65
- if (this .server != null ) {
66
- this .server .shutdown ();
67
- }
68
- }
69
-
70
63
@ Test
71
64
void autoConfigurationShouldBeConditionalOnRelyingPartyRegistrationRepositoryClass () {
72
65
this .contextRunner .withPropertyValues (getPropertyValues ()).withClassLoader (new FilteredClassLoader (
@@ -126,15 +119,16 @@ void autoConfigurationWhenSignRequestsFalseAndNoSigningCredentialsShouldNotThrow
126
119
127
120
@ Test
128
121
void autoconfigurationShouldQueryIdentityProviderMetadataWhenMetadataUrlIsPresent () throws Exception {
129
- this .server = new MockWebServer ();
130
- this .server .start ();
131
- String metadataUrl = this .server .url ("" ).toString ();
132
- setupMockResponse ();
133
- this .contextRunner .withPropertyValues (PREFIX + ".foo.identityprovider.metadata-url=" + metadataUrl )
134
- .run ((context ) -> {
135
- assertThat (context ).hasSingleBean (RelyingPartyRegistrationRepository .class );
136
- assertThat (this .server .getRequestCount ()).isEqualTo (1 );
137
- });
122
+ try (MockWebServer server = new MockWebServer ()) {
123
+ server .start ();
124
+ String metadataUrl = server .url ("" ).toString ();
125
+ setupMockResponse (server );
126
+ this .contextRunner .withPropertyValues (PREFIX + ".foo.identityprovider.metadata-uri=" + metadataUrl )
127
+ .run ((context ) -> {
128
+ assertThat (context ).hasSingleBean (RelyingPartyRegistrationRepository .class );
129
+ assertThat (server .getRequestCount ()).isEqualTo (1 );
130
+ });
131
+ }
138
132
}
139
133
140
134
@ Test
@@ -201,44 +195,12 @@ private boolean hasFilter(AssertableWebApplicationContext context, Class<? exten
201
195
return filters .stream ().anyMatch (filter ::isInstance );
202
196
}
203
197
204
- private void setupMockResponse () {
205
- String metadataResponse = "<md:EntityDescriptor entityID=\" https://idp.example.com/idp/shibboleth\" \n "
206
- + " xmlns:ds=\" http://www.w3.org/2000/09/xmldsig#\" \n "
207
- + " xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" \n "
208
- + " xmlns:shibmd=\" urn:mace:shibboleth:metadata:1.0\" \n "
209
- + " xmlns:md=\" urn:oasis:names:tc:SAML:2.0:metadata\" \n "
210
- + " xmlns:mdui=\" urn:oasis:names:tc:SAML:metadata:ui\" >\n " + " \n "
211
- + " <md:IDPSSODescriptor protocolSupportEnumeration=\" urn:oasis:names:tc:SAML:2.0:protocol\" >\n "
212
- + " <md:KeyDescriptor>\n " + " <ds:KeyInfo>\n " + " <ds:X509Data>\n "
213
- + " <ds:X509Certificate>\n "
214
- + " MIIDZjCCAk6gAwIBAgIVAL9O+PA7SXtlwZZY8MVSE9On1cVWMA0GCSqGSIb3DQEB\n "
215
- + " BQUAMCkxJzAlBgNVBAMTHmlkZW0tcHVwYWdlbnQuZG16LWludC51bmltby5pdDAe\n "
216
- + " Fw0xMzA3MjQwMDQ0MTRaFw0zMzA3MjQwMDQ0MTRaMCkxJzAlBgNVBAMTHmlkZW0t\n "
217
- + " cHVwYWdlbnQuZG16LWludC51bmltby5pdDCCASIwDQYJKoZIhvcNAMIIDQADggEP\n "
218
- + " ADCCAQoCggEBAIAcp/VyzZGXUF99kwj4NvL/Rwv4YvBgLWzpCuoxqHZ/hmBwJtqS\n "
219
- + " v0y9METBPFbgsF3hCISnxbcmNVxf/D0MoeKtw1YPbsUmow/bFe+r72hZ+IVAcejN\n "
220
- + " iDJ7t5oTjsRN1t1SqvVVk6Ryk5AZhpFW+W9pE9N6c7kJ16Rp2/mbtax9OCzxpece\n "
221
- + " byi1eiLfIBmkcRawL/vCc2v6VLI18i6HsNVO3l2yGosKCbuSoGDx2fCdAOk/rgdz\n "
222
- + " cWOvFsIZSKuD+FVbSS/J9GVs7yotsS4PRl4iX9UMnfDnOMfO7bcBgbXtDl4SCU1v\n "
223
- + " dJrRw7IL/pLz34Rv9a8nYitrzrxtLOp3nYUCAwEAAaOBhDCBgTBgBgMIIDEEWTBX\n "
224
- + " gh5pZGVtLXB1cGFnZW50LmRtei1pbnQudW5pbW8uaXSGNWh0dHBzOi8vaWRlbS1w\n "
225
- + " dXBhZ2VudC5kbXotaW50LnVuaW1vLml0L2lkcC9zaGliYm9sZXRoMB0GA1UdDgQW\n "
226
- + " BBT8PANzz+adGnTRe8ldcyxAwe4VnzANBgkqhkiG9w0BAQUFAAOCAQEAOEnO8Clu\n "
227
- + " 9z/Lf/8XOOsTdxJbV29DIF3G8KoQsB3dBsLwPZVEAQIP6ceS32Xaxrl6FMTDDNkL\n "
228
- + " qUvvInUisw0+I5zZwYHybJQCletUWTnz58SC4C9G7FpuXHFZnOGtRcgGD1NOX4UU\n "
229
- + " duus/4nVcGSLhDjszZ70Xtj0gw2Sn46oQPHTJ81QZ3Y9ih+Aj1c9OtUSBwtWZFkU\n "
230
- + " yooAKoR8li68Yb21zN2N65AqV+ndL98M8xUYMKLONuAXStDeoVCipH6PJ09Z5U2p\n "
231
- + " V5p4IQRV6QBsNw9CISJFuHzkVYTH5ZxzN80Ru46vh4y2M0Nu8GQ9I085KoZkrf5e\n "
232
- + " Cq53OZt9ISjHEw==\n " + " </ds:X509Certificate>\n "
233
- + " </ds:X509Data>\n " + " </ds:KeyInfo>\n " + " </md:KeyDescriptor>\n " + " \n "
234
- + " <md:SingleSignOnService\n "
235
- + " Binding=\" urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" \n "
236
- + " Location=\" https://idp.example.com/sso\" />\n " + " </md:IDPSSODescriptor>\n " + " \n "
237
- + " <md:ContactPerson contactType=\" technical\" >\n "
238
- +
" <md:EmailAddress>mailto:[email protected] </md:EmailAddress>\n "
239
- + " </md:ContactPerson>\n " + " \n " + "</md:EntityDescriptor>" ;
240
- MockResponse mockResponse = new MockResponse ().setBody (metadataResponse );
241
- this .server .enqueue (mockResponse );
198
+ private void setupMockResponse (MockWebServer server ) throws Exception {
199
+ try (InputStream metadataSource = new ClassPathResource ("saml/idp-metadata" ).getInputStream ()) {
200
+ Buffer metadataBuffer = new Buffer ().readFrom (metadataSource );
201
+ MockResponse metadataResponse = new MockResponse ().setBody (metadataBuffer );
202
+ server .enqueue (metadataResponse );
203
+ }
242
204
}
243
205
244
206
@ Configuration (proxyBeanMethods = false )
0 commit comments