use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.
the class ScriptConditionTest method missingScriptConfiguration.
@Test(expectedExceptions = EntitlementException.class, expectedExceptionsMessageRegExp = "Script condition is unable to load script 123-456-789.")
public void missingScriptConfiguration() throws ScriptException, EntitlementException {
// Given
Subject subject = new Subject();
subject.getPrincipals().add(new AuthSPrincipal("user"));
Map<String, Set<String>> env = new HashMap<>();
scriptCondition = new ScriptCondition() {
@Override
protected ScriptConfiguration getScriptConfiguration(String realm) throws ScriptException {
return null;
}
};
// When
scriptCondition.setScriptId("123-456-789");
scriptCondition.evaluate("/abc", subject, "http://a:b/c", env);
}
use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.
the class ScriptExceptionMappingHandlerTest method shouldTranslateMessageToAcceptLanguage.
@Test
public void shouldTranslateMessageToAcceptLanguage() throws Exception {
// given
final Context serverContext = getHttpServerContext("te");
for (ScriptErrorCode errorCode : ScriptErrorCode.values()) {
// when
ResourceException re = mappingHandler.handleError(serverContext, null, new ScriptException(errorCode));
// then
assertNotNull(re);
// assertEquals("Test message", re.getMessage()); //TODO dont get this...
}
}
use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.
the class ScriptConfigurationServiceTest method shouldFailIfUuidDoesNotExistOnDelete.
@Test
public void shouldFailIfUuidDoesNotExistOnDelete() throws ScriptException {
// given
String uuid = "1234567890";
when(dataStore.containsUuid(anyString())).thenReturn(false);
// when
try {
service.delete(uuid);
fail("shouldFailIfUuidDoesNotExistOnDelete");
} catch (ScriptException e) {
// then
assertEquals(e.getScriptErrorCode(), SCRIPT_UUID_NOT_FOUND);
}
}
use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.
the class ScriptCondition method evaluate.
@Override
public ConditionDecision evaluate(String realm, Subject subject, String resourceName, Map<String, Set<String>> environment) throws EntitlementException {
try {
ScriptConfiguration configuration = getScriptConfiguration(realm);
if (configuration == null) {
throw new EntitlementException(EntitlementException.INVALID_SCRIPT_ID, scriptId);
}
ScriptObject script = new ScriptObject(configuration.getName(), configuration.getScript(), configuration.getLanguage());
Map<String, List<String>> advice = new HashMap<>();
Map<String, List<String>> responseAttributes = new HashMap<>();
Bindings scriptVariables = new SimpleBindings();
scriptVariables.put("logger", PolicyConstants.DEBUG);
scriptVariables.put("username", SubjectUtils.getPrincipalId(subject));
scriptVariables.put("resourceURI", resourceName);
scriptVariables.put("environment", environment);
scriptVariables.put("advice", advice);
scriptVariables.put("responseAttributes", responseAttributes);
scriptVariables.put("httpClient", getHttpClient(configuration.getLanguage()));
scriptVariables.put("authorized", Boolean.FALSE);
scriptVariables.put("ttl", Long.MAX_VALUE);
SSOToken ssoToken = SubjectUtils.getSSOToken(subject);
if (ssoToken != null) {
// If a token is present include the corresponding identity and session objects.
scriptVariables.put("identity", new ScriptedIdentity(coreWrapper.getIdentity(ssoToken)));
scriptVariables.put("session", new ScriptedSession(ssoToken));
}
evaluator.evaluateScript(script, scriptVariables);
boolean authorized = (Boolean) scriptVariables.get("authorized");
if (!authorized) {
return ConditionDecision.newFailureBuilder().setAdvice(transformMap(advice, LIST_TO_SET)).setResponseAttributes(transformMap(responseAttributes, LIST_TO_SET)).build();
}
long ttl = ((Number) scriptVariables.get("ttl")).longValue();
return ConditionDecision.newSuccessBuilder().setResponseAttributes(transformMap(responseAttributes, LIST_TO_SET)).setTimeToLive(ttl).build();
} catch (ScriptException | javax.script.ScriptException | IdRepoException | SSOException ex) {
throw new EntitlementException(EntitlementException.CONDITION_EVALUATION_FAILED, ex);
}
}
use of org.forgerock.openam.scripting.ScriptException in project OpenAM by OpenRock.
the class ScriptConfigurationDataStore method delete.
@Override
public void delete(String uuid) throws ScriptException {
ScriptConfiguration scriptConfig = get(uuid);
if (containsGlobalUuid(uuid) || isDefaultScript(scriptConfig)) {
throw new ScriptException(DELETING_DEFAULT_SCRIPT, scriptConfig.getName());
}
int usageCount = getUsageCount(scriptConfig);
if (usageCount > 0) {
ScriptContext scriptContext = scriptConfig.getContext();
if (usageCount == 1) {
throw new ScriptException(DELETING_SCRIPT_IN_USE_SINGULAR, scriptConfig.getName());
}
throw new ScriptException(DELETING_SCRIPT_IN_USE_PLURAL, scriptConfig.getName(), Integer.toString(usageCount));
}
try {
getSubOrgConfig().removeSubConfig(uuid);
} catch (SSOException | SMSException e) {
throw createAndLogError(logger, DELETE_FAILED, e, uuid, realm);
}
}
Aggregations