Search in sources :

Example 16 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class AbstractSandboxTests method shouldBeAllowedToAccessWhiteListedInstance.

@Test
public void shouldBeAllowedToAccessWhiteListedInstance() throws Exception {
    // Given
    Allowed allowed = new Allowed();
    Bindings bindings = new SimpleBindings();
    bindings.put("allowed", allowed);
    // When
    eval(bindings, "allowed.setDirty()");
    // Then
    assertThat(allowed.dirty).isTrue();
}
Also used : SimpleBindings(javax.script.SimpleBindings) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) Test(org.testng.annotations.Test)

Example 17 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class ChainedBindingsTest method setupTestObjects.

@BeforeMethod
public void setupTestObjects() {
    currentScope = new SimpleBindings();
    parentScope = new SimpleBindings();
    chainedBindings = new ChainedBindings(parentScope, currentScope);
}
Also used : SimpleBindings(javax.script.SimpleBindings) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 18 with SimpleBindings

use of javax.script.SimpleBindings in project OpenAM by OpenRock.

the class OpenAMScopeValidator method getUserInfo.

/**
     * {@inheritDoc}
     */
public UserInfoClaims getUserInfo(AccessToken token, OAuth2Request request) throws UnauthorizedClientException, NotFoundException {
    Map<String, Object> response = new HashMap<>();
    Bindings scriptVariables = new SimpleBindings();
    SSOToken ssoToken = getUsersSession(request);
    String realm;
    Set<String> scopes;
    AMIdentity id;
    OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(request);
    Map<String, Set<String>> requestedClaimsValues = gatherRequestedClaims(providerSettings, request, token);
    try {
        if (token != null) {
            OpenIdConnectClientRegistration clientRegistration;
            try {
                clientRegistration = clientRegistrationStore.get(token.getClientId(), request);
            } catch (InvalidClientException e) {
                logger.message("Unable to retrieve client from store.");
                throw new NotFoundException("No valid client registration found.");
            }
            final String subId = clientRegistration.getSubValue(token.getResourceOwnerId(), providerSettings);
            //data comes from token when we have one
            realm = token.getRealm();
            scopes = token.getScope();
            id = identityManager.getResourceOwnerIdentity(token.getResourceOwnerId(), realm);
            response.put(OAuth2Constants.JWTTokenParams.SUB, subId);
            response.put(OAuth2Constants.JWTTokenParams.UPDATED_AT, getUpdatedAt(token.getResourceOwnerId(), token.getRealm(), request));
        } else {
            //otherwise we're simply reading claims into the id_token, so grab it from the request/ssoToken
            realm = DNMapper.orgNameToRealmName(ssoToken.getProperty(ISAuthConstants.ORGANIZATION));
            id = identityManager.getResourceOwnerIdentity(ssoToken.getProperty(ISAuthConstants.USER_ID), realm);
            String scopeStr = request.getParameter(OAuth2Constants.Params.SCOPE);
            scopes = splitScope(scopeStr);
        }
        scriptVariables.put(OAuth2Constants.ScriptParams.SCOPES, getScriptFriendlyScopes(scopes));
        scriptVariables.put(OAuth2Constants.ScriptParams.IDENTITY, id);
        scriptVariables.put(OAuth2Constants.ScriptParams.LOGGER, logger);
        scriptVariables.put(OAuth2Constants.ScriptParams.CLAIMS, response);
        scriptVariables.put(OAuth2Constants.ScriptParams.SESSION, ssoToken);
        scriptVariables.put(OAuth2Constants.ScriptParams.REQUESTED_CLAIMS, requestedClaimsValues);
        ScriptObject script = getOIDCClaimsExtensionScript(realm);
        try {
            return scriptEvaluator.evaluateScript(script, scriptVariables);
        } catch (ScriptException e) {
            logger.message("Error running OIDC claims script", e);
            throw new ServerException("Error running OIDC claims script: " + e.getMessage());
        }
    } catch (ServerException e) {
        //API does not allow ServerExceptions to be thrown!
        throw new NotFoundException(e.getMessage());
    } catch (SSOException e) {
        throw new NotFoundException(e.getMessage());
    }
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) OpenIdConnectClientRegistration(org.forgerock.openidconnect.OpenIdConnectClientRegistration) SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashSet(java.util.HashSet) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) AMHashMap(com.iplanet.am.sdk.AMHashMap) HashMap(java.util.HashMap) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) SSOException(com.iplanet.sso.SSOException) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) SimpleBindings(javax.script.SimpleBindings) AMIdentity(com.sun.identity.idm.AMIdentity) InvalidClientException(org.forgerock.oauth2.core.exceptions.InvalidClientException) JSONObject(org.json.JSONObject) ScriptObject(org.forgerock.openam.scripting.ScriptObject) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings)

Example 19 with SimpleBindings

use of javax.script.SimpleBindings in project logging-log4j2 by apache.

the class ScriptCondition method selectFilesToDelete.

/**
     * Executes the script
     * 
     * @param baseDir
     * @param candidates
     * @return
     */
@SuppressWarnings("unchecked")
public List<PathWithAttributes> selectFilesToDelete(final Path basePath, final List<PathWithAttributes> candidates) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("basePath", basePath);
    bindings.put("pathList", candidates);
    bindings.putAll(configuration.getProperties());
    bindings.put("configuration", configuration);
    bindings.put("substitutor", configuration.getStrSubstitutor());
    bindings.put("statusLogger", LOGGER);
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return (List<PathWithAttributes>) object;
}
Also used : SimpleBindings(javax.script.SimpleBindings) List(java.util.List)

Example 20 with SimpleBindings

use of javax.script.SimpleBindings in project logging-log4j2 by apache.

the class ScriptFilter method filter.

@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", msg instanceof String ? new SimpleMessage((String) msg) : new ObjectMessage(msg));
    bindings.put("parameters", null);
    bindings.put("throwable", t);
    bindings.putAll(configuration.getProperties());
    bindings.put("substitutor", configuration.getStrSubstitutor());
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
Also used : SimpleBindings(javax.script.SimpleBindings) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Aggregations

SimpleBindings (javax.script.SimpleBindings)67 Bindings (javax.script.Bindings)47 Test (org.junit.Test)20 ScriptContext (javax.script.ScriptContext)14 SimpleScriptContext (javax.script.SimpleScriptContext)12 Test (org.testng.annotations.Test)11 ScriptException (javax.script.ScriptException)8 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 StringWriter (java.io.StringWriter)5 HashMap (java.util.HashMap)5 ScriptEngine (javax.script.ScriptEngine)5 PrintWriter (java.io.PrintWriter)4 CompiledScript (javax.script.CompiledScript)4 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)4 StringReader (java.io.StringReader)3 Map (java.util.Map)3 ScriptEngineManager (javax.script.ScriptEngineManager)3 Resource (org.apache.sling.api.resource.Resource)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 ProtectedBindings (org.apache.sling.scripting.core.impl.helper.ProtectedBindings)3