Search in sources :

Example 1 with TimeBasedAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties in project cas by apereo.

the class TimedMultifactorAuthenticationPolicyEventResolver method checkTimedMultifactorProvidersForRequest.

private Set<Event> checkTimedMultifactorProvidersForRequest(final RequestContext context, final RegisteredService service, final Authentication authentication) {
    final LocalDateTime now = LocalDateTime.now();
    final DayOfWeek dow = DayOfWeek.from(now);
    final List<String> dayNamesForToday = Arrays.stream(TextStyle.values()).map(style -> dow.getDisplayName(style, Locale.getDefault())).collect(Collectors.toList());
    final Map<String, MultifactorAuthenticationProvider> providerMap = MultifactorAuthenticationUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    final TimeBasedAuthenticationProperties timed = this.timedMultifactor.stream().filter(t -> {
        boolean providerEvent = false;
        if (!t.getOnDays().isEmpty()) {
            providerEvent = t.getOnDays().stream().filter(dayNamesForToday::contains).findAny().isPresent();
        }
        if (t.getOnOrAfterHour() >= 0) {
            providerEvent = now.getHour() >= t.getOnOrAfterHour();
        }
        if (t.getOnOrBeforeHour() >= 0) {
            providerEvent = now.getHour() <= t.getOnOrBeforeHour();
        }
        return providerEvent;
    }).findFirst().orElse(null);
    if (timed != null) {
        final Optional<MultifactorAuthenticationProvider> providerFound = resolveProvider(providerMap, timed.getProviderId());
        if (!providerFound.isPresent()) {
            LOGGER.error("Adaptive authentication is configured to require [{}] for [{}], yet [{}] absent in the configuration.", timed.getProviderId(), service, timed.getProviderId());
            throw new AuthenticationException();
        }
        return buildEvent(context, service, authentication, providerFound.get());
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Arrays(java.util.Arrays) MultifactorAuthenticationProviderSelector(org.apereo.cas.services.MultifactorAuthenticationProviderSelector) LocalDateTime(java.time.LocalDateTime) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) TicketRegistrySupport(org.apereo.cas.ticket.registry.TicketRegistrySupport) RequestContext(org.springframework.webflow.execution.RequestContext) TimeBasedAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties) Authentication(org.apereo.cas.authentication.Authentication) Locale(java.util.Locale) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) AuthenticationSystemSupport(org.apereo.cas.authentication.AuthenticationSystemSupport) MultifactorAuthenticationUtils(org.apereo.cas.authentication.MultifactorAuthenticationUtils) CookieGenerator(org.springframework.web.util.CookieGenerator) ServicesManager(org.apereo.cas.services.ServicesManager) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) TextStyle(java.time.format.TextStyle) Audit(org.apereo.inspektr.audit.annotation.Audit) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) Set(java.util.Set) Collectors(java.util.stream.Collectors) RegisteredService(org.apereo.cas.services.RegisteredService) BaseMultifactorAuthenticationProviderEventResolver(org.apereo.cas.web.flow.authentication.BaseMultifactorAuthenticationProviderEventResolver) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DayOfWeek(java.time.DayOfWeek) Optional(java.util.Optional) WebUtils(org.apereo.cas.web.support.WebUtils) Event(org.springframework.webflow.execution.Event) DayOfWeek(java.time.DayOfWeek) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) TimeBasedAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties)

Example 2 with TimeBasedAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties in project cas by apereo.

the class TimedMultifactorAuthenticationTriggerTests method verifyNoProviders.

@Test
@Tag("DisableProviderRegistration")
@Order(1)
public void verifyNoProviders() {
    val props = new CasConfigurationProperties();
    val trigger = new TimedMultifactorAuthenticationTrigger(props, applicationContext);
    val timeProps = new TimeBasedAuthenticationProperties();
    timeProps.setProviderId(TestMultifactorAuthenticationProvider.ID);
    timeProps.setOnOrAfterHour(2);
    timeProps.setOnOrBeforeHour(2);
    timeProps.setOnDays(List.of("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"));
    props.getAuthn().getAdaptive().getPolicy().getRequireTimedMultifactor().add(timeProps);
    assertThrows(AuthenticationException.class, () -> trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class)));
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) TimeBasedAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 3 with TimeBasedAuthenticationProperties

use of org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties in project cas by apereo.

the class TimedMultifactorAuthenticationTriggerTests method verifyProvider.

@Test
@Order(3)
public void verifyProvider() {
    val props = new CasConfigurationProperties();
    val timeProps = new TimeBasedAuthenticationProperties();
    timeProps.setProviderId(TestMultifactorAuthenticationProvider.ID);
    timeProps.setOnOrAfterHour(0);
    timeProps.setOnOrBeforeHour(24);
    timeProps.setOnDays(List.of("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"));
    props.getAuthn().getAdaptive().getPolicy().getRequireTimedMultifactor().add(timeProps);
    var trigger = new TimedMultifactorAuthenticationTrigger(props, applicationContext);
    var result = trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class));
    assertTrue(result.isPresent());
    timeProps.setProviderId("bad-id");
    val trigger2 = new TimedMultifactorAuthenticationTrigger(props, applicationContext);
    assertThrows(AuthenticationException.class, () -> trigger2.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class)));
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Service(org.apereo.cas.authentication.principal.Service) TimeBasedAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)3 TimeBasedAuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.TimeBasedAuthenticationProperties)3 lombok.val (lombok.val)2 Order (org.junit.jupiter.api.Order)2 Test (org.junit.jupiter.api.Test)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 DayOfWeek (java.time.DayOfWeek)1 LocalDateTime (java.time.LocalDateTime)1 TextStyle (java.time.format.TextStyle)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)1 Authentication (org.apereo.cas.authentication.Authentication)1 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)1