Search in sources :

Example 1 with ScriptConfiguration

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();
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryByStringFilterConverter(org.forgerock.openam.rest.query.QueryByStringFilterConverter) ArrayList(java.util.ArrayList) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) JsonPointer(org.forgerock.json.JsonPointer)

Example 2 with ScriptConfiguration

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ServiceConfigQueryFilterVisitor(org.forgerock.openam.sm.ServiceConfigQueryFilterVisitor) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) SSOException(com.iplanet.sso.SSOException)

Example 3 with ScriptConfiguration

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) SSOException(com.iplanet.sso.SSOException)

Example 4 with ScriptConfiguration

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);
    }
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) Test(org.testng.annotations.Test)

Example 5 with ScriptConfiguration

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);
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashMap(java.util.HashMap) ConditionDecision(com.sun.identity.entitlement.ConditionDecision) Bindings(javax.script.Bindings) Subject(javax.security.auth.Subject) ScriptException(org.forgerock.openam.scripting.ScriptException) AMIdentity(com.sun.identity.idm.AMIdentity) AuthSPrincipal(com.sun.identity.rest.AuthSPrincipal) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) Test(org.testng.annotations.Test)

Aggregations

ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)11 ScriptException (org.forgerock.openam.scripting.ScriptException)7 SSOException (com.iplanet.sso.SSOException)5 SMSException (com.sun.identity.sm.SMSException)4 ScriptObject (org.forgerock.openam.scripting.ScriptObject)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 Bindings (javax.script.Bindings)3 SSOToken (com.iplanet.sso.SSOToken)2 AuthSPrincipal (com.sun.identity.rest.AuthSPrincipal)2 ServiceConfig (com.sun.identity.sm.ServiceConfig)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 SimpleBindings (javax.script.SimpleBindings)2 Subject (javax.security.auth.Subject)2 ResourceResponse (org.forgerock.json.resource.ResourceResponse)2 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1 EntitlementException (com.sun.identity.entitlement.EntitlementException)1 AMIdentity (com.sun.identity.idm.AMIdentity)1 IdRepoException (com.sun.identity.idm.IdRepoException)1