Search in sources :

Example 16 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project carbon-identity-framework by wso2.

the class XACMLBasedRuleHandler method evaluateXACMLResponse.

private boolean evaluateXACMLResponse(String xacmlResponse) throws IdentityProvisioningException {
    try {
        DocumentBuilderFactory documentBuilderFactory = IdentityUtil.getSecuredDocumentBuilderFactory();
        DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xacmlResponse));
        Document doc = db.parse(is);
        String decision = "";
        NodeList decisionNode = doc.getDocumentElement().getElementsByTagName(ProvisioningRuleConstanats.XACML_RESPONSE_DECISION_NODE);
        if (decisionNode != null && decisionNode.item(0) != null) {
            decision = decisionNode.item(0).getTextContent();
        }
        if (decision.equalsIgnoreCase(EntitlementPolicyConstants.RULE_EFFECT_PERMIT) || decision.equalsIgnoreCase(EntitlementPolicyConstants.RULE_EFFECT_NOT_APPLICABLE)) {
            return true;
        }
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new IdentityProvisioningException("Exception occurred while xacmlResponse processing", e);
    }
    return false;
}
Also used : InputSource(org.xml.sax.InputSource) IdentityProvisioningException(org.wso2.carbon.identity.provisioning.IdentityProvisioningException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 17 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project carbon-identity-framework by wso2.

the class UserRegistrationService method getTenantSignUpConfig.

private TenantRegistrationConfig getTenantSignUpConfig(int tenantId) throws IdentityException {
    TenantRegistrationConfig config;
    NodeList nodes;
    try {
        // start tenant flow to load tenant registry
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        Registry registry = (Registry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE);
        if (registry.resourceExists(SelfRegistrationConstants.SIGN_UP_CONFIG_REG_PATH)) {
            Resource resource = registry.get(SelfRegistrationConstants.SIGN_UP_CONFIG_REG_PATH);
            // build config from tenant registry resource
            DocumentBuilder builder = getSecuredDocumentBuilder();
            String configXml = new String((byte[]) resource.getContent());
            InputSource configInputSource = new InputSource();
            configInputSource.setCharacterStream(new StringReader(configXml.trim()));
            Document doc = builder.parse(configInputSource);
            nodes = doc.getElementsByTagName(SelfRegistrationConstants.SELF_SIGN_UP_ELEMENT);
            if (nodes.getLength() > 0) {
                config = new TenantRegistrationConfig();
                config.setSignUpDomain(((Element) nodes.item(0)).getElementsByTagName(SelfRegistrationConstants.SIGN_UP_DOMAIN_ELEMENT).item(0).getTextContent());
                // there can be more than one <SignUpRole> elements, iterate through all elements
                NodeList rolesEl = ((Element) nodes.item(0)).getElementsByTagName(SelfRegistrationConstants.SIGN_UP_ROLE_ELEMENT);
                for (int i = 0; i < rolesEl.getLength(); i++) {
                    Element tmpEl = (Element) rolesEl.item(i);
                    String tmpRole = tmpEl.getElementsByTagName(SelfRegistrationConstants.ROLE_NAME_ELEMENT).item(0).getTextContent();
                    boolean tmpIsExternal = Boolean.parseBoolean(tmpEl.getElementsByTagName(SelfRegistrationConstants.IS_EXTERNAL_ELEMENT).item(0).getTextContent());
                    config.getRoles().put(tmpRole, tmpIsExternal);
                }
                return config;
            } else {
                return null;
            }
        }
    } catch (RegistryException e) {
        throw IdentityException.error("Error retrieving sign up config from registry " + e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        throw IdentityException.error("Error parsing tenant sign up configuration " + e.getMessage(), e);
    } catch (SAXException e) {
        throw IdentityException.error("Error parsing tenant sign up configuration " + e.getMessage(), e);
    } catch (IOException e) {
        throw IdentityException.error("Error parsing tenant sign up configuration " + e.getMessage(), e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Resource(org.wso2.carbon.registry.core.Resource) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) Document(org.w3c.dom.Document) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) TenantRegistrationConfig(org.wso2.carbon.identity.user.registration.dto.TenantRegistrationConfig) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 18 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project carbon-identity-framework by wso2.

the class InMemoryPersistenceManager method buildDataHolder.

private PolicyEditorDataHolder buildDataHolder(String type, String xmlConfig) throws PolicyEditorException {
    if (xmlConfig == null) {
        return null;
    }
    PolicyEditorDataHolder holder = new PolicyEditorDataHolder();
    DocumentBuilder builder;
    ByteArrayInputStream inputStream;
    Element root = null;
    inputStream = new ByteArrayInputStream(xmlConfig.getBytes());
    try {
        builder = getSecuredDocumentBuilder();
        Document doc = builder.parse(inputStream);
        root = doc.getDocumentElement();
    } catch (Exception e) {
        log.error("DOM of request element can not be created from String", e);
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            log.error("Error in closing input stream of XACML request");
        }
    }
    if (root == null) {
        return holder;
    }
    NodeList nodeList = root.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeName().equals("categories")) {
            parseCategories(type, node, holder);
        } else if (node.getNodeName().equals("ruleCombiningAlgorithm")) {
            parseAlgorithm(node, holder, false);
        } else if (node.getNodeName().equals("policyCombiningAlgorithm")) {
            parseAlgorithm(node, holder, true);
        } else if (node.getNodeName().equals("attributeIds")) {
            parseAttributeIds(node, holder);
        } else if (node.getNodeName().equals("dataTypes")) {
            parseDataTypes(node, holder);
        } else if (node.getNodeName().equals("functions")) {
            parseFunctions(node, holder);
        } else if (node.getNodeName().equals("preFunctions")) {
            parsePreFunctions(node, holder);
        } else if (node.getNodeName().equals("rule")) {
            parseRule(node, holder);
        } else if (node.getNodeName().equals("policyDescription")) {
            if ("true".equals(node.getTextContent())) {
                holder.setShowPolicyDescription(true);
            }
        }
    }
    return holder;
}
Also used : PolicyEditorDataHolder(org.wso2.carbon.identity.entitlement.common.dto.PolicyEditorDataHolder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 19 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project carbon-mediation by wso2.

the class RestApiAdminClient method ReadWSDLPrefix.

private String ReadWSDLPrefix() throws AxisFault {
    try {
        String confPath = System.getProperty(CONF_LOCATION);
        if (confPath == null) {
            confPath = Paths.get("repository", "conf").toString();
        }
        InputStream in = new FileInputStream(Paths.get(confPath, "axis2", "axis2.xml").toString());
        OMElement results = OMXMLBuilderFactory.createOMBuilder(in).getDocumentElement();
        AXIOMXPath xpathExpression = new AXIOMXPath("/axisconfig/transportReceiver/parameter[@name='WSDLEPRPrefix']");
        List nodeList = (List) xpathExpression.selectNodes(results);
        if (!nodeList.isEmpty()) {
            OMNode value = (OMNode) nodeList.get(0);
            String server = ((OMElementImpl) value).getText();
            if (server.contains("http") || server.contains("https")) {
                return server;
            }
        }
    } catch (Exception e) {
        handleException(bundle.getString("failed.to.read.wsdl.prefix"), e);
    }
    return null;
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElementImpl(org.apache.axiom.om.impl.llom.OMElementImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OMElement(org.apache.axiom.om.OMElement) ArrayList(java.util.ArrayList) List(java.util.List) AXIOMXPath(org.apache.axiom.om.xpath.AXIOMXPath) FileInputStream(java.io.FileInputStream) RestApiAdminAPIException(org.wso2.carbon.rest.api.stub.RestApiAdminAPIException) RemoteException(java.rmi.RemoteException)

Example 20 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project product-is by wso2.

the class SAML2SSOTestBase method extractAndProcessSAMLResponse.

/**
 * Get SAML response object from the HTTP response.
 *
 * @param response HTTP response
 * @return SAML response instance.
 * @throws Exception
 */
public Response extractAndProcessSAMLResponse(HttpResponse response) throws Exception {
    String encodedSAML2ResponseString = extractSAMLResponse(response);
    EntityUtils.consume(response.getEntity());
    String saml2ResponseString = new String(Base64.decode(encodedSAML2ResponseString), Charset.forName(StandardCharsets.UTF_8.name()));
    XMLObject samlResponse = unmarshall(saml2ResponseString);
    // Check for duplicate samlp:Response
    NodeList list = samlResponse.getDOM().getElementsByTagNameNS(SAMLConstants.SAML20P_NS, RESPONSE_TAG_NAME);
    if (list.getLength() > 0) {
        log.error("Invalid schema for the SAML2 response. Multiple Response elements found.");
        throw new Exception("Error occurred while processing SAML2 response.");
    }
    // Checking for multiple Assertions
    NodeList assertionList = samlResponse.getDOM().getElementsByTagNameNS(SAMLConstants.SAML20_NS, ASSERTION_TAG_NAME);
    if (assertionList.getLength() > 1) {
        log.error("Invalid schema for the SAML2 response. Multiple Assertion elements found.");
        throw new Exception("Error occurred while processing SAML2 response.");
    }
    return (Response) samlResponse;
}
Also used : DataExtractUtil.extractValueFromResponse(org.wso2.identity.scenarios.commons.util.DataExtractUtil.extractValueFromResponse) HttpResponse(org.apache.http.HttpResponse) Response(org.opensaml.saml2.core.Response) NodeList(org.w3c.dom.NodeList) XMLObject(org.opensaml.xml.XMLObject) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ValidationException(org.opensaml.xml.validation.ValidationException) IdentitySAMLSSOConfigServiceIdentityException(org.wso2.carbon.identity.sso.saml.stub.IdentitySAMLSSOConfigServiceIdentityException) RemoteException(java.rmi.RemoteException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.opensaml.xml.ConfigurationException)

Aggregations

NodeList (org.w3c.dom.NodeList)20 Element (org.w3c.dom.Element)12 Node (org.w3c.dom.Node)11 NodeList (org.wso2.ei.dashboard.core.rest.model.NodeList)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 List (java.util.List)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 JsonObject (com.google.gson.JsonObject)4 QName (javax.xml.namespace.QName)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 Document (org.w3c.dom.Document)4 NodeListInner (org.wso2.ei.dashboard.core.rest.model.NodeListInner)4 SAXException (org.xml.sax.SAXException)4 JsonElement (com.google.gson.JsonElement)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3