Search in sources :

Example 16 with TestMultifactorAuthenticationProvider

use of org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider in project cas by apereo.

the class DefaultMultifactorAuthenticationTriggerSelectionStrategyTests method getMultifactorAuthenticationTrigger.

private static MultifactorAuthenticationTrigger getMultifactorAuthenticationTrigger() {
    val trigger = mock(MultifactorAuthenticationTrigger.class);
    when(trigger.supports(any(), any(), any(), any())).thenReturn(true);
    when(trigger.isActivated(any(), any(), any(), any(), any())).thenReturn(Optional.of(new TestMultifactorAuthenticationProvider()));
    return trigger;
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider)

Example 17 with TestMultifactorAuthenticationProvider

use of org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider in project cas by apereo.

the class RestMultifactorAuthenticationProviderBypassEvaluatorTests method verifyOperationShouldProceed.

@Test
public void verifyOperationShouldProceed() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    try (val webServer = new MockWebServer(9316, new ByteArrayResource("Y".getBytes(StandardCharsets.UTF_8), "REST Output"), HttpStatus.ACCEPTED)) {
        webServer.start();
        val props = new MultifactorAuthenticationProviderBypassProperties();
        props.getRest().setUrl("http://localhost:9316");
        val provider = new TestMultifactorAuthenticationProvider();
        val r = new RestMultifactorAuthenticationProviderBypassEvaluator(props, provider.getId());
        val res = r.shouldMultifactorAuthenticationProviderExecute(MultifactorAuthenticationTestUtils.getAuthentication("casuser"), MultifactorAuthenticationTestUtils.getRegisteredService(), provider, new MockHttpServletRequest());
        assertTrue(res);
    }
}
Also used : lombok.val(lombok.val) MultifactorAuthenticationProviderBypassProperties(org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) RestMultifactorAuthenticationProviderBypassEvaluator(org.apereo.cas.authentication.bypass.RestMultifactorAuthenticationProviderBypassEvaluator) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 18 with TestMultifactorAuthenticationProvider

use of org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider in project cas by apereo.

the class DefaultMultifactorAuthenticationFailureModeEvaluatorTests method executeEvaluation.

protected void executeEvaluation(final BaseMultifactorAuthenticationProviderProperties.MultifactorAuthenticationProviderFailureModes providerMode, final BaseMultifactorAuthenticationProviderProperties.MultifactorAuthenticationProviderFailureModes serviceMode, final BaseMultifactorAuthenticationProviderProperties.MultifactorAuthenticationProviderFailureModes expected) {
    val eval = new DefaultMultifactorAuthenticationFailureModeEvaluator(casProperties);
    val provider = new TestMultifactorAuthenticationProvider();
    provider.setFailureMode(providerMode);
    val service = CoreAuthenticationTestUtils.getRegisteredService();
    val policy = new DefaultRegisteredServiceMultifactorPolicy();
    policy.setFailureMode(serviceMode);
    when(service.getMultifactorPolicy()).thenReturn(policy);
    val result = eval.evaluate(service, provider);
    assertEquals(expected, result);
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) DefaultRegisteredServiceMultifactorPolicy(org.apereo.cas.services.DefaultRegisteredServiceMultifactorPolicy)

Example 19 with TestMultifactorAuthenticationProvider

use of org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider in project cas by apereo.

the class DefaultMultifactorAuthenticationProviderResolverTests method assertProviderResolutionFromManyProviders.

private static void assertProviderResolutionFromManyProviders(final MultifactorAuthenticationTrigger trigger, final ConfigurableApplicationContext applicationContext, final boolean assertPresence) {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val provider1 = new TestMultifactorAuthenticationProvider();
    provider1.setOrder(10);
    registerProviderInApplicationContext(applicationContext, context, provider1);
    val provider2 = new TestMultifactorAuthenticationProvider("mfa-other");
    provider2.setOrder(1);
    registerProviderInApplicationContext(applicationContext, context, provider2);
    val principal = CoreAuthenticationTestUtils.getPrincipal("casuser", CollectionUtils.wrap("mfa-principal", List.of(provider2.getId())));
    val result = trigger.isActivated(CoreAuthenticationTestUtils.getAuthentication(principal, CollectionUtils.wrap("mfa-authn", List.of(provider2.getId()))), CoreAuthenticationTestUtils.getRegisteredService(), request, new MockHttpServletResponse(), CoreAuthenticationTestUtils.getService());
    if (assertPresence) {
        assertTrue(result.isPresent());
        assertEquals(provider2.getId(), result.get().getId());
    } else {
        assertTrue(result.isEmpty());
    }
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 20 with TestMultifactorAuthenticationProvider

use of org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider 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)20 TestMultifactorAuthenticationProvider (org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider)20 Test (org.junit.jupiter.api.Test)15 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 MockServletContext (org.springframework.mock.web.MockServletContext)6 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)6 MockRequestContext (org.springframework.webflow.test.MockRequestContext)6 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)5 Service (org.apereo.cas.authentication.principal.Service)3 MultifactorAuthenticationProviderBypassProperties (org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties)3 DefaultAuthenticationResultBuilder (org.apereo.cas.authentication.DefaultAuthenticationResultBuilder)2 DefaultMultifactorAuthenticationProviderResolver (org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver)2 MultifactorAuthenticationPrincipalResolver (org.apereo.cas.authentication.MultifactorAuthenticationPrincipalResolver)2 RestMultifactorAuthenticationProviderBypassEvaluator (org.apereo.cas.authentication.bypass.RestMultifactorAuthenticationProviderBypassEvaluator)2 MockWebServer (org.apereo.cas.util.MockWebServer)2 Order (org.junit.jupiter.api.Order)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 LiteralExpression (org.springframework.binding.expression.support.LiteralExpression)2