ExternalLoginAuthenticationManager implements AuthenticationManager, ApplicationEventPublisherAware, BeanNameAware { @Override public Authentication authenticate(Authentication request) throws AuthenticationException { logger.debug("Starting external authentication for:"+request); ExternalAuthenticationDetails authenticationData = getExternalAuthenticationDetails(request); UaaUser userFromRequest = getUser(request, authenticationData); if (userFromRequest == null) { return null; } UaaUser userFromDb; try { logger.debug(String.format("Searching for user by (username:%s , origin:%s)", userFromRequest.getUsername(), getOrigin())); userFromDb = userDatabase.retrieveUserByName(userFromRequest.getUsername(), getOrigin()); } catch (UsernameNotFoundException e) { logger.debug(String.format("Searching for user by (email:%s , origin:%s)", userFromRequest.getEmail(), getOrigin())); userFromDb = userDatabase.retrieveUserByEmail(userFromRequest.getEmail(), getOrigin()); } if (userFromDb == null) { if (!isAddNewShadowUser()) { throw new AccountNotPreCreatedException("The user account must be pre-created. Please contact your system administrator."); } publish(new NewUserAuthenticatedEvent(userFromRequest)); try { userFromDb = userDatabase.retrieveUserByName(userFromRequest.getUsername(), getOrigin()); } catch (UsernameNotFoundException ex) { throw new BadCredentialsException("Unable to register user in internal UAA store."); } } UaaUser user = userAuthenticated(request, userFromRequest, userFromDb); UaaAuthenticationDetails uaaAuthenticationDetails; if (request.getDetails() instanceof UaaAuthenticationDetails) { uaaAuthenticationDetails = (UaaAuthenticationDetails) request.getDetails(); } else { uaaAuthenticationDetails = UaaAuthenticationDetails.UNKNOWN; } UaaAuthentication success = new UaaAuthentication(new UaaPrincipal(user), user.getAuthorities(), uaaAuthenticationDetails); populateAuthenticationAttributes(success, request, authenticationData); publish(new IdentityProviderAuthenticationSuccessEvent(user, success, user.getOrigin(), IdentityZoneHolder.getCurrentZoneId())); return success; } ExternalLoginAuthenticationManager(IdentityProviderProvisioning providerProvisioning); IdentityProviderProvisioning getProviderProvisioning(); void setProviderProvisioning(IdentityProviderProvisioning providerProvisioning); ScimGroupExternalMembershipManager getExternalMembershipManager(); void setExternalMembershipManager(ScimGroupExternalMembershipManager externalMembershipManager); String getOrigin(); void setOrigin(String origin); @Override void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher); void setUserDatabase(UaaUserDatabase userDatabase); UaaUserDatabase getUserDatabase(); @Override Authentication authenticate(Authentication request); @Override void setBeanName(String name); static final String USER_ATTRIBUTE_PREFIX; }
@Test public void testAuthenticateNullPrincipal() { Authentication auth = mock(Authentication.class); when(auth.getPrincipal()).thenReturn(null); Authentication result = manager.authenticate(auth); assertNull(result); }
@Test public void testAuthenticateUnknownPrincipal() { Authentication auth = mock(Authentication.class); when(auth.getPrincipal()).thenReturn(userName); Authentication result = manager.authenticate(auth); assertNull(result); }
@Test(expected = BadCredentialsException.class) public void testAuthenticateUserInsertFails() { when(uaaUserDatabase.retrieveUserByName(anyString(),anyString())).thenThrow(new UsernameNotFoundException("")); manager.authenticate(inputAuth); }