UsernameSubstringPreAuthenticatedProcessingFilter extends J2eePreAuthenticatedProcessingFilter { @Override protected Object getPreAuthenticatedPrincipal( final HttpServletRequest httpRequest ) { String username = httpRequest.getUserPrincipal() == null ? null : httpRequest.getUserPrincipal().getName(); logger.debug( "original user principal: " + username ); httpRequest.setAttribute( PENTAHO_ORIG_USER_PRINCIPAL, username ); if ( username != null && pattern != null ) { Matcher m = pattern.matcher( username ); logger.debug( "pattern: " + pattern ); logger.debug( "input: " + username ); if ( m.find() ) { logger.debug( "pattern matches input; saving capture group" ); username = m.group( 1 ); } } if ( logger.isDebugEnabled() ) { logger.debug( "Windows PreAuthenticated J2EE principal: " + username ); } return username; }  void setRegex( final String regex ); static final String PENTAHO_ORIG_USER_PRINCIPAL; }
@Test public void testGetPreAuthenticatedPrincipal() { UsernameSubstringPreAuthenticatedProcessingFilter filter = new UsernameSubstringPreAuthenticatedProcessingFilter(); String expected = "testUser"; String user = "testUser"; HttpServletRequest req = null; filter.setRegex( null ); req = getRequest( user ); assertEquals( expected, filter.getPreAuthenticatedPrincipal( req ) ); assertEquals( user, req .getAttribute( UsernameSubstringPreAuthenticatedProcessingFilter.PENTAHO_ORIG_USER_PRINCIPAL ) ); filter.setRegex( " " ); req = getRequest( user ); assertEquals( expected, filter.getPreAuthenticatedPrincipal( req ) ); assertEquals( user, req .getAttribute( UsernameSubstringPreAuthenticatedProcessingFilter.PENTAHO_ORIG_USER_PRINCIPAL ) ); filter.setRegex( "" ); req = getRequest( user ); assertEquals( expected, filter.getPreAuthenticatedPrincipal( req ) ); assertEquals( user, req .getAttribute( UsernameSubstringPreAuthenticatedProcessingFilter.PENTAHO_ORIG_USER_PRINCIPAL ) ); user = "domain\\testUser"; filter.setRegex( ".+\\\\(.+)" ); req = getRequest( user ); assertEquals( expected, filter.getPreAuthenticatedPrincipal( req ) ); assertEquals( user, req .getAttribute( UsernameSubstringPreAuthenticatedProcessingFilter.PENTAHO_ORIG_USER_PRINCIPAL ) ); user = "testUser@domain.org"; filter.setRegex( "(.+)@.+" ); req = getRequest( user ); assertEquals( expected, filter.getPreAuthenticatedPrincipal( req ) ); assertEquals( user, req .getAttribute( UsernameSubstringPreAuthenticatedProcessingFilter.PENTAHO_ORIG_USER_PRINCIPAL ) ); filter.setRegex( "(.+)@.+" ); req = getRequest( null ); assertEquals( null, filter.getPreAuthenticatedPrincipal( req ) ); assertEquals( null, req .getAttribute( UsernameSubstringPreAuthenticatedProcessingFilter.PENTAHO_ORIG_USER_PRINCIPAL ) ); }