Search in sources :

Example 1 with TestMultifactorAuthenticationProvider

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

the class UserAuthenticationResourceTests method verifyAuthWithMfaFails.

@Test
public void verifyAuthWithMfaFails() throws Exception {
    val builder = new DefaultAuthenticationResultBuilder().collect(CoreAuthenticationTestUtils.getAuthentication());
    when(authenticationSupport.handleInitialAuthenticationTransaction(any(), any())).thenReturn(builder);
    when(requestedContextValidator.validateAuthenticationContext(any(), any(), any(), any(), any())).thenReturn(AuthenticationContextValidationResult.builder().success(false).build());
    when(multifactorTriggerSelectionStrategy.resolve(any(), any(), any(), any(), any())).thenReturn(Optional.of(new TestMultifactorAuthenticationProvider("mfa-unknown")));
    this.mockMvc.perform(post(TICKETS_RESOURCE_URL).param("username", "casuser").param("password", "Mellon")).andExpect(status().is4xxClientError());
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) DefaultAuthenticationResultBuilder(org.apereo.cas.authentication.DefaultAuthenticationResultBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with TestMultifactorAuthenticationProvider

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

the class GlobalMultifactorAuthenticationTriggerTests method verifyOperationByValidProviders.

@Test
@Order(3)
public void verifyOperationByValidProviders() {
    val props = new CasConfigurationProperties();
    val otherProvider = new TestMultifactorAuthenticationProvider();
    otherProvider.setId("mfa-other");
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, otherProvider);
    props.getAuthn().getMfa().getTriggers().getGlobal().setGlobalProviderId(TestMultifactorAuthenticationProvider.ID + ',' + otherProvider.getId());
    val trigger = new GlobalMultifactorAuthenticationTrigger(props, applicationContext, (providers, service, principal) -> providers.iterator().next());
    val result = trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class));
    assertTrue(result.isPresent());
    assertEquals(TestMultifactorAuthenticationProvider.ID, result.get().getId());
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Service(org.apereo.cas.authentication.principal.Service) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 3 with TestMultifactorAuthenticationProvider

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

the class GroovyMultifactorAuthenticationProviderBypassEvaluatorTests method runGroovyBypassFor.

private static boolean runGroovyBypassFor(final Authentication authentication) {
    val request = new MockHttpServletRequest();
    val properties = new MultifactorAuthenticationProviderBypassProperties();
    properties.getGroovy().setLocation(new ClassPathResource("GroovyBypass.groovy"));
    val provider = new TestMultifactorAuthenticationProvider();
    val groovy = new GroovyMultifactorAuthenticationProviderBypassEvaluator(properties, provider.getId());
    val registeredService = mock(RegisteredService.class);
    when(registeredService.getName()).thenReturn("Service");
    when(registeredService.getServiceId()).thenReturn("http://app.org");
    when(registeredService.getId()).thenReturn(1000L);
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    return groovy.shouldMultifactorAuthenticationProviderExecute(authentication, registeredService, provider, request);
}
Also used : lombok.val(lombok.val) MultifactorAuthenticationProviderBypassProperties(org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) GroovyMultifactorAuthenticationProviderBypassEvaluator(org.apereo.cas.authentication.bypass.GroovyMultifactorAuthenticationProviderBypassEvaluator) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 4 with TestMultifactorAuthenticationProvider

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

the class RestMultifactorAuthenticationProviderBypassEvaluatorTests method verifyOperationFailsWithNoProvider.

@Test
public void verifyOperationFailsWithNoProvider() {
    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(), null, 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) 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 5 with TestMultifactorAuthenticationProvider

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

the class DefaultMultifactorAuthenticationProviderResolverTests method verifyResolutionByPrincipalAttribute.

@Test
public void verifyResolutionByPrincipalAttribute() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val provider = registerProviderInApplicationContext(applicationContext, context, new TestMultifactorAuthenticationProvider());
    val resolver = new DefaultMultifactorAuthenticationProviderResolver(MultifactorAuthenticationPrincipalResolver.identical());
    val principal = CoreAuthenticationTestUtils.getPrincipal("casuser", CollectionUtils.wrap("authlevel", List.of(provider.getId())));
    var results = resolver.resolveEventViaPrincipalAttribute(principal, List.of("authlevel"), CoreAuthenticationTestUtils.getRegisteredService(), Optional.of(context), List.of(provider), (input, mfaProvider) -> input.equalsIgnoreCase(provider.getId()));
    assertNotNull(results);
    assertEquals(provider.getId(), results.iterator().next().getId());
    results = resolver.resolveEventViaPrincipalAttribute(principal, List.of("authlevel"), CoreAuthenticationTestUtils.getRegisteredService(), Optional.of(context), List.of(), (input, mfaProvider) -> input.equalsIgnoreCase(provider.getId()));
    assertNull(results);
    results = resolver.resolveEventViaPrincipalAttribute(principal, List.of(), CoreAuthenticationTestUtils.getRegisteredService(), Optional.of(context), List.of(), (input, mfaProvider) -> input.equalsIgnoreCase(provider.getId()));
    assertNull(results);
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) AuthenticationAttributeMultifactorAuthenticationTrigger(org.apereo.cas.authentication.mfa.trigger.AuthenticationAttributeMultifactorAuthenticationTrigger) MockRequestContext(org.springframework.webflow.test.MockRequestContext) DefaultTransitionCriteria(org.springframework.webflow.engine.support.DefaultTransitionCriteria) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) LiteralExpression(org.springframework.binding.expression.support.LiteralExpression) MultifactorAuthenticationTestUtils(org.apereo.cas.authentication.mfa.MultifactorAuthenticationTestUtils) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) Tag(org.junit.jupiter.api.Tag) MockServletContext(org.springframework.mock.web.MockServletContext) PrincipalAttributeMultifactorAuthenticationTrigger(org.apereo.cas.authentication.mfa.trigger.PrincipalAttributeMultifactorAuthenticationTrigger) lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) UnsatisfiedAuthenticationContextTicketValidationException(org.apereo.cas.ticket.UnsatisfiedAuthenticationContextTicketValidationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Transition(org.springframework.webflow.engine.Transition) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) DefaultTargetStateResolver(org.springframework.webflow.engine.support.DefaultTargetStateResolver) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) 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) 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