Search in sources :

Example 1 with WatchableGroovyScriptResource

use of org.apereo.cas.util.scripting.WatchableGroovyScriptResource in project cas by apereo.

the class LdapUtils method newLdaptiveSearchFilter.

/**
 * New ldaptive search filter search filter.
 *
 * @param filterQuery the filter query
 * @param paramName   the param name
 * @param values      the params
 * @return the search filter
 */
public static FilterTemplate newLdaptiveSearchFilter(final String filterQuery, final List<String> paramName, final List<String> values) {
    val filter = new FilterTemplate();
    if (ResourceUtils.doesResourceExist(filterQuery)) {
        ApplicationContextProvider.getScriptResourceCacheManager().ifPresentOrElse(cacheMgr -> {
            val cacheKey = ScriptResourceCacheManager.computeKey(filterQuery);
            var script = (ExecutableCompiledGroovyScript) null;
            if (cacheMgr.containsKey(cacheKey)) {
                script = cacheMgr.get(cacheKey);
                LOGGER.trace("Located cached groovy script [{}] for key [{}]", script, cacheKey);
            } else {
                val resource = Unchecked.supplier(() -> ResourceUtils.getRawResourceFrom(filterQuery)).get();
                LOGGER.trace("Groovy script [{}] for key [{}] is not cached", resource, cacheKey);
                script = new WatchableGroovyScriptResource(resource);
                cacheMgr.put(cacheKey, script);
                LOGGER.trace("Cached groovy script [{}] for key [{}]", script, cacheKey);
            }
            if (script != null) {
                val parameters = new LinkedHashMap<String, String>();
                IntStream.range(0, values.size()).forEachOrdered(i -> parameters.put(paramName.get(i), values.get(i)));
                val args = CollectionUtils.<String, Object>wrap("filter", filter, "parameters", parameters, "applicationContext", ApplicationContextProvider.getApplicationContext(), "logger", LOGGER);
                script.setBinding(args);
                script.execute(args.values().toArray(), FilterTemplate.class);
            }
        }, () -> {
            throw new RuntimeException("Script cache manager unavailable to handle LDAP filter");
        });
    } else {
        filter.setFilter(filterQuery);
        if (values != null) {
            IntStream.range(0, values.size()).forEach(i -> {
                val value = values.get(i);
                if (filter.getFilter().contains("{" + i + '}')) {
                    filter.setParameter(i, value);
                }
                val name = paramName.get(i);
                if (filter.getFilter().contains('{' + name + '}')) {
                    filter.setParameter(name, value);
                }
            });
        }
    }
    LOGGER.debug("Constructed LDAP search filter [{}]", filter.format());
    return filter;
}
Also used : lombok.val(lombok.val) ExecutableCompiledGroovyScript(org.apereo.cas.util.scripting.ExecutableCompiledGroovyScript) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource) FilterTemplate(org.ldaptive.FilterTemplate) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with WatchableGroovyScriptResource

use of org.apereo.cas.util.scripting.WatchableGroovyScriptResource in project cas by apereo.

the class RegisteredServiceScriptedAttributeFilter method initializeWatchableScriptIfNeeded.

@PostLoad
@SneakyThrows
private void initializeWatchableScriptIfNeeded() {
    if (this.executableScript == null) {
        val matcherInline = ScriptingUtils.getMatcherForInlineGroovyScript(script);
        val matcherFile = ScriptingUtils.getMatcherForExternalGroovyScript(script);
        if (matcherFile.find()) {
            val resource = ResourceUtils.getRawResourceFrom(matcherFile.group(2));
            this.executableScript = new WatchableGroovyScriptResource(resource);
        } else if (matcherInline.find()) {
            this.executableScript = new GroovyShellScript(matcherInline.group(1));
        }
    }
}
Also used : lombok.val(lombok.val) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource) GroovyShellScript(org.apereo.cas.util.scripting.GroovyShellScript) PostLoad(javax.persistence.PostLoad) SneakyThrows(lombok.SneakyThrows)

Example 3 with WatchableGroovyScriptResource

use of org.apereo.cas.util.scripting.WatchableGroovyScriptResource in project cas by apereo.

the class GroovyDelegatedClientIdentityProviderRedirectionStrategyTests method verifyOperation.

@Test
public void verifyOperation() {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val provider = DelegatedClientIdentityProviderConfiguration.builder().name("SomeClient").type("CasClient").redirectUrl("https://localhost:8443/redirect").build();
    val service = RegisteredServiceTestUtils.getService();
    val resource = new ClassPathResource("GroovyClientRedirectStrategy.groovy");
    val strategy = new GroovyDelegatedClientIdentityProviderRedirectionStrategy(this.servicesManager, new WatchableGroovyScriptResource(resource));
    assertFalse(strategy.getPrimaryDelegatedAuthenticationProvider(context, service, provider).isEmpty());
    assertEquals(0, strategy.getOrder());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) GroovyDelegatedClientIdentityProviderRedirectionStrategy(org.apereo.cas.pac4j.client.GroovyDelegatedClientIdentityProviderRedirectionStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 4 with WatchableGroovyScriptResource

use of org.apereo.cas.util.scripting.WatchableGroovyScriptResource in project cas by apereo.

the class GroovyRegisteredServiceUsernameProvider method initializeWatchableScriptIfNeeded.

@PostLoad
@SneakyThrows
private void initializeWatchableScriptIfNeeded() {
    if (this.executableScript == null) {
        val matcherInline = ScriptingUtils.getMatcherForInlineGroovyScript(groovyScript);
        val matcherFile = ScriptingUtils.getMatcherForExternalGroovyScript(groovyScript);
        if (matcherFile.find()) {
            val script = SpringExpressionLanguageValueResolver.getInstance().resolve(matcherFile.group());
            val resource = ResourceUtils.getRawResourceFrom(script);
            this.executableScript = new WatchableGroovyScriptResource(resource);
        } else if (matcherInline.find()) {
            this.executableScript = new GroovyShellScript(matcherInline.group(1));
        }
    }
}
Also used : lombok.val(lombok.val) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource) GroovyShellScript(org.apereo.cas.util.scripting.GroovyShellScript) PostLoad(javax.persistence.PostLoad) SneakyThrows(lombok.SneakyThrows)

Example 5 with WatchableGroovyScriptResource

use of org.apereo.cas.util.scripting.WatchableGroovyScriptResource in project cas by apereo.

the class GroovyScriptAuthenticationPolicy method initializeWatchableScriptIfNeeded.

private void initializeWatchableScriptIfNeeded() throws Exception {
    if (this.executableScript == null) {
        val matcherFile = ScriptingUtils.getMatcherForExternalGroovyScript(script);
        if (!matcherFile.find()) {
            throw new IllegalArgumentException("Unable to locate groovy script file at " + script);
        }
        val resource = ResourceUtils.getRawResourceFrom(matcherFile.group(2));
        this.executableScript = new WatchableGroovyScriptResource(resource);
    }
}
Also used : lombok.val(lombok.val) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource)

Aggregations

lombok.val (lombok.val)6 WatchableGroovyScriptResource (org.apereo.cas.util.scripting.WatchableGroovyScriptResource)6 GroovyShellScript (org.apereo.cas.util.scripting.GroovyShellScript)3 PostLoad (javax.persistence.PostLoad)2 SneakyThrows (lombok.SneakyThrows)2 ExecutableCompiledGroovyScript (org.apereo.cas.util.scripting.ExecutableCompiledGroovyScript)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Transient (javax.persistence.Transient)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Getter (lombok.Getter)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Authentication (org.apereo.cas.authentication.Authentication)1