Search in sources :

Example 81 with StaticApplicationContext

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

the class DefaultRequestedAuthenticationContextValidatorTests method buildApplicationContext.

private static ConfigurableApplicationContext buildApplicationContext() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    return applicationContext;
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext)

Example 82 with StaticApplicationContext

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

the class DefaultChainingMultifactorAuthenticationBypassProviderTests method verifyEmptyChainOperation.

@Test
public void verifyEmptyChainOperation() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val p = new DefaultChainingMultifactorAuthenticationBypassProvider();
    val res = p.filterMultifactorAuthenticationProviderBypassEvaluatorsBy("unknown");
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    val principal = MultifactorAuthenticationTestUtils.getPrincipal("casuser");
    val authentication = MultifactorAuthenticationTestUtils.getAuthentication(principal);
    val service = MultifactorAuthenticationTestUtils.getRegisteredService();
    assertTrue(res.shouldMultifactorAuthenticationProviderExecute(authentication, service, provider, new MockHttpServletRequest()));
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultChainingMultifactorAuthenticationBypassProvider(org.apereo.cas.authentication.bypass.DefaultChainingMultifactorAuthenticationBypassProvider) Test(org.junit.jupiter.api.Test)

Example 83 with StaticApplicationContext

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

the class DefaultChainingMultifactorAuthenticationBypassProviderTests method verifyOperation.

@Test
public void verifyOperation() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val request = new MockHttpServletRequest();
    request.addHeader("headerbypass", "true");
    val props = new MultifactorAuthenticationProviderBypassProperties();
    props.setHttpRequestHeaders("headerbypass");
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    val principal = MultifactorAuthenticationTestUtils.getPrincipal("casuser");
    val authentication = MultifactorAuthenticationTestUtils.getAuthentication(principal);
    val p = new DefaultChainingMultifactorAuthenticationBypassProvider();
    p.addMultifactorAuthenticationProviderBypassEvaluator(new HttpRequestMultifactorAuthenticationProviderBypassEvaluator(props, provider.getId()));
    assertFalse(p.isEmpty());
    assertNotNull(p.getId());
    assertNotNull(p.getProviderId());
    assertEquals(1, p.size());
    assertFalse(p.isMultifactorAuthenticationBypassed(authentication, provider.getId()));
    p.rememberBypass(authentication, provider);
    mockRememberBypass(provider, authentication);
    assertTrue(p.isMultifactorAuthenticationBypassed(authentication, provider.getId()));
    when(authentication.getAttributes()).thenReturn(new HashMap<>());
    p.forgetBypass(authentication);
    assertFalse(p.isMultifactorAuthenticationBypassed(authentication, provider.getId()));
    val service = MultifactorAuthenticationTestUtils.getRegisteredService();
    assertFalse(p.shouldMultifactorAuthenticationProviderExecute(authentication, service, provider, request));
    assertTrue(p.belongsToMultifactorAuthenticationProvider(provider.getId()).isPresent());
    assertFalse(p.filterMultifactorAuthenticationProviderBypassEvaluatorsBy(provider.getId()).isEmpty());
}
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) DefaultChainingMultifactorAuthenticationBypassProvider(org.apereo.cas.authentication.bypass.DefaultChainingMultifactorAuthenticationBypassProvider) HttpRequestMultifactorAuthenticationProviderBypassEvaluator(org.apereo.cas.authentication.bypass.HttpRequestMultifactorAuthenticationProviderBypassEvaluator) Test(org.junit.jupiter.api.Test)

Example 84 with StaticApplicationContext

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

the class BaseMultifactorAuthenticationTriggerTests method setup.

@BeforeEach
public void setup(final TestInfo info) {
    this.applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    if (!info.getTags().contains("DisableProviderRegistration")) {
        this.multifactorAuthenticationProvider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    }
    this.authentication = mock(Authentication.class);
    val principal = mock(Principal.class);
    when(principal.getId()).thenReturn("casuser");
    when(principal.getAttributes()).thenReturn(Map.of("email", List.of("casuser@example.org")));
    when(authentication.getAttributes()).thenReturn(Map.of("category", List.of("user-object"), "mfa-mode", List.of("mfa-other")));
    when(authentication.getPrincipal()).thenReturn(principal);
    this.registeredService = mock(RegisteredService.class);
    when(registeredService.getName()).thenReturn("Service");
    when(registeredService.getServiceId()).thenReturn("http://app.org");
    when(registeredService.getId()).thenReturn(1000L);
    this.geoLocationService = mock(GeoLocationService.class);
    this.httpRequest = new MockHttpServletRequest();
    this.httpRequest.setRemoteAddr("185.86.151.11");
    this.httpRequest.setLocalAddr("185.88.151.12");
    this.httpRequest.addHeader(HttpRequestUtils.USER_AGENT_HEADER, "Mozilla/5.0 (Windows NT 10.0; WOW64)");
    var clientInfo = new ClientInfo(this.httpRequest);
    ClientInfoHolder.setClientInfo(clientInfo);
    this.httpResponse = new MockHttpServletResponse();
}
Also used : lombok.val(lombok.val) RegisteredService(org.apereo.cas.services.RegisteredService) GeoLocationService(org.apereo.cas.authentication.adaptive.geo.GeoLocationService) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) Authentication(org.apereo.cas.authentication.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 85 with StaticApplicationContext

use of org.springframework.context.support.StaticApplicationContext 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)

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