Search in sources :

Example 11 with FederatedIdentityRepresentation

use of org.keycloak.representations.idm.FederatedIdentityRepresentation 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 12 with FederatedIdentityRepresentation

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

the class ReAuthenticationTest method identityFirstFormReauthenticationWithGithubLink.

// Re-authentication with user form separate to the password form. The username form is shown due the user linked with "github"
@Test
@AuthServerContainerExclude(REMOTE)
public void identityFirstFormReauthenticationWithGithubLink() {
    // Set identity-first as realm flow
    setupIdentityFirstFlow();
    // Add fake federated link to the user
    UserResource user = ApiUtil.findUserByUsernameId(testRealm(), "test-user@localhost");
    FederatedIdentityRepresentation fedLink = FederatedIdentityBuilder.create().identityProvider("github").userId("123").userName("test").build();
    user.addFederatedIdentity("github", fedLink);
    // Login user
    loginPage.open();
    loginUsernameOnlyPage.assertCurrent();
    loginUsernameOnlyPage.login("test-user@localhost");
    passwordPage.assertCurrent();
    passwordPage.login("password");
    Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
    // See that user can re-authenticate with the github link present on the page as user has link to github social provider
    setTimeOffset(10);
    oauth.maxAge("1");
    loginPage.open();
    // Username input hidden as well as register and rememberMe. Info message should be present
    loginPage.assertCurrent();
    assertUsernameFieldAndOtherFields(false);
    assertInfoMessageAboutReAuthenticate(true);
    // Check there is NO password field
    Assert.assertThat(true, is(driver.findElements(By.id("password")).isEmpty()));
    // Github present, Google hidden
    assertSocialButtonsPresent(true, false);
    // Confirm login with password
    loginUsernameOnlyPage.clickSubmitButton();
    // Login with password. Info message should not be there anymore
    passwordPage.assertCurrent();
    passwordPage.login("password");
    assertInfoMessageAboutReAuthenticate(false);
    Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
    // Remove link and flow
    user.removeFederatedIdentity("github");
    BrowserFlowTest.revertFlows(testRealm(), "browser - identity first");
}
Also used : UserResource(org.keycloak.admin.client.resource.UserResource) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) SocialLoginTest(org.keycloak.testsuite.broker.SocialLoginTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 13 with FederatedIdentityRepresentation

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

the class ManyUsersTest method createSocialLink.

private void createSocialLink(String provider, UsersResource users, String userId) {
    String uuid = UUID.randomUUID().toString();
    FederatedIdentityRepresentation link = new FederatedIdentityRepresentation();
    link.setIdentityProvider(provider);
    link.setUserId(uuid);
    link.setUserName(uuid);
    users.get(userId).addFederatedIdentity(provider, link);
}
Also used : FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Example 14 with FederatedIdentityRepresentation

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

the class LinkedAccountsTest method unlinkAccountTest.

@Test
public void unlinkAccountTest() {
    FederatedIdentityRepresentation fid = new FederatedIdentityRepresentation();
    fid.setIdentityProvider(SOCIAL_IDP_ALIAS);
    fid.setUserId("Homer lost his ID at Moe's last night");
    fid.setUserName(homerUser.getUsername());
    testUserResource().addFederatedIdentity(SOCIAL_IDP_ALIAS, fid);
    assertEquals(1, testUserResource().getFederatedIdentity().size());
    linkedAccountsPage.navigateTo();
    assertProvider(systemIdp, false, false, "");
    assertProvider(socialIdp, true, true, homerUser.getUsername());
    socialIdp.clickUnlinkBtn();
    linkedAccountsPage.assertCurrent();
    assertProvider(systemIdp, false, false, "");
    assertProvider(socialIdp, false, true, "");
    assertEquals(0, testUserResource().getFederatedIdentity().size());
}
Also used : FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Test(org.junit.Test)

Example 15 with FederatedIdentityRepresentation

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

the class LinkedAccountsTest method linkAccountTest.

@Test
public void linkAccountTest() {
    assertEquals(0, testUserResource().getFederatedIdentity().size());
    assertProvider(socialIdp, false, true, "");
    assertProvider(systemIdp, false, false, "");
    systemIdp.clickLinkBtn();
    loginPage.form().login(homerUser);
    linkedAccountsPage.assertCurrent();
    assertProvider(systemIdp, true, false, homerUser.getUsername());
    assertProvider(socialIdp, false, true, "");
    // check through admin REST endpoints
    List<FederatedIdentityRepresentation> fids = testUserResource().getFederatedIdentity();
    assertEquals(1, fids.size());
    FederatedIdentityRepresentation fid = fids.get(0);
    assertEquals(SYSTEM_IDP_ALIAS, fid.getIdentityProvider());
    assertEquals(homerUser.getUsername(), fid.getUserName());
    // try to login using IdP
    deleteAllSessionsInTestRealm();
    linkedAccountsPage.navigateTo();
    loginPageWithSocialBtns.clickSocial(SYSTEM_IDP_ALIAS);
    // no need for re-login to REALM2
    linkedAccountsPage.assertCurrent();
}
Also used : FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Test(org.junit.Test)

Aggregations

FederatedIdentityRepresentation (org.keycloak.representations.idm.FederatedIdentityRepresentation)30 Test (org.junit.Test)18 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)12 RealmResource (org.keycloak.admin.client.resource.RealmResource)9 LinkedList (java.util.LinkedList)7 List (java.util.List)7 UriBuilder (javax.ws.rs.core.UriBuilder)6 ArrayList (java.util.ArrayList)5 AbstractServletsAdapterTest (org.keycloak.testsuite.adapter.AbstractServletsAdapterTest)5 Map (java.util.Map)4 UserResource (org.keycloak.admin.client.resource.UserResource)4 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 CredentialRepresentation (org.keycloak.representations.idm.CredentialRepresentation)4 IdentityProviderRepresentation (org.keycloak.representations.idm.IdentityProviderRepresentation)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Response (javax.ws.rs.core.Response)3 UsersResource (org.keycloak.admin.client.resource.UsersResource)3 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)3 UserConsentRepresentation (org.keycloak.representations.idm.UserConsentRepresentation)3