Search in sources :

Example 1 with StringAttribute

use of org.wso2.balana.attr.StringAttribute in project carbon-identity-framework by wso2.

the class JSONResponseWriter method getJsonObject.

/**
 * Create json object value of an Attribute
 *
 * @param attributes an element of type Attributes
 * @return a JSONObject
 */
private static JsonObject getJsonObject(Attributes attributes) {
    JsonObject jsonObject = new JsonObject();
    JsonArray jsonArray = new JsonArray();
    for (Object att : attributes.getAttributes().toArray()) {
        Attribute attrib = (Attribute) att;
        if (attrib.isIncludeInResult()) {
            JsonObject element = new JsonObject();
            if (attrib.getId() != null) {
                if (xacmlJSONProfileShortFormEnable) {
                    element.addProperty(EntitlementEndpointConstants.ATTRIBUTE_ID, uriToShortenForm(attrib.getId().toString()));
                } else {
                    element.addProperty(EntitlementEndpointConstants.ATTRIBUTE_ID, attrib.getId().toString());
                }
            }
            if (attrib.getValues() != null) {
                for (AttributeValue val : attrib.getValues()) {
                    if (((StringAttribute) val).getValue() != null) {
                        element.addProperty(EntitlementEndpointConstants.ATTRIBUTE_VALUE, ((StringAttribute) val).getValue());
                    }
                }
            }
            element.addProperty(EntitlementEndpointConstants.ATTRIBUTE_INCLUDE_IN_RESULT, String.valueOf(attrib.isIncludeInResult()));
            if (attrib.getType() != null) {
                if (xacmlJSONProfileShortFormEnable) {
                    element.addProperty(EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE, uriToShortenForm(attrib.getType().toString()));
                } else {
                    element.addProperty(EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE, attrib.getType().toString());
                }
            }
            jsonArray.add(element);
        }
    }
    jsonObject.add(EntitlementEndpointConstants.ATTRIBUTE, jsonArray);
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) AttributeValue(org.wso2.balana.attr.AttributeValue) StringAttribute(org.wso2.balana.attr.StringAttribute) Attribute(org.wso2.balana.ctx.Attribute) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 2 with StringAttribute

use of org.wso2.balana.attr.StringAttribute in project carbon-identity-framework by wso2.

the class EvalPermissionTreeFunction method evaluate.

public EvaluationResult evaluate(List<Evaluatable> inputs, EvaluationCtx context) {
    AttributeValue[] argValues = new AttributeValue[inputs.size()];
    EvaluationResult result = evalArgs(inputs, context, argValues);
    if (result != null) {
        return result;
    }
    switch(getFunctionId()) {
        case ID_EVAL_PERMISSION_TREE:
            String resource = ((StringAttribute) argValues[0]).getValue().trim();
            String subject = ((StringAttribute) argValues[1]).getValue().trim();
            boolean isAuthorised = false;
            try {
                isAuthorised = EntitlementServiceComponent.getRealmservice().getBootstrapRealm().getAuthorizationManager().isUserAuthorized(subject, resource, "ui.execute");
            } catch (UserStoreException e) {
                log.error("Error while authorising" + subject + " to perform ui.execute on " + resource, e);
            }
            result = new EvaluationResult(BooleanAttribute.getInstance(isAuthorised));
            break;
    }
    return result;
}
Also used : AttributeValue(org.wso2.balana.attr.AttributeValue) UserStoreException(org.wso2.carbon.user.core.UserStoreException) EvaluationResult(org.wso2.balana.cond.EvaluationResult)

Example 3 with StringAttribute

use of org.wso2.balana.attr.StringAttribute in project carbon-identity-framework by wso2.

the class TestJSONRequestParser method testParse.

@Test
public void testParse() {
    AttributeValue attributeValue = new StringAttribute("http://127.0.0.1");
    List<AttributeValue> attributeValues = new ArrayList<>();
    attributeValues.add(attributeValue);
    Attribute attribute = new Attribute(URI.create("urn:oasis:names:tc:xacml:1.0:resource:resource-id"), null, null, null, attributeValues, false, XACMLConstants.XACML_VERSION_3_0);
    Set<Attribute> attributeSet = new HashSet<>();
    attributeSet.add(attribute);
    Attributes category = new Attributes(URI.create(EntitlementEndpointConstants.CATEGORY_RESOURCE_URI), attributeSet);
    Set<Attributes> categories = new HashSet<>();
    categories.add(category);
    RequestCtx requestCtx = new RequestCtx(categories, null);
    String jsonRequest = "{\n" + "  \"Request\":{\n" + "    \"Action\":{\n" + "      \"Attribute\":[{\n" + "        \"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:action:action-id\",\n" + "        \"Value\":\"read\"\n" + "      }]\n" + "    },\n" + "    \"Resource\":{\n" + "      \"Attribute\":[{\n" + "        \"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:resource-id\",\n" + "        \"Value\":\"http://127.0.0.1/service/very_secure/\"\n" + "      }]\n" + "    }\n" + "  }\n" + "}";
    String jsonRequest2 = "{\"Request\":\n" + "{\n" + "\"AccessSubject\":{\n" + "            \"Content\": \"PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8Y2F0YWxvZz48Ym9vayBpZD0iYmsxMDEiPjxhdXRob3I+R2FtYmFyZGVsbGEsIE1hdHRoZXc8L2F1dGhvcj48dGl0bGU+WE1MIERldmVsb3BlcidzIEd1aWRlPC90aXRsZT48Z2VucmU+Q29tcHV0ZXI8L2dlbnJlPjxwcmljZT40NC45NTwvcHJpY2U+PHB1Ymxpc2hfZGF0ZT4yMDAwLTEwLTAxPC9wdWJsaXNoX2RhdGU+PGRlc2NyaXB0aW9uPkFuIGluLWRlcHRoIGxvb2sgYXQgY3JlYXRpbmcgYXBwbGljYXRpb25zIHdpdGggWE1MLjwvZGVzY3JpcHRpb24+PC9ib29rPjwvY2F0YWxvZz4=\"\n" + "}\n" + "}}";
    try {
        RequestCtx requestCtx1 = JSONRequestParser.parse(jsonRequest);
    } catch (Exception e) {
        log.error("Exception in JSON Parser Test");
    }
}
Also used : AttributeValue(org.wso2.balana.attr.AttributeValue) StringAttribute(org.wso2.balana.attr.StringAttribute) Attribute(org.wso2.balana.ctx.Attribute) StringAttribute(org.wso2.balana.attr.StringAttribute) ArrayList(java.util.ArrayList) Attributes(org.wso2.balana.xacml3.Attributes) HashSet(java.util.HashSet) RequestCtx(org.wso2.balana.ctx.xacml3.RequestCtx) Test(org.testng.annotations.Test)

Aggregations

AttributeValue (org.wso2.balana.attr.AttributeValue)3 StringAttribute (org.wso2.balana.attr.StringAttribute)2 Attribute (org.wso2.balana.ctx.Attribute)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (org.testng.annotations.Test)1 EvaluationResult (org.wso2.balana.cond.EvaluationResult)1 RequestCtx (org.wso2.balana.ctx.xacml3.RequestCtx)1 Attributes (org.wso2.balana.xacml3.Attributes)1 UserStoreException (org.wso2.carbon.user.core.UserStoreException)1