use of org.forgerock.openam.scripting.service.ScriptConfiguration in project OpenAM by OpenRock.
the class ScriptResource method queryCollection.
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler resultHandler) {
final QueryFilter<JsonPointer> filter = request.getQueryFilter();
try {
final Set<ScriptConfiguration> configs;
if (filter == null) {
configs = serviceFactory.create(getContextSubject(context), getRealm(context)).getAll();
} else {
QueryFilter<String> stringQueryFilter = filter.accept(new QueryByStringFilterConverter(), null);
configs = serviceFactory.create(getContextSubject(context), getRealm(context)).get(stringQueryFilter);
}
List<ResourceResponse> results = new ArrayList<>();
for (ScriptConfiguration configuration : configs) {
String id = configuration.getId();
results.add(newResourceResponse(id, null, asJson(configuration)));
}
QueryResponsePresentation.enableDeprecatedRemainingQueryResponse(request);
return QueryResponsePresentation.perform(resultHandler, request, results);
} catch (ScriptException se) {
return exceptionMappingHandler.handleError(context, request, se).asPromise();
}
}
use of org.forgerock.openam.scripting.service.ScriptConfiguration in project OpenAM by OpenRock.
the class ScriptConfigurationDataStore method get.
@Override
public Set<ScriptConfiguration> get(QueryFilter<String> queryFilter) throws ScriptException {
final Set<ScriptConfiguration> scriptConfigurations = new LinkedHashSet<>();
try {
ServiceConfig config = getSubOrgConfig();
Set<String> uuids = config.getSubConfigNames();
for (String uuid : uuids) {
if (queryFilter.accept(new ServiceConfigQueryFilterVisitor(), config.getSubConfig(uuid))) {
scriptConfigurations.add(get(uuid));
}
}
config = getSubGlobalConfig();
uuids = config.getSubConfigNames();
for (String uuid : uuids) {
if (queryFilter.accept(new ServiceConfigQueryFilterVisitor(), config.getSubConfig(uuid))) {
scriptConfigurations.add(get(uuid));
}
}
} catch (SMSException | SSOException e) {
throw createAndLogError(logger, RETRIEVE_ALL_FAILED, e, realm);
} catch (UnsupportedOperationException e) {
throw createAndLogError(logger, ScriptErrorCode.valueOf(e.getMessage()), e);
}
return scriptConfigurations;
}
use of org.forgerock.openam.scripting.service.ScriptConfiguration in project OpenAM by OpenRock.
the class ScriptConfigurationDataStore method getAll.
@Override
public Set<ScriptConfiguration> getAll() throws ScriptException {
final Set<ScriptConfiguration> scriptConfigurations = new LinkedHashSet<>();
try {
ServiceConfig config = getSubOrgConfig();
Set<String> uuids = config.getSubConfigNames();
for (String uuid : uuids) {
scriptConfigurations.add(scriptConfigurationFromMap(uuid, config.getSubConfig(uuid).getAttributesForRead()));
}
config = getSubGlobalConfig();
uuids = config.getSubConfigNames();
for (String uuid : uuids) {
scriptConfigurations.add(scriptConfigurationFromMap(uuid, config.getSubConfig(uuid).getAttributesForRead()));
}
} catch (SSOException | SMSException e) {
throw createAndLogError(logger, RETRIEVE_ALL_FAILED, e, realm);
}
return scriptConfigurations;
}
use of org.forgerock.openam.scripting.service.ScriptConfiguration in project OpenAM by OpenRock.
the class ScriptConfigurationDataStoreTest method shouldFailIfConfigDoesNotExistOnSave.
@Test
public void shouldFailIfConfigDoesNotExistOnSave() throws ScriptException {
// given
ScriptConfiguration sc = ScriptConfiguration.builder().generateId().setName("MyJavaScript").setScript("var a = 123;var b = 456;").setLanguage(JAVASCRIPT).setContext(POLICY_CONDITION).build();
// when
try {
dataStore.save(sc);
fail("shouldFailIfConfigDoesNotExistOnSave");
} catch (ScriptException e) {
// then
assertEquals(e.getScriptErrorCode(), SAVE_FAILED);
}
}
use of org.forgerock.openam.scripting.service.ScriptConfiguration in project OpenAM by OpenRock.
the class ScriptConditionTest method successfulEvaluation.
@Test
public void successfulEvaluation() throws EntitlementException, ScriptException, javax.script.ScriptException, IdRepoException, SSOException {
// Given
Subject subject = new Subject();
SSOToken token = mock(SSOToken.class);
subject.getPrivateCredentials().add(token);
subject.getPrincipals().add(new AuthSPrincipal("user"));
Map<String, Set<String>> env = new HashMap<>();
Map<String, Set<String>> advice = new HashMap<>();
final ScriptConfiguration configuration = ScriptConfiguration.builder().setId("123-456-789").setName("test-script").setContext(ScriptContext.POLICY_CONDITION).setLanguage(SupportedScriptingLanguage.JAVASCRIPT).setScript("some-script-here").build();
scriptCondition = new ScriptCondition() {
@Override
protected ScriptConfiguration getScriptConfiguration(String realm) throws ScriptException {
return configuration;
}
};
given(coreWrapper.getIdentity(token)).willReturn(mock(AMIdentity.class));
// When
scriptCondition.setScriptId("123-456-789");
ConditionDecision decision = scriptCondition.evaluate("/abc", subject, "http://a:b/c", env);
// Then
// Hard to test true scenario
assertThat(decision.isSatisfied()).isFalse();
verify(scriptEvaluator).evaluateScript(scriptObjectCaptor.capture(), bindingsCaptor.capture());
ScriptObject scriptObject = scriptObjectCaptor.getValue();
assertThat(scriptObject.getName()).isEqualTo("test-script");
assertThat(scriptObject.getLanguage()).isEqualTo(SupportedScriptingLanguage.JAVASCRIPT);
assertThat(scriptObject.getScript()).isEqualTo("some-script-here");
Bindings bindings = bindingsCaptor.getValue();
assertThat(bindings.get("logger")).isEqualTo(PolicyConstants.DEBUG);
assertThat(bindings.get("username")).isEqualTo("user");
assertThat(bindings.get("resourceURI")).isEqualTo("http://a:b/c");
assertThat(bindings.get("environment")).isEqualTo(env);
assertThat(bindings.get("advice")).isEqualTo(advice);
assertThat(bindings.get("httpClient")).isEqualTo(restletHttpClient);
assertThat(bindings.get("authorized")).isEqualTo(Boolean.FALSE);
assertThat(bindings.get("ttl")).isEqualTo(Long.MAX_VALUE);
}
Aggregations