LogsearchFileAuthenticationProvider extends LogsearchAbstractAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!authPropsConfig.isAuthFileEnabled()) { logger.debug("File auth is disabled."); return authentication; } String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (StringUtils.isBlank(username)) { throw new BadCredentialsException("Username can't be null or empty."); } if (StringUtils.isBlank(password)) { throw new BadCredentialsException("Password can't be null or empty."); } password = StringEscapeUtils.unescapeHtml(password); username = StringEscapeUtils.unescapeHtml(username); UserDetails user = userDetailsService.loadUserByUsername(username); if (user == null) { logger.error("Username not found."); throw new BadCredentialsException("User not found."); } if (StringUtils.isEmpty(user.getPassword())) { logger.error("Password can't be null or empty."); throw new BadCredentialsException("Password can't be null or empty."); } String encPassword = CommonUtil.encryptPassword(username, password); if (!encPassword.equals(user.getPassword())) { logger.error("Wrong password for user=" + username); throw new BadCredentialsException("Wrong password."); } Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); authentication = new UsernamePasswordAuthenticationToken(username, encPassword, authorities); return authentication; }  @Override Authentication authenticate(Authentication authentication);  }
@Test public void testAuthenticationDisabled() { expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(false); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); assertSame(provider.authenticate(authentication), authentication); verify(mockAuthPropsConfig); }
@Test public void testAuthenticationEmptyUser() { expect(mockAuthPropsConfig.isAuthFileEnabled()).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.isAuthFileEnabled()).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 testAuthenticationEmptyPassword() { expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("principal", ""); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch(BadCredentialsException e) { assertEquals("Password can't be null or empty.", e.getMessage()); } verify(mockAuthPropsConfig); }
@Test public void testAuthenticationNullPassword() { expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); replay(mockAuthPropsConfig); Authentication authentication = new TestingAuthenticationToken("principal", null); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch(BadCredentialsException e) { assertEquals("Password can't be null or empty.", e.getMessage()); } verify(mockAuthPropsConfig); }
@Test public void testAuthenticationUnknownUser() { expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); expect(mockUserDetailsService.loadUserByUsername("principal")).andReturn(null); replay(mockAuthPropsConfig, mockUserDetailsService); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch (BadCredentialsException e) { assertEquals("User not found.", e.getMessage()); } verify(mockAuthPropsConfig, mockUserDetailsService); }
@Test public void testAuthenticationNoPassword() { List<GrantedAuthority> grantedAuths = Arrays.<GrantedAuthority>asList(new SimpleGrantedAuthority("ROLE_USER")); User user = new User("principal", null, grantedAuths); expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); expect(mockUserDetailsService.loadUserByUsername("principal")).andReturn(user); replay(mockAuthPropsConfig, mockUserDetailsService); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch (BadCredentialsException e) { assertEquals("Password can't be null or empty.", e.getMessage()); } verify(mockAuthPropsConfig, mockUserDetailsService); }
@Test public void testAuthenticationWrongPassword() { List<GrantedAuthority> grantedAuths = Arrays.<GrantedAuthority>asList(new SimpleGrantedAuthority("ROLE_USER")); User user = new User("principal", CommonUtil.encryptPassword("principal", "notCredentials"), grantedAuths); expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); expect(mockUserDetailsService.loadUserByUsername("principal")).andReturn(user); replay(mockAuthPropsConfig, mockUserDetailsService); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); try { provider.authenticate(authentication); assertTrue("Should have thrown BadCredentialsException", false); } catch (BadCredentialsException e) { assertEquals("Wrong password.", e.getMessage()); } verify(mockAuthPropsConfig, mockUserDetailsService); }
@Test public void testAuthenticationSuccessful() { List<GrantedAuthority> grantedAuths = Arrays.<GrantedAuthority>asList(new SimpleGrantedAuthority("ROLE_USER")); User user = new User("principal", CommonUtil.encryptPassword("principal", "credentials"), grantedAuths); expect(mockAuthPropsConfig.isAuthFileEnabled()).andReturn(true); expect(mockUserDetailsService.loadUserByUsername("principal")).andReturn(user); replay(mockAuthPropsConfig, mockUserDetailsService); Authentication authentication = new TestingAuthenticationToken("principal", "credentials"); Authentication authenticationResult = provider.authenticate(authentication); assertEquals("principal", authenticationResult.getName()); assertEquals(CommonUtil.encryptPassword("principal", "credentials"), authenticationResult.getCredentials()); assertEquals(1, authenticationResult.getAuthorities().size()); assertEquals(new SimpleGrantedAuthority("ROLE_USER"), authenticationResult.getAuthorities().iterator().next()); verify(mockAuthPropsConfig, mockUserDetailsService); }