Search in sources :

Example 1 with CollectionUtils

use of org.apereo.cas.util.CollectionUtils in project cas by apereo.

the class DefaultMultifactorTriggerSelectionStrategy method resolve.

@Override
public Optional<String> resolve(final Collection<MultifactorAuthenticationProvider> providers, final HttpServletRequest request, final RegisteredService service, final Principal principal) {
    Optional<String> provider = Optional.empty();
    // short-circuit if we don't have any available MFA providers
    if (providers == null || providers.isEmpty()) {
        return provider;
    }
    final Set<String> validProviderIds = providers.stream().map(MultifactorAuthenticationProvider::getId).collect(Collectors.toSet());
    // check for an opt-in provider id parameter trigger, we only care about the first value
    if (!provider.isPresent() && request != null) {
        provider = Optional.ofNullable(request.getParameter(requestParameter)).filter(validProviderIds::contains);
    }
    // check for a RegisteredService configured trigger
    if (!provider.isPresent() && service != null) {
        final RegisteredServiceMultifactorPolicy policy = service.getMultifactorPolicy();
        if (shouldApplyRegisteredServiceMultifactorPolicy(policy, principal)) {
            provider = policy.getMultifactorAuthenticationProviders().stream().filter(validProviderIds::contains).findFirst();
        }
    }
    // check for principal attribute trigger
    if (!provider.isPresent() && principal != null && StringUtils.hasText(globalPrincipalAttributeNameTriggers)) {
        provider = StreamSupport.stream(ATTR_NAMES.split(globalPrincipalAttributeNameTriggers).spliterator(), false).map(principal.getAttributes()::get).filter(Objects::nonNull).map(CollectionUtils::toCollection).flatMap(Set::stream).filter(String.class::isInstance).map(String.class::cast).filter(validProviderIds::contains).findFirst();
    }
    // return the resolved trigger
    return provider;
}
Also used : RegisteredServiceMultifactorPolicy(org.apereo.cas.services.RegisteredServiceMultifactorPolicy) CollectionUtils(org.apereo.cas.util.CollectionUtils)

Aggregations

RegisteredServiceMultifactorPolicy (org.apereo.cas.services.RegisteredServiceMultifactorPolicy)1 CollectionUtils (org.apereo.cas.util.CollectionUtils)1