Search in sources :

Example 91 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project cas by apereo.

the class MultifactorAuthenticationProviderBypassAuditResourceResolverTests method verifyJsonOperation.

@Test
public void verifyJsonOperation() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val resolver = new MultifactorAuthenticationProviderBypassAuditResourceResolver();
    resolver.setAuditFormat(AuditTrailManager.AuditFormats.JSON);
    val jp = mock(JoinPoint.class);
    val args = new Object[] { MultifactorAuthenticationTestUtils.getAuthentication("casuser"), MultifactorAuthenticationTestUtils.getRegisteredService(), TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext) };
    when(jp.getArgs()).thenReturn(args);
    when(jp.getTarget()).thenReturn("TargetObject");
    val outcome = resolver.resolveFrom(jp, new Object());
    assertTrue(outcome.length > 0);
    assertNotNull(resolver.resolveFrom(jp, new RuntimeException("failed")));
    when(jp.getArgs()).thenReturn(null);
    assertEquals(0, resolver.resolveFrom(jp, new Object()).length);
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MultifactorAuthenticationProviderBypassAuditResourceResolver(org.apereo.cas.authentication.bypass.audit.MultifactorAuthenticationProviderBypassAuditResourceResolver) Test(org.junit.jupiter.api.Test)

Example 92 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project cas by apereo.

the class AuthenticationMultifactorAuthenticationProviderBypassEvaluatorTests method verifyOperation.

@Test
public void verifyOperation() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    val eval = new DefaultChainingMultifactorAuthenticationBypassProvider();
    val bypassProps = new MultifactorAuthenticationProviderBypassProperties();
    bypassProps.setAuthenticationAttributeName("cn");
    bypassProps.setAuthenticationAttributeValue("ex.+");
    eval.addMultifactorAuthenticationProviderBypassEvaluator(new AuthenticationMultifactorAuthenticationProviderBypassEvaluator(bypassProps, TestMultifactorAuthenticationProvider.ID));
    val authentication = CoreAuthenticationTestUtils.getAuthentication("casuser", Map.of("cn", List.of("example")));
    val registeredService = CoreAuthenticationTestUtils.getRegisteredService();
    val policy = new DefaultRegisteredServiceMultifactorPolicy();
    when(registeredService.getMultifactorPolicy()).thenReturn(policy);
    assertFalse(eval.shouldMultifactorAuthenticationProviderExecute(authentication, registeredService, provider, new MockHttpServletRequest()));
}
Also used : lombok.val(lombok.val) MultifactorAuthenticationProviderBypassProperties(org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties) DefaultRegisteredServiceMultifactorPolicy(org.apereo.cas.services.DefaultRegisteredServiceMultifactorPolicy) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 93 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project cas by apereo.

the class ScriptedRegisteredServiceMultifactorAuthenticationTriggerTests method verifyEmptyProviders.

@Test
public void verifyEmptyProviders() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val policy = mock(RegisteredServiceMultifactorPolicy.class);
    when(policy.getScript()).thenReturn("groovy { return '" + multifactorAuthenticationProvider.getId() + "' }");
    when(this.registeredService.getMultifactorPolicy()).thenReturn(policy);
    val props = new CasConfigurationProperties();
    val trigger = new ScriptedRegisteredServiceMultifactorAuthenticationTrigger(props, applicationContext);
    assertThrows(AuthenticationException.class, () -> trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class)));
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Test(org.junit.jupiter.api.Test)

Example 94 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project cas by apereo.

the class HttpRequestMultifactorAuthenticationProviderBypassEvaluatorTests method verifyShouldProceed.

@Test
public void verifyShouldProceed() {
    val properties = new MultifactorAuthenticationProviderBypassProperties();
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    val eval = new HttpRequestMultifactorAuthenticationProviderBypassEvaluator(properties, provider.getId());
    val principal = CoreAuthenticationTestUtils.getPrincipal(Map.of("cn", List.of("example")));
    val authentication = CoreAuthenticationTestUtils.getAuthentication(principal);
    val registeredService = CoreAuthenticationTestUtils.getRegisteredService();
    val request = new MockHttpServletRequest();
    assertTrue(eval.shouldMultifactorAuthenticationProviderExecute(authentication, registeredService, provider, request));
}
Also used : lombok.val(lombok.val) MultifactorAuthenticationProviderBypassProperties(org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 95 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext in project cas by apereo.

the class RegisteredServicePrincipalAttributeMultifactorAuthenticationProviderBypassEvaluatorTests method verifyOperation.

@Test
public void verifyOperation() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    val eval = new DefaultChainingMultifactorAuthenticationBypassProvider();
    eval.addMultifactorAuthenticationProviderBypassEvaluator(new RegisteredServicePrincipalAttributeMultifactorAuthenticationProviderBypassEvaluator(TestMultifactorAuthenticationProvider.ID));
    val principal = CoreAuthenticationTestUtils.getPrincipal(Map.of("cn", List.of("example")));
    val authentication = CoreAuthenticationTestUtils.getAuthentication(principal);
    val registeredService = CoreAuthenticationTestUtils.getRegisteredService();
    val policy = new DefaultRegisteredServiceMultifactorPolicy();
    policy.setBypassEnabled(true);
    policy.setBypassPrincipalAttributeName("cn");
    policy.setBypassPrincipalAttributeValue("^e[x]am.*");
    when(registeredService.getMultifactorPolicy()).thenReturn(policy);
    assertFalse(eval.shouldMultifactorAuthenticationProviderExecute(authentication, registeredService, provider, new MockHttpServletRequest()));
}
Also used : lombok.val(lombok.val) DefaultRegisteredServiceMultifactorPolicy(org.apereo.cas.services.DefaultRegisteredServiceMultifactorPolicy) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)322 Test (org.junit.jupiter.api.Test)218 lombok.val (lombok.val)159 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)59 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)39 BeforeEach (org.junit.jupiter.api.BeforeEach)36 Test (org.junit.Test)34 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)31 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)25 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)20 HashSet (java.util.HashSet)19 MockRequestContext (org.springframework.webflow.test.MockRequestContext)19 InMemoryServiceRegistry (org.apereo.cas.services.InMemoryServiceRegistry)16 MockServletContext (org.springframework.mock.web.MockServletContext)16 MultifactorAuthenticationProviderBypassProperties (org.apereo.cas.configuration.model.support.mfa.MultifactorAuthenticationProviderBypassProperties)15 BeanDefinitionReader (org.springframework.beans.factory.support.BeanDefinitionReader)13 ApplicationContext (org.springframework.context.ApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 QueueChannel (org.springframework.integration.channel.QueueChannel)11