Skip to content

Commit

Permalink
Update custom-urls Sample to use Docker-based IdP
Browse files Browse the repository at this point in the history
Issue gh-127
  • Loading branch information
jzheaux committed Nov 5, 2024
1 parent 29d9bec commit 8eb0646
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 23 deletions.
8 changes: 8 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/.idea/codeStyles

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions servlet/spring-boot/java/saml2/custom-urls/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ repositories {
maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" }
}

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

if (plugins.hasPlugin("io.spring.javaformat")) {
tasks.formatMain {
dependsOn(":servlet:spring-boot:java:saml2:identity-provider:formatMain")
}
}

dependencies {
constraints {
implementation "org.opensaml:opensaml-saml-api:5.1.3"
Expand All @@ -26,6 +35,7 @@ dependencies {
testImplementation 'org.htmlunit:htmlunit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
runtimeOnly "org.springframework.boot:spring-boot-docker-compose"
}

tasks.withType(Test).configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,33 @@

import org.htmlunit.ElementNotFoundException;
import org.htmlunit.WebClient;
import org.htmlunit.html.HtmlButton;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlInput;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.html.HtmlPasswordInput;
import org.htmlunit.html.HtmlSubmitInput;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.web.servlet.MockMvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
public class CustomUrlsApplicationITests {

@LocalServerPort
int port;

@Autowired
MockMvc mvc;

Expand All @@ -59,7 +63,7 @@ void setup() {
void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
performLogin();
HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
assertThat(home.asNormalizedText()).contains("You're email address is [email protected]");
assertThat(home.asNormalizedText()).contains("You're email address is [email protected]");
}

@Test
Expand All @@ -83,22 +87,22 @@ void metadataWhenGetThenForwardToUrl() throws Exception {
}

private void performLogin() throws Exception {
HtmlPage login = this.webClient.getPage("/");
HtmlPage login = this.webClient.getPage("http://localhost:" + this.port + "/saml/login");
this.webClient.waitForBackgroundJavaScript(10000);
HtmlForm form = findForm(login);
HtmlInput username = form.getInputByName("username");
HtmlPasswordInput password = form.getInputByName("password");
HtmlSubmitInput submit = login.getHtmlElementById("okta-signin-submit");
username.type("[email protected]");
password.type("12345678");
HtmlButton submit = (HtmlButton) form.getElementsByTagName("button").iterator().next();
username.type("user1");
password.type("user1pass");
submit.click();
this.webClient.waitForBackgroundJavaScript(10000);
}

private HtmlForm findForm(HtmlPage login) {
for (HtmlForm form : login.getForms()) {
try {
if (form.getId().equals("form19")) {
if (form.getNameAttribute().equals("f")) {
return form;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example;

import java.io.IOException;
import java.net.ServerSocket;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;

/**
* Spring Boot doesn't determine the port before the docker containers are loaded, so
* we'll decide the test port here and override the associated properties.
*
* @author Josh Cummings
*/
public class PreDockerComposeServerPortInitializer implements EnvironmentPostProcessor {

private static final Integer port = getPort();

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.getPropertySources().addFirst(new ServerPortPropertySource(port));
}

private static Integer getPort() {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}

private static class ServerPortPropertySource extends PropertySource<Integer> {

ServerPortPropertySource(Integer port) {
super("server.port.override", port);
}

@Override
public Object getProperty(String name) {
if ("server.port".equals(name)) {
return getSource();
}
if ("SERVER_PORT".equals(name)) {
return getSource();
}
return null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.env.EnvironmentPostProcessor=example.PreDockerComposeServerPortInitializer
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,14 @@
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.saml2.provider.service.metadata.OpenSamlMetadataResolver;
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
import org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver;
import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver;
import org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter;
import org.springframework.security.saml2.provider.service.web.authentication.Saml2WebSsoAuthenticationFilter;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http,
RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) throws Exception {
RelyingPartyRegistrationResolver relyingPartyRegistrationResolver = new DefaultRelyingPartyRegistrationResolver(
relyingPartyRegistrationRepository);
Saml2MetadataFilter metadataFilter = new Saml2MetadataFilter(relyingPartyRegistrationResolver,
new OpenSamlMetadataResolver());
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeHttpRequests((authorize) -> authorize
Expand All @@ -48,7 +37,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity http,
)
.saml2Login(Customizer.withDefaults())
.saml2Logout(Customizer.withDefaults())
.addFilterBefore(metadataFilter, Saml2WebSsoAuthenticationFilter.class);
.saml2Metadata(Customizer.withDefaults());
// @formatter:on
return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
logging.level:
org.springframework.security: TRACE

spring:
docker:
compose:
file: docker:docker/compose.yml
readiness:
wait: never
skip:
in-tests: false
security:
filter:
dispatcher-types: async, error, request, forward
saml2:
relyingparty:
registration:
one:
entity-id: "{baseUrl}/saml/metadata"
signing.credentials:
- private-key-location: classpath:credentials/rp-private.key
certificate-location: classpath:credentials/rp-certificate.crt
assertingparty.metadata-uri: https://dev-05937739.okta.com/app/exk598vc9bHhwoTXM5d7/sso/saml/metadata
assertingparty.metadata-uri: http://idp-one.7f000001.nip.io/simplesaml/saml2/idp/metadata.php
singlelogout:
binding: POST
binding: REDIRECT
url: "{baseUrl}/saml/logout"
responseUrl: "{baseUrl}/saml/SingleLogout"
acs:
Expand Down
Loading

0 comments on commit 8eb0646

Please sign in to comment.