Search in sources :

Example 11 with DefaultMultifactorAuthenticationProviderResolver

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

the class PrincipalAttributeMultifactorAuthenticationTriggerTests method verifyDenyWhenUnmatched.

@Test
public void verifyDenyWhenUnmatched() {
    val props = new CasConfigurationProperties();
    val principal = props.getAuthn().getMfa().getTriggers().getPrincipal();
    principal.setGlobalPrincipalAttributeNameTriggers("email");
    principal.setGlobalPrincipalAttributeValueRegex("-nothing-");
    principal.setDenyIfUnmatched(true);
    val resolver = new DefaultMultifactorAuthenticationProviderResolver(MultifactorAuthenticationPrincipalResolver.identical());
    val trigger = new PrincipalAttributeMultifactorAuthenticationTrigger(props, resolver, applicationContext);
    val e = assertThrows(AuthenticationException.class, () -> trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class)));
    assertNotNull(e.getCode());
    assertTrue(e.getHandlerErrors().containsKey(MultifactorAuthenticationRequiredException.class.getSimpleName()));
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) DefaultMultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver) Test(org.junit.jupiter.api.Test)

Example 12 with DefaultMultifactorAuthenticationProviderResolver

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

the class PrincipalAttributeMultifactorAuthenticationTriggerTests method verifyOperationByProvider.

@Test
public void verifyOperationByProvider() {
    val props = new CasConfigurationProperties();
    val principal = props.getAuthn().getMfa().getTriggers().getPrincipal();
    principal.setGlobalPrincipalAttributeNameTriggers("email");
    principal.setGlobalPrincipalAttributeValueRegex(".+@example.*");
    val resolver = new DefaultMultifactorAuthenticationProviderResolver(MultifactorAuthenticationPrincipalResolver.identical());
    val trigger = new PrincipalAttributeMultifactorAuthenticationTrigger(props, resolver, applicationContext);
    val result = trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class));
    assertTrue(result.isPresent());
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Service(org.apereo.cas.authentication.principal.Service) DefaultMultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver) Test(org.junit.jupiter.api.Test)

Example 13 with DefaultMultifactorAuthenticationProviderResolver

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

the class RegisteredServicePrincipalAttributeMultifactorAuthenticationTriggerTests method verifyMismatchAttributesMustDeny.

@Test
public void verifyMismatchAttributesMustDeny() {
    val policy = mock(RegisteredServiceMultifactorPolicy.class);
    when(policy.getPrincipalAttributeNameTrigger()).thenReturn("bad-attribute");
    when(policy.getPrincipalAttributeValueToMatch()).thenReturn(".+@example.*");
    when(policy.getMultifactorAuthenticationProviders()).thenReturn(Set.of(TestMultifactorAuthenticationProvider.ID));
    when(this.registeredService.getMultifactorPolicy()).thenReturn(policy);
    val props = new CasConfigurationProperties();
    props.getAuthn().getMfa().getTriggers().getPrincipal().setDenyIfUnmatched(true);
    val trigger = new RegisteredServicePrincipalAttributeMultifactorAuthenticationTrigger(props, new DefaultMultifactorAuthenticationProviderResolver(MultifactorAuthenticationPrincipalResolver.identical()), applicationContext, mock(MultifactorAuthenticationProviderSelector.class));
    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) MultifactorAuthenticationProviderSelector(org.apereo.cas.authentication.MultifactorAuthenticationProviderSelector) DefaultMultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver) Test(org.junit.jupiter.api.Test)

Example 14 with DefaultMultifactorAuthenticationProviderResolver

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

the class RegisteredServicePrincipalAttributeMultifactorAuthenticationTriggerTests method verifyOperationByMultipleProviders.

@Test
public void verifyOperationByMultipleProviders() {
    val appCtx = new StaticApplicationContext();
    appCtx.refresh();
    val provider1 = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(appCtx);
    val provider2 = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(appCtx, new TestMultifactorAuthenticationProvider("mfa-example"));
    val policy = mock(RegisteredServiceMultifactorPolicy.class);
    when(policy.getPrincipalAttributeNameTrigger()).thenReturn("email");
    when(policy.getPrincipalAttributeValueToMatch()).thenReturn(".+@example.*");
    when(policy.getMultifactorAuthenticationProviders()).thenReturn(Set.of(provider1.getId(), provider2.getId()));
    when(registeredService.getMultifactorPolicy()).thenReturn(policy);
    val props = new CasConfigurationProperties();
    val selector = mock(MultifactorAuthenticationProviderSelector.class);
    when(selector.resolve(any(Collection.class), any(), any())).thenReturn(provider2);
    val trigger = new RegisteredServicePrincipalAttributeMultifactorAuthenticationTrigger(props, new DefaultMultifactorAuthenticationProviderResolver(MultifactorAuthenticationPrincipalResolver.identical()), appCtx, selector);
    val result = trigger.isActivated(authentication, registeredService, httpRequest, this.httpResponse, mock(Service.class));
    assertTrue(result.isPresent());
    assertEquals(provider2.getId(), result.get().getId());
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Collection(java.util.Collection) Service(org.apereo.cas.authentication.principal.Service) DefaultMultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)14 DefaultMultifactorAuthenticationProviderResolver (org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver)14 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)14 Test (org.junit.jupiter.api.Test)14 Service (org.apereo.cas.authentication.principal.Service)12 Order (org.junit.jupiter.api.Order)7 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)7 MultifactorAuthenticationProviderSelector (org.apereo.cas.authentication.MultifactorAuthenticationProviderSelector)4 MockWebServer (org.apereo.cas.util.MockWebServer)3 ByteArrayResource (org.springframework.core.io.ByteArrayResource)3 TestMultifactorAuthenticationProvider (org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider)2 Collection (java.util.Collection)1 Tag (org.junit.jupiter.api.Tag)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1