@Test public void authenticate_firstOneWorks() { AcmUser user = new AcmUser(); user.setUserId("test-user"); Map<String, AuthenticationProvider> providers = getAuthenticationProviderMap(); Set<AcmGrantedAuthority> authsFromProvider = new HashSet<>(Arrays.asList( new AcmGrantedGroupAuthority("LDAP_INVESTIGATOR", 1L))); Set<AcmGrantedAuthority> authsFromMapper = new HashSet<>(Arrays.asList( new AcmGrantedAuthority("INVESTIGATOR"))); AcmAuthentication successAuthentication = new AcmAuthentication(authsFromProvider, null, null, true, user.getUserId(), user.getIdentifier()); Set<AcmGrantedAuthority> authsGroups = new HashSet<>(Arrays.asList( new AcmGrantedGroupAuthority("ADHOC_ADMINISTRATOR", 1L), new AcmGrantedGroupAuthority("LDAP_ADMINISTRATOR", 2L))); AcmGroup ldapGroup = new AcmGroup(); ldapGroup.setName("LDAP_ADMINISTRATOR"); AcmGroup adhocGroup = new AcmGroup(); adhocGroup.setName("ADHOC_ADMINISTRATOR"); List<AcmGroup> groups = new ArrayList<>(); groups.add(ldapGroup); groups.add(adhocGroup); expect(mockContextHolder.getAllBeansOfType(AuthenticationProvider.class)).andReturn(providers); expect(mockFirstProvider.authenticate(mockAuthentication)).andReturn(successAuthentication); expect(mockAuthoritiesMapper.mapAuthorities(authsFromProvider)).andReturn(authsFromMapper); expect(mockUserDao.findByUserId(user.getUserId())).andReturn(user); expect(mockAuthoritiesMapper.getAuthorityGroups(user)).andReturn(authsGroups); expect(mockAuthoritiesMapper.mapAuthorities(authsGroups)).andReturn(authsGroups); expect(mockAuthentication.getName()).andReturn(user.getUserId()); replayAll(); Authentication found = unit.authenticate(mockAuthentication); verifyAll(); assertEquals(authsFromMapper, found.getAuthorities()); }
@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);  }