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;
}
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)));
}
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)));
}
Aggregations