@Test public void shouldRedirectToSsoServerAnonymousIsNotAllowed() throws IOException, ServletException, URISyntaxException { setFieldValue(filter, "attemptToGetNewTokenIfNotExist", true); HttpServletRequest request = new MockHttpServletRequest("http: when(clientUrlExtractor.getClientUrl(eq(request))) .thenReturn("http: setFieldValue( filter, "tokenHandler", new RecoverableTokenHandler(requestWrapper, clientUrlExtractor, false)); filter.doFilter(request, response, chain); verify(response) .sendRedirect( eq( "/api/internal/sso/server/refresh?" + "redirect_url=http%3A%2F%2Flocalhost%3A8080%2Fws%2Fmyworkspace" + "&client_url=http%3A%2F%2Flocalhost%3A8080%2Fws%2Fmyworkspace" + "&allowAnonymous=false")); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpReq = (HttpServletRequest) request; final HttpServletResponse httpResp = (HttpServletResponse) response; if (requestFilter.shouldSkip(httpReq)) { chain.doFilter(request, response); return; } final String method = httpReq.getMethod(); final String clientUrl = clientUrlExtractor.getClientUrl(httpReq); if ("GET".equals(method) && httpReq.getParameter("login") != null) { UriBuilder redirectUrlParameter = UriBuilder.fromUri(httpReq.getRequestURL().toString()) .replaceQuery(httpReq.getQueryString()); redirectUrlParameter.replaceQueryParam("login"); String redirectUrl = UriBuilder.fromUri(loginPageUrl) .queryParam("redirect_url", encode(redirectUrlParameter.build().toString(), "UTF-8")) .queryParam("client_url", encode(clientUrl, "UTF-8")) .build() .toString(); LOG.debug("Redirect to login {} ", redirectUrl); httpResp.sendRedirect(redirectUrl); } else { String token = tokenExtractor.getToken(httpReq); HttpSession session; if (token != null) { session = sessionStore.getSession(token); if (session == null) { session = httpReq.getSession(); sessionStore.saveSession(token, session); } final SsoClientPrincipal principal = getPrincipal(session, token, clientUrl); if (principal == null) { tokenHandler.handleBadToken(httpReq, httpResp, chain, token); } else { tokenHandler.handleValidToken(httpReq, httpResp, chain, session, principal); } } else { if (httpReq.getParameter("cookiePresent") != null) { httpResp.sendRedirect(cookiesDisabledErrorPageUrl); } else { tokenHandler.handleMissingToken(httpReq, httpResp, chain); } } } }
LoginFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpReq = (HttpServletRequest) request; final HttpServletResponse httpResp = (HttpServletResponse) response; if (requestFilter.shouldSkip(httpReq)) { chain.doFilter(request, response); return; } final String method = httpReq.getMethod(); final String clientUrl = clientUrlExtractor.getClientUrl(httpReq); if ("GET".equals(method) && httpReq.getParameter("login") != null) { UriBuilder redirectUrlParameter = UriBuilder.fromUri(httpReq.getRequestURL().toString()) .replaceQuery(httpReq.getQueryString()); redirectUrlParameter.replaceQueryParam("login"); String redirectUrl = UriBuilder.fromUri(loginPageUrl) .queryParam("redirect_url", encode(redirectUrlParameter.build().toString(), "UTF-8")) .queryParam("client_url", encode(clientUrl, "UTF-8")) .build() .toString(); LOG.debug("Redirect to login {} ", redirectUrl); httpResp.sendRedirect(redirectUrl); } else { String token = tokenExtractor.getToken(httpReq); HttpSession session; if (token != null) { session = sessionStore.getSession(token); if (session == null) { session = httpReq.getSession(); sessionStore.saveSession(token, session); } final SsoClientPrincipal principal = getPrincipal(session, token, clientUrl); if (principal == null) { tokenHandler.handleBadToken(httpReq, httpResp, chain, token); } else { tokenHandler.handleValidToken(httpReq, httpResp, chain, session, principal); } } else { if (httpReq.getParameter("cookiePresent") != null) { httpResp.sendRedirect(cookiesDisabledErrorPageUrl); } else { tokenHandler.handleMissingToken(httpReq, httpResp, chain); } } } } }
LoginFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpReq = (HttpServletRequest) request; final HttpServletResponse httpResp = (HttpServletResponse) response; if (requestFilter.shouldSkip(httpReq)) { chain.doFilter(request, response); return; } final String method = httpReq.getMethod(); final String clientUrl = clientUrlExtractor.getClientUrl(httpReq); if ("GET".equals(method) && httpReq.getParameter("login") != null) { UriBuilder redirectUrlParameter = UriBuilder.fromUri(httpReq.getRequestURL().toString()) .replaceQuery(httpReq.getQueryString()); redirectUrlParameter.replaceQueryParam("login"); String redirectUrl = UriBuilder.fromUri(loginPageUrl) .queryParam("redirect_url", encode(redirectUrlParameter.build().toString(), "UTF-8")) .queryParam("client_url", encode(clientUrl, "UTF-8")) .build() .toString(); LOG.debug("Redirect to login {} ", redirectUrl); httpResp.sendRedirect(redirectUrl); } else { String token = tokenExtractor.getToken(httpReq); HttpSession session; if (token != null) { session = sessionStore.getSession(token); if (session == null) { session = httpReq.getSession(); sessionStore.saveSession(token, session); } final SsoClientPrincipal principal = getPrincipal(session, token, clientUrl); if (principal == null) { tokenHandler.handleBadToken(httpReq, httpResp, chain, token); } else { tokenHandler.handleValidToken(httpReq, httpResp, chain, session, principal); } } else { if (httpReq.getParameter("cookiePresent") != null) { httpResp.sendRedirect(cookiesDisabledErrorPageUrl); } else { tokenHandler.handleMissingToken(httpReq, httpResp, chain); } } } }  }
LoginFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpReq = (HttpServletRequest) request; final HttpServletResponse httpResp = (HttpServletResponse) response; if (requestFilter.shouldSkip(httpReq)) { chain.doFilter(request, response); return; } final String method = httpReq.getMethod(); final String clientUrl = clientUrlExtractor.getClientUrl(httpReq); if ("GET".equals(method) && httpReq.getParameter("login") != null) { UriBuilder redirectUrlParameter = UriBuilder.fromUri(httpReq.getRequestURL().toString()) .replaceQuery(httpReq.getQueryString()); redirectUrlParameter.replaceQueryParam("login"); String redirectUrl = UriBuilder.fromUri(loginPageUrl) .queryParam("redirect_url", encode(redirectUrlParameter.build().toString(), "UTF-8")) .queryParam("client_url", encode(clientUrl, "UTF-8")) .build() .toString(); LOG.debug("Redirect to login {} ", redirectUrl); httpResp.sendRedirect(redirectUrl); } else { String token = tokenExtractor.getToken(httpReq); HttpSession session; if (token != null) { session = sessionStore.getSession(token); if (session == null) { session = httpReq.getSession(); sessionStore.saveSession(token, session); } final SsoClientPrincipal principal = getPrincipal(session, token, clientUrl); if (principal == null) { tokenHandler.handleBadToken(httpReq, httpResp, chain, token); } else { tokenHandler.handleValidToken(httpReq, httpResp, chain, session, principal); } } else { if (httpReq.getParameter("cookiePresent") != null) { httpResp.sendRedirect(cookiesDisabledErrorPageUrl); } else { tokenHandler.handleMissingToken(httpReq, httpResp, chain); } } } }  @Override void init(FilterConfig config); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy(); }
LoginFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpReq = (HttpServletRequest) request; final HttpServletResponse httpResp = (HttpServletResponse) response; if (requestFilter.shouldSkip(httpReq)) { chain.doFilter(request, response); return; } final String method = httpReq.getMethod(); final String clientUrl = clientUrlExtractor.getClientUrl(httpReq); if ("GET".equals(method) && httpReq.getParameter("login") != null) { UriBuilder redirectUrlParameter = UriBuilder.fromUri(httpReq.getRequestURL().toString()) .replaceQuery(httpReq.getQueryString()); redirectUrlParameter.replaceQueryParam("login"); String redirectUrl = UriBuilder.fromUri(loginPageUrl) .queryParam("redirect_url", encode(redirectUrlParameter.build().toString(), "UTF-8")) .queryParam("client_url", encode(clientUrl, "UTF-8")) .build() .toString(); LOG.debug("Redirect to login {} ", redirectUrl); httpResp.sendRedirect(redirectUrl); } else { String token = tokenExtractor.getToken(httpReq); HttpSession session; if (token != null) { session = sessionStore.getSession(token); if (session == null) { session = httpReq.getSession(); sessionStore.saveSession(token, session); } final SsoClientPrincipal principal = getPrincipal(session, token, clientUrl); if (principal == null) { tokenHandler.handleBadToken(httpReq, httpResp, chain, token); } else { tokenHandler.handleValidToken(httpReq, httpResp, chain, session, principal); } } else { if (httpReq.getParameter("cookiePresent") != null) { httpResp.sendRedirect(cookiesDisabledErrorPageUrl); } else { tokenHandler.handleMissingToken(httpReq, httpResp, chain); } } } }  @Override void init(FilterConfig config); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }