KnoxSSOAuthenticationFilter implements Filter { protected Authentication getAuthentication(String userName, HttpServletRequest httpRequest) { String ldapName = LdapNameBuilder.newInstance().add(userSearchBase).add("uid", userName).build().toString(); List<GrantedAuthority> grantedAuths = ldapTemplate.search(query() .where("objectclass") .is("groupOfNames") .and("member") .is(ldapName), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get()) .stream() .map(group -> String.format("%s%s", SECURITY_ROLE_PREFIX, group.toUpperCase())) .map(SimpleGrantedAuthority::new).collect(Collectors.toList()); final UserDetails principal = new User(userName, "", grantedAuths); final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( principal, "", grantedAuths); WebAuthenticationDetails webDetails = new WebAuthenticationDetails(httpRequest); authentication.setDetails(webDetails); return authentication; } KnoxSSOAuthenticationFilter(String userSearchBase,
                                     Path knoxKeyFile,
                                     String knoxKeyString,
                                     String knoxCookie,
                                     LdapTemplate ldapTemplate); @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }
@SuppressWarnings("unchecked") @Test public void getAuthenticationShouldProperlyPopulateAuthentication() { LdapTemplate ldapTemplate = mock(LdapTemplate.class); KnoxSSOAuthenticationFilter knoxSSOAuthenticationFilter = spy(new KnoxSSOAuthenticationFilter("ou=people,dc=hadoop,dc=apache,dc=org", mock(Path.class), "knoxKeyString", "knoxCookie", ldapTemplate )); HttpServletRequest request = mock(HttpServletRequest.class); when(ldapTemplate.search(any(LdapQuery.class), any(AttributesMapper.class))).thenReturn(Arrays.asList("USER", "ADMIN")); Authentication authentication = knoxSSOAuthenticationFilter.getAuthentication("userName", request); Object[] grantedAuthorities = authentication.getAuthorities().toArray(); assertEquals("ROLE_USER", grantedAuthorities[0].toString()); assertEquals("ROLE_ADMIN", grantedAuthorities[1].toString()); assertEquals("userName", authentication.getName()); }