@Test(expected = AuthenticationServiceException.class) public void authenticate_emptyUsername_shouldThrowBadCredentials() { Map<String, AuthenticationProvider> providers = getAuthenticationProviderMap(); BadCredentialsException badCredentialsException = new BadCredentialsException("Bad credentials"); expect(mockContextHolder.getAllBeansOfType(AuthenticationProvider.class)).andReturn(providers); expect(mockFirstProvider.authenticate(mockAuthentication)).andThrow(badCredentialsException); expect(mockSecondProvider.authenticate(mockAuthentication)).andReturn(null); expect(mockAuthentication.getName()).andReturn("").times(2); expect(mockUserDao.isUserPasswordExpired("")).andReturn(false); Capture<AuthenticationServiceException> authenticationServiceExceptionCapture = Capture.newInstance(); mockEventPublisher.publishAuthenticationFailure(capture(authenticationServiceExceptionCapture), eq(mockAuthentication)); expectLastCall().once(); replayAll(); unit.authenticate(mockAuthentication); verifyAll(); AuthenticationServiceException actualException = authenticationServiceExceptionCapture.getValue(); AuthenticationServiceException expected = new AuthenticationServiceException( ExceptionUtils.getRootCauseMessage(badCredentialsException), badCredentialsException); assertNotNull(actualException); assertEquals(expected, actualException); }
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Exception lastException = null; try { String principal = authentication.getName(); Map<String, AuthenticationProvider> providerMap = getSpringContextHolder().getAllBeansOfType(AuthenticationProvider.class); Authentication providerAuthentication = null; for (Map.Entry<String, AuthenticationProvider> providerEntry : providerMap.entrySet()) { try { if (providerEntry.getValue() instanceof AcmLdapAuthenticationProvider) { if (principal.isEmpty()) { throw new BadCredentialsException("Empty Username"); } AcmLdapAuthenticationProvider provider = (AcmLdapAuthenticationProvider) providerEntry.getValue(); String userDomain = provider.getLdapSyncService().getLdapSyncConfig().getUserDomain(); if (principal.endsWith(userDomain)) { providerAuthentication = provider.authenticate(authentication); } } else { providerAuthentication = providerEntry.getValue().authenticate(authentication); } if (providerAuthentication != null) { break; } } catch (Exception ae) { lastException = ae; } } if (providerAuthentication != null) { return getAcmAuthentication(providerAuthentication); } if (lastException != null) { AuthenticationException ae; if (lastException instanceof ProviderNotFoundException) { ae = new NoProviderFoundException("Authentication problem. Please contact your administrator."); } else if (lastException instanceof BadCredentialsException) { if (getUserDao().isUserPasswordExpired(authentication.getName())) { ae = new AuthenticationServiceException( "Your password has expired! An email with reset password link was sent to you.", lastException); } else { ae = new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } } else { ae = ExceptionUtils.getRootCauseMessage(lastException).contains("UnknownHostException") ? new AuthenticationServiceException( "There was an unknown error in connecting with the authentication services!", lastException) : new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } getAuthenticationEventPublisher().publishAuthenticationFailure(ae, authentication); log.debug("Detailed exception: ", lastException); lastException = ae; throw ae; } } catch (RuntimeException e) { if (e instanceof NoProviderFoundException) throw e; else if (lastException != null) throw new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException)); else { throw new InternalAuthenticationServiceException("Unknown server error", e); } } throw new NoProviderFoundException("Authentication problem. Please contact your administrator."); }
AcmAuthenticationManager implements AuthenticationManager { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Exception lastException = null; try { String principal = authentication.getName(); Map<String, AuthenticationProvider> providerMap = getSpringContextHolder().getAllBeansOfType(AuthenticationProvider.class); Authentication providerAuthentication = null; for (Map.Entry<String, AuthenticationProvider> providerEntry : providerMap.entrySet()) { try { if (providerEntry.getValue() instanceof AcmLdapAuthenticationProvider) { if (principal.isEmpty()) { throw new BadCredentialsException("Empty Username"); } AcmLdapAuthenticationProvider provider = (AcmLdapAuthenticationProvider) providerEntry.getValue(); String userDomain = provider.getLdapSyncService().getLdapSyncConfig().getUserDomain(); if (principal.endsWith(userDomain)) { providerAuthentication = provider.authenticate(authentication); } } else { providerAuthentication = providerEntry.getValue().authenticate(authentication); } if (providerAuthentication != null) { break; } } catch (Exception ae) { lastException = ae; } } if (providerAuthentication != null) { return getAcmAuthentication(providerAuthentication); } if (lastException != null) { AuthenticationException ae; if (lastException instanceof ProviderNotFoundException) { ae = new NoProviderFoundException("Authentication problem. Please contact your administrator."); } else if (lastException instanceof BadCredentialsException) { if (getUserDao().isUserPasswordExpired(authentication.getName())) { ae = new AuthenticationServiceException( "Your password has expired! An email with reset password link was sent to you.", lastException); } else { ae = new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } } else { ae = ExceptionUtils.getRootCauseMessage(lastException).contains("UnknownHostException") ? new AuthenticationServiceException( "There was an unknown error in connecting with the authentication services!", lastException) : new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } getAuthenticationEventPublisher().publishAuthenticationFailure(ae, authentication); log.debug("Detailed exception: ", lastException); lastException = ae; throw ae; } } catch (RuntimeException e) { if (e instanceof NoProviderFoundException) throw e; else if (lastException != null) throw new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException)); else { throw new InternalAuthenticationServiceException("Unknown server error", e); } } throw new NoProviderFoundException("Authentication problem. Please contact your administrator."); } }
AcmAuthenticationManager implements AuthenticationManager { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Exception lastException = null; try { String principal = authentication.getName(); Map<String, AuthenticationProvider> providerMap = getSpringContextHolder().getAllBeansOfType(AuthenticationProvider.class); Authentication providerAuthentication = null; for (Map.Entry<String, AuthenticationProvider> providerEntry : providerMap.entrySet()) { try { if (providerEntry.getValue() instanceof AcmLdapAuthenticationProvider) { if (principal.isEmpty()) { throw new BadCredentialsException("Empty Username"); } AcmLdapAuthenticationProvider provider = (AcmLdapAuthenticationProvider) providerEntry.getValue(); String userDomain = provider.getLdapSyncService().getLdapSyncConfig().getUserDomain(); if (principal.endsWith(userDomain)) { providerAuthentication = provider.authenticate(authentication); } } else { providerAuthentication = providerEntry.getValue().authenticate(authentication); } if (providerAuthentication != null) { break; } } catch (Exception ae) { lastException = ae; } } if (providerAuthentication != null) { return getAcmAuthentication(providerAuthentication); } if (lastException != null) { AuthenticationException ae; if (lastException instanceof ProviderNotFoundException) { ae = new NoProviderFoundException("Authentication problem. Please contact your administrator."); } else if (lastException instanceof BadCredentialsException) { if (getUserDao().isUserPasswordExpired(authentication.getName())) { ae = new AuthenticationServiceException( "Your password has expired! An email with reset password link was sent to you.", lastException); } else { ae = new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } } else { ae = ExceptionUtils.getRootCauseMessage(lastException).contains("UnknownHostException") ? new AuthenticationServiceException( "There was an unknown error in connecting with the authentication services!", lastException) : new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } getAuthenticationEventPublisher().publishAuthenticationFailure(ae, authentication); log.debug("Detailed exception: ", lastException); lastException = ae; throw ae; } } catch (RuntimeException e) { if (e instanceof NoProviderFoundException) throw e; else if (lastException != null) throw new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException)); else { throw new InternalAuthenticationServiceException("Unknown server error", e); } } throw new NoProviderFoundException("Authentication problem. Please contact your administrator."); }  }
AcmAuthenticationManager implements AuthenticationManager { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Exception lastException = null; try { String principal = authentication.getName(); Map<String, AuthenticationProvider> providerMap = getSpringContextHolder().getAllBeansOfType(AuthenticationProvider.class); Authentication providerAuthentication = null; for (Map.Entry<String, AuthenticationProvider> providerEntry : providerMap.entrySet()) { try { if (providerEntry.getValue() instanceof AcmLdapAuthenticationProvider) { if (principal.isEmpty()) { throw new BadCredentialsException("Empty Username"); } AcmLdapAuthenticationProvider provider = (AcmLdapAuthenticationProvider) providerEntry.getValue(); String userDomain = provider.getLdapSyncService().getLdapSyncConfig().getUserDomain(); if (principal.endsWith(userDomain)) { providerAuthentication = provider.authenticate(authentication); } } else { providerAuthentication = providerEntry.getValue().authenticate(authentication); } if (providerAuthentication != null) { break; } } catch (Exception ae) { lastException = ae; } } if (providerAuthentication != null) { return getAcmAuthentication(providerAuthentication); } if (lastException != null) { AuthenticationException ae; if (lastException instanceof ProviderNotFoundException) { ae = new NoProviderFoundException("Authentication problem. Please contact your administrator."); } else if (lastException instanceof BadCredentialsException) { if (getUserDao().isUserPasswordExpired(authentication.getName())) { ae = new AuthenticationServiceException( "Your password has expired! An email with reset password link was sent to you.", lastException); } else { ae = new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } } else { ae = ExceptionUtils.getRootCauseMessage(lastException).contains("UnknownHostException") ? new AuthenticationServiceException( "There was an unknown error in connecting with the authentication services!", lastException) : new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } getAuthenticationEventPublisher().publishAuthenticationFailure(ae, authentication); log.debug("Detailed exception: ", lastException); lastException = ae; throw ae; } } catch (RuntimeException e) { if (e instanceof NoProviderFoundException) throw e; else if (lastException != null) throw new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException)); else { throw new InternalAuthenticationServiceException("Unknown server error", e); } } throw new NoProviderFoundException("Authentication problem. Please contact your administrator."); }  @Override Authentication authenticate(Authentication authentication); AcmAuthentication getAcmAuthentication(Authentication providerAuthentication); SpringContextHolder getSpringContextHolder(); void setSpringContextHolder(SpringContextHolder springContextHolder); AcmGrantedAuthoritiesMapper getAuthoritiesMapper(); void setAuthoritiesMapper(AcmGrantedAuthoritiesMapper authoritiesMapper); DefaultAuthenticationEventPublisher getAuthenticationEventPublisher(); void setAuthenticationEventPublisher(DefaultAuthenticationEventPublisher authenticationEventPublisher); UserDao getUserDao(); void setUserDao(UserDao userDao); }
AcmAuthenticationManager implements AuthenticationManager { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Exception lastException = null; try { String principal = authentication.getName(); Map<String, AuthenticationProvider> providerMap = getSpringContextHolder().getAllBeansOfType(AuthenticationProvider.class); Authentication providerAuthentication = null; for (Map.Entry<String, AuthenticationProvider> providerEntry : providerMap.entrySet()) { try { if (providerEntry.getValue() instanceof AcmLdapAuthenticationProvider) { if (principal.isEmpty()) { throw new BadCredentialsException("Empty Username"); } AcmLdapAuthenticationProvider provider = (AcmLdapAuthenticationProvider) providerEntry.getValue(); String userDomain = provider.getLdapSyncService().getLdapSyncConfig().getUserDomain(); if (principal.endsWith(userDomain)) { providerAuthentication = provider.authenticate(authentication); } } else { providerAuthentication = providerEntry.getValue().authenticate(authentication); } if (providerAuthentication != null) { break; } } catch (Exception ae) { lastException = ae; } } if (providerAuthentication != null) { return getAcmAuthentication(providerAuthentication); } if (lastException != null) { AuthenticationException ae; if (lastException instanceof ProviderNotFoundException) { ae = new NoProviderFoundException("Authentication problem. Please contact your administrator."); } else if (lastException instanceof BadCredentialsException) { if (getUserDao().isUserPasswordExpired(authentication.getName())) { ae = new AuthenticationServiceException( "Your password has expired! An email with reset password link was sent to you.", lastException); } else { ae = new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } } else { ae = ExceptionUtils.getRootCauseMessage(lastException).contains("UnknownHostException") ? new AuthenticationServiceException( "There was an unknown error in connecting with the authentication services!", lastException) : new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException), lastException); } getAuthenticationEventPublisher().publishAuthenticationFailure(ae, authentication); log.debug("Detailed exception: ", lastException); lastException = ae; throw ae; } } catch (RuntimeException e) { if (e instanceof NoProviderFoundException) throw e; else if (lastException != null) throw new AuthenticationServiceException(ExceptionUtils.getRootCauseMessage(lastException)); else { throw new InternalAuthenticationServiceException("Unknown server error", e); } } throw new NoProviderFoundException("Authentication problem. Please contact your administrator."); }  @Override Authentication authenticate(Authentication authentication); AcmAuthentication getAcmAuthentication(Authentication providerAuthentication); SpringContextHolder getSpringContextHolder(); void setSpringContextHolder(SpringContextHolder springContextHolder); AcmGrantedAuthoritiesMapper getAuthoritiesMapper(); void setAuthoritiesMapper(AcmGrantedAuthoritiesMapper authoritiesMapper); DefaultAuthenticationEventPublisher getAuthenticationEventPublisher(); void setAuthenticationEventPublisher(DefaultAuthenticationEventPublisher authenticationEventPublisher); UserDao getUserDao(); void setUserDao(UserDao userDao);  }