Search in sources :

Example 61 with EventFactorySupport

use of org.springframework.webflow.action.EventFactorySupport in project cas by apereo.

the class MultifactorAuthenticationFailureAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    val service = WebUtils.getRegisteredService(requestContext);
    val failureMode = provider.getFailureModeEvaluator().evaluate(service, provider);
    LOGGER.debug("Final failure mode has been determined to be [{}]", failureMode);
    if (failureMode == BaseMultifactorAuthenticationProviderProperties.MultifactorAuthenticationProviderFailureModes.OPEN) {
        return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_BYPASS);
    }
    return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_UNAVAILABLE);
}
Also used : lombok.val(lombok.val) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport)

Example 62 with EventFactorySupport

use of org.springframework.webflow.action.EventFactorySupport in project cas by apereo.

the class SwivelAuthenticationWebflowEventResolverTests method verifyResolver.

@Test
public void verifyResolver() {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val context = mock(RequestContext.class);
    when(context.getMessageContext()).thenReturn(mock(MessageContext.class));
    when(context.getFlowScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getRequestParameters()).thenReturn(new MockParameterMap());
    when(context.getConversationScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getRequestScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getFlashScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getExternalContext()).thenReturn(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val authn = RegisteredServiceTestUtils.getAuthentication("casuser");
    val builder = mock(AuthenticationResultBuilder.class);
    when(builder.getInitialAuthentication()).thenReturn(Optional.of(authn));
    when(builder.collect(any(Authentication.class))).thenReturn(builder);
    WebUtils.putAuthenticationResultBuilder(builder, context);
    WebUtils.putAuthentication(authn, context);
    WebUtils.putCredential(context, new SwivelTokenCredential("token"));
    val event = resolver.resolveSingle(context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, event.getId());
    val support = new EventFactorySupport();
    assertTrue(event.getAttributes().contains(support.getExceptionAttributeName()));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Authentication(org.apereo.cas.authentication.Authentication) SwivelTokenCredential(org.apereo.cas.adaptors.swivel.SwivelTokenCredential) MockParameterMap(org.springframework.webflow.test.MockParameterMap) MessageContext(org.springframework.binding.message.MessageContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 63 with EventFactorySupport

use of org.springframework.webflow.action.EventFactorySupport in project cas by apereo.

the class YubiKeyAuthenticationWebflowEventResolverTests method verifyOperationFails.

@Test
public void verifyOperationFails() {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val context = mock(RequestContext.class);
    when(context.getRequestScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getConversationScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getFlowScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getMessageContext()).thenReturn(mock(MessageContext.class));
    when(context.getRequestParameters()).thenReturn(new MockParameterMap());
    when(context.getExternalContext()).thenReturn(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    WebUtils.putCredential(context, RegisteredServiceTestUtils.getCredentialsWithDifferentUsernameAndPassword("casuser", "123456"));
    val event = yubikeyAuthenticationWebflowEventResolver.resolveSingle(context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, event.getId());
    assertEquals(HttpStatus.UNAUTHORIZED.value(), response.getStatus());
    val support = new EventFactorySupport();
    assertTrue(event.getAttributes().contains(support.getExceptionAttributeName()));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockParameterMap(org.springframework.webflow.test.MockParameterMap) MessageContext(org.springframework.binding.message.MessageContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 64 with EventFactorySupport

use of org.springframework.webflow.action.EventFactorySupport in project cas by apereo.

the class AccepttoMultifactorValidateUserDeviceRegistrationAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    val eventAttributes = new LocalAttributeMap<>();
    val acceptto = casProperties.getAuthn().getMfa().getAcceptto();
    val authentication = WebUtils.getInProgressAuthentication();
    val email = AccepttoApiUtils.getUserEmail(authentication, acceptto);
    try {
        if (verifyUserDeviceIsPaired()) {
            val credential = new AccepttoEmailCredential(email);
            WebUtils.putCredential(requestContext, credential);
            return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_FINALIZE);
        }
    } catch (final Exception e) {
        eventAttributes.put("error", e);
        LoggingUtils.error(LOGGER, e);
    }
    LOGGER.warn("Device linked to [{}] is not paired; authentication cannot proceed", email);
    return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_DENY, eventAttributes);
}
Also used : lombok.val(lombok.val) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) AccepttoEmailCredential(org.apereo.cas.mfa.accepto.AccepttoEmailCredential) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport)

Example 65 with EventFactorySupport

use of org.springframework.webflow.action.EventFactorySupport in project cas by apereo.

the class AccepttoQRCodeValidateWebSocketChannelAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    val request = WebUtils.getHttpServletRequestFromExternalWebflowContext(requestContext);
    val response = WebUtils.getHttpServletResponseFromExternalWebflowContext(requestContext);
    val webContext = new JEEContext(request, response);
    val channel = request.getParameter("channel");
    if (channel == null) {
        return returnError("Unable to locate websocket channel");
    }
    val acceptto = casProperties.getAuthn().getMfa().getAcceptto();
    val url = StringUtils.appendIfMissing(acceptto.getApiUrl(), "/") + "get_user_by_websocket_channel";
    LOGGER.trace("Contacting API [{}] to fetch email address", url);
    val parameters = CollectionUtils.<String, Object>wrap("uid", acceptto.getApplicationId(), "secret", acceptto.getSecret(), "websocket_channel", channel);
    HttpResponse apiResponse = null;
    try {
        val exec = HttpUtils.HttpExecutionRequest.builder().method(HttpMethod.POST).url(url).parameters(parameters).build();
        apiResponse = HttpUtils.execute(exec);
        if (apiResponse != null) {
            val status = apiResponse.getStatusLine().getStatusCode();
            LOGGER.debug("Response API status code is [{}]", status);
            if (status == HttpStatus.SC_OK) {
                val result = IOUtils.toString(apiResponse.getEntity().getContent(), StandardCharsets.UTF_8);
                val results = MAPPER.readValue(JsonValue.readHjson(result).toString(), Map.class);
                LOGGER.debug("Received API results for channel [{}] as [{}]", channel, results);
                if (results.isEmpty()) {
                    throw new AuthenticationException("No API results were returned for channel " + channel);
                }
                val success = BooleanUtils.toBoolean(results.get("success").toString());
                if (success) {
                    val email = results.get("user_email").toString();
                    LOGGER.trace("Storing channel [{}] in http session", channel);
                    AccepttoWebflowUtils.storeChannelInSessionStore(channel, webContext, sessionStore);
                    WebUtils.putCredential(requestContext, new AccepttoEmailCredential(email));
                    return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_FINALIZE);
                }
                val message = results.get("message").toString();
                LOGGER.error(message);
                return returnError(message);
            }
            if (status == HttpStatus.SC_FORBIDDEN) {
                return returnError("Invalid uid and secret combination; application not found");
            }
            if (status == HttpStatus.SC_UNAUTHORIZED) {
                return returnError("Email address provided is not a valid registered account");
            }
        }
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    } finally {
        HttpUtils.close(apiResponse);
    }
    return returnError("Unable to validate websocket channel");
}
Also used : lombok.val(lombok.val) AccepttoEmailCredential(org.apereo.cas.mfa.accepto.AccepttoEmailCredential) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) UnauthorizedAuthenticationException(org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException) JEEContext(org.pac4j.core.context.JEEContext) HttpResponse(org.apache.http.HttpResponse) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) UnauthorizedAuthenticationException(org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException)

Aggregations

EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)78 lombok.val (lombok.val)61 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)26 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)26 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)26 MockServletContext (org.springframework.mock.web.MockServletContext)25 MockRequestContext (org.springframework.webflow.test.MockRequestContext)23 Test (org.junit.jupiter.api.Test)21 Event (org.springframework.webflow.execution.Event)12 Test (org.junit.Test)7 LocalAttributeMap (org.springframework.webflow.core.collection.LocalAttributeMap)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HashMap (java.util.HashMap)4 Authentication (org.apereo.cas.authentication.Authentication)4 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 WebUtils (org.apereo.cas.web.support.WebUtils)3 LiteralExpression (org.springframework.binding.expression.support.LiteralExpression)3 MessageContext (org.springframework.binding.message.MessageContext)3 Transition (org.springframework.webflow.engine.Transition)3