@Override
    public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication,
                                                                   final RegisteredService registeredService,
                                                                   final HttpServletRequest httpServletRequest,
                                                                   final HttpServletResponse response,
                                                                   final Service service) throws Throwable {
        if (authentication == null || registeredService == null) {
            LOGGER.debug("No authentication or service is available to determine event for principal");
            return Optional.empty();
        }

        LOGGER.trace("Evaluating multifactor authentication policy for registered service [{}]", registeredService);
        val policy = registeredService.getMultifactorAuthenticationPolicy();
        if (policy == null || registeredService.getMultifactorAuthenticationPolicy().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, service, Optional.empty(), providers,
            (attributeValue, mfaProvider) ->
                attributeValue != null && RegexUtils.find(policy.getPrincipalAttributeValueToMatch(), attributeValue));

        if (result != null && !result.isEmpty()) {
            return CollectionUtils.firstElement(result)
                .map(Event.class::cast)
                .map(event -> {
                    val provider = CollectionUtils.firstElement(providers, MultifactorAuthenticationProvider.class).orElseThrow();
                    if (provider instanceof final ChainingMultifactorAuthenticationProvider chain && provider.getId().equals(event.getId())) {
                        val matched = chain.getMultifactorAuthenticationProviders()
                            .stream()
                            .map(p -> MultifactorAuthenticationUtils.getMultifactorAuthenticationProviderById(p.getId(), applicationContext))
                            .allMatch(Optional::isPresent);
                        return matched ? Optional.of(provider) : unmatchedMultifactorAuthenticationTrigger(principal, registeredService);
                    }
                    return MultifactorAuthenticationUtils.getMultifactorAuthenticationProviderById(event.getId(), applicationContext);
                })
                .orElseGet(() -> unmatchedMultifactorAuthenticationTrigger(principal, registeredService));
        }

        return unmatchedMultifactorAuthenticationTrigger(principal, registeredService);
    }
This is {@link RegisteredServicePrincipalAttributeMultifactorAuthenticationTrigger}.

@author Misagh Moayyed
@since 6.0.0
core/cas-server-core-authentication-mfa-api/src/main/java/org/apereo/cas/authentication/mfa/trigger/RegisteredServicePrincipalAttributeMultifactorAuthenticationTrigger.java