Search in sources :

Example 31 with IdentityProviderRepresentation

use of org.keycloak.representations.idm.IdentityProviderRepresentation in project keycloak by keycloak.

the class IdentityProviderTest method failUpdateInvalidUrl.

@Test
public void failUpdateInvalidUrl() throws Exception {
    try (RealmAttributeUpdater rau = new RealmAttributeUpdater(realm).updateWith(r -> r.setSslRequired(SslRequired.ALL.name())).update()) {
        IdentityProviderRepresentation representation = createRep(UUID.randomUUID().toString(), "oidc");
        representation.getConfig().put("clientId", "clientId");
        representation.getConfig().put("clientSecret", "some secret value");
        try (Response response = realm.identityProviders().create(representation)) {
            assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        }
        IdentityProviderResource resource = this.realm.identityProviders().get(representation.getAlias());
        representation = resource.toRepresentation();
        OIDCIdentityProviderConfigRep oidcConfig = new OIDCIdentityProviderConfigRep(representation);
        oidcConfig.setAuthorizationUrl("invalid://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [authorization_url] is malformed", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl("http://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [token_url] requires secure connections", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl(null);
        oidcConfig.setJwksUrl("http://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [jwks_url] requires secure connections", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl(null);
        oidcConfig.setJwksUrl(null);
        oidcConfig.setLogoutUrl("http://test");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [logout_url] requires secure connections", error.getErrorMessage());
        }
        oidcConfig.setAuthorizationUrl(null);
        oidcConfig.setTokenUrl(null);
        oidcConfig.setJwksUrl(null);
        oidcConfig.setLogoutUrl(null);
        oidcConfig.setUserInfoUrl("http://localhost");
        try {
            resource.update(representation);
            fail("Invalid URL");
        } catch (Exception e) {
            assertTrue(e instanceof ClientErrorException);
            Response response = ClientErrorException.class.cast(e).getResponse();
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
            ErrorRepresentation error = ((ClientErrorException) e).getResponse().readEntity(ErrorRepresentation.class);
            assertEquals("The url [userinfo_url] requires secure connections", error.getErrorMessage());
        }
        rau.updateWith(r -> r.setSslRequired(SslRequired.EXTERNAL.name())).update();
        resource.update(representation);
    }
}
Also used : EndpointType(org.keycloak.dom.saml.v2.metadata.EndpointType) Arrays(java.util.Arrays) ResourceType(org.keycloak.events.admin.ResourceType) OIDCIdentityProviderConfigRep(org.keycloak.testsuite.broker.OIDCIdentityProviderConfigRep) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Matchers.not(org.hamcrest.Matchers.not) ClientErrorException(javax.ws.rs.ClientErrorException) Assert.assertThat(org.junit.Assert.assertThat) SAMLIdentityProviderConfig(org.keycloak.broker.saml.SAMLIdentityProviderConfig) MediaType(javax.ws.rs.core.MediaType) MultipartFormDataOutput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) REMOTE(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer.REMOTE) Document(org.w3c.dom.Document) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AdminEventPaths(org.keycloak.testsuite.util.AdminEventPaths) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Set(java.util.Set) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) UUID(java.util.UUID) RealmAttributeUpdater(org.keycloak.testsuite.updaters.RealmAttributeUpdater) NotFoundException(javax.ws.rs.NotFoundException) DocumentUtil(org.keycloak.saml.common.util.DocumentUtil) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) List(java.util.List) SslRequired(org.keycloak.common.enums.SslRequired) Response(javax.ws.rs.core.Response) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) XMLSignature(javax.xml.crypto.dsig.XMLSignature) SAMLParser(org.keycloak.saml.processing.core.parsers.saml.SAMLParser) OperationType(org.keycloak.events.admin.OperationType) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) Assert(org.keycloak.testsuite.Assert) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) HashMap(java.util.HashMap) IdentityProviderMapperTypeRepresentation(org.keycloak.representations.idm.IdentityProviderMapperTypeRepresentation) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) HashSet(java.util.HashSet) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) Charset(java.nio.charset.Charset) AUTH_SERVER_SSL_REQUIRED(org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_SSL_REQUIRED) IndexedEndpointType(org.keycloak.dom.saml.v2.metadata.IndexedEndpointType) AdminEventRepresentation(org.keycloak.representations.idm.AdminEventRepresentation) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) StripSecretsUtils(org.keycloak.models.utils.StripSecretsUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) SPSSODescriptorType(org.keycloak.dom.saml.v2.metadata.SPSSODescriptorType) Matchers.empty(org.hamcrest.Matchers.empty) NodeList(org.w3c.dom.NodeList) EntityDescriptorType(org.keycloak.dom.saml.v2.metadata.EntityDescriptorType) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) KeyTypes(org.keycloak.dom.saml.v2.metadata.KeyTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) XMLDSIG_NSURI(org.keycloak.saml.common.constants.JBossSAMLURIConstants.XMLDSIG_NSURI) Element(org.w3c.dom.Element) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) KeyDescriptorType(org.keycloak.dom.saml.v2.metadata.KeyDescriptorType) IdentityProviderMapperSyncMode(org.keycloak.models.IdentityProviderMapperSyncMode) Assert.assertEquals(org.junit.Assert.assertEquals) Response(javax.ws.rs.core.Response) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) OIDCIdentityProviderConfigRep(org.keycloak.testsuite.broker.OIDCIdentityProviderConfigRep) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) ClientErrorException(javax.ws.rs.ClientErrorException) RealmAttributeUpdater(org.keycloak.testsuite.updaters.RealmAttributeUpdater) URISyntaxException(java.net.URISyntaxException) ClientErrorException(javax.ws.rs.ClientErrorException) NotFoundException(javax.ws.rs.NotFoundException) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) IOException(java.io.IOException) Test(org.junit.Test)

Example 32 with IdentityProviderRepresentation

use of org.keycloak.representations.idm.IdentityProviderRepresentation in project keycloak by keycloak.

the class KcSamlIdPInitiatedSsoTest method testProviderTransientIdpInitiatedLogin.

@Test
public void testProviderTransientIdpInitiatedLogin() throws Exception {
    IdentityProviderResource idp = adminClient.realm(REALM_CONS_NAME).identityProviders().get("saml-leaf");
    IdentityProviderRepresentation rep = idp.toRepresentation();
    rep.getConfig().put(SAMLIdentityProviderConfig.NAME_ID_POLICY_FORMAT, JBossSAMLURIConstants.NAMEID_FORMAT_TRANSIENT.get());
    rep.getConfig().put(SAMLIdentityProviderConfig.PRINCIPAL_TYPE, SamlPrincipalType.ATTRIBUTE.name());
    rep.getConfig().put(SAMLIdentityProviderConfig.PRINCIPAL_ATTRIBUTE, X500SAMLProfileConstants.UID.get());
    idp.update(rep);
    SAMLDocumentHolder samlResponse = new SamlClientBuilder().navigateTo(getSamlIdpInitiatedUrl(REALM_PROV_NAME, "samlbroker")).login().user(PROVIDER_REALM_USER_NAME, PROVIDER_REALM_USER_PASSWORD).build().processSamlResponse(Binding.POST).transformObject(ob -> {
        assertThat(ob, Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
        ResponseType resp = (ResponseType) ob;
        assertThat(resp.getDestination(), is(getSamlBrokerIdpInitiatedUrl(REALM_CONS_NAME, "sales")));
        assertAudience(resp, getSamlBrokerIdpInitiatedUrl(REALM_CONS_NAME, "sales"));
        NameIDType nameId = new NameIDType();
        nameId.setFormat(URI.create(JBossSAMLURIConstants.NAMEID_FORMAT_TRANSIENT.get()));
        nameId.setValue("subjectId1");
        resp.getAssertions().get(0).getAssertion().getSubject().getSubType().addBaseID(nameId);
        Set<StatementAbstractType> statements = resp.getAssertions().get(0).getAssertion().getStatements();
        AttributeStatementType attributeType = (AttributeStatementType) statements.stream().filter(statement -> statement instanceof AttributeStatementType).findFirst().orElse(new AttributeStatementType());
        AttributeType attr = new AttributeType(X500SAMLProfileConstants.UID.get());
        attr.addAttributeValue(PROVIDER_REALM_USER_NAME);
        attributeType.addAttribute(new AttributeStatementType.ASTChoiceType(attr));
        resp.getAssertions().get(0).getAssertion().addStatement(attributeType);
        return ob;
    }).build().navigateTo(getSamlIdpInitiatedUrl(REALM_PROV_NAME, "samlbroker-2")).login().sso(true).build().processSamlResponse(Binding.POST).transformObject(ob -> {
        assertThat(ob, Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
        ResponseType resp = (ResponseType) ob;
        assertThat(resp.getDestination(), is(getSamlBrokerIdpInitiatedUrl(REALM_CONS_NAME, "sales2")));
        assertAudience(resp, getSamlBrokerIdpInitiatedUrl(REALM_CONS_NAME, "sales2"));
        NameIDType nameId = new NameIDType();
        nameId.setFormat(URI.create(JBossSAMLURIConstants.NAMEID_FORMAT_TRANSIENT.get()));
        nameId.setValue("subjectId2");
        resp.getAssertions().get(0).getAssertion().getSubject().getSubType().addBaseID(nameId);
        Set<StatementAbstractType> statements = resp.getAssertions().get(0).getAssertion().getStatements();
        AttributeStatementType attributeType = (AttributeStatementType) statements.stream().filter(statement -> statement instanceof AttributeStatementType).findFirst().orElse(new AttributeStatementType());
        AttributeType attr = new AttributeType(X500SAMLProfileConstants.UID.get());
        attr.addAttributeValue(PROVIDER_REALM_USER_NAME);
        attributeType.addAttribute(new AttributeStatementType.ASTChoiceType(attr));
        resp.getAssertions().get(0).getAssertion().addStatement(attributeType);
        return ob;
    }).build().updateProfile().username(CONSUMER_CHOSEN_USERNAME).email("test@localhost").firstName("Firstname").lastName("Lastname").build().followOneRedirect().getSamlResponse(Binding.POST);
    assertThat(samlResponse.getSamlObject(), Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
    ResponseType resp = (ResponseType) samlResponse.getSamlObject();
    assertThat(resp.getDestination(), is(urlRealmConsumer + "/app/auth2/saml"));
    assertAudience(resp, urlRealmConsumer + "/app/auth2");
    UsersResource users = adminClient.realm(REALM_CONS_NAME).users();
    List<UserRepresentation> userList = users.search(CONSUMER_CHOSEN_USERNAME);
    assertEquals(1, userList.size());
    String id = userList.get(0).getId();
    FederatedIdentityRepresentation fed = users.get(id).getFederatedIdentity().get(0);
    assertThat(fed.getUserId(), is(PROVIDER_REALM_USER_NAME));
    assertThat(fed.getUserName(), is(PROVIDER_REALM_USER_NAME));
    // check that no user with sent subject-id was sent
    userList = users.search("subjectId1");
    assertTrue(userList.isEmpty());
    userList = users.search("subjectId2");
    assertTrue(userList.isEmpty());
}
Also used : AssertionUtil(org.keycloak.saml.processing.core.saml.v2.util.AssertionUtil) Page(org.jboss.arquillian.graphene.page.Page) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) Assert.assertThat(org.junit.Assert.assertThat) SAMLIdentityProviderConfig(org.keycloak.broker.saml.SAMLIdentityProviderConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) Map(java.util.Map) REALM_PROV_NAME(org.keycloak.testsuite.broker.BrokerTestConstants.REALM_PROV_NAME) URI(java.net.URI) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers(org.keycloak.testsuite.util.Matchers) Set(java.util.Set) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) IOUtil(org.keycloak.testsuite.utils.io.IOUtil) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Response(javax.ws.rs.core.Response) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) LoginPage(org.keycloak.testsuite.pages.LoginPage) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) SamlPrincipalType(org.keycloak.protocol.saml.SamlPrincipalType) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) Assert(org.keycloak.testsuite.Assert) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) WebDriver(org.openqa.selenium.WebDriver) X500SAMLProfileConstants(org.keycloak.saml.processing.core.saml.v2.constants.X500SAMLProfileConstants) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) UsersResource(org.keycloak.admin.client.resource.UsersResource) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) AudienceRestrictionType(org.keycloak.dom.saml.v2.assertion.AudienceRestrictionType) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) StringPropertyReplacer(org.keycloak.common.util.StringPropertyReplacer) UpdateAccountInformationPage(org.keycloak.testsuite.pages.UpdateAccountInformationPage) PageUtils(org.keycloak.testsuite.pages.PageUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) StreamUtil(org.keycloak.common.util.StreamUtil) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) Before(org.junit.Before) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Properties(java.util.Properties) JBossSAMLURIConstants(org.keycloak.saml.common.constants.JBossSAMLURIConstants) By(org.openqa.selenium.By) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) AssertionType(org.keycloak.dom.saml.v2.assertion.AssertionType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) StatementAbstractType(org.keycloak.dom.saml.v2.assertion.StatementAbstractType) REALM_CONS_NAME(org.keycloak.testsuite.broker.BrokerTestConstants.REALM_CONS_NAME) Binding(org.keycloak.testsuite.util.SamlClient.Binding) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Set(java.util.Set) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) Matchers.containsString(org.hamcrest.Matchers.containsString) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) UsersResource(org.keycloak.admin.client.resource.UsersResource) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) StatementAbstractType(org.keycloak.dom.saml.v2.assertion.StatementAbstractType) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 33 with IdentityProviderRepresentation

use of org.keycloak.representations.idm.IdentityProviderRepresentation in project keycloak by keycloak.

the class KcSamlIdPInitiatedSsoTest method testProviderIdpInitiatedLoginWithPrincipalAttribute.

// KEYCLOAK-7969
@Test
public void testProviderIdpInitiatedLoginWithPrincipalAttribute() throws Exception {
    IdentityProviderResource idp = adminClient.realm(REALM_CONS_NAME).identityProviders().get("saml-leaf");
    IdentityProviderRepresentation rep = idp.toRepresentation();
    rep.getConfig().put(SAMLIdentityProviderConfig.PRINCIPAL_TYPE, SamlPrincipalType.ATTRIBUTE.name());
    rep.getConfig().put(SAMLIdentityProviderConfig.PRINCIPAL_ATTRIBUTE, X500SAMLProfileConstants.UID.get());
    idp.update(rep);
    SAMLDocumentHolder samlResponse = new SamlClientBuilder().navigateTo(getSamlIdpInitiatedUrl(REALM_PROV_NAME, "samlbroker")).login().user(PROVIDER_REALM_USER_NAME, PROVIDER_REALM_USER_PASSWORD).build().processSamlResponse(Binding.POST).transformObject(ob -> {
        assertThat(ob, Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
        ResponseType resp = (ResponseType) ob;
        assertThat(resp.getDestination(), is(getSamlBrokerIdpInitiatedUrl(REALM_CONS_NAME, "sales")));
        assertAudience(resp, getSamlBrokerIdpInitiatedUrl(REALM_CONS_NAME, "sales"));
        Set<StatementAbstractType> statements = resp.getAssertions().get(0).getAssertion().getStatements();
        AttributeStatementType attributeType = (AttributeStatementType) statements.stream().filter(statement -> statement instanceof AttributeStatementType).findFirst().orElse(new AttributeStatementType());
        AttributeType attr = new AttributeType(X500SAMLProfileConstants.UID.get());
        attr.addAttributeValue(PROVIDER_REALM_USER_NAME);
        attributeType.addAttribute(new AttributeStatementType.ASTChoiceType(attr));
        resp.getAssertions().get(0).getAssertion().addStatement(attributeType);
        return ob;
    }).build().updateProfile().username(CONSUMER_CHOSEN_USERNAME).email("test@localhost").firstName("Firstname").lastName("Lastname").build().followOneRedirect().getSamlResponse(Binding.POST);
    assertThat(samlResponse.getSamlObject(), Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
    ResponseType resp = (ResponseType) samlResponse.getSamlObject();
    assertThat(resp.getDestination(), is(urlRealmConsumer + "/app/auth"));
    assertAudience(resp, urlRealmConsumer + "/app/auth");
    UsersResource users = adminClient.realm(REALM_CONS_NAME).users();
    String id = users.search(CONSUMER_CHOSEN_USERNAME).get(0).getId();
    FederatedIdentityRepresentation fed = users.get(id).getFederatedIdentity().get(0);
    assertThat(fed.getUserId(), is(PROVIDER_REALM_USER_NAME));
    assertThat(fed.getUserName(), is(PROVIDER_REALM_USER_NAME));
}
Also used : AssertionUtil(org.keycloak.saml.processing.core.saml.v2.util.AssertionUtil) Page(org.jboss.arquillian.graphene.page.Page) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) Assert.assertThat(org.junit.Assert.assertThat) SAMLIdentityProviderConfig(org.keycloak.broker.saml.SAMLIdentityProviderConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) Map(java.util.Map) REALM_PROV_NAME(org.keycloak.testsuite.broker.BrokerTestConstants.REALM_PROV_NAME) URI(java.net.URI) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers(org.keycloak.testsuite.util.Matchers) Set(java.util.Set) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) IOUtil(org.keycloak.testsuite.utils.io.IOUtil) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Response(javax.ws.rs.core.Response) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) LoginPage(org.keycloak.testsuite.pages.LoginPage) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) SamlPrincipalType(org.keycloak.protocol.saml.SamlPrincipalType) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) Assert(org.keycloak.testsuite.Assert) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) WebDriver(org.openqa.selenium.WebDriver) X500SAMLProfileConstants(org.keycloak.saml.processing.core.saml.v2.constants.X500SAMLProfileConstants) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) UsersResource(org.keycloak.admin.client.resource.UsersResource) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) AudienceRestrictionType(org.keycloak.dom.saml.v2.assertion.AudienceRestrictionType) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) StringPropertyReplacer(org.keycloak.common.util.StringPropertyReplacer) UpdateAccountInformationPage(org.keycloak.testsuite.pages.UpdateAccountInformationPage) PageUtils(org.keycloak.testsuite.pages.PageUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) StreamUtil(org.keycloak.common.util.StreamUtil) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) Before(org.junit.Before) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Properties(java.util.Properties) JBossSAMLURIConstants(org.keycloak.saml.common.constants.JBossSAMLURIConstants) By(org.openqa.selenium.By) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) AssertionType(org.keycloak.dom.saml.v2.assertion.AssertionType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) StatementAbstractType(org.keycloak.dom.saml.v2.assertion.StatementAbstractType) REALM_CONS_NAME(org.keycloak.testsuite.broker.BrokerTestConstants.REALM_CONS_NAME) Binding(org.keycloak.testsuite.util.SamlClient.Binding) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Set(java.util.Set) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) Matchers.containsString(org.hamcrest.Matchers.containsString) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) IdentityProviderResource(org.keycloak.admin.client.resource.IdentityProviderResource) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) UsersResource(org.keycloak.admin.client.resource.UsersResource) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 34 with IdentityProviderRepresentation

use of org.keycloak.representations.idm.IdentityProviderRepresentation in project keycloak by keycloak.

the class KcOidcBrokerVaultConfiguration method setUpIdentityProvider.

@Override
public IdentityProviderRepresentation setUpIdentityProvider(IdentityProviderSyncMode syncMode) {
    IdentityProviderRepresentation idpRep = super.setUpIdentityProvider(syncMode);
    idpRep.getConfig().put("clientSecret", VAULT_CLIENT_SECRET);
    return idpRep;
}
Also used : IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation)

Example 35 with IdentityProviderRepresentation

use of org.keycloak.representations.idm.IdentityProviderRepresentation in project keycloak by keycloak.

the class KcOidcFirstBrokerLoginTest method testLinkAccountByReauthenticationWithDifferentBroker.

/**
 * Tests that duplication is detected and user wants to link federatedIdentity with existing account. He will confirm link by reauthentication
 * with different broker already linked to his account
 */
@Test
public void testLinkAccountByReauthenticationWithDifferentBroker() {
    KcSamlBrokerConfiguration samlBrokerConfig = KcSamlBrokerConfiguration.INSTANCE;
    ClientRepresentation samlClient = samlBrokerConfig.createProviderClients().get(0);
    IdentityProviderRepresentation samlBroker = samlBrokerConfig.setUpIdentityProvider();
    RealmResource consumerRealm = adminClient.realm(bc.consumerRealmName());
    try {
        updateExecutions(AbstractBrokerTest::disableUpdateProfileOnFirstLogin);
        adminClient.realm(bc.providerRealmName()).clients().create(samlClient);
        consumerRealm.identityProviders().create(samlBroker);
        driver.navigate().to(getAccountUrl(getConsumerRoot(), bc.consumerRealmName()));
        logInWithBroker(samlBrokerConfig);
        waitForAccountManagementTitle();
        accountUpdateProfilePage.assertCurrent();
        logoutFromRealm(getConsumerRoot(), bc.consumerRealmName());
        logInWithBroker(bc);
        waitForPage(driver, "account already exists", false);
        assertTrue(idpConfirmLinkPage.isCurrent());
        assertEquals("User with email user@localhost.com already exists. How do you want to continue?", idpConfirmLinkPage.getMessage());
        idpConfirmLinkPage.clickLinkAccount();
        assertEquals("Authenticate to link your account with " + bc.getIDPAlias(), loginPage.getInfoMessage());
        try {
            this.loginPage.findSocialButton(bc.getIDPAlias());
            org.junit.Assert.fail("Not expected to see social button with " + samlBrokerConfig.getIDPAlias());
        } catch (NoSuchElementException expected) {
        }
        log.debug("Clicking social " + samlBrokerConfig.getIDPAlias());
        loginPage.clickSocial(samlBrokerConfig.getIDPAlias());
        waitForAccountManagementTitle();
        accountUpdateProfilePage.assertCurrent();
        assertNumFederatedIdentities(consumerRealm.users().search(samlBrokerConfig.getUserLogin()).get(0).getId(), 2);
    } finally {
        updateExecutions(AbstractBrokerTest::setUpMissingUpdateProfileOnFirstLogin);
        removeUserByUsername(consumerRealm, "consumer");
    }
}
Also used : RealmResource(org.keycloak.admin.client.resource.RealmResource) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) NoSuchElementException(org.openqa.selenium.NoSuchElementException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Aggregations

IdentityProviderRepresentation (org.keycloak.representations.idm.IdentityProviderRepresentation)91 Test (org.junit.Test)45 IdentityProviderResource (org.keycloak.admin.client.resource.IdentityProviderResource)23 RealmResource (org.keycloak.admin.client.resource.RealmResource)22 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)17 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)16 Response (javax.ws.rs.core.Response)15 Matchers.containsString (org.hamcrest.Matchers.containsString)10 List (java.util.List)9 MultipartFormDataOutput (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput)8 URL (java.net.URL)7 IdentityProviderMapperRepresentation (org.keycloak.representations.idm.IdentityProviderMapperRepresentation)7 OAuthClient (org.keycloak.testsuite.util.OAuthClient)7 IOException (java.io.IOException)6 URI (java.net.URI)6 Map (java.util.Map)6 Matchers.hasSize (org.hamcrest.Matchers.hasSize)6 Matchers.is (org.hamcrest.Matchers.is)6 SAMLIdentityProviderConfig (org.keycloak.broker.saml.SAMLIdentityProviderConfig)6 AttributeType (org.keycloak.dom.saml.v2.assertion.AttributeType)6