Search in sources :

Example 1 with IdentityProvider

use of io.gravitee.repository.management.model.IdentityProvider in project gravitee-management-rest-api by gravitee-io.

the class IdentityProviderServiceImpl method convert.

private IdentityProvider convert(UpdateIdentityProviderEntity updateIdentityProvider) {
    IdentityProvider identityProvider = new IdentityProvider();
    identityProvider.setName(updateIdentityProvider.getName());
    identityProvider.setDescription(updateIdentityProvider.getDescription());
    identityProvider.setEnabled(updateIdentityProvider.isEnabled());
    identityProvider.setConfiguration(updateIdentityProvider.getConfiguration());
    identityProvider.setUserProfileMapping(updateIdentityProvider.getUserProfileMapping());
    identityProvider.setEmailRequired(updateIdentityProvider.isEmailRequired());
    identityProvider.setSyncMappings(updateIdentityProvider.isSyncMappings());
    if (updateIdentityProvider.getGroupMappings() != null && !updateIdentityProvider.getGroupMappings().isEmpty()) {
        identityProvider.setGroupMappings(updateIdentityProvider.getGroupMappings().stream().collect(Collectors.toMap(GroupMappingEntity::getCondition, groupMappingEntity -> {
            String[] groups = new String[groupMappingEntity.getGroups().size()];
            return groupMappingEntity.getGroups().toArray(groups);
        })));
    }
    if (updateIdentityProvider.getRoleMappings() != null && !updateIdentityProvider.getRoleMappings().isEmpty()) {
        identityProvider.setRoleMappings(updateIdentityProvider.getRoleMappings().stream().collect(Collectors.toMap(RoleMappingEntity::getCondition, roleMapping -> {
            List<String> lstRoles = new ArrayList<>();
            if (roleMapping.getOrganizations() != null && !roleMapping.getOrganizations().isEmpty()) {
                roleMapping.getOrganizations().forEach(organizationRoleName -> {
                    // Ensure that the role is existing
                    roleService.findByScopeAndName(RoleScope.ORGANIZATION, organizationRoleName);
                    lstRoles.add(io.gravitee.repository.management.model.RoleScope.ORGANIZATION.name() + ":" + organizationRoleName);
                });
            }
            if (roleMapping.getEnvironments() != null && !roleMapping.getEnvironments().isEmpty()) {
                roleMapping.getEnvironments().forEach((environmentId, environmentRoles) -> {
                    // Ensure that the role is existing
                    environmentRoles.forEach(environmentRoleName -> {
                        roleService.findByScopeAndName(RoleScope.ENVIRONMENT, environmentRoleName);
                        lstRoles.add(io.gravitee.repository.management.model.RoleScope.ENVIRONMENT.name() + ":" + environmentId + ":" + environmentRoleName);
                    });
                });
            }
            String[] roles = new String[lstRoles.size()];
            return lstRoles.toArray(roles);
        })));
    }
    return identityProvider;
}
Also used : IdentityProvider(io.gravitee.repository.management.model.IdentityProvider)

Example 2 with IdentityProvider

use of io.gravitee.repository.management.model.IdentityProvider in project gravitee-management-rest-api by gravitee-io.

the class IdentityProviderServiceImpl method create.

@Override
public IdentityProviderEntity create(NewIdentityProviderEntity newIdentityProviderEntity) {
    try {
        LOGGER.debug("Create identity provider {}", newIdentityProviderEntity);
        Optional<IdentityProvider> optIdentityProvider = identityProviderRepository.findById(IdGenerator.generate(newIdentityProviderEntity.getName()));
        if (optIdentityProvider.isPresent()) {
            throw new IdentityProviderAlreadyExistsException(newIdentityProviderEntity.getName());
        }
        IdentityProvider identityProvider = convert(newIdentityProviderEntity);
        identityProvider.setOrganizationId(GraviteeContext.getCurrentOrganization());
        // If provider is a social type, we must ensure required parameters
        if (identityProvider.getType() == IdentityProviderType.GOOGLE || identityProvider.getType() == IdentityProviderType.GITHUB) {
            checkSocialProvider(identityProvider);
        }
        // Set date fields
        identityProvider.setCreatedAt(new Date());
        identityProvider.setUpdatedAt(identityProvider.getCreatedAt());
        IdentityProvider createdIdentityProvider = identityProviderRepository.create(identityProvider);
        identityProviderActivationService.activateIdpOnTargets(createdIdentityProvider.getId(), new IdentityProviderActivationService.ActivationTarget(GraviteeContext.getCurrentOrganization(), IdentityProviderActivationReferenceType.ORGANIZATION));
        auditService.createOrganizationAuditLog(singletonMap(IDENTITY_PROVIDER, createdIdentityProvider.getId()), IdentityProvider.AuditEvent.IDENTITY_PROVIDER_CREATED, createdIdentityProvider.getUpdatedAt(), null, createdIdentityProvider);
        return convert(createdIdentityProvider);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create identity provider {}", newIdentityProviderEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying to create " + newIdentityProviderEntity, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) IdentityProviderActivationService(io.gravitee.rest.api.service.configuration.identity.IdentityProviderActivationService) IdentityProvider(io.gravitee.repository.management.model.IdentityProvider) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with IdentityProvider

use of io.gravitee.repository.management.model.IdentityProvider in project gravitee-management-rest-api by gravitee-io.

the class IdentityProviderServiceImpl method convert.

private IdentityProvider convert(NewIdentityProviderEntity newIdentityProviderEntity) {
    IdentityProvider identityProvider = new IdentityProvider();
    identityProvider.setId(IdGenerator.generate(newIdentityProviderEntity.getName()));
    identityProvider.setName(newIdentityProviderEntity.getName());
    identityProvider.setDescription(newIdentityProviderEntity.getDescription());
    identityProvider.setConfiguration(newIdentityProviderEntity.getConfiguration());
    identityProvider.setType(IdentityProviderType.valueOf(newIdentityProviderEntity.getType().name().toUpperCase()));
    identityProvider.setEnabled(newIdentityProviderEntity.isEnabled());
    identityProvider.setUserProfileMapping(newIdentityProviderEntity.getUserProfileMapping());
    identityProvider.setEmailRequired(newIdentityProviderEntity.isEmailRequired());
    identityProvider.setSyncMappings(newIdentityProviderEntity.isSyncMappings());
    return identityProvider;
}
Also used : IdentityProvider(io.gravitee.repository.management.model.IdentityProvider)

Example 4 with IdentityProvider

use of io.gravitee.repository.management.model.IdentityProvider in project gravitee-management-rest-api by gravitee-io.

the class IdentityProviderServiceImpl method update.

@Override
public IdentityProviderEntity update(String id, UpdateIdentityProviderEntity updateIdentityProvider) {
    try {
        LOGGER.debug("Update identity provider {}", updateIdentityProvider);
        Optional<IdentityProvider> optIdentityProvider = identityProviderRepository.findById(id);
        if (!optIdentityProvider.isPresent()) {
            throw new IdentityProviderNotFoundException(updateIdentityProvider.getName());
        }
        // TODO: Find a way to validate mapping expression
        IdentityProvider identityProvider = convert(updateIdentityProvider);
        final IdentityProvider idpToUpdate = optIdentityProvider.get();
        identityProvider.setId(id);
        identityProvider.setType(idpToUpdate.getType());
        identityProvider.setCreatedAt(idpToUpdate.getCreatedAt());
        identityProvider.setUpdatedAt(new Date());
        identityProvider.setOrganizationId(optIdentityProvider.get().getOrganizationId());
        IdentityProvider updatedIdentityProvider = identityProviderRepository.update(identityProvider);
        // Audit
        auditService.createOrganizationAuditLog(singletonMap(IDENTITY_PROVIDER, id), IdentityProvider.AuditEvent.IDENTITY_PROVIDER_UPDATED, identityProvider.getUpdatedAt(), idpToUpdate, updatedIdentityProvider);
        return convert(updatedIdentityProvider);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update identity provider {}", updateIdentityProvider, ex);
        throw new TechnicalManagementException("An error occurs while trying to update " + updateIdentityProvider, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) IdentityProvider(io.gravitee.repository.management.model.IdentityProvider) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with IdentityProvider

use of io.gravitee.repository.management.model.IdentityProvider in project gravitee-management-rest-api by gravitee-io.

the class V3UpgraderServiceTest method shouldDoNothing.

@Test
public void shouldDoNothing() throws TechnicalException {
    String[] roles = { "ORGANIZATION:ADMIN", "ENVIRONMENT:USER" };
    Map<String, String[]> roleMappings = new HashMap<>();
    roleMappings.put("KEY", roles);
    IdentityProvider idp = new IdentityProvider();
    idp.setId("my-idp");
    idp.setRoleMappings(roleMappings);
    idp.setOrganizationId("DEFAULT");
    when(identityProviderRepository.findAll()).thenReturn(Collections.singleton(idp));
    service.convertIDPRoleMapping();
    verify(roleRepository, never()).findByScopeAndNameAndReferenceIdAndReferenceType(any(), any(), any(), any());
    verify(identityProviderRepository, never()).update(any());
}
Also used : HashMap(java.util.HashMap) IdentityProvider(io.gravitee.repository.management.model.IdentityProvider) Test(org.junit.Test)

Aggregations

IdentityProvider (io.gravitee.repository.management.model.IdentityProvider)10 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Role (io.gravitee.repository.management.model.Role)1 IdentityProviderActivationService (io.gravitee.rest.api.service.configuration.identity.IdentityProviderActivationService)1