Search in sources :

Example 6 with MultifactorAuthenticationProviderAbsentException

use of org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException in project cas by apereo.

the class RadiusAccessChallengedMultifactorAuthenticationTrigger method isActivated.

@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest request, final HttpServletResponse response, final Service service) {
    if (authentication == null) {
        LOGGER.debug("No authentication or service is available to determine event for principal");
        return Optional.empty();
    }
    if (!supports(authentication)) {
        LOGGER.trace("Authentication attempt does not qualify for radius multifactor authentication");
        return Optional.empty();
    }
    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());
    }
    val id = casProperties.getAuthn().getMfa().getRadius().getId();
    LOGGER.debug("Authentication requires multifactor authentication via provider [{}]", id);
    return MultifactorAuthenticationUtils.resolveProvider(providerMap, id);
}
Also used : lombok.val(lombok.val) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MultifactorAuthenticationProviderAbsentException(org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException)

Example 7 with MultifactorAuthenticationProviderAbsentException

use of org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException 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();
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Ordered(org.springframework.core.Ordered) Setter(lombok.Setter) Getter(lombok.Getter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) MultifactorAuthenticationTrigger(org.apereo.cas.authentication.MultifactorAuthenticationTrigger) StringUtils(org.apache.commons.lang3.StringUtils) MultifactorAuthenticationProviderAbsentException(org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException) LoggingUtils(org.apereo.cas.util.LoggingUtils) ScriptingUtils(org.apereo.cas.util.scripting.ScriptingUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) GroovyShellScript(org.apereo.cas.util.scripting.GroovyShellScript) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) ResourceUtils(org.apereo.cas.util.ResourceUtils) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource) ApplicationContext(org.springframework.context.ApplicationContext) RegisteredService(org.apereo.cas.services.RegisteredService) Slf4j(lombok.extern.slf4j.Slf4j) SpringExpressionLanguageValueResolver(org.apereo.cas.util.spring.SpringExpressionLanguageValueResolver) Transient(javax.persistence.Transient) Service(org.apereo.cas.authentication.principal.Service) Optional(java.util.Optional) ExecutableCompiledGroovyScript(org.apereo.cas.util.scripting.ExecutableCompiledGroovyScript) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MultifactorAuthenticationProviderAbsentException(org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException) WatchableGroovyScriptResource(org.apereo.cas.util.scripting.WatchableGroovyScriptResource) GroovyShellScript(org.apereo.cas.util.scripting.GroovyShellScript) MultifactorAuthenticationProviderAbsentException(org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException) AuthenticationException(org.apereo.cas.authentication.AuthenticationException)

Example 8 with MultifactorAuthenticationProviderAbsentException

use of org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException in project cas by apereo.

the class GlobalMultifactorAuthenticationTrigger 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) {
        LOGGER.debug("No authentication is available to determine event for principal");
        return Optional.empty();
    }
    val globalProviderId = casProperties.getAuthn().getMfa().getTriggers().getGlobal().getGlobalProviderId();
    if (StringUtils.isBlank(globalProviderId)) {
        LOGGER.trace("No value could be found for for the global provider id");
        return Optional.empty();
    }
    LOGGER.debug("Attempting to globally activate [{}]", globalProviderId);
    val providerMap = MultifactorAuthenticationUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    if (providerMap.isEmpty()) {
        LOGGER.error("No multifactor authentication providers are available in the application context to handle [{}]", globalProviderId);
        throw new AuthenticationException(new MultifactorAuthenticationProviderAbsentException());
    }
    val providers = org.springframework.util.StringUtils.commaDelimitedListToSet(globalProviderId);
    val resolvedProviders = providers.stream().map(provider -> MultifactorAuthenticationUtils.resolveProvider(providerMap, provider)).filter(Optional::isPresent).map(Optional::get).sorted(Comparator.comparing(MultifactorAuthenticationProvider::getOrder)).collect(Collectors.toList());
    if (resolvedProviders.size() != providers.size()) {
        handleAbsentMultifactorProvider(globalProviderId, resolvedProviders);
    }
    if (resolvedProviders.size() == 1) {
        return resolveSingleMultifactorProvider(resolvedProviders.get(0));
    }
    return resolveMultifactorProvider(authentication, registeredService, resolvedProviders);
}
Also used : lombok.val(lombok.val) Optional(java.util.Optional) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MultifactorAuthenticationProviderAbsentException(org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider)

Aggregations

lombok.val (lombok.val)8 MultifactorAuthenticationProviderAbsentException (org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException)8 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)7 Optional (java.util.Optional)3 MultifactorAuthenticationProvider (org.apereo.cas.authentication.MultifactorAuthenticationProvider)3 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Getter (lombok.Getter)2 RequiredArgsConstructor (lombok.RequiredArgsConstructor)2 Setter (lombok.Setter)2 Slf4j (lombok.extern.slf4j.Slf4j)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Authentication (org.apereo.cas.authentication.Authentication)2 MultifactorAuthenticationTrigger (org.apereo.cas.authentication.MultifactorAuthenticationTrigger)2 MultifactorAuthenticationUtils (org.apereo.cas.authentication.MultifactorAuthenticationUtils)2 Service (org.apereo.cas.authentication.principal.Service)2 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)2 RegisteredService (org.apereo.cas.services.RegisteredService)2 ApplicationContext (org.springframework.context.ApplicationContext)2