Skip to content

Commit 84738c5

Browse files
committed
Move refreshable-metadata to use docker IdP
Issue gh-127
1 parent ecec3f9 commit 84738c5

File tree

6 files changed

+129
-12
lines changed

6 files changed

+129
-12
lines changed

servlet/spring-boot/java/saml2/refreshable-metadata/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ repositories {
1313
maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" }
1414
}
1515

16+
sourceSets.main.java.srcDirs += "$projectDir/../identity-provider/src/main/java"
17+
sourceSets.main.resources.srcDirs += "$projectDir/../identity-provider/src/main/resources"
1618

1719
dependencies {
1820
constraints {
@@ -28,6 +30,7 @@ dependencies {
2830
testImplementation 'org.htmlunit:htmlunit'
2931
testImplementation 'org.springframework.boot:spring-boot-starter-test'
3032
testImplementation 'org.springframework.security:spring-security-test'
33+
runtimeOnly "org.springframework.boot:spring-boot-docker-compose"
3134
}
3235

3336
tasks.withType(Test).configureEach {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2021 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+
17+
package example;
18+
19+
import java.io.IOException;
20+
import java.net.ServerSocket;
21+
22+
import org.springframework.boot.SpringApplication;
23+
import org.springframework.boot.env.EnvironmentPostProcessor;
24+
import org.springframework.core.env.ConfigurableEnvironment;
25+
import org.springframework.core.env.PropertySource;
26+
27+
/**
28+
* Spring Boot doesn't determine the port before the docker containers are loaded, so
29+
* we'll decide the test port here and override the associated properties.
30+
*
31+
* @author Josh Cummings
32+
*/
33+
public class PreDockerComposeServerPortInitializer implements EnvironmentPostProcessor {
34+
35+
private static final Integer port = getPort();
36+
37+
@Override
38+
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
39+
environment.getPropertySources().addFirst(new ServerPortPropertySource(port));
40+
}
41+
42+
private static Integer getPort() {
43+
try (ServerSocket serverSocket = new ServerSocket(0)) {
44+
return serverSocket.getLocalPort();
45+
}
46+
catch (IOException ex) {
47+
throw new RuntimeException(ex);
48+
}
49+
}
50+
51+
private static class ServerPortPropertySource extends PropertySource<Integer> {
52+
53+
ServerPortPropertySource(Integer port) {
54+
super("server.port.override", port);
55+
}
56+
57+
@Override
58+
public Object getProperty(String name) {
59+
if ("server.port".equals(name)) {
60+
return getSource();
61+
}
62+
if ("SERVER_PORT".equals(name)) {
63+
return getSource();
64+
}
65+
return null;
66+
}
67+
68+
}
69+
70+
}

servlet/spring-boot/java/saml2/refreshable-metadata/src/integTest/java/example/Saml2LoginApplicationITests.java

+38-11
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@
1616

1717
package example;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
import org.htmlunit.ElementNotFoundException;
2023
import org.htmlunit.WebClient;
24+
import org.htmlunit.html.HtmlButton;
25+
import org.htmlunit.html.HtmlElement;
2126
import org.htmlunit.html.HtmlForm;
2227
import org.htmlunit.html.HtmlInput;
2328
import org.htmlunit.html.HtmlPage;
2429
import org.htmlunit.html.HtmlPasswordInput;
25-
import org.htmlunit.html.HtmlSubmitInput;
2630
import org.junit.jupiter.api.BeforeEach;
2731
import org.junit.jupiter.api.Test;
2832

2933
import org.springframework.beans.factory.annotation.Autowired;
3034
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
3135
import org.springframework.boot.test.context.SpringBootTest;
32-
import org.springframework.test.web.servlet.MockMvc;
36+
import org.springframework.boot.test.web.server.LocalServerPort;
3337

3438
import static org.assertj.core.api.Assertions.assertThat;
3539

36-
@SpringBootTest
40+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
3741
@AutoConfigureMockMvc
3842
public class Saml2LoginApplicationITests {
3943

40-
@Autowired
41-
MockMvc mvc;
44+
45+
@LocalServerPort
46+
int port;
4247

4348
@Autowired
4449
WebClient webClient;
@@ -52,26 +57,48 @@ void setup() {
5257
void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
5358
performLogin();
5459
HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
55-
assertThat(home.asNormalizedText()).contains("You're email address is testuser2@spring.security.saml");
60+
assertThat(home.asNormalizedText()).contains("You're email address is user1@example.org");
61+
}
62+
63+
@Test
64+
void logoutWhenRelyingPartyInitiatedLogoutThenLoginPageWithLogoutParam() throws Exception {
65+
performLogin();
66+
HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
67+
HtmlElement rpLogoutButton = home.getHtmlElementById("rp_logout_button");
68+
HtmlPage loginPage = rpLogoutButton.click();
69+
this.webClient.waitForBackgroundJavaScript(10000);
70+
List<String> urls = new ArrayList<>();
71+
urls.add(loginPage.getUrl().getFile());
72+
urls.add(((HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage()).getUrl().getFile());
73+
assertThat(urls).withFailMessage(() -> {
74+
// @formatter:off
75+
String builder = loginPage.asXml()
76+
+ "\n\n\n"
77+
+ "Enclosing Page"
78+
+ "\n\n\n"
79+
+ ((HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage()).asXml();
80+
// @formatter:on
81+
return builder;
82+
}).contains("/login?logout");
5683
}
5784

5885
private void performLogin() throws Exception {
59-
HtmlPage login = this.webClient.getPage("/");
86+
HtmlPage login = this.webClient.getPage("http://localhost:" + this.port + "/saml2/authenticate/one");
6087
this.webClient.waitForBackgroundJavaScript(10000);
6188
HtmlForm form = findForm(login);
6289
HtmlInput username = form.getInputByName("username");
6390
HtmlPasswordInput password = form.getInputByName("password");
64-
HtmlSubmitInput submit = login.getHtmlElementById("okta-signin-submit");
65-
username.type("testuser2@spring.security.saml");
66-
password.type("12345678");
91+
HtmlButton submit = (HtmlButton) form.getElementsByTagName("button").iterator().next();
92+
username.type("user1");
93+
password.type("user1pass");
6794
submit.click();
6895
this.webClient.waitForBackgroundJavaScript(10000);
6996
}
7097

7198
private HtmlForm findForm(HtmlPage login) {
7299
for (HtmlForm form : login.getForms()) {
73100
try {
74-
if (form.getId().equals("form19")) {
101+
if (form.getNameAttribute().equals("f")) {
75102
return form;
76103
}
77104
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.env.EnvironmentPostProcessor=example.PreDockerComposeServerPortInitializer

servlet/spring-boot/java/saml2/refreshable-metadata/src/main/java/example/RefreshableRelyingPartyRegistrationRepository.java

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public void refreshMetadata() {
7070
private void fetchMetadata(String registrationId, Saml2RelyingPartyProperties.Registration registration) {
7171
RelyingPartyRegistration relyingPartyRegistration = RelyingPartyRegistrations
7272
.fromMetadataLocation(registration.getAssertingparty().getMetadataUri())
73+
.entityId(registration.getEntityId())
74+
.assertionConsumerServiceLocation(registration.getAcs().getLocation())
75+
.singleLogoutServiceLocation(registration.getSinglelogout().getUrl())
76+
.singleLogoutServiceBinding(registration.getSinglelogout().getBinding())
7377
.signingX509Credentials((credentials) -> registration.getSigning()
7478
.getCredentials()
7579
.stream()
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
spring:
2+
docker:
3+
compose:
4+
file: docker:docker/compose.yml
5+
readiness:
6+
wait: never
7+
skip:
8+
in-tests: false
29
security:
310
saml2:
411
relyingparty:
512
registration:
613
one:
14+
entity-id: "{baseUrl}/saml2/metadata"
15+
acs.location: "{baseUrl}/login/saml2/sso"
716
signing.credentials:
817
- private-key-location: classpath:credentials/rp-private.key
918
certificate-location: classpath:credentials/rp-certificate.crt
19+
singlelogout:
20+
binding: REDIRECT
21+
url: "{baseUrl}/logout/saml2/slo"
1022
assertingparty:
11-
metadata-uri: https://dev-05937739.okta.com/app/exk46xofd8NZvFCpS5d7/sso/saml/metadata
23+
metadata-uri: http://idp-one.7f000001.nip.io/simplesaml/saml2/idp/metadata.php
1224

1325
logging.level:
1426
org.springframework.security: TRACE

0 commit comments

Comments
 (0)