Search in sources :

Example 1 with Result

use of org.wso2.balana.ctx.xacml3.Result in project product-microgateway by wso2.

the class HashUtils method getAnnotatedHash.

/**
 * Calculates the hash of the object using the Hash annotations added to the getter methods of the object.
 *
 * @param obj object whose hash needs to be calculated.
 * @return calculated hash value
 * @throws HashingException error while calculating hash
 */
private static String getAnnotatedHash(Object obj) throws HashingException {
    ObjectMapper mapper = new ObjectMapper();
    TreeSet<String> sortedSet = new TreeSet<>();
    for (Method method : obj.getClass().getMethods()) {
        if (method.isAnnotationPresent(Hash.class)) {
            try {
                Object value = method.invoke(obj);
                String stringifiedField = mapper.writeValueAsString(value);
                // The method name needs to be added here. The reason is, the array of methods returned from
                // getClass().getMethods() is not always in a particular order. If the order changes, this would
                // result in change of the hash even through the object didn't change. To fix this, a TreeSet is
                // used which will insert entries sorted in natural ordering. The method name appended in the front
                // to always have a fixed string at the beginning.
                sortedSet.add(method.getName() + HashingConstants.HASH_SEPARATOR + stringifiedField);
            } catch (JsonProcessingException | IllegalAccessException | InvocationTargetException e) {
                throw new HashingException("Error while generating hash for " + obj, e);
            }
        }
    }
    StringBuilder builder = new StringBuilder();
    for (String entry : sortedSet) {
        builder.append(entry);
        builder.append(HashingConstants.HASH_SEPARATOR);
    }
    String val = builder.toString();
    return getMD5Hex(val);
}
Also used : TreeSet(java.util.TreeSet) Method(java.lang.reflect.Method) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashingException(org.wso2.apimgt.gateway.cli.exception.HashingException)

Example 2 with Result

use of org.wso2.balana.ctx.xacml3.Result in project carbon-identity-framework by wso2.

the class LongWaitStatusDAOImpl method getWaitStatus.

public LongWaitStatus getWaitStatus(String waitKey) throws FrameworkException {
    String query = "SELECT WAIT_STATUS FROM IDN_AUTH_WAIT_STATUS WHERE LONG_WAIT_KEY=?";
    LongWaitStatus longWaitStatus = new LongWaitStatus();
    try (Connection connection = IdentityDatabaseUtil.getSessionDBConnection(false)) {
        try (PreparedStatement addPrepStmt = connection.prepareStatement(query)) {
            addPrepStmt.setString(1, waitKey);
            try (ResultSet resultSet = addPrepStmt.executeQuery()) {
                if (resultSet.next()) {
                    String waitStatus = resultSet.getString("WAIT_STATUS");
                    if (log.isDebugEnabled()) {
                        log.debug("Searched for wait status for wait key: " + waitKey + ". Result: " + ("1".equals(waitStatus) ? "WAITING" : "COMPLETED"));
                    }
                    if (waitStatus.equals("1")) {
                        longWaitStatus.setStatus(LongWaitStatus.Status.WAITING);
                    } else {
                        longWaitStatus.setStatus(LongWaitStatus.Status.COMPLETED);
                    }
                } else {
                    longWaitStatus.setStatus(LongWaitStatus.Status.UNKNOWN);
                    if (log.isDebugEnabled()) {
                        log.debug("Searched for wait status for wait key: " + waitKey + ". Result: UNKNOWN");
                    }
                }
            }
        } catch (SQLException e) {
            throw new FrameworkException("Error while searching for wait status with key:" + waitKey, e);
        }
    } catch (SQLException e) {
        throw new FrameworkException("Error while searching for wait status with key:" + waitKey, e);
    }
    if (longWaitStatus.getStatus() == null) {
        longWaitStatus.setStatus(LongWaitStatus.Status.UNKNOWN);
    }
    return longWaitStatus;
}
Also used : FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) SQLException(java.sql.SQLException) LongWaitStatus(org.wso2.carbon.identity.application.authentication.framework.model.LongWaitStatus) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with Result

use of org.wso2.balana.ctx.xacml3.Result in project carbon-identity-framework by wso2.

the class JsAuthenticationContextTest method testClaimAssignment.

@Test
public void testClaimAssignment() throws ScriptException {
    ClaimMapping claimMapping1 = ClaimMapping.build("", "", "", false);
    ClaimMapping claimMapping2 = ClaimMapping.build("Test.Remote.Claim.Url.2", "Test.Remote.Claim.Url.2", "", false);
    AuthenticatedUser authenticatedUser = new AuthenticatedUser();
    authenticatedUser.getUserAttributes().put(claimMapping1, "TestClaimVal1");
    authenticatedUser.getUserAttributes().put(claimMapping2, "TestClaimVal2");
    AuthenticationContext authenticationContext = new AuthenticationContext();
    setupAuthContextWithStepData(authenticationContext, authenticatedUser);
    JsAuthenticationContext jsAuthenticationContext = new JsAuthenticationContext(authenticationContext);
    Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
    bindings.put("context", jsAuthenticationContext);
    Object result = scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.1']");
    assertNull(result);
    result = scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.2']");
    assertEquals(result, "TestClaimVal2");
    scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.2'] = 'Modified2'");
    result = scriptEngine.eval("context.steps[1].subject.remoteClaims['Test.Remote.Claim.Url.2']");
    assertEquals(result, "Modified2");
}
Also used : ClaimMapping(org.wso2.carbon.identity.application.common.model.ClaimMapping) AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 4 with Result

use of org.wso2.balana.ctx.xacml3.Result in project carbon-identity-framework by wso2.

the class JsAuthenticationContextTest method testGetLastLoginFailedUserNullFromWrappedContext.

@Test
public void testGetLastLoginFailedUserNullFromWrappedContext() throws Exception {
    AuthenticationContext authenticationContext = new AuthenticationContext();
    authenticationContext.setProperty(FrameworkConstants.JSAttributes.JS_LAST_LOGIN_FAILED_USER, null);
    JsAuthenticationContext jsAuthenticationContext = new JsAuthenticationContext(authenticationContext);
    Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE);
    bindings.put("context", jsAuthenticationContext);
    Object result = scriptEngine.eval("context.lastLoginFailedUser");
    assertNull(result);
}
Also used : AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 5 with Result

use of org.wso2.balana.ctx.xacml3.Result in project carbon-identity-framework by wso2.

the class FrameworkUtils method removeAuthenticationResultFromCache.

/**
 *  Removes authentication result from cache.
 * @param autheticationResultId
 */
public static void removeAuthenticationResultFromCache(String autheticationResultId) {
    if (autheticationResultId != null) {
        AuthenticationResultCacheKey cacheKey = new AuthenticationResultCacheKey(autheticationResultId);
        AuthenticationResultCache.getInstance().clearCacheEntry(cacheKey);
    }
}
Also used : AuthenticationResultCacheKey(org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationResultCacheKey)

Aggregations

ArrayList (java.util.ArrayList)106 Test (org.testng.annotations.Test)84 Test (org.junit.Test)73 HashMap (java.util.HashMap)59 SQLException (java.sql.SQLException)54 Connection (java.sql.Connection)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)42 List (java.util.List)40 DataServiceFault (org.wso2.carbon.dataservices.core.DataServiceFault)38 PreparedStatement (java.sql.PreparedStatement)37 ResultSet (java.sql.ResultSet)36 Result (edu.stanford.CVC4.Result)35 IOException (java.io.IOException)34 Expr (edu.stanford.CVC4.Expr)32 SExpr (edu.stanford.CVC4.SExpr)32 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)31 Map (java.util.Map)30 XMLStreamException (javax.xml.stream.XMLStreamException)29 JSONObject (org.json.simple.JSONObject)29 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)29