LogsearchSimpleAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthSimpleEnabled()) { logger.debug("Simple auth is disabled"); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); username = StringEscapeUtils.unescapeHtml(username); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } User user = new User(); user.setUsername(username); authentication = new UsernamePasswordAuthenticationToken(username, password, getAuthorities()); return authentication; }  @Override Authentication authenticate(Authentication authentication);  }
@Test public void testAuthenticationDisabled() { expect(mockAuthPropsConfig.isAuthSimpleEnabled()).andReturn(false); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); assertSame(provider.authenticate(authentication), authentication); verify(mockAuthPropsConfig); }
@Test public void testAuthenticationEmptyUser() { expect(mockAuthPropsConfig.isAuthSimpleEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("", "credentials"); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch(BadCredentialsException e) { assertEquals("Username can't be null or empty.", e.getMessage()); } verify(mockAuthPropsConfig); }
@Test public void testAuthenticationNullUser() { expect(mockAuthPropsConfig.isAuthSimpleEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken(null, "credentials"); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch(BadCredentialsException e) { assertEquals("Username can't be null or empty.", e.getMessage()); } verify(mockAuthPropsConfig); }
@Test public void testAuthenticationSuccessful() { expect(mockAuthPropsConfig.isAuthSimpleEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); Authentication authenticationResult = provider.authenticate(authentication); assertEquals("principal", authenticationResult.getName()); assertEquals("credentials", authenticationResult.getCredentials()); assertEquals(1, authenticationResult.getAuthorities().size()); assertEquals(new SimpleGrantedAuthority("ROLE_USER"), authenticationResult.getAuthorities().iterator().next()); verify(mockAuthPropsConfig); }