use of org.apereo.cas.util.scripting.WatchableGroovyScriptResource in project cas by apereo.
the class ScriptedRegisteredServiceMultifactorAuthenticationTrigger method isActivated.
@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest httpServletRequest, final HttpServletResponse response, final Service service) {
if (authentication == null || registeredService == null) {
LOGGER.debug("No authentication or service is available to determine event for principal");
return Optional.empty();
}
val policy = registeredService.getMultifactorPolicy();
if (policy == null || StringUtils.isBlank(policy.getScript())) {
LOGGER.trace("Multifactor authentication policy is absent or does not define a script to trigger multifactor authentication");
return Optional.empty();
}
val mfaScript = policy.getScript();
val providerMap = MultifactorAuthenticationUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
if (providerMap.isEmpty()) {
LOGGER.error("No multifactor authentication providers are available in the application context");
throw new AuthenticationException(new MultifactorAuthenticationProviderAbsentException());
}
LOGGER.trace("Locating multifactor authentication trigger script [{}] in script cache...", mfaScript);
if (!scriptCache.containsKey(mfaScript)) {
val matcherInline = ScriptingUtils.getMatcherForInlineGroovyScript(mfaScript);
val matcherFile = ScriptingUtils.getMatcherForExternalGroovyScript(mfaScript);
if (matcherInline.find()) {
val script = new GroovyShellScript(matcherInline.group(1));
scriptCache.put(mfaScript, script);
LOGGER.trace("Caching multifactor authentication trigger script as an executable shell script");
} else if (matcherFile.find()) {
try {
val scriptPath = SpringExpressionLanguageValueResolver.getInstance().resolve(matcherFile.group());
val resource = ResourceUtils.getResourceFrom(scriptPath);
val script = new WatchableGroovyScriptResource(resource);
scriptCache.put(mfaScript, script);
LOGGER.trace("Caching multifactor authentication trigger script as script resource [{}]", resource);
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
}
}
if (scriptCache.containsKey(mfaScript)) {
val executableScript = scriptCache.get(mfaScript);
LOGGER.debug("Executing multifactor authentication trigger script [{}]", executableScript);
val result = executableScript.execute(new Object[] { authentication, registeredService, httpServletRequest, service, applicationContext, LOGGER }, String.class);
LOGGER.debug("Multifactor authentication provider delivered by trigger script is [{}]", result);
if (StringUtils.isBlank(result)) {
LOGGER.debug("No multifactor authentication is returned from trigger script");
return Optional.empty();
}
val providerResult = providerMap.values().stream().filter(provider -> provider.getId().equalsIgnoreCase(result)).findFirst();
if (providerResult.isEmpty()) {
LOGGER.error("Unable to locate multifactor authentication provider [{}] in the application context", result);
throw new AuthenticationException(new MultifactorAuthenticationProviderAbsentException());
}
return providerResult;
}
return Optional.empty();
}
Aggregations