Search in sources :

Example 46 with AuthnRequestType

use of org.keycloak.dom.saml.v2.protocol.AuthnRequestType in project keycloak by keycloak.

the class BasicSamlTest method testIsPassiveAttributeOmittedWhenFalse.

@Test
public void testIsPassiveAttributeOmittedWhenFalse() throws Exception {
    // Verifies that the IsPassive attribute is not emitted in the authnRequest
    // when it is set to false
    // Build the login request document
    AuthnRequestType loginRep = SamlClient.createLoginRequestDocument(SAML_CLIENT_ID_SALES_POST, SAML_ASSERTION_CONSUMER_URL_SALES_POST, getAuthServerSamlEndpoint(REALM_NAME));
    loginRep.setIsPassive(false);
    Document document = SAML2Request.convert(loginRep);
    // Find the AuthnRequest element
    Element authnRequestElement = document.getDocumentElement();
    Attr isPassiveAttribute = authnRequestElement.getAttributeNode("IsPassive");
    assertThat("AuthnRequest element shouldn't contain the IsPassive attribute when isPassive is false, but it does", isPassiveAttribute, nullValue());
}
Also used : AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Example 47 with AuthnRequestType

use of org.keycloak.dom.saml.v2.protocol.AuthnRequestType in project keycloak by keycloak.

the class BasicSamlTest method testNoDestinationSignedPost.

@Test
public void testNoDestinationSignedPost() throws Exception {
    AuthnRequestType loginRep = SamlClient.createLoginRequestDocument(SAML_CLIENT_ID_SALES_POST_SIG, SAML_ASSERTION_CONSUMER_URL_SALES_POST_SIG, null);
    Document doc = SAML2Request.convert(loginRep);
    HttpUriRequest post = Binding.POST.createSamlSignedRequest(getAuthServerSamlEndpoint(REALM_NAME), null, doc, SAML_CLIENT_SALES_POST_SIG_PRIVATE_KEY, SAML_CLIENT_SALES_POST_SIG_PUBLIC_KEY);
    try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new RedirectStrategyWithSwitchableFollowRedirect()).build();
        CloseableHttpResponse response = client.execute(post)) {
        assertThat(response, statusCodeIsHC(Status.BAD_REQUEST));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) RedirectStrategyWithSwitchableFollowRedirect(org.keycloak.testsuite.util.SamlClient.RedirectStrategyWithSwitchableFollowRedirect) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 48 with AuthnRequestType

use of org.keycloak.dom.saml.v2.protocol.AuthnRequestType in project keycloak by keycloak.

the class SAMLParserTest method testAuthnRequestOptionalIsPassive.

// https://issues.jboss.org/browse/KEYCLOAK-7316
@Test
public void testAuthnRequestOptionalIsPassive() throws Exception {
    AuthnRequestType req = assertParsed("KEYCLOAK-7316-noAtrributes.xml", AuthnRequestType.class);
    assertThat("Not null!", req.isIsPassive(), nullValue());
    assertThat("Not null!", req.isForceAuthn(), nullValue());
    req = assertParsed("KEYCLOAK-7316-withTrueAttributes.xml", AuthnRequestType.class);
    assertThat(req.isIsPassive(), notNullValue());
    assertTrue("Wrong value!", req.isIsPassive().booleanValue());
    assertThat(req.isForceAuthn(), notNullValue());
    assertTrue("Wrong value!", req.isForceAuthn().booleanValue());
    req = assertParsed("KEYCLOAK-7316-withFalseAttributes.xml", AuthnRequestType.class);
    assertThat(req.isIsPassive(), notNullValue());
    assertFalse("Wrong value!", req.isIsPassive().booleanValue());
    assertThat(req.isForceAuthn(), notNullValue());
    assertFalse("Wrong value!", req.isForceAuthn().booleanValue());
}
Also used : AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) Test(org.junit.Test)

Example 49 with AuthnRequestType

use of org.keycloak.dom.saml.v2.protocol.AuthnRequestType in project keycloak by keycloak.

the class SAMLParserTest method testAuthnRequest.

@Test
public void testAuthnRequest() throws Exception {
    AuthnRequestType req = assertParsed("saml20-authnrequest.xml", AuthnRequestType.class);
    assertThat(req.getRequestedAuthnContext(), notNullValue());
    assertThat(req.getRequestedAuthnContext().getAuthnContextClassRef(), hasItem(is("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport")));
    assertThat(req.getRequestedAuthnContext().getAuthnContextDeclRef(), hasItem(is("urn:kc:SAML:2.0:ac:ref:demo:decl")));
}
Also used : AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) Test(org.junit.Test)

Example 50 with AuthnRequestType

use of org.keycloak.dom.saml.v2.protocol.AuthnRequestType in project keycloak by keycloak.

the class SamlClient method createLoginRequestDocument.

/**
 * Creates a SAML login request document with the given parameters. See SAML <AuthnRequest> description for more details.
 *
 * @param issuer
 * @param assertionConsumerURL
 * @param destination
 * @return
 */
public static AuthnRequestType createLoginRequestDocument(String issuer, String assertionConsumerURL, URI destination) {
    try {
        SAML2Request samlReq = new SAML2Request();
        AuthnRequestType loginReq = samlReq.createAuthnRequestType(UUID.randomUUID().toString(), assertionConsumerURL, destination == null ? null : destination.toString(), issuer);
        return loginReq;
    } catch (ConfigurationException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) SAML2Request(org.keycloak.saml.processing.api.saml.v2.request.SAML2Request)

Aggregations

AuthnRequestType (org.keycloak.dom.saml.v2.protocol.AuthnRequestType)56 Test (org.junit.Test)41 Document (org.w3c.dom.Document)36 SamlClientBuilder (org.keycloak.testsuite.util.SamlClientBuilder)30 AbstractSamlTest (org.keycloak.testsuite.saml.AbstractSamlTest)21 Element (org.w3c.dom.Element)16 Closeable (java.io.Closeable)13 SAMLDocumentHolder (org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder)13 IdentityProviderAttributeUpdater (org.keycloak.testsuite.updaters.IdentityProviderAttributeUpdater)13 ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)10 ConfigurationException (org.keycloak.saml.common.exceptions.ConfigurationException)8 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)7 StatusResponseType (org.keycloak.dom.saml.v2.protocol.StatusResponseType)7 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 NameIDType (org.keycloak.dom.saml.v2.assertion.NameIDType)5 SAML2Request (org.keycloak.saml.processing.api.saml.v2.request.SAML2Request)4 URI (java.net.URI)3