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));
}
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");
}
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);
}
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());
}
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();
}
Aggregations