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;
}
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));
}
}
}
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());
}
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));
}
}
}
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);
}
}
Aggregations