Search in sources :

Example 26 with FederatedIdentityRepresentation

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

the class UserTest method searchByIdp.

@Test
public void searchByIdp() {
    // Add user without IDP
    createUser();
    // add sample Identity Providers
    final String identityProviderAlias1 = "identity-provider-alias1";
    addSampleIdentityProvider(identityProviderAlias1, 0);
    final String identityProviderAlias2 = "identity-provider-alias2";
    addSampleIdentityProvider(identityProviderAlias2, 1);
    final String commonIdpUserId = "commonIdpUserId";
    // create first IDP1 User with link
    final String idp1User1Username = "idp1user1";
    final String idp1User1KeycloakId = createUser(idp1User1Username, "idp1user1@localhost");
    final String idp1User1UserId = "idp1user1Id";
    FederatedIdentityRepresentation link1_1 = new FederatedIdentityRepresentation();
    link1_1.setUserId(idp1User1UserId);
    link1_1.setUserName(idp1User1Username);
    addFederatedIdentity(idp1User1KeycloakId, identityProviderAlias1, link1_1);
    // create second IDP1 User with link
    final String idp1User2Username = "idp1user2";
    final String idp1User2KeycloakId = createUser(idp1User2Username, "idp1user2@localhost");
    FederatedIdentityRepresentation link1_2 = new FederatedIdentityRepresentation();
    link1_2.setUserId(commonIdpUserId);
    link1_2.setUserName(idp1User2Username);
    addFederatedIdentity(idp1User2KeycloakId, identityProviderAlias1, link1_2);
    // create IDP2 user with link
    final String idp2UserUsername = "idp2user";
    final String idp2UserKeycloakId = createUser(idp2UserUsername, "idp2user@localhost");
    FederatedIdentityRepresentation link2 = new FederatedIdentityRepresentation();
    link2.setUserId(commonIdpUserId);
    link2.setUserName(idp2UserUsername);
    addFederatedIdentity(idp2UserKeycloakId, identityProviderAlias2, link2);
    // run search tests
    List<UserRepresentation> searchForAllUsers = realm.users().search(null, null, null, null, null, null, null, null, null, null, null);
    assertEquals(4, searchForAllUsers.size());
    List<UserRepresentation> searchByIdpAlias = realm.users().search(null, null, null, null, null, identityProviderAlias1, null, null, null, null, null);
    assertEquals(2, searchByIdpAlias.size());
    assertEquals(idp1User1Username, searchByIdpAlias.get(0).getUsername());
    assertEquals(idp1User2Username, searchByIdpAlias.get(1).getUsername());
    List<UserRepresentation> searchByIdpUserId = realm.users().search(null, null, null, null, null, null, commonIdpUserId, null, null, null, null);
    assertEquals(2, searchByIdpUserId.size());
    assertEquals(idp1User2Username, searchByIdpUserId.get(0).getUsername());
    assertEquals(idp2UserUsername, searchByIdpUserId.get(1).getUsername());
    List<UserRepresentation> searchByIdpAliasAndUserId = realm.users().search(null, null, null, null, null, identityProviderAlias1, idp1User1UserId, null, null, null, null);
    assertEquals(1, searchByIdpAliasAndUserId.size());
    assertEquals(idp1User1Username, searchByIdpAliasAndUserId.get(0).getUsername());
}
Also used : FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 27 with FederatedIdentityRepresentation

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

the class RepresentationToModel method createFederatedIdentities.

public static void createFederatedIdentities(UserRepresentation userRep, KeycloakSession session, RealmModel realm, UserModel user) {
    if (userRep.getFederatedIdentities() != null) {
        for (FederatedIdentityRepresentation identity : userRep.getFederatedIdentities()) {
            FederatedIdentityModel mappingModel = new FederatedIdentityModel(identity.getIdentityProvider(), identity.getUserId(), identity.getUserName());
            session.users().addFederatedIdentity(realm, user, mappingModel);
        }
    }
}
Also used : FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Example 28 with FederatedIdentityRepresentation

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

the class RepresentationToModel method importFederatedUser.

public static void importFederatedUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep) {
    UserFederatedStorageProvider federatedStorage = session.userFederatedStorage();
    if (userRep.getAttributes() != null) {
        for (Map.Entry<String, List<String>> entry : userRep.getAttributes().entrySet()) {
            String key = entry.getKey();
            List<String> value = entry.getValue();
            if (value != null) {
                federatedStorage.setAttribute(newRealm, userRep.getId(), key, new LinkedList<>(value));
            }
        }
    }
    if (userRep.getRequiredActions() != null) {
        for (String action : userRep.getRequiredActions()) {
            federatedStorage.addRequiredAction(newRealm, userRep.getId(), action);
        }
    }
    if (userRep.getCredentials() != null) {
        for (CredentialRepresentation cred : userRep.getCredentials()) {
            federatedStorage.createCredential(newRealm, userRep.getId(), toModel(cred));
        }
    }
    createFederatedRoleMappings(federatedStorage, userRep, newRealm);
    if (userRep.getGroups() != null) {
        for (String path : userRep.getGroups()) {
            GroupModel group = KeycloakModelUtils.findGroupByPath(newRealm, path);
            if (group == null) {
                throw new RuntimeException("Unable to find group specified by path: " + path);
            }
            federatedStorage.joinGroup(newRealm, userRep.getId(), group);
        }
    }
    if (userRep.getFederatedIdentities() != null) {
        for (FederatedIdentityRepresentation identity : userRep.getFederatedIdentities()) {
            FederatedIdentityModel mappingModel = new FederatedIdentityModel(identity.getIdentityProvider(), identity.getUserId(), identity.getUserName());
            federatedStorage.addFederatedIdentity(newRealm, userRep.getId(), mappingModel);
        }
    }
    if (userRep.getClientConsents() != null) {
        for (UserConsentRepresentation consentRep : userRep.getClientConsents()) {
            UserConsentModel consentModel = toModel(newRealm, consentRep);
            federatedStorage.addConsent(newRealm, userRep.getId(), consentModel);
        }
    }
    if (userRep.getNotBefore() != null) {
        federatedStorage.setNotBeforeForUser(newRealm, userRep.getId(), userRep.getNotBefore());
    }
}
Also used : FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) GroupModel(org.keycloak.models.GroupModel) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) UserConsentModel(org.keycloak.models.UserConsentModel) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Example 29 with FederatedIdentityRepresentation

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

the class RepresentationToModel method convertDeprecatedSocialProviders.

private static void convertDeprecatedSocialProviders(UserRepresentation user) {
    if (user.getSocialLinks() != null && !user.getSocialLinks().isEmpty() && user.getFederatedIdentities() == null) {
        logger.warnf("Using deprecated 'socialLinks' configuration in JSON representation for user '%s'. It will be removed in future versions", user.getUsername());
        List<FederatedIdentityRepresentation> federatedIdentities = new LinkedList<>();
        for (SocialLinkRepresentation social : user.getSocialLinks()) {
            FederatedIdentityRepresentation federatedIdentity = new FederatedIdentityRepresentation();
            federatedIdentity.setIdentityProvider(social.getSocialProvider());
            federatedIdentity.setUserId(social.getSocialUserId());
            federatedIdentity.setUserName(social.getSocialUsername());
            federatedIdentities.add(federatedIdentity);
        }
        user.setFederatedIdentities(federatedIdentities);
    }
    user.setSocialLinks(null);
}
Also used : SocialLinkRepresentation(org.keycloak.representations.idm.SocialLinkRepresentation) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) LinkedList(java.util.LinkedList)

Example 30 with FederatedIdentityRepresentation

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

the class ReAuthenticationTest method usernamePasswordFormReauthentication.

@Test
public void usernamePasswordFormReauthentication() {
    // Add fake github link to user account
    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();
    loginPage.assertCurrent();
    assertUsernameFieldAndOtherFields(true);
    assertSocialButtonsPresent(true, true);
    loginPage.login("test-user@localhost", "password");
    Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
    // Set time offset
    setTimeOffset(10);
    // Request re-authentication
    oauth.maxAge("1");
    loginPage.open();
    loginPage.assertCurrent();
    // Username input hidden as well as register and rememberMe. Info message should be shown
    assertUsernameFieldAndOtherFields(false);
    assertInfoMessageAboutReAuthenticate(true);
    // Assert github link present as it is linked to user account. Google link should be hidden
    assertSocialButtonsPresent(true, false);
    // Try bad password and assert things still hidden
    loginPage.login("bad-password");
    loginPage.assertCurrent();
    Assert.assertEquals("Invalid password.", loginPage.getInputError());
    assertUsernameFieldAndOtherFields(false);
    assertInfoMessageAboutReAuthenticate(false);
    loginPage.login("password");
    Assert.assertEquals(AppPage.RequestType.AUTH_RESPONSE, appPage.getRequestType());
    // Remove link
    user.removeFederatedIdentity("github");
}
Also used : UserResource(org.keycloak.admin.client.resource.UserResource) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) SocialLoginTest(org.keycloak.testsuite.broker.SocialLoginTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

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