Search in sources :

Example 6 with TestMultifactorAuthenticationProvider

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

the class DefaultMultifactorAuthenticationProviderResolverTests method verifyResolutionByAuthenticationAttribute.

@Test
public void verifyResolutionByAuthenticationAttribute() {
    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 authentication = CoreAuthenticationTestUtils.getAuthentication("casuser", CollectionUtils.wrap("authlevel", List.of(provider.getId())));
    val results = resolver.resolveEventViaAuthenticationAttribute(authentication, 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());
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) 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)

Example 7 with TestMultifactorAuthenticationProvider

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

the class DefaultMultifactorAuthenticationProviderResolverTests method registerProviderInApplicationContext.

private static TestMultifactorAuthenticationProvider registerProviderInApplicationContext(final ConfigurableApplicationContext applicationContext, final MockRequestContext context, final TestMultifactorAuthenticationProvider candidateProvider) {
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, candidateProvider);
    val targetResolver = new DefaultTargetStateResolver(provider.getId());
    val transition = new Transition(new DefaultTransitionCriteria(new LiteralExpression(provider.getId())), targetResolver);
    context.getRootFlow().getGlobalTransitionSet().add(transition);
    return (TestMultifactorAuthenticationProvider) provider;
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) DefaultTransitionCriteria(org.springframework.webflow.engine.support.DefaultTransitionCriteria) LiteralExpression(org.springframework.binding.expression.support.LiteralExpression) Transition(org.springframework.webflow.engine.Transition) DefaultTargetStateResolver(org.springframework.webflow.engine.support.DefaultTargetStateResolver)

Example 8 with TestMultifactorAuthenticationProvider

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

the class AuthenticationAttributeMultifactorAuthenticationTriggerTests method verifyMultipleProvider.

@Test
@Order(2)
public void verifyMultipleProvider() {
    val otherProvider = new TestMultifactorAuthenticationProvider();
    otherProvider.setId("mfa-other");
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, otherProvider);
    val props = new CasConfigurationProperties();
    val mfa = props.getAuthn().getMfa().getTriggers().getAuthentication();
    mfa.setGlobalAuthenticationAttributeNameTriggers("mfa-mode");
    mfa.setGlobalAuthenticationAttributeValueRegex(otherProvider.getId());
    val trigger = new AuthenticationAttributeMultifactorAuthenticationTrigger(props, new DefaultMultifactorAuthenticationProviderResolver(MultifactorAuthenticationPrincipalResolver.identical()), applicationContext);
    val result = trigger.isActivated(authentication, registeredService, this.httpRequest, this.httpResponse, mock(Service.class));
    assertTrue(result.isPresent());
}
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) DefaultMultifactorAuthenticationProviderResolver(org.apereo.cas.authentication.DefaultMultifactorAuthenticationProviderResolver) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 9 with TestMultifactorAuthenticationProvider

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

the class PrepareMultifactorProviderSelectionActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val flowSession = new MockFlowSession(new Flow(CasWebflowConfigurer.FLOW_ID_LOGIN));
    flowSession.setState(new ViewState(flowSession.getDefinitionInternal(), "viewState", mock(ViewFactory.class)));
    val exec = new MockFlowExecutionContext(flowSession);
    val context = new MockRequestContext(exec);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val chain = new DefaultChainingMultifactorAuthenticationProvider(new DefaultMultifactorAuthenticationFailureModeEvaluator(casProperties));
    val provider = new TestMultifactorAuthenticationProvider();
    provider.setBypassEvaluator(new DefaultChainingMultifactorAuthenticationBypassProvider());
    chain.addMultifactorAuthenticationProvider(provider);
    val attributes = new LocalAttributeMap(RegisteredService.class.getName(), RegisteredServiceTestUtils.getRegisteredService());
    attributes.put(MultifactorAuthenticationProvider.class.getName(), chain);
    val event = new EventFactorySupport().event(this, ChainingMultifactorAuthenticationProvider.DEFAULT_IDENTIFIER, attributes);
    context.setCurrentEvent(event);
    assertNull(action.execute(context));
    assertNotNull(WebUtils.getSelectableMultifactorAuthenticationProviders(context));
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) RegisteredService(org.apereo.cas.services.RegisteredService) MockFlowSession(org.springframework.webflow.test.MockFlowSession) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ViewState(org.springframework.webflow.engine.ViewState) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) DefaultChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.DefaultChainingMultifactorAuthenticationProvider) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) ChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.ChainingMultifactorAuthenticationProvider) MockServletContext(org.springframework.mock.web.MockServletContext) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Flow(org.springframework.webflow.engine.Flow) DefaultMultifactorAuthenticationFailureModeEvaluator(org.apereo.cas.authentication.DefaultMultifactorAuthenticationFailureModeEvaluator) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DefaultChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.DefaultChainingMultifactorAuthenticationProvider) DefaultChainingMultifactorAuthenticationBypassProvider(org.apereo.cas.authentication.bypass.DefaultChainingMultifactorAuthenticationBypassProvider) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 10 with TestMultifactorAuthenticationProvider

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

the class DuoSecurityUniversalPromptPrepareLoginActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val identifier = casProperties.getAuthn().getMfa().getDuo().get(0).getId();
    val provider = TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, new TestMultifactorAuthenticationProvider(identifier));
    configurableApplicationContext.getBeansOfType(MultifactorAuthenticationPrincipalResolver.class).forEach((key, value) -> ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, value, key));
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication(), context);
    WebUtils.putRegisteredService(context, RegisteredServiceTestUtils.getRegisteredService());
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, provider);
    val result = duoUniversalPromptPrepareLoginAction.execute(context);
    assertNotNull(result);
    assertNotNull(result.getAttributes().get("result"));
    assertTrue(context.getFlowScope().contains("duoUniversalPromptLoginUrl"));
}
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) MultifactorAuthenticationPrincipalResolver(org.apereo.cas.authentication.MultifactorAuthenticationPrincipalResolver) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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