Search in sources :

Example 61 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-api-server by wso2.

the class ServerApplicationManagementService method getAllApplications.

public ApplicationListResponse getAllApplications(Integer limit, Integer offset, String filter, String sortOrder, String sortBy, String requiredAttributes) {
    handleNotImplementedCapabilities(sortOrder, sortBy, requiredAttributes);
    String tenantDomain = ContextLoader.getTenantDomainFromContext();
    boolean isEqualFilterUsed = false;
    limit = validateAndGetLimit(limit);
    offset = validateAndGetOffset(offset);
    // Format the filter to a value that can be interpreted by the backend.
    ExpressionNode expressionNode = buildFilterNode(filter);
    String formattedFilter = null;
    if (expressionNode != null) {
        // Handle eq operation as special case, there will be only one application with a given name in tenant.
        if (isEqualOperation(expressionNode)) {
            isEqualFilterUsed = true;
        }
        formattedFilter = generateFilterStringForBackend(expressionNode.getAttributeValue(), expressionNode.getOperation(), expressionNode.getValue());
    }
    String username = ContextLoader.getUsernameFromContext();
    try {
        int totalResults = getApplicationManagementService().getCountOfApplications(tenantDomain, username, formattedFilter);
        ApplicationBasicInfo[] filteredAppList;
        if (isEqualFilterUsed) {
            ApplicationBasicInfo applicationBasicInfo = getApplicationManagementService().getApplicationBasicInfoByName(expressionNode.getValue(), tenantDomain);
            if (applicationBasicInfo == null) {
                filteredAppList = new ApplicationBasicInfo[0];
            } else {
                filteredAppList = new ApplicationBasicInfo[] { applicationBasicInfo };
            }
        } else {
            filteredAppList = getApplicationManagementService().getApplicationBasicInfo(tenantDomain, username, formattedFilter, offset, limit);
        }
        int resultsInCurrentPage = filteredAppList.length;
        return new ApplicationListResponse().totalResults(totalResults).startIndex(offset + 1).count(resultsInCurrentPage).applications(getApplicationListItems(filteredAppList)).links(Util.buildPaginationLinks(limit, offset, totalResults, APPLICATION_MANAGEMENT_PATH_COMPONENT).entrySet().stream().map(link -> new Link().rel(link.getKey()).href(link.getValue())).collect(Collectors.toList()));
    } catch (IdentityApplicationManagementException e) {
        String msg = "Error listing applications of tenantDomain: " + tenantDomain;
        throw handleIdentityApplicationManagementException(e, msg);
    }
}
Also used : ApplicationListResponse(org.wso2.carbon.identity.api.server.application.management.v1.ApplicationListResponse) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo) Link(org.wso2.carbon.identity.api.server.application.management.v1.Link)

Example 62 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-api-server by wso2.

the class ServerAuthenticatorManagementService method getExpressionNodesForIdp.

private List<ExpressionNode> getExpressionNodesForIdp(String filter) {
    // Filter example : name sw go and (tag eq 2fa or tag eq Social-Login)
    List<ExpressionNode> expressionNodes = new ArrayList<>();
    FilterTreeBuilder filterTreeBuilder;
    try {
        if (StringUtils.isNotBlank(filter)) {
            filterTreeBuilder = new FilterTreeBuilder(filter);
            Node rootNode = filterTreeBuilder.buildTree();
            setExpressionNodeListForIdp(rootNode, expressionNodes);
        }
    } catch (IOException | IdentityException e) {
        throw buildClientError(Constants.ErrorMessage.ERROR_CODE_INVALID_FILTER_FORMAT, null);
    }
    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 63 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-api-server by wso2.

the class ServerAuthenticatorManagementService method validateFilter.

private void validateFilter(Node rootNode) {
    if (!(rootNode instanceof OperationNode)) {
        String attributeValue = ((ExpressionNode) rootNode).getAttributeValue();
        String operation = ((ExpressionNode) rootNode).getOperation();
        if (StringUtils.equalsIgnoreCase(attributeValue, Constants.FilterAttributes.NAME)) {
            if (StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.SW) || StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.EQ)) {
                return;
            } else {
                throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_NAME, operation);
            }
        } else if (StringUtils.equalsIgnoreCase(attributeValue, Constants.FilterAttributes.TAG)) {
            if (StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.EQ)) {
                return;
            } else {
                throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_TAG, operation);
            }
        } else {
            throw buildClientError(Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, attributeValue);
        }
    }
    Node rootNodeLeftNode = rootNode.getLeftNode();
    Node rootNodeRightNode = rootNode.getRightNode();
    boolean nameFiltering = false;
    boolean tagAvailableInLeftNode = false;
    boolean tagAvailableInRightNode = false;
    if (rootNodeLeftNode instanceof ExpressionNode) {
        String attributeValue = ((ExpressionNode) rootNodeLeftNode).getAttributeValue();
        nameFiltering = attributeValue.equals(Constants.FilterAttributes.NAME);
        tagAvailableInLeftNode = attributeValue.equals(Constants.FilterAttributes.TAG);
        if (!(nameFiltering || tagAvailableInLeftNode)) {
            throw buildClientError(Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, attributeValue);
        }
        String operation = ((ExpressionNode) rootNodeLeftNode).getOperation();
        if (nameFiltering && (!(StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.SW) || StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.EQ)))) {
            throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_NAME, operation);
        }
        if (tagAvailableInLeftNode && !StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.EQ)) {
            throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_TAG, operation);
        }
    } else {
        validateChildNode(rootNodeLeftNode);
    }
    if (rootNodeRightNode instanceof ExpressionNode) {
        String attributeValue = ((ExpressionNode) rootNodeRightNode).getAttributeValue();
        tagAvailableInRightNode = attributeValue.equals(Constants.FilterAttributes.TAG);
        boolean nameAvailableInRightNode = attributeValue.equals(Constants.FilterAttributes.NAME);
        if (!(tagAvailableInRightNode || nameAvailableInRightNode)) {
            throw buildClientError(Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, attributeValue);
        }
        if (nameFiltering && nameAvailableInRightNode) {
            throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_FOR_MULTIPLE_NAMES, null);
        }
        String operation = ((ExpressionNode) rootNodeRightNode).getOperation();
        if (!nameFiltering) {
            nameFiltering = nameAvailableInRightNode;
            if (nameFiltering && (!(StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.SW) || StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.EQ)))) {
                throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_NAME, operation);
            }
        }
        if (tagAvailableInRightNode && !StringUtils.equalsIgnoreCase(operation, Constants.FilterOperations.EQ)) {
            throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_TAG, operation);
        }
    } else {
        validateChildNode(rootNodeRightNode);
    }
    String operation = ((OperationNode) rootNode).getOperation();
    if (nameFiltering && !StringUtils.equalsIgnoreCase(operation, Constants.ComplexQueryOperations.AND)) {
        throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_COMPLEX_QUERY_OPERATION_FOR_NAME, operation);
    }
    if (tagAvailableInLeftNode && tagAvailableInRightNode && !StringUtils.equalsIgnoreCase(operation, Constants.ComplexQueryOperations.OR)) {
        throw handleException(Response.Status.NOT_IMPLEMENTED, Constants.ErrorMessage.ERROR_CODE_UNSUPPORTED_COMPLEX_QUERY_OPERATION_FOR_TAG, operation);
    }
}
Also used : OperationNode(org.wso2.carbon.identity.core.model.OperationNode) 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)

Example 64 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-api-server by wso2.

the class ServerAuthenticatorManagementService method buildAuthenticatorsListResponse.

private List<Authenticator> buildAuthenticatorsListResponse(String filter, List<String> requestedAttributeList, String localAuthNames, String authenticatorNameFilterOperator, ArrayList<String> filterTagsList, LocalAuthenticatorConfig[] localAuthenticatorConfigs, RequestPathAuthenticatorConfig[] requestPathAuthenticatorConfigs, List<IdentityProvider> identityProviders) {
    int maximumItemsPerPage = IdentityUtil.getMaximumItemPerPage();
    List<Authenticator> authenticators = new ArrayList<>();
    if (localAuthenticatorConfigs != null) {
        for (LocalAuthenticatorConfig config : localAuthenticatorConfigs) {
            addLocalAuthenticator(config, authenticators, localAuthNames, authenticatorNameFilterOperator, filterTagsList, maximumItemsPerPage);
        }
    }
    if ((authenticators.size() < maximumItemsPerPage) && requestPathAuthenticatorConfigs != null) {
        for (RequestPathAuthenticatorConfig config : requestPathAuthenticatorConfigs) {
            addLocalAuthenticator(config, authenticators, localAuthNames, authenticatorNameFilterOperator, filterTagsList, maximumItemsPerPage);
        }
    }
    if (StringUtils.isBlank(filter)) {
        if ((authenticators.size() < maximumItemsPerPage) && identityProviders != null) {
            for (IdentityProvider identityProvider : identityProviders) {
                if (authenticators.size() < maximumItemsPerPage) {
                    List<String> configTagsListDistinct = getDistinctTags(identityProvider);
                    addIdp(identityProvider, authenticators, configTagsListDistinct);
                }
            }
        }
    } else {
        List<ExpressionNode> expressionNodesForIdp = getExpressionNodesForIdp(filter);
        int idPCountToBeRetrieved = maximumItemsPerPage - authenticators.size();
        IdpSearchResult idpSearchResult;
        try {
            idpSearchResult = AuthenticatorsServiceHolder.getInstance().getIdentityProviderManager().getIdPs(idPCountToBeRetrieved, null, null, null, ContextLoader.getTenantDomainFromContext(), requestedAttributeList, expressionNodesForIdp);
            identityProviders = idpSearchResult.getIdPs();
            if (identityProviders != null) {
                addIdPsToAuthenticatorList(maximumItemsPerPage, identityProviders, authenticators, filterTagsList);
                int limit = idpSearchResult.getLimit();
                int offSet = idpSearchResult.getOffSet();
                int totalIdpCount = idpSearchResult.getTotalIDPCount();
                while (authenticators.size() < maximumItemsPerPage && limit > 0 && offSet >= 0 && totalIdpCount > (limit + offSet)) {
                    identityProviders = new ArrayList<>();
                    getFilteredIdPs(limit, offSet, requestedAttributeList, identityProviders, expressionNodesForIdp);
                    addIdPsToAuthenticatorList(maximumItemsPerPage, identityProviders, authenticators, filterTagsList);
                    offSet = offSet + limit;
                }
            }
        } catch (IdentityProviderManagementException e) {
            throw handleIdPException(e, Constants.ErrorMessage.ERROR_CODE_ERROR_LISTING_IDPS, null);
        }
    }
    return authenticators;
}
Also used : ArrayList(java.util.ArrayList) LocalAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IdpSearchResult(org.wso2.carbon.idp.mgt.model.IdpSearchResult) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) RequestPathAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException) Authenticator(org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator)

Example 65 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project identity-api-server by wso2.

the class ServerAuthenticatorManagementService method getExpressionNodesForAuthenticator.

private List<ExpressionNode> getExpressionNodesForAuthenticator(String filter) {
    // Filter example : name sw go and (tag eq 2fa or tag eq Social-Login)
    List<ExpressionNode> expressionNodes = new ArrayList<>();
    FilterTreeBuilder filterTreeBuilder;
    try {
        if (StringUtils.isNotBlank(filter)) {
            filterTreeBuilder = new FilterTreeBuilder(filter);
            Node rootNode = filterTreeBuilder.buildTree();
            validateFilter(rootNode);
            setExpressionNodeListForAuthenticator(rootNode, expressionNodes);
        }
    } catch (IOException | IdentityException e) {
        throw buildClientError(Constants.ErrorMessage.ERROR_CODE_INVALID_FILTER_FORMAT, null);
    }
    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)

Aggregations

ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)21 ArrayList (java.util.ArrayList)16 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)16 ExpressionNode (org.wso2.carbon.identity.core.model.ExpressionNode)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)12 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 ExpressionNode (org.wso2.charon3.core.utils.codeutils.ExpressionNode)7 Map (java.util.Map)6 IdentityException (org.wso2.carbon.identity.base.IdentityException)6 Attribute (org.wso2.charon3.core.attributes.Attribute)6 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)6 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)6 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)6 Connection (java.sql.Connection)5 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)5 JSONObject (org.json.JSONObject)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 Node (org.wso2.carbon.identity.core.model.Node)5 IdpSearchResult (org.wso2.carbon.idp.mgt.model.IdpSearchResult)5