Search in sources :

Example 1 with IdentityProviderManagementClientException

use of org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException in project carbon-identity-framework by wso2.

the class IdentityProviderManager method getExpressionNodes.

/**
 * Get the filter node as a list.
 *
 * @param filter value of the filter.
 * @return node tree.
 * @throws IdentityProviderManagementClientException Error when validate filters.
 */
private List<ExpressionNode> getExpressionNodes(String filter) throws IdentityProviderManagementClientException {
    // Filter example : name sw "te" and name ew "st" and isEnabled eq "true".
    List<ExpressionNode> expressionNodes = new ArrayList<>();
    FilterTreeBuilder filterTreeBuilder;
    try {
        if (StringUtils.isNotBlank(filter)) {
            filterTreeBuilder = new FilterTreeBuilder(filter);
            Node rootNode = filterTreeBuilder.buildTree();
            setExpressionNodeList(rootNode, expressionNodes);
        }
    } catch (IOException | IdentityException e) {
        String message = "Error occurred while validate filter, filter: " + filter;
        throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message, e);
    }
    return expressionNodes;
}
Also used : FilterTreeBuilder(org.wso2.carbon.identity.core.model.FilterTreeBuilder) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) OperationNode(org.wso2.carbon.identity.core.model.OperationNode) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) Node(org.wso2.carbon.identity.core.model.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 2 with IdentityProviderManagementClientException

use of org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException in project carbon-identity-framework by wso2.

the class IdPManagementDAO method appendFilterQuery.

/**
 * Create a sql query and prepared statement for filter.
 *
 * @param expressionNodes    list of filters.
 * @param filterQueryBuilder Sql builder object.
 * @throws IdentityProviderManagementClientException throw invalid filer attribute exception.
 */
private void appendFilterQuery(List<ExpressionNode> expressionNodes, FilterQueryBuilder filterQueryBuilder) throws IdentityProviderManagementClientException {
    StringBuilder filter = new StringBuilder();
    if (CollectionUtils.isEmpty(expressionNodes)) {
        filterQueryBuilder.setFilterQuery(IdPManagementConstants.EMPTY_STRING);
    } else {
        for (ExpressionNode expressionNode : expressionNodes) {
            String operation = expressionNode.getOperation();
            String value = expressionNode.getValue();
            String attributeName = expressionNode.getAttributeValue();
            if (StringUtils.isNotBlank(attributeName) && StringUtils.isNotBlank(value) && StringUtils.isNotBlank(operation)) {
                switch(attributeName) {
                    case IdPManagementConstants.IDP_NAME:
                        attributeName = IdPManagementConstants.NAME;
                        break;
                    case IdPManagementConstants.IDP_DESCRIPTION:
                        attributeName = IdPManagementConstants.DESCRIPTION;
                        break;
                    case IdPManagementConstants.IDP_HOME_REALM_ID:
                        attributeName = IdPManagementConstants.HOME_REALM_ID;
                        break;
                    case IdPManagementConstants.IDP_IS_ENABLED:
                        attributeName = IdPManagementConstants.IS_ENABLED;
                        break;
                    case IdPManagementConstants.IDP_UUID:
                        attributeName = IdPManagementConstants.UUID;
                        break;
                    default:
                        String message = "Invalid filter attribute name. Filter attribute : " + attributeName;
                        throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message);
                }
                if (IdPManagementConstants.EQ.equals(operation)) {
                    filter.append(attributeName).append(" = ? AND ");
                    filterQueryBuilder.setFilterAttributeValue(value);
                } else if (IdPManagementConstants.SW.equals(operation)) {
                    filter.append(attributeName).append(" like ? AND ");
                    filterQueryBuilder.setFilterAttributeValue(value + "%");
                } else if (IdPManagementConstants.EW.equals(operation)) {
                    filter.append(attributeName).append(" like ? AND ");
                    filterQueryBuilder.setFilterAttributeValue("%" + value);
                } else if (IdPManagementConstants.CO.equals(operation)) {
                    filter.append(attributeName).append(" like ? AND ");
                    filterQueryBuilder.setFilterAttributeValue("%" + value + "%");
                } else {
                    String message = "Invalid filter value. filter: " + operation;
                    throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message);
                }
            }
        }
        if (StringUtils.isBlank(filter.toString())) {
            filterQueryBuilder.setFilterQuery(IdPManagementConstants.EMPTY_STRING);
        } else {
            filterQueryBuilder.setFilterQuery(filter.toString());
        }
    }
}
Also used : ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode)

Example 3 with IdentityProviderManagementClientException

use of org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException in project carbon-identity-framework by wso2.

the class IdPManagementDAO method getCountOfFilteredIdPs.

/**
 * Get number of IdP count for a matching filter.
 *
 * @param tenantId       Tenant Id of the identity provider.
 * @param expressionNode filter value list for IdP search.
 * @return number of IdP count for a given filter
 * @throws IdentityProviderManagementServerException Error when getting count of Identity Providers.
 * @throws IdentityProviderManagementClientException Error when append the filer string.
 */
int getCountOfFilteredIdPs(int tenantId, List<ExpressionNode> expressionNode) throws IdentityProviderManagementServerException, IdentityProviderManagementClientException {
    String sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_COUNT_SQL;
    int countOfFilteredIdp = 0;
    FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
    appendFilterQuery(expressionNode, filterQueryBuilder);
    Map<Integer, String> filterAttributeValue = filterQueryBuilder.getFilterAttributeValue();
    sqlStmt = sqlStmt + filterQueryBuilder.getFilterQuery() + IdPManagementConstants.SQLQueries.GET_IDP_COUNT_SQL_TAIL;
    try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement prepStmt = dbConnection.prepareStatement(sqlStmt)) {
        for (Map.Entry<Integer, String> prepareStatement : filterAttributeValue.entrySet()) {
            prepStmt.setString(prepareStatement.getKey(), prepareStatement.getValue());
        }
        prepStmt.setInt(filterAttributeValue.entrySet().size() + 1, tenantId);
        try (ResultSet rs = prepStmt.executeQuery()) {
            if (rs.next()) {
                countOfFilteredIdp = Integer.parseInt(rs.getString(1));
            }
        }
    } catch (SQLException e) {
        String message = "Error occurred while retrieving Identity Provider count for a tenant : " + IdentityTenantUtil.getTenantDomain(tenantId);
        throw IdPManagementUtil.handleServerException(IdPManagementConstants.ErrorMessage.ERROR_CODE_CONNECTING_DATABASE, message, e);
    }
    return countOfFilteredIdp;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) FilterQueryBuilder(org.wso2.carbon.idp.mgt.model.FilterQueryBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with IdentityProviderManagementClientException

use of org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException in project carbon-identity-framework by wso2.

the class IdPManagementDAO method getIdPsSearch.

/**
 * Get all identity provider's Basic information along with additionally requested information depends on the
 * requiredAttributes for a given matching filter.
 *
 * @param tenantId           Tenant Id of the identity provider.
 * @param expressionNode     List of filter value for IdP search.
 * @param limit              Limit per page.
 * @param offset             Offset value.
 * @param sortOrder          Order of IdP ASC/DESC.
 * @param sortBy             The attribute need to sort.
 * @param requiredAttributes Required attributes which needs to be return.
 * @return Identity Provider's Basic Information array along with requested attribute information.
 * @throws IdentityProviderManagementServerException Error when getting list of Identity Providers.
 * @throws IdentityProviderManagementClientException Error when append the filer string.
 */
List<IdentityProvider> getIdPsSearch(int tenantId, List<ExpressionNode> expressionNode, int limit, int offset, String sortOrder, String sortBy, List<String> requiredAttributes) throws IdentityProviderManagementServerException, IdentityProviderManagementClientException {
    FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
    appendFilterQuery(expressionNode, filterQueryBuilder);
    String sortedOrder = sortBy + " " + sortOrder;
    try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
        ResultSet resultSet = getIdpQueryResultSet(dbConnection, sortedOrder, tenantId, offset, limit, filterQueryBuilder, requiredAttributes)) {
        return populateIdentityProviderList(resultSet, dbConnection, requiredAttributes, tenantId);
    } catch (SQLException e) {
        String message = "Error occurred while retrieving Identity Provider for tenant: " + IdentityTenantUtil.getTenantDomain(tenantId);
        throw IdPManagementUtil.handleServerException(IdPManagementConstants.ErrorMessage.ERROR_CODE_CONNECTING_DATABASE, message, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) FilterQueryBuilder(org.wso2.carbon.idp.mgt.model.FilterQueryBuilder)

Example 5 with IdentityProviderManagementClientException

use of org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException in project carbon-identity-framework by wso2.

the class IdPManagementDAO method addIdPWithResourceId.

/**
 * Add IDP.
 *
 * @param identityProvider  Identity provider information.
 * @param tenantId          Tenant ID.
 * @return Resource ID of created IDP.
 * @throws IdentityProviderManagementException
 */
public String addIdPWithResourceId(IdentityProvider identityProvider, int tenantId) throws IdentityProviderManagementException {
    Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true);
    PreparedStatement prepStmt = null;
    try {
        if (identityProvider.isPrimary()) {
            // this is going to be the primary. Switch off any other primary set up in the
            // system.
            switchOffPrimary(dbConnection, tenantId);
        }
        // SP_TENANT_ID, SP_IDP_NAME, SP_IDP_PRIMARY, SP_IDP_HOME_REALM_ID, SP_IDP_CERTIFICATE,
        // SP_IDP_TOKEN_EP_ALIAS,
        // SP_IDP_INBOUND_PROVISIONING_ENABLED,SP_IDP_INBOUND_PROVISIONING_USER_STORE_ID,
        // SP_IDP_USER_CLAIM_URI,SP_IDP_ROLE_CLAIM_URI,SP_IDP_DEFAULT_AUTHENTICATOR_NAME,
        // SP_IDP_DEFAULT_PRO_CONNECTOR_NAME
        String sqlStmt = IdPManagementConstants.SQLQueries.ADD_IDP_SQL;
        prepStmt = dbConnection.prepareStatement(sqlStmt);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, identityProvider.getIdentityProviderName());
        if (identityProvider.isPrimary()) {
            prepStmt.setString(3, IdPManagementConstants.IS_TRUE_VALUE);
        } else {
            prepStmt.setString(3, IdPManagementConstants.IS_FALSE_VALUE);
        }
        prepStmt.setString(4, identityProvider.getHomeRealmId());
        if (ArrayUtils.isNotEmpty(identityProvider.getCertificateInfoArray())) {
            try {
                // Check whether certificate decoding and certificate generation fails or not.
                IdentityApplicationManagementUtil.getCertDataArray(identityProvider.getCertificateInfoArray());
            } catch (CertificateException ex) {
                throw new IdentityProviderManagementClientException("Malformed Public Certificate file has been " + "provided.", ex);
            }
        }
        JSONArray certificateInfoJsonArray = new JSONArray(identityProvider.getCertificateInfoArray());
        setBlobValue(certificateInfoJsonArray.toString(), prepStmt, 5);
        prepStmt.setString(6, identityProvider.getAlias());
        if (identityProvider.getJustInTimeProvisioningConfig() != null) {
            // provisioned locally.
            if (identityProvider.getJustInTimeProvisioningConfig().isProvisioningEnabled()) {
                prepStmt.setString(7, IdPManagementConstants.IS_TRUE_VALUE);
            } else {
                prepStmt.setString(7, IdPManagementConstants.IS_FALSE_VALUE);
            }
            // user will be provisioned to the configured user store.
            prepStmt.setString(8, identityProvider.getJustInTimeProvisioningConfig().getProvisioningUserStore());
        } else {
            prepStmt.setString(7, IdPManagementConstants.IS_FALSE_VALUE);
            prepStmt.setString(8, null);
        }
        if (identityProvider.getClaimConfig() != null) {
            // this is how we find the subject name from the authentication response.
            // this claim URI is in identity provider's own dialect.
            prepStmt.setString(9, identityProvider.getClaimConfig().getUserClaimURI());
            // this is how we find the role name from the authentication response.
            // this claim URI is in identity provider's own dialect.
            prepStmt.setString(10, identityProvider.getClaimConfig().getRoleClaimURI());
        } else {
            prepStmt.setString(9, null);
            prepStmt.setString(10, null);
        }
        if (identityProvider.getDefaultAuthenticatorConfig() != null) {
            prepStmt.setString(11, identityProvider.getDefaultAuthenticatorConfig().getName());
        } else {
            prepStmt.setString(11, null);
        }
        if (identityProvider.getDefaultProvisioningConnectorConfig() != null) {
            prepStmt.setString(12, identityProvider.getDefaultProvisioningConnectorConfig().getName());
        } else {
            prepStmt.setString(12, null);
        }
        prepStmt.setString(13, identityProvider.getIdentityProviderDescription());
        if (identityProvider.isFederationHub()) {
            prepStmt.setString(14, IdPManagementConstants.IS_TRUE_VALUE);
        } else {
            prepStmt.setString(14, IdPManagementConstants.IS_FALSE_VALUE);
        }
        if (identityProvider.getClaimConfig() != null && identityProvider.getClaimConfig().isLocalClaimDialect()) {
            prepStmt.setString(15, IdPManagementConstants.IS_TRUE_VALUE);
        } else {
            prepStmt.setString(15, IdPManagementConstants.IS_FALSE_VALUE);
        }
        prepStmt.setString(16, identityProvider.getProvisioningRole());
        // enabled by default
        prepStmt.setString(17, IdPManagementConstants.IS_TRUE_VALUE);
        prepStmt.setString(18, identityProvider.getDisplayName());
        prepStmt.setString(19, identityProvider.getImageUrl());
        String resourceId = UUID.randomUUID().toString();
        prepStmt.setString(20, resourceId);
        prepStmt.executeUpdate();
        prepStmt.clearParameters();
        // get newly added Identity provider.
        IdentityProvider createdIDP = getIDPbyResourceId(dbConnection, resourceId, tenantId, IdentityTenantUtil.getTenantDomain(tenantId));
        // get the id of the just added identity provider.
        int idPId = Integer.parseInt(createdIDP.getId());
        if (idPId <= 0) {
            String msg = "Error adding Identity Provider for tenant " + tenantId;
            throw new IdentityProviderManagementException(msg);
        }
        // add provisioning connectors.
        if (identityProvider.getProvisioningConnectorConfigs() != null && identityProvider.getProvisioningConnectorConfigs().length > 0) {
            addProvisioningConnectorConfigs(identityProvider.getProvisioningConnectorConfigs(), dbConnection, idPId, tenantId);
        }
        // add federated authenticators.
        addFederatedAuthenticatorConfigs(identityProvider.getFederatedAuthenticatorConfigs(), dbConnection, idPId, tenantId);
        // add role configuration.
        if (identityProvider.getPermissionAndRoleConfig() != null) {
            if (identityProvider.getPermissionAndRoleConfig().getIdpRoles() != null && identityProvider.getPermissionAndRoleConfig().getIdpRoles().length > 0) {
                // add roles.
                addIdPRoles(dbConnection, idPId, tenantId, identityProvider.getPermissionAndRoleConfig().getIdpRoles());
                if (identityProvider.getPermissionAndRoleConfig().getRoleMappings() != null && identityProvider.getPermissionAndRoleConfig().getRoleMappings().length > 0) {
                    // add role mappings.
                    addIdPRoleMappings(dbConnection, idPId, tenantId, identityProvider.getPermissionAndRoleConfig().getRoleMappings());
                }
            }
        }
        // add claim configuration.
        if (identityProvider.getClaimConfig() != null && identityProvider.getClaimConfig().getClaimMappings() != null && identityProvider.getClaimConfig().getClaimMappings().length > 0) {
            if (identityProvider.getClaimConfig().isLocalClaimDialect()) {
                // identity provider is using local claim dialect - we do not need to add
                // claims.
                addDefaultClaimValuesForLocalIdP(dbConnection, idPId, tenantId, identityProvider.getClaimConfig().getClaimMappings());
            } else {
                addIdPClaims(dbConnection, idPId, tenantId, identityProvider.getClaimConfig().getIdpClaims());
                addIdPClaimMappings(dbConnection, idPId, tenantId, identityProvider.getClaimConfig().getClaimMappings());
            }
        }
        IdentityProviderProperty[] idpProperties = identityProvider.getIdpProperties();
        if (IdentityApplicationConstants.RESIDENT_IDP_RESERVED_NAME.equals(identityProvider.getIdentityProviderName())) {
            idpProperties = filterConnectorProperties(idpProperties, IdentityTenantUtil.getTenantDomain(tenantId)).toArray(new IdentityProviderProperty[0]);
        }
        List<IdentityProviderProperty> identityProviderProperties = getCombinedProperties(identityProvider.getJustInTimeProvisioningConfig(), idpProperties);
        identityProviderProperties.add(buildTemplateIdProperty(identityProvider));
        addIdentityProviderProperties(dbConnection, idPId, identityProviderProperties, tenantId);
        IdentityDatabaseUtil.commitTransaction(dbConnection);
        return resourceId;
    } catch (IOException e) {
        throw new IdentityProviderManagementException("An error occurred while processing content stream.", e);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(dbConnection);
        throw new IdentityProviderManagementException("Error occurred while adding Identity Provider for tenant " + tenantId, e);
    } catch (ConnectorException e) {
        throw new IdentityProviderManagementException("An error occurred while filtering IDP properties.", e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(dbConnection, null, prepStmt);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JSONArray(org.json.JSONArray) PreparedStatement(java.sql.PreparedStatement) CertificateException(java.security.cert.CertificateException) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IOException(java.io.IOException) IdentityProviderManagementClientException(org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException) IdentityProviderProperty(org.wso2.carbon.identity.application.common.model.IdentityProviderProperty) ConnectorException(org.wso2.carbon.identity.core.ConnectorException) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Aggregations

IdentityProviderManagementClientException (org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException)6 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 IOException (java.io.IOException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Response (javax.ws.rs.core.Response)3 APIError (org.wso2.carbon.identity.api.server.common.error.APIError)3 ErrorResponse (org.wso2.carbon.identity.api.server.common.error.ErrorResponse)3 IdentityProviderProperty (org.wso2.carbon.identity.application.common.model.IdentityProviderProperty)3 IdentityProviderManagementServerException (org.wso2.carbon.idp.mgt.IdentityProviderManagementServerException)3 CertificateException (java.security.cert.CertificateException)2 JSONArray (org.json.JSONArray)2 ConnectorException (org.wso2.carbon.identity.core.ConnectorException)2 ExpressionNode (org.wso2.carbon.identity.core.model.ExpressionNode)2 IdentityProviderManagementException (org.wso2.carbon.idp.mgt.IdentityProviderManagementException)2 FilterQueryBuilder (org.wso2.carbon.idp.mgt.model.FilterQueryBuilder)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1