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