Re-enable SAML 2.0 samples with Okta IdP
Closes gh-55
This commit is contained in:
@@ -16,15 +16,14 @@
|
||||
|
||||
package example;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlElement;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlInput;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -40,6 +39,8 @@ import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = ApplicationConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
@@ -66,35 +67,45 @@ public class Saml2JavaConfigurationITests {
|
||||
|
||||
@Test
|
||||
void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
|
||||
HtmlPage relyingParty = performLogin();
|
||||
Assertions.assertThat(relyingParty.asText()).contains("You're email address is testuser@spring.security.saml");
|
||||
performLogin();
|
||||
HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
|
||||
assertThat(home.asText()).contains("You're email address is testuser@spring.security.saml");
|
||||
}
|
||||
|
||||
@Test
|
||||
void logoutWhenRelyingPartyInitiatedLogoutThenLoginPageWithLogoutParam() throws Exception {
|
||||
HtmlPage relyingParty = performLogin();
|
||||
HtmlElement rpLogoutButton = relyingParty.getHtmlElementById("rp_logout_button");
|
||||
performLogin();
|
||||
HtmlPage home = (HtmlPage) this.webClient.getCurrentWindow().getEnclosedPage();
|
||||
HtmlElement rpLogoutButton = home.getHtmlElementById("rp_logout_button");
|
||||
HtmlPage loginPage = rpLogoutButton.click();
|
||||
Assertions.assertThat(loginPage.getUrl().getFile()).isEqualTo("/login?logout");
|
||||
assertThat(loginPage.getUrl().getFile()).isEqualTo("/login?logout");
|
||||
}
|
||||
|
||||
@Test
|
||||
void logoutWhenAssertingPartyInitiatedLogoutThenLoginPageWithLogoutParam() throws Exception {
|
||||
HtmlPage relyingParty = performLogin();
|
||||
HtmlElement apLogoutButton = relyingParty.getHtmlElementById("ap_logout_button");
|
||||
HtmlPage loginPage = apLogoutButton.click();
|
||||
Assertions.assertThat(loginPage.getUrl().getFile()).isEqualTo("/login?logout");
|
||||
}
|
||||
|
||||
private HtmlPage performLogin() throws IOException {
|
||||
private void performLogin() throws Exception {
|
||||
HtmlPage login = this.webClient.getPage("/");
|
||||
HtmlForm form = login.getFormByName("f");
|
||||
this.webClient.waitForBackgroundJavaScript(10000);
|
||||
HtmlForm form = findForm(login);
|
||||
HtmlInput username = form.getInputByName("username");
|
||||
HtmlInput password = form.getInputByName("password");
|
||||
HtmlSubmitInput submit = login.getHtmlElementById("submit_button");
|
||||
username.setValueAttribute("user");
|
||||
password.setValueAttribute("password");
|
||||
return submit.click();
|
||||
HtmlPasswordInput password = form.getInputByName("password");
|
||||
HtmlSubmitInput submit = login.getHtmlElementById("okta-signin-submit");
|
||||
username.type("testuser@spring.security.saml");
|
||||
password.type("12345678");
|
||||
submit.click();
|
||||
this.webClient.waitForBackgroundJavaScript(10000);
|
||||
}
|
||||
|
||||
private HtmlForm findForm(HtmlPage login) {
|
||||
for (HtmlForm form : login.getForms()) {
|
||||
try {
|
||||
if (form.getId().equals("form19")) {
|
||||
return form;
|
||||
}
|
||||
}
|
||||
catch (ElementNotFoundException ex) {
|
||||
// Continue
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Could not resolve login form");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class IndexController {
|
||||
|
||||
@GetMapping("/")
|
||||
public String index(Model model, @AuthenticationPrincipal Saml2AuthenticatedPrincipal principal) {
|
||||
String emailAddress = principal.getFirstAttribute("emailAddress");
|
||||
String emailAddress = principal.getFirstAttribute("email");
|
||||
model.addAttribute("emailAddress", emailAddress);
|
||||
model.addAttribute("userAttributes", principal.getAttributes());
|
||||
return "index";
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.security.saml2.provider.service.registration.InMemory
|
||||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
|
||||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
|
||||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrations;
|
||||
import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@EnableWebSecurity
|
||||
@@ -57,13 +58,16 @@ public class SecurityConfiguration {
|
||||
@Bean
|
||||
RelyingPartyRegistrationRepository relyingPartyRegistrationRepository() {
|
||||
RelyingPartyRegistration relyingPartyRegistration = RelyingPartyRegistrations
|
||||
.fromMetadataLocation("https://simplesaml-for-spring-saml.apps.pcfone.io/saml2/idp/metadata.php")
|
||||
.fromMetadataLocation("https://dev-05937739.okta.com/app/exk46xofd8NZvFCpS5d7/sso/saml/metadata")
|
||||
.registrationId("one")
|
||||
.decryptionX509Credentials(
|
||||
(c) -> c.add(Saml2X509Credential.decryption(this.privateKey, relyingPartyCertificate())))
|
||||
.signingX509Credentials(
|
||||
(c) -> c.add(Saml2X509Credential.signing(this.privateKey, relyingPartyCertificate())))
|
||||
.build();
|
||||
.singleLogoutServiceLocation(
|
||||
"https://dev-05937739.okta.com/app/dev-05937739_springgsecuritysaml2idp_1/exk46xofd8NZvFCpS5d7/slo/saml")
|
||||
.singleLogoutServiceResponseLocation("http://localhost:8080/logout/saml2/slo")
|
||||
.singleLogoutServiceBinding(Saml2MessageBinding.POST).build();
|
||||
|
||||
return new InMemoryRelyingPartyRegistrationRepository(relyingPartyRegistration);
|
||||
}
|
||||
|
||||
@@ -36,11 +36,6 @@
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a id="ap_logout_button" class="nav-link" href="https://simplesaml-for-spring-saml.apps.pcfone.io/saml2/idp/SingleLogoutService.php?ReturnTo=http://localhost:8080/login?logout">
|
||||
AP-initiated Logout
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<main role="main" class="container">
|
||||
|
||||
Reference in New Issue
Block a user