OpenStackAuthenticationFilter extends GenericFilterBean { public final void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) throws IOException, ServletException { final boolean debug = logger.isDebugEnabled(); final HttpServletRequest request = (HttpServletRequest) req; final HttpServletResponse response = (HttpServletResponse) res; String headerToken = request.getHeader(OPENSTACK_HEADER_TOKEN); String pathInfo = request.getPathInfo(); logger.debug(headerToken); logger.debug(pathInfo); if (!validateAcceptHeader(request, response)) { return; } MDC.put("txId", ((HttpServletRequest) req).getSession().getId()); if (pathInfo != null && (pathInfo.equals("/") || pathInfo.equals("/extensions"))) { logger.debug("Operation does not need to Authenticate"); } else { if (headerToken == null) { headerToken = ""; } try { String token = headerToken; if ("".equals(token)) { String str = "Missing token header"; logger.info(str); throw new BadCredentialsException(str); } String tenantId = request.getHeader(OPENSTACK_HEADER_TENANTID); logger.debug(tenantId); logger.debug(token); if (debug) { logger.debug("OpenStack Authentication Authorization header " + "found for user '" + token + "' and tenant " + tenantId); } UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(token, tenantId); authRequest.setDetails(authenticationDetailsSource.buildDetails(request)); Authentication authResult = authenticationManager.authenticate(authRequest); if (debug) { logger.debug("Authentication success: " + authResult); } String uri = request.getRequestURI(); logger.debug("URI: " + uri); if (uri.contains("vdc") && !uri.contains(tenantId)) { String str = "Bad credentials for requested VDC"; logger.info(str); throw new AccessDeniedException(str); } UserDetails user = (UserDetails) authResult.getPrincipal(); logger.debug("User: " + user.getUsername()); logger.debug("Token: " + user.getPassword()); if (authResult.isAuthenticated()) { SecurityContextHolder.getContext().setAuthentication(authRequest); } rememberMeServices.loginSuccess(request, response, authResult); onSuccessfulAuthentication(request, response, authResult); } catch (AuthenticationException failed) { SecurityContextHolder.clearContext(); if (debug) { logger.debug("Authentication request for failed: " + failed); } rememberMeServices.loginFail(request, response); onUnsuccessfulAuthentication(request, response, failed); if (ignoreFailure) { chain.doFilter(request, response); } else { authenticationEntryPoint.commence(request, response, failed); } return; } catch (AccessDeniedException ex) { throw ex; } catch (Exception ex) { SecurityContextHolder.clearContext(); if (debug) { logger.debug("Authentication exception: " + ex); } rememberMeServices.loginFail(request, response); if (ignoreFailure) { chain.doFilter(request, response); } else { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); } return; } String keystoneURL = systemPropertiesProvider.getProperty(SystemPropertiesProvider.KEYSTONE_URL); response.addHeader("Www-Authenticate", "Keystone uri='" + keystoneURL + "'"); } chain.doFilter(request, response); } protected  OpenStackAuthenticationFilter();  OpenStackAuthenticationFilter(final AuthenticationManager pAuthenticationManager);  OpenStackAuthenticationFilter(final AuthenticationManager pAuthenticationManager,
            final AuthenticationEntryPoint pAuthenticationEntryPoint); final void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain); final AuthenticationDetailsSource<HttpServletRequest, ?> getAuthenticationDetailsSource(); final void setAuthenticationDetailsSource(
            final AuthenticationDetailsSource<HttpServletRequest, ?> pAuthenticationDetailsSource); final boolean isIgnoreFailure(); final void setIgnoreFailure(final boolean pIgnoreFailure); final AuthenticationManager getAuthenticationManager(); final void setAuthenticationManager(final AuthenticationManager pAuthenticationEntryPoint); final AuthenticationEntryPoint getAuthenticationEntryPoint(); final void setAuthenticationEntryPoint(final AuthenticationEntryPoint pAuthenticationEntryPoint); final RememberMeServices getRememberMeServices(); final void setRememberMeServices(final RememberMeServices pRememberMeServices); final SystemPropertiesProvider getSystemPropertiesProvider(); final void setSystemPropertiesProvider(final SystemPropertiesProvider pSystemPropertiesProvider); static final String HEADER_ACCEPT; static final String OPENSTACK_HEADER_TOKEN; static final String OPENSTACK_HEADER_TENANTID; }
@Test public void doFilterTestRootPath() throws IOException, ServletException { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); FilterChain filterChain = mock(FilterChain.class); HttpSession httpSession = mock(HttpSession.class); when(servletRequest.getHeader(anyString())).thenReturn("3df25213cac246f8bccad5c70cb3582e").thenReturn( "00000000000000000000000000000194"); when(servletRequest.getPathInfo()).thenReturn("/"); when(servletRequest.getRequestURI()).thenReturn("/vdc/00000000000000000000000000000194/"); when(servletRequest.getSession()).thenReturn(httpSession); when(httpSession.getId()).thenReturn("1234"); openStackAuthenticationFilter.doFilter(servletRequest, servletResponse, filterChain); }
@Test public void doFilterTestAnyPath() throws IOException, ServletException { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); FilterChain filterChain = mock(FilterChain.class); HttpSession httpSession = mock(HttpSession.class); Authentication authResult = mock(Authentication.class); PaasManagerUser paasUser = mock(PaasManagerUser.class); when(servletRequest.getHeader(OpenStackAuthenticationFilter.OPENSTACK_HEADER_TOKEN)) .thenReturn("3df25213cac246f8bccad5c70cb3582e").thenReturn("00000000000000000000000000000194") .thenReturn("1234"); when(servletRequest.getRequestURI()).thenReturn("/vdc/00000000000000000000000000000194/"); when(servletRequest.getPathInfo()).thenReturn("/path"); when(servletRequest.getSession()).thenReturn(httpSession); when(httpSession.getId()).thenReturn("1234"); when(authenticationManager.authenticate(any(Authentication.class))).thenReturn(authResult); when(authResult.getPrincipal()).thenReturn(paasUser); openStackAuthenticationFilter.doFilter(servletRequest, servletResponse, filterChain); }
@Test(expected = AccessDeniedException.class) public void doFilterOtherTenantAccess() throws IOException, ServletException { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); FilterChain filterChain = mock(FilterChain.class); HttpSession httpSession = mock(HttpSession.class); Authentication authResult = mock(Authentication.class); PaasManagerUser paasUser = mock(PaasManagerUser.class); when(servletRequest.getHeader(OpenStackAuthenticationFilter.OPENSTACK_HEADER_TOKEN)).thenReturn( "3df25213cac246f8bccad5c70cb3582e"); when(servletRequest.getHeader(OpenStackAuthenticationFilter.HEADER_ACCEPT)).thenReturn("application/json"); when(servletRequest.getHeader(OpenStackAuthenticationFilter.OPENSTACK_HEADER_TENANTID)).thenReturn( "00000000000000000000000000000194"); when(servletRequest.getRequestURI()).thenReturn("/vdc/00000000000000000000000000000001/"); when(servletRequest.getPathInfo()).thenReturn("/path"); when(servletRequest.getSession()).thenReturn(httpSession); when(httpSession.getId()).thenReturn("1234"); when(authenticationManager.authenticate(any(Authentication.class))).thenReturn(authResult); when(authResult.getPrincipal()).thenReturn(paasUser); openStackAuthenticationFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletRequest).getHeader(OpenStackAuthenticationFilter.OPENSTACK_HEADER_TOKEN); verify(servletRequest).getHeader(OpenStackAuthenticationFilter.HEADER_ACCEPT); verify(httpSession).getId(); }
@Test public void doFilterOtherTennantAccessNoVDCinReq() throws IOException, ServletException { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); FilterChain filterChain = mock(FilterChain.class); HttpSession httpSession = mock(HttpSession.class); Authentication authResult = mock(Authentication.class); PaasManagerUser paasUser = mock(PaasManagerUser.class); when(servletRequest.getHeader(anyString())).thenReturn("3df25213cac246f8bccad5c70cb3582e") .thenReturn("00000000000000000000000000000194").thenReturn("1234"); when(servletRequest.getRequestURI()).thenReturn("/catalog/application/"); when(servletRequest.getPathInfo()).thenReturn("/path"); when(servletRequest.getSession()).thenReturn(httpSession); when(httpSession.getId()).thenReturn("1234"); when(authenticationManager.authenticate(any(Authentication.class))).thenReturn(authResult); when(authResult.getPrincipal()).thenReturn(paasUser); openStackAuthenticationFilter.doFilter(servletRequest, servletResponse, filterChain); }
@Test public void shouldReturn406WhenAcceptHeaderIsTextPlain() throws IOException, ServletException { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); FilterChain filterChain = mock(FilterChain.class); when(servletRequest.getHeader(OpenStackAuthenticationFilter.HEADER_ACCEPT)).thenReturn("text/plain"); openStackAuthenticationFilter.doFilter(servletRequest, servletResponse, filterChain); verify(servletResponse).sendError(eq(HttpServletResponse.SC_NOT_ACCEPTABLE), anyString()); }