Search in sources :

Example 1 with Keycloak

use of org.keycloak.admin.client.Keycloak in project airavata by apache.

the class TenantManagementKeycloakImpl method updateUserProfile.

@Override
public void updateUserProfile(PasswordCredential realmAdminCreds, String tenantId, String username, UserProfile userDetails) throws IamAdminServicesException {
    Keycloak client = null;
    try {
        client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
        List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
        if (!retrieveUserList.isEmpty()) {
            UserRepresentation userRepresentation = retrieveUserList.get(0);
            userRepresentation.setFirstName(userDetails.getFirstName());
            userRepresentation.setLastName(userDetails.getLastName());
            userRepresentation.setEmail(userDetails.getEmails().get(0));
            UserResource userResource = client.realm(tenantId).users().get(userRepresentation.getId());
            userResource.update(userRepresentation);
        } else {
            throw new IamAdminServicesException("User [" + username + "] wasn't found in Keycloak!");
        }
    } catch (ApplicationSettingsException ex) {
        logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
        throw exception;
    } catch (Exception ex) {
        logger.error("Error updating user profile in keycloak server, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error updating user profile in keycloak server, reason: " + ex.getMessage());
        throw exception;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) UserResource(org.keycloak.admin.client.resource.UserResource) Keycloak(org.keycloak.admin.client.Keycloak) IOException(java.io.IOException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)

Example 2 with Keycloak

use of org.keycloak.admin.client.Keycloak in project airavata by apache.

the class TenantManagementKeycloakImpl method addRoleToUser.

@Override
public boolean addRoleToUser(PasswordCredential realmAdminCreds, String tenantId, String username, String roleName) throws IamAdminServicesException {
    Keycloak client = null;
    try {
        client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
        List<UserRepresentation> retrieveCreatedUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
        UserResource retrievedUser = client.realm(tenantId).users().get(retrieveCreatedUserList.get(0).getId());
        // Add user to the role
        RoleResource roleResource = client.realm(tenantId).roles().get(roleName);
        retrievedUser.roles().realmLevel().add(Arrays.asList(roleResource.toRepresentation()));
        return true;
    } catch (ApplicationSettingsException ex) {
        logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
        throw exception;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) RoleResource(org.keycloak.admin.client.resource.RoleResource) UserResource(org.keycloak.admin.client.resource.UserResource) Keycloak(org.keycloak.admin.client.Keycloak)

Example 3 with Keycloak

use of org.keycloak.admin.client.Keycloak in project airavata by apache.

the class TenantManagementKeycloakImpl method resetUserPassword.

@Override
public boolean resetUserPassword(PasswordCredential realmAdminCreds, String tenantId, String username, String newPassword) throws IamAdminServicesException {
    Keycloak client = null;
    try {
        client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
        List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
        if (!retrieveUserList.isEmpty()) {
            UserResource retrievedUser = client.realm(tenantId).users().get(retrieveUserList.get(0).getId());
            CredentialRepresentation credential = new CredentialRepresentation();
            credential.setType(CredentialRepresentation.PASSWORD);
            credential.setValue(newPassword);
            credential.setTemporary(false);
            retrievedUser.resetPassword(credential);
            // Remove the UPDATE_PASSWORD required action
            UserRepresentation userRepresentation = retrievedUser.toRepresentation();
            userRepresentation.getRequiredActions().remove("UPDATE_PASSWORD");
            retrievedUser.update(userRepresentation);
            return true;
        } else {
            logger.error("requested User not found");
            return false;
        }
    } catch (ApplicationSettingsException ex) {
        logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
        throw exception;
    } catch (Exception ex) {
        logger.error("Error resetting user password in keycloak server, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error resetting user password in keycloak server, reason: " + ex.getMessage());
        throw exception;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) UserResource(org.keycloak.admin.client.resource.UserResource) Keycloak(org.keycloak.admin.client.Keycloak) IOException(java.io.IOException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)

Example 4 with Keycloak

use of org.keycloak.admin.client.Keycloak in project airavata by apache.

the class TenantManagementKeycloakImpl method findUser.

@Override
public List<UserProfile> findUser(PasswordCredential realmAdminCreds, String tenantId, String email, String userName) throws IamAdminServicesException {
    Keycloak client = null;
    try {
        client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
        List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(userName, null, null, email, 0, 1);
        if (!retrieveUserList.isEmpty()) {
            List<UserProfile> userList = new ArrayList<>();
            for (UserRepresentation user : retrieveUserList) {
                UserProfile profile = new UserProfile();
                profile.setUserId(user.getUsername());
                profile.setFirstName(user.getFirstName());
                profile.setLastName(user.getLastName());
                profile.setEmails(Arrays.asList(new String[] { user.getEmail() }));
                userList.add(profile);
            }
            return userList;
        } else {
            logger.error("requested User not found");
            return null;
        }
    } catch (ApplicationSettingsException ex) {
        logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
        throw exception;
    } catch (Exception ex) {
        logger.error("Error finding user in keycloak server, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error finding user in keycloak server, reason: " + ex.getMessage());
        throw exception;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UserProfile(org.apache.airavata.model.user.UserProfile) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) ArrayList(java.util.ArrayList) Keycloak(org.keycloak.admin.client.Keycloak) IOException(java.io.IOException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)

Example 5 with Keycloak

use of org.keycloak.admin.client.Keycloak in project airavata by apache.

the class TenantManagementKeycloakImpl method removeRoleFromUser.

@Override
public boolean removeRoleFromUser(PasswordCredential realmAdminCreds, String tenantId, String username, String roleName) throws IamAdminServicesException {
    Keycloak client = null;
    try {
        client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
        List<UserRepresentation> retrieveCreatedUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
        UserResource retrievedUser = client.realm(tenantId).users().get(retrieveCreatedUserList.get(0).getId());
        // Remove role from user
        RoleResource roleResource = client.realm(tenantId).roles().get(roleName);
        retrievedUser.roles().realmLevel().remove(Arrays.asList(roleResource.toRepresentation()));
        return true;
    } catch (ApplicationSettingsException ex) {
        logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
        IamAdminServicesException exception = new IamAdminServicesException();
        exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
        throw exception;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) IamAdminServicesException(org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException) RoleResource(org.keycloak.admin.client.resource.RoleResource) UserResource(org.keycloak.admin.client.resource.UserResource) Keycloak(org.keycloak.admin.client.Keycloak)

Aggregations

Keycloak (org.keycloak.admin.client.Keycloak)12 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)11 IamAdminServicesException (org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException)11 UserResource (org.keycloak.admin.client.resource.UserResource)9 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)4 Response (javax.ws.rs.core.Response)4 RoleResource (org.keycloak.admin.client.resource.RoleResource)3 UserProfile (org.apache.airavata.model.user.UserProfile)2 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)1 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)1 KeycloakBuilder (org.keycloak.admin.client.KeycloakBuilder)1 CredentialRepresentation (org.keycloak.representations.idm.CredentialRepresentation)1 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)1 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)1