Search in sources :

Example 1 with MultifactorAuthenticationProvider

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

the class GrouperMultifactorAuthenticationTrigger method isActivated.

@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest request, final HttpServletResponse response, final Service service) {
    val grouperField = casProperties.getAuthn().getMfa().getTriggers().getGrouper().getGrouperGroupField();
    if (StringUtils.isBlank(grouperField)) {
        LOGGER.debug("No group field is defined to process for Grouper multifactor trigger");
        return Optional.empty();
    }
    if (authentication == null || registeredService == null) {
        LOGGER.debug("No authentication or service is available to determine event for principal");
        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();
    }
    val principal = authentication.getPrincipal();
    val results = grouperFacade.getGroupsForSubjectId(principal.getId());
    if (results.isEmpty()) {
        LOGGER.debug("No groups could be found for [{}] to resolve events for MFA", principal);
        return Optional.empty();
    }
    val groupField = GrouperGroupField.valueOf(grouperField);
    val values = results.stream().map(wsGetGroupsResult -> Stream.of(wsGetGroupsResult.getWsGroups())).flatMap(Function.identity()).map(g -> GrouperFacade.getGrouperGroupAttribute(groupField, g)).collect(Collectors.toSet());
    return MultifactorAuthenticationUtils.resolveProvider(providerMap, values);
}
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) Function(java.util.function.Function) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) GrouperFacade(org.apereo.cas.grouper.GrouperFacade) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) MultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.MultifactorAuthenticationProviderResolver) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) GrouperGroupField(org.apereo.cas.grouper.GrouperGroupField) StringUtils(edu.internet2.middleware.grouperClientExt.org.apache.commons.lang3.StringUtils) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) RegisteredService(org.apereo.cas.services.RegisteredService) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Service(org.apereo.cas.authentication.principal.Service) Optional(java.util.Optional) AuthenticationException(org.apereo.cas.authentication.AuthenticationException)

Example 2 with MultifactorAuthenticationProvider

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

the class SamlIdPMultifactorAuthenticationTrigger method isActivated.

@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest request, final HttpServletResponse response, final Service service) {
    val context = new JEEContext(request, response);
    val result = SamlIdPUtils.retrieveSamlRequest(context, distributedSessionStore, openSamlConfigBean, AuthnRequest.class);
    val mappings = getAuthenticationContextMappings();
    return result.map(pair -> (AuthnRequest) pair.getLeft()).flatMap(authnRequest -> authnRequest.getRequestedAuthnContext().getAuthnContextClassRefs().stream().filter(Objects::nonNull).filter(ref -> StringUtils.isNotBlank(ref.getURI())).filter(ref -> {
        val clazz = ref.getURI();
        return mappings.containsKey(clazz);
    }).findFirst().map(mapped -> mappings.get(mapped.getURI()))).flatMap(id -> {
        val providerMap = MultifactorAuthenticationUtils.getAvailableMultifactorAuthenticationProviders(applicationContext);
        return MultifactorAuthenticationUtils.resolveProvider(providerMap, id);
    });
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthnRequest(org.opensaml.saml.saml2.core.AuthnRequest) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) MultifactorAuthenticationTrigger(org.apereo.cas.authentication.MultifactorAuthenticationTrigger) StringUtils(org.apache.commons.lang3.StringUtils) SessionStore(org.pac4j.core.context.session.SessionStore) ApplicationContext(org.springframework.context.ApplicationContext) RegisteredService(org.apereo.cas.services.RegisteredService) SamlIdPUtils(org.apereo.cas.support.saml.SamlIdPUtils) OpenSamlConfigBean(org.apereo.cas.support.saml.OpenSamlConfigBean) HttpRequestUtils(org.apereo.cas.util.HttpRequestUtils) Objects(java.util.Objects) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) Service(org.apereo.cas.authentication.principal.Service) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) JEEContext(org.pac4j.core.context.JEEContext) JEEContext(org.pac4j.core.context.JEEContext) Objects(java.util.Objects)

Example 3 with MultifactorAuthenticationProvider

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

the class RegisteredServicePrincipalAttributeMultifactorAuthenticationTrigger 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 || registeredService.getMultifactorPolicy().getMultifactorAuthenticationProviders().isEmpty()) {
        LOGGER.trace("Authentication policy is absent or does not contain any multifactor authentication providers");
        return Optional.empty();
    }
    if (StringUtils.isBlank(policy.getPrincipalAttributeNameTrigger()) || StringUtils.isBlank(policy.getPrincipalAttributeValueToMatch())) {
        LOGGER.debug("Authentication policy does not define a principal attribute and/or value to trigger multifactor authentication");
        return Optional.empty();
    }
    val principal = multifactorAuthenticationProviderResolver.resolvePrincipal(authentication.getPrincipal());
    val providers = MultifactorAuthenticationUtils.getMultifactorAuthenticationProviderForService(registeredService, applicationContext);
    if (providers.size() > 1) {
        val resolvedProvider = multifactorAuthenticationProviderSelector.resolve(providers, registeredService, principal);
        providers.clear();
        providers.add(resolvedProvider);
    }
    LOGGER.debug("Resolved multifactor providers are [{}]", providers);
    val result = multifactorAuthenticationProviderResolver.resolveEventViaPrincipalAttribute(principal, org.springframework.util.StringUtils.commaDelimitedListToSet(policy.getPrincipalAttributeNameTrigger()), registeredService, Optional.empty(), providers, (attributeValue, mfaProvider) -> attributeValue != null && RegexUtils.matches(Pattern.compile(policy.getPrincipalAttributeValueToMatch()), attributeValue));
    if (result != null && !result.isEmpty()) {
        return CollectionUtils.firstElement(result).map(value -> MultifactorAuthenticationUtils.getMultifactorAuthenticationProviderById(value.toString(), this.applicationContext)).orElseGet(() -> unmatchedMultifactorAuthenticationTrigger(principal, registeredService));
    }
    return unmatchedMultifactorAuthenticationTrigger(principal, registeredService);
}
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) HttpServletRequest(javax.servlet.http.HttpServletRequest) MultifactorAuthenticationProviderSelector(org.apereo.cas.authentication.MultifactorAuthenticationProviderSelector) MultifactorAuthenticationRequiredException(org.apereo.cas.authentication.MultifactorAuthenticationRequiredException) Authentication(org.apereo.cas.authentication.Authentication) CollectionUtils(org.apereo.cas.util.CollectionUtils) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) MultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.MultifactorAuthenticationProviderResolver) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) ApplicationContext(org.springframework.context.ApplicationContext) RegisteredService(org.apereo.cas.services.RegisteredService) RegexUtils(org.apereo.cas.util.RegexUtils) Slf4j(lombok.extern.slf4j.Slf4j) Service(org.apereo.cas.authentication.principal.Service) Optional(java.util.Optional) Principal(org.apereo.cas.authentication.principal.Principal) Pattern(java.util.regex.Pattern)

Example 4 with MultifactorAuthenticationProvider

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

the class OidcMultifactorAuthenticationTrigger method isActivated.

@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest request, final HttpServletResponse response, final Service service) {
    val acr = getAuthenticationClassReference(request, response);
    if (StringUtils.isBlank(acr)) {
        LOGGER.debug("No ACR provided in the authentication request");
        return Optional.empty();
    }
    val values = List.of(org.springframework.util.StringUtils.delimitedListToStringArray(acr, " "));
    val providerMap = MultifactorAuthenticationUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    if (providerMap.isEmpty()) {
        LOGGER.error("No multifactor authentication providers are available in the application context to handle [{}]", values);
        throw new AuthenticationException(new MultifactorAuthenticationProviderAbsentException());
    }
    val authnContexts = casProperties.getAuthn().getOidc().getCore().getAuthenticationContextReferenceMappings();
    val mappings = CollectionUtils.convertDirectedListToMap(authnContexts);
    val mappedAcrValues = values.stream().map(acrValue -> mappings.getOrDefault(acrValue, acrValue)).collect(Collectors.toList());
    LOGGER.debug("Mapped ACR values are [{}] to compare against [{}]", mappedAcrValues, providerMap.values());
    return providerMap.values().stream().filter(v -> mappedAcrValues.contains(v.getId())).findAny();
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Ordered(org.springframework.core.Ordered) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) 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) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.apereo.cas.authentication.Authentication) CollectionUtils(org.apereo.cas.util.CollectionUtils) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) JEEContext(org.pac4j.core.context.JEEContext) MultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.MultifactorAuthenticationProviderResolver) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) CasProtocolConstants(org.apereo.cas.CasProtocolConstants) OAuth20Constants(org.apereo.cas.support.oauth.OAuth20Constants) Unchecked(org.jooq.lambda.Unchecked) OAuth20Utils(org.apereo.cas.support.oauth.util.OAuth20Utils) URIBuilder(org.apache.http.client.utils.URIBuilder) lombok.val(lombok.val) HttpServletResponse(javax.servlet.http.HttpServletResponse) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) RegisteredService(org.apereo.cas.services.RegisteredService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Service(org.apereo.cas.authentication.principal.Service) Optional(java.util.Optional) EncodingUtils(org.apereo.cas.util.EncodingUtils) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MultifactorAuthenticationProviderAbsentException(org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException)

Example 5 with MultifactorAuthenticationProvider

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

the class CompositeProviderSelectionMultifactorWebflowEventResolver method filterEventsByMultifactorAuthenticationProvider.

@Override
protected Optional<Pair<Collection<Event>, Collection<MultifactorAuthenticationProvider>>> filterEventsByMultifactorAuthenticationProvider(final Collection<Event> resolveEvents, final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest request) {
    val composite = resolveEvents.stream().allMatch(event -> event.getId().equalsIgnoreCase(ChainingMultifactorAuthenticationProvider.DEFAULT_IDENTIFIER));
    if (!composite) {
        return super.filterEventsByMultifactorAuthenticationProvider(resolveEvents, authentication, registeredService, request);
    }
    val event = resolveEvents.iterator().next();
    val chainingProvider = (ChainingMultifactorAuthenticationProvider) event.getAttributes().get(MultifactorAuthenticationProvider.class.getName());
    return chainingProvider.getMultifactorAuthenticationProviders().stream().map(provider -> getConfigurationContext().getAuthenticationContextValidator().validate(authentication, provider.getId(), Optional.ofNullable(registeredService))).filter(MultifactorAuthenticationContextValidationResult::isSuccess).map(result -> {
        val validatedProvider = result.getProvider().orElseThrow();
        val validatedEvent = CollectionUtils.wrapCollection(new Event(this, validatedProvider.getId(), event.getAttributes()));
        val validatedProviders = CollectionUtils.wrapCollection(validatedProvider);
        return Optional.of(Pair.of(validatedEvent, validatedProviders));
    }).findAny().orElseGet(() -> {
        val activeProviders = chainingProvider.getMultifactorAuthenticationProviders().stream().filter(provider -> {
            val bypass = provider.getBypassEvaluator();
            return bypass == null || bypass.shouldMultifactorAuthenticationProviderExecute(authentication, registeredService, provider, request);
        }).collect(Collectors.toList());
        LOGGER.debug("Finalized set of resolved events are [{}] with providers [{}]", resolveEvents, activeProviders);
        return activeProviders.isEmpty() ? Optional.empty() : Optional.of(Pair.of(resolveEvents, activeProviders));
    });
}
Also used : lombok.val(lombok.val) Collection(java.util.Collection) lombok.val(lombok.val) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) Collectors(java.util.stream.Collectors) RegisteredService(org.apereo.cas.services.RegisteredService) Slf4j(lombok.extern.slf4j.Slf4j) HttpServletRequest(javax.servlet.http.HttpServletRequest) ChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.ChainingMultifactorAuthenticationProvider) Pair(org.apache.commons.lang3.tuple.Pair) Authentication(org.apereo.cas.authentication.Authentication) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) MultifactorAuthenticationContextValidationResult(org.apereo.cas.authentication.MultifactorAuthenticationContextValidationResult) Event(org.springframework.webflow.execution.Event) ChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.ChainingMultifactorAuthenticationProvider) Event(org.springframework.webflow.execution.Event)

Aggregations

lombok.val (lombok.val)13 MultifactorAuthenticationProvider (org.apereo.cas.authentication.MultifactorAuthenticationProvider)13 Authentication (org.apereo.cas.authentication.Authentication)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 Optional (java.util.Optional)9 RegisteredService (org.apereo.cas.services.RegisteredService)8 Slf4j (lombok.extern.slf4j.Slf4j)7 MultifactorAuthenticationUtils (org.apereo.cas.authentication.MultifactorAuthenticationUtils)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 RequiredArgsConstructor (lombok.RequiredArgsConstructor)6 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)6 MultifactorAuthenticationTrigger (org.apereo.cas.authentication.MultifactorAuthenticationTrigger)6 Service (org.apereo.cas.authentication.principal.Service)6 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)6 ApplicationContext (org.springframework.context.ApplicationContext)6 Getter (lombok.Getter)5 Setter (lombok.Setter)5 Ordered (org.springframework.core.Ordered)5 Collectors (java.util.stream.Collectors)4 MultifactorAuthenticationProviderAbsentException (org.apereo.cas.authentication.MultifactorAuthenticationProviderAbsentException)4