Search in sources :

Example 11 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project midpoint by Evolveum.

the class MidPointAuthenticationSuccessHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
    String urlSuffix = AuthConstants.DEFAULT_PATH_AFTER_LOGIN;
    String authenticatedChannel = null;
    if (authentication instanceof MidpointAuthentication) {
        MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
        ModuleAuthenticationImpl moduleAuthentication = (ModuleAuthenticationImpl) mpAuthentication.getProcessingModuleAuthentication();
        moduleAuthentication.setState(AuthenticationModuleState.SUCCESSFULLY);
        if (mpAuthentication.getAuthenticationChannel() != null) {
            authenticatedChannel = mpAuthentication.getAuthenticationChannel().getChannelId();
            if (mpAuthentication.isAuthenticated()) {
                urlSuffix = mpAuthentication.getAuthenticationChannel().getPathAfterSuccessfulAuthentication();
                mpAuthentication.getAuthenticationChannel().postSuccessAuthenticationProcessing();
                if (mpAuthentication.getAuthenticationChannel().isPostAuthenticationEnabled()) {
                    getRedirectStrategy().sendRedirect(request, response, urlSuffix);
                    return;
                }
            } else {
                urlSuffix = mpAuthentication.getAuthenticationChannel().getPathDuringProccessing();
            }
        }
    }
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null && savedRequest.getRedirectUrl().contains(ModuleWebSecurityConfigurationImpl.DEFAULT_PREFIX_OF_MODULE_WITH_SLASH + "/")) {
        String target = savedRequest.getRedirectUrl().substring(0, savedRequest.getRedirectUrl().indexOf(ModuleWebSecurityConfigurationImpl.DEFAULT_PREFIX_OF_MODULE_WITH_SLASH + "/")) + urlSuffix;
        getRedirectStrategy().sendRedirect(request, response, target);
        return;
    }
    if (savedRequest != null && authenticatedChannel != null) {
        int startIndex = savedRequest.getRedirectUrl().indexOf(request.getContextPath()) + request.getContextPath().length();
        int endIndex = savedRequest.getRedirectUrl().length() - 1;
        String channelSavedRequest = null;
        if ((startIndex < endIndex)) {
            String localePath = savedRequest.getRedirectUrl().substring(startIndex, endIndex);
            channelSavedRequest = AuthSequenceUtil.searchChannelByPath(localePath);
        }
        if (channelSavedRequest == null) {
            channelSavedRequest = SecurityPolicyUtil.DEFAULT_CHANNEL;
        }
        if (!(channelSavedRequest.equals(authenticatedChannel))) {
            getRedirectStrategy().sendRedirect(request, response, urlSuffix);
            return;
        }
    } else {
        setDefaultTargetUrl(urlSuffix);
    }
    super.onAuthenticationSuccess(request, response, authentication);
}
Also used : ModuleAuthenticationImpl(com.evolveum.midpoint.authentication.impl.module.authentication.ModuleAuthenticationImpl) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest)

Example 12 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project midpoint by Evolveum.

the class MidpointAuthenticationFailureHandler method onAuthenticationFailure.

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String urlSuffix = AuthConstants.DEFAULT_PATH_AFTER_LOGIN;
    if (authentication instanceof MidpointAuthentication) {
        MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
        if (mpAuthentication.isAuthenticated()) {
            getRedirectStrategy().sendRedirect(request, response, urlSuffix);
            return;
        }
        ModuleAuthentication moduleAuthentication = mpAuthentication.getProcessingModuleAuthentication();
        if (mpAuthentication.getAuthenticationChannel() != null) {
            if (mpAuthentication.isLast(moduleAuthentication) && mpAuthentication.getAuthenticationChannel().isDefault()) {
                urlSuffix = getPathAfterUnsuccessfulAuthentication(mpAuthentication.getAuthenticationChannel());
            } else {
                urlSuffix = mpAuthentication.getAuthenticationChannel().getPathDuringProccessing();
            }
        }
        moduleAuthentication.setState(AuthenticationModuleState.FAILURE);
    }
    saveException(request, exception);
    SavedRequest savedRequest = getRequestCache().getRequest(request, response);
    if (savedRequest == null || StringUtils.isBlank(savedRequest.getRedirectUrl()) || ((DefaultSavedRequest) savedRequest).getServletPath().startsWith(ModuleWebSecurityConfiguration.DEFAULT_PREFIX_OF_MODULE_WITH_SLASH)) {
        getRedirectStrategy().sendRedirect(request, response, urlSuffix);
        return;
    }
    getRedirectStrategy().sendRedirect(request, response, savedRequest.getRedirectUrl());
}
Also used : ModuleAuthentication(com.evolveum.midpoint.authentication.api.config.ModuleAuthentication) ModuleAuthentication(com.evolveum.midpoint.authentication.api.config.ModuleAuthentication) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) Authentication(org.springframework.security.core.Authentication) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest)

Example 13 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.

the class ReAuthenticationWithRedirectToLoginFilterTest method shouldInvokeHandler.

@Test
void shouldInvokeHandler() throws IOException {
    final ReAuthenticationWithRedirectToLoginFilter filter = new ReAuthenticationWithRedirectToLoginFilter(null, null, null, null, null, null);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final String message = "foo";
    SavedRequest savedRequest = mock(SavedRequest.class);
    SessionUtils.saveRequest(request, savedRequest);
    HttpSession originalSession = request.getSession(true);
    filter.onAuthenticationFailure(request, response, message);
    assertThat(SessionUtils.getAuthenticationError(request)).isEqualTo("foo");
    assertThat(request.getSession(false)).isNotSameAs(originalSession);
    assertThat(SessionUtils.savedRequest(request)).isSameAs(savedRequest);
    assertThat(SessionUtils.hasAuthenticationToken(request)).isFalse();
    MockHttpServletResponseAssert.assertThat(response).redirectsTo("/go/auth/login");
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.jupiter.api.Test)

Example 14 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.

the class AuthenticationController method performLogin.

@RequestMapping(value = "/auth/security_check", method = RequestMethod.POST)
public RedirectView performLogin(@RequestParam("j_username") String username, @RequestParam("j_password") String password, HttpServletRequest request) {
    if (securityIsDisabledOrAlreadyLoggedIn(request)) {
        return new RedirectView("/pipelines", true);
    }
    LOGGER.debug("Requesting authentication for form auth.");
    try {
        SavedRequest savedRequest = SessionUtils.savedRequest(request);
        final AuthenticationToken<UsernamePassword> authenticationToken = passwordBasedPluginAuthenticationProvider.authenticate(new UsernamePassword(username, password), null);
        if (authenticationToken == null) {
            return badAuthentication(request, BAD_CREDENTIALS_MSG);
        } else {
            SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
        }
        String redirectUrl = savedRequest == null ? "/go/pipelines" : savedRequest.getRedirectUrl();
        return new RedirectView(redirectUrl, false);
    } catch (AuthenticationException e) {
        LOGGER.error("Failed to authenticate user: {} ", username, e);
        return badAuthentication(request, e.getMessage());
    } catch (Exception e) {
        return unknownAuthenticationError(request);
    }
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) RedirectView(org.springframework.web.servlet.view.RedirectView) AuthenticationException(org.springframework.security.core.AuthenticationException) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with SavedRequest

use of org.springframework.security.web.savedrequest.SavedRequest in project gocd by gocd.

the class AuthenticationController method authenticateWithWebBasedPlugin.

@RequestMapping(value = "/plugin/{pluginId}/authenticate")
public RedirectView authenticateWithWebBasedPlugin(@PathVariable("pluginId") String pluginId, HttpServletRequest request) {
    if (securityIsDisabledOrAlreadyLoggedIn(request)) {
        return new RedirectView("/pipelines", true);
    }
    LOGGER.debug("Requesting authentication for form auth.");
    SavedRequest savedRequest = SessionUtils.savedRequest(request);
    try {
        final AccessToken accessToken = webBasedPluginAuthenticationProvider.fetchAccessToken(pluginId, getRequestHeaders(request), getParameterMap(request));
        AuthenticationToken<AccessToken> authenticationToken = webBasedPluginAuthenticationProvider.authenticate(accessToken, pluginId);
        if (authenticationToken == null) {
            return unknownAuthenticationError(request);
        }
        SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
    } catch (AuthenticationException e) {
        LOGGER.error("Failed to authenticate user.", e);
        return badAuthentication(request, e.getMessage());
    } catch (Exception e) {
        return unknownAuthenticationError(request);
    }
    SessionUtils.removeAuthenticationError(request);
    String redirectUrl = savedRequest == null ? "/go/pipelines" : savedRequest.getRedirectUrl();
    return new RedirectView(redirectUrl, false);
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) AccessToken(com.thoughtworks.go.server.newsecurity.models.AccessToken) RedirectView(org.springframework.web.servlet.view.RedirectView) AuthenticationException(org.springframework.security.core.AuthenticationException) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)28 HttpSessionRequestCache (org.springframework.security.web.savedrequest.HttpSessionRequestCache)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 HttpSession (javax.servlet.http.HttpSession)4 Test (org.junit.jupiter.api.Test)4 Authentication (org.springframework.security.core.Authentication)4 Test (org.junit.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 DefaultSavedRequest (org.springframework.security.web.savedrequest.DefaultSavedRequest)3 RedirectView (org.springframework.web.servlet.view.RedirectView)3 MidpointAuthentication (com.evolveum.midpoint.authentication.api.config.MidpointAuthentication)2 MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)2 MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)2 RequestInfoForm (org.orcid.pojo.ajaxForm.RequestInfoForm)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 RequestCache (org.springframework.security.web.savedrequest.RequestCache)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ModuleAuthentication (com.evolveum.midpoint.authentication.api.config.ModuleAuthentication)1 ModuleAuthenticationImpl (com.evolveum.midpoint.authentication.impl.module.authentication.ModuleAuthenticationImpl)1