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);
}
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;
}
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");
}
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);
}
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);
}
}
Aggregations