Search in sources :

Example 6 with ScriptException

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);
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Set(java.util.Set) HashMap(java.util.HashMap) AuthSPrincipal(com.sun.identity.rest.AuthSPrincipal) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 7 with ScriptException

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...
    }
}
Also used : Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) ClientContext(org.forgerock.services.context.ClientContext) ScriptException(org.forgerock.openam.scripting.ScriptException) ScriptErrorCode(org.forgerock.openam.scripting.ScriptConstants.ScriptErrorCode) ResourceException(org.forgerock.json.resource.ResourceException) Test(org.testng.annotations.Test)

Example 8 with ScriptException

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

Example 9 with ScriptException

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);
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) SSOToken(com.iplanet.sso.SSOToken) HashMap(java.util.HashMap) IdRepoException(com.sun.identity.idm.IdRepoException) SSOException(com.iplanet.sso.SSOException) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) EntitlementException(com.sun.identity.entitlement.EntitlementException) ScriptException(org.forgerock.openam.scripting.ScriptException) SimpleBindings(javax.script.SimpleBindings) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) List(java.util.List) ScriptedSession(org.forgerock.openam.scripting.api.ScriptedSession) ScriptedIdentity(org.forgerock.openam.scripting.api.ScriptedIdentity)

Example 10 with ScriptException

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

Aggregations

ScriptException (org.forgerock.openam.scripting.ScriptException)14 Test (org.testng.annotations.Test)8 ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)7 ScriptObject (org.forgerock.openam.scripting.ScriptObject)4 HashMap (java.util.HashMap)3 Bindings (javax.script.Bindings)3 Subject (javax.security.auth.Subject)3 ResourceException (org.forgerock.json.resource.ResourceException)3 SSOException (com.iplanet.sso.SSOException)2 SSOToken (com.iplanet.sso.SSOToken)2 AuthSPrincipal (com.sun.identity.rest.AuthSPrincipal)2 Set (java.util.Set)2 SimpleBindings (javax.script.SimpleBindings)2 JsonValue (org.forgerock.json.JsonValue)2 NotSupportedException (org.forgerock.json.resource.NotSupportedException)2 HttpContext (org.forgerock.json.resource.http.HttpContext)2 ScriptErrorCode (org.forgerock.openam.scripting.ScriptConstants.ScriptErrorCode)2 Context (org.forgerock.services.context.Context)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1