@Test public void testDoFilterSuccess() throws Exception { SignedJWT token = getSignedToken("foobar"); AmbariEntryPoint entryPoint = createMock(AmbariEntryPoint.class); JwtAuthenticationProperties properties = createMock(JwtAuthenticationProperties.class); expect(properties.getAuthenticationProviderUrl()).andReturn("some url").once(); expect(properties.getPublicKey()).andReturn(publicKey).once(); expect(properties.getAudiences()).andReturn(Collections.singletonList("foobar")).once(); expect(properties.getCookieName()).andReturn("chocolate chip").once(); expect(properties.getOriginalUrlQueryParam()).andReturn("question").once(); Configuration configuration = createMock(Configuration.class); expect(configuration.getJwtProperties()).andReturn(properties).once(); User user = createMock(User.class); expect(user.getUserName()).andReturn("test-user").once(); expect(user.getUserType()).andReturn(UserType.JWT).once(); Users users = createMock(Users.class); expect(users.getUser("test-user", UserType.JWT)).andReturn(user).once(); expect(users.getUserAuthorities("test-user", UserType.JWT)).andReturn(null).once(); AuditLogger auditLogger = createMock(AuditLogger.class); expect(auditLogger.isEnabled()).andReturn(false).times(2); PermissionHelper permissionHelper = createMock(PermissionHelper.class); Cookie cookie = createMock(Cookie.class); expect(cookie.getName()).andReturn("chocolate chip").once(); expect(cookie.getValue()).andReturn(token.serialize()).once(); HttpServletRequest servletRequest = createMock(HttpServletRequest.class); expect(servletRequest.getCookies()).andReturn(new Cookie[]{cookie}).once(); HttpServletResponse servletResponse = createMock(HttpServletResponse.class); FilterChain filterChain = createMock(FilterChain.class); filterChain.doFilter(servletRequest, servletResponse); expectLastCall().once(); replayAll(); AmbariJWTAuthenticationFilter filter = new AmbariJWTAuthenticationFilter(entryPoint, configuration, users, auditLogger, permissionHelper); filter.doFilter(servletRequest, servletResponse, filterChain); verifyAll(); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; if (auditLogger.isEnabled() && shouldApply(httpServletRequest) && (AuthorizationHelper.getAuthenticatedName() == null)) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(httpServletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Authentication required") .withUserName(null) .build(); auditLogger.log(loginFailedAuditEvent); } super.doFilter(servletRequest, servletResponse, chain); }
AmbariJWTAuthenticationFilter extends JwtAuthenticationFilter implements AmbariAuthenticationFilter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; if (auditLogger.isEnabled() && shouldApply(httpServletRequest) && (AuthorizationHelper.getAuthenticatedName() == null)) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(httpServletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Authentication required") .withUserName(null) .build(); auditLogger.log(loginFailedAuditEvent); } super.doFilter(servletRequest, servletResponse, chain); } }
AmbariJWTAuthenticationFilter extends JwtAuthenticationFilter implements AmbariAuthenticationFilter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; if (auditLogger.isEnabled() && shouldApply(httpServletRequest) && (AuthorizationHelper.getAuthenticatedName() == null)) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(httpServletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Authentication required") .withUserName(null) .build(); auditLogger.log(loginFailedAuditEvent); } super.doFilter(servletRequest, servletResponse, chain); } AmbariJWTAuthenticationFilter(AuthenticationEntryPoint ambariEntryPoint,
                                       Configuration configuration,
                                       Users users,
                                       AuditLogger auditLogger,
                                       PermissionHelper permissionHelper); }
AmbariJWTAuthenticationFilter extends JwtAuthenticationFilter implements AmbariAuthenticationFilter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; if (auditLogger.isEnabled() && shouldApply(httpServletRequest) && (AuthorizationHelper.getAuthenticatedName() == null)) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(httpServletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Authentication required") .withUserName(null) .build(); auditLogger.log(loginFailedAuditEvent); } super.doFilter(servletRequest, servletResponse, chain); } AmbariJWTAuthenticationFilter(AuthenticationEntryPoint ambariEntryPoint,
                                       Configuration configuration,
                                       Users users,
                                       AuditLogger auditLogger,
                                       PermissionHelper permissionHelper); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain); }
AmbariJWTAuthenticationFilter extends JwtAuthenticationFilter implements AmbariAuthenticationFilter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; if (auditLogger.isEnabled() && shouldApply(httpServletRequest) && (AuthorizationHelper.getAuthenticatedName() == null)) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(httpServletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Authentication required") .withUserName(null) .build(); auditLogger.log(loginFailedAuditEvent); } super.doFilter(servletRequest, servletResponse, chain); } AmbariJWTAuthenticationFilter(AuthenticationEntryPoint ambariEntryPoint,
                                       Configuration configuration,
                                       Users users,
                                       AuditLogger auditLogger,
                                       PermissionHelper permissionHelper); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain);  }