VistaAuthenticationProvider implements AuthenticationProvider, MessageSourceAware, InitializingBean { public Authentication authenticate(Authentication authentication) throws AuthenticationException { Assert.isInstanceOf(VistaAuthenticationToken.class, authentication, messages.getMessage("VistaAuthenticationProvider.onlySupports", "Only VistaAuthenticationToken is supported")); VistaAuthenticationToken auth = (VistaAuthenticationToken) authentication; String username = auth.getName(); boolean cacheWasUsed = true; VistaUserDetails user = (VistaUserDetails) this.userCache.getUserFromCache(username); if (user == null) { cacheWasUsed = false; user = retrieveUser(auth); Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract"); } if (!user.isAccountNonLocked()) { throw new LockedException(messages.getMessage("VistaAuthenticationProvider.locked", "User account is locked")); } if (!user.isEnabled()) { throw new DisabledException(messages.getMessage("VistaAuthenticationProvider.disabled", "User is disabled")); } if (!user.isAccountNonExpired()) { throw new AccountExpiredException(messages.getMessage("VistaAuthenticationProvider.expired", "User account has expired")); } try { additionalAuthenticationChecks(user, auth); } catch (AuthenticationException exception) { if (cacheWasUsed) { cacheWasUsed = false; user = retrieveUser(auth); additionalAuthenticationChecks(user, auth); } else { throw exception; } } if (!user.isCredentialsNonExpired()) { throw new CredentialsExpiredException(messages.getMessage( "VistaAuthenticationProvider.credentialsExpired", "User credentials have expired")); } if (!cacheWasUsed) { this.userCache.putUserInCache(user); } return createSuccessAuthentication(user, auth); }  void afterPropertiesSet(); Authentication authenticate(Authentication authentication); void setMessageSource(MessageSource messageSource); void setUserCache(UserCache userCache); boolean supports(Class authentication); VistaUserDetailsService getUserDetailsService(); @Required void setUserDetailsService(VistaUserDetailsService userDetailsService); UserCache getUserCache();  }
@Test public void testReceivedBadCredentialsWhenCredentialsNotProvided() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, null, null, null, null, null)).thenThrow(new BadCredentialsException("missing credentials")); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, null, null, null); try { provider.authenticate(token); fail("Expected BadCredentialsException"); } catch (BadCredentialsException expected) { } verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, null, null, null, null, null); }
@Test public void testAuthenticateFailsIfAccountExpired() { user = createUser("54321.123456789", TEST_DIVISION, TEST_DUZ, null, false, true, true, true); when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); try { provider.authenticate(token); fail("Should have thrown AccountExpiredException"); } catch (AccountExpiredException expected) { assertTrue(true); } }
@Test public void testAuthenticateFailsIfAccountLocked() { user = createUser("54321.123456789", TEST_DIVISION, TEST_DUZ, null, true, false, true, true); when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); try { provider.authenticate(token); fail("Should have thrown LockedException"); } catch (LockedException expected) { assertTrue(true); } }
@Test public void testAuthenticateFailsIfCredentialsExpired() { user = createUser("54321.123456789", TEST_DIVISION, TEST_DUZ, null, true, true, false, true); when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); try { provider.authenticate(token); fail("Expected CredentialsExpiredException"); } catch (CredentialsExpiredException expected) { } verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); }
@Test public void testAuthenticateFailsIfUserDisabled() { user = createUser("54321.123456789", TEST_DIVISION, TEST_DUZ, null, true, true, true, false); when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); try { provider.authenticate(token); fail("Should have thrown DisabledException"); } catch (DisabledException expected) { assertTrue(true); } }
@Test public void testAuthenticateFailsWhenAuthenticationDaoHasBackendFailure() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenThrow(new DataRetrievalFailureException("This mock simulator is designed to fail")); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); try { provider.authenticate(token); fail("Should have thrown AuthenticationServiceException"); } catch (AuthenticationServiceException expected) { assertTrue(true); } verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); }
@Test public void testAuthenticates() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); Authentication result = provider.authenticate(token); if (!(result instanceof VistaAuthenticationToken)) { fail("Should have returned instance of VistaAuthenticationToken"); } assertNotSame(token, result); VistaAuthenticationToken castResult = (VistaAuthenticationToken) result; assertTrue(VistaUserDetails.class.isAssignableFrom(castResult.getPrincipal().getClass())); assertEquals(TEST_VISTA_ID, castResult.getVistaId()); assertEquals(TEST_ACCESS, castResult.getAccessCode()); assertEquals(TEST_VERIFY, castResult.getVerifyCode()); assertEquals(TEST_REMOTE_ADDRESS, castResult.getRemoteAddress()); assertEquals(TEST_REMOTE_HOSTNAME, castResult.getRemoteHostName()); assertEquals("ROLE_ONE", new ArrayList<GrantedAuthority>(castResult.getAuthorities()).get(0).getAuthority()); assertEquals("ROLE_TWO", new ArrayList<GrantedAuthority>(castResult.getAuthorities()).get(1).getAuthority()); assertEquals(TEST_REMOTE_HOSTNAME + "(" + TEST_REMOTE_ADDRESS + ")", castResult.getDetails()); verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); }
@Test public void testAuthenticatesASecondTime() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); Authentication result = provider.authenticate(token); if (!(result instanceof VistaAuthenticationToken)) { fail("Should have returned instance of VistaAuthenticationToken"); } Authentication result2 = provider.authenticate(result); if (!(result2 instanceof VistaAuthenticationToken)) { fail("Should have returned instance of VistaAuthenticationToken"); } assertNotSame(result, result2); assertEquals(result.getCredentials(), result2.getCredentials()); }
@Test public void testDetectsNullBeingReturnedFromAuthenticationDao() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(null); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); try { provider.authenticate(token); fail("Should have thrown AuthenticationServiceException"); } catch (AuthenticationServiceException expected) { assertEquals("VistaUserDetailsService returned null, which is an interface contract violation; it should either return an authenticated VistaUserDetails instance or throw an AuthenticationException", expected.getMessage()); } }
@Test public void testGoesBackToAuthenticationDaoToObtainLatestVerifyCodeIfCachedVerifyCodeSeemsIncorrect() { when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); provider.authenticate(token); assertEquals(TEST_REMOTE_HOSTNAME + "(" + TEST_REMOTE_ADDRESS + ")" + TEST_ACCESS + ";" + TEST_VERIFY, mockCache.getUserFromCache(TEST_DUZ + "@" + TEST_VISTA_ID + ";" + TEST_DIVISION).getPassword()); verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, TEST_VERIFY, null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); reset(mockUserDetailService); user = createUser(TEST_VISTA_ID, TEST_DIVISION, TEST_DUZ, TEST_REMOTE_HOSTNAME + "(" + TEST_REMOTE_ADDRESS + ")" + TEST_ACCESS + ";easternLongNeckTurtle", true, true, true, true, new SimpleGrantedAuthority("ROLE_ONE"), new SimpleGrantedAuthority("ROLE_TWO")); when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, "easternLongNeckTurtle", null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, "easternLongNeckTurtle", TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); provider.authenticate(token); assertEquals("www.example.org(192.168.0.1)10VEHU;easternLongNeckTurtle", mockCache.getUserFromCache(TEST_DUZ + "@" + TEST_VISTA_ID + ";" + TEST_DIVISION).getPassword()); verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_ACCESS, "easternLongNeckTurtle", null, null, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); }
@Test public void testAuthenticatesWithAppHandle() { user = createUser(TEST_VISTA_ID, TEST_DIVISION, TEST_DUZ, TEST_REMOTE_HOSTNAME + "(" + TEST_REMOTE_ADDRESS + ")" + TEST_APP_HANDLE, true, true, true, true, new SimpleGrantedAuthority("ROLE_ONE"), new SimpleGrantedAuthority("ROLE_TWO")); when(mockUserDetailService.login(TEST_VISTA_ID, TEST_DIVISION, TEST_APP_HANDLE, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME)).thenReturn(user); VistaAuthenticationToken token = new VistaAuthenticationToken(TEST_VISTA_ID, TEST_DIVISION, TEST_APP_HANDLE, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); Authentication result = provider.authenticate(token); if (!(result instanceof VistaAuthenticationToken)) { fail("Should have returned instance of VistaAuthenticationToken"); } assertNotSame(token, result); VistaAuthenticationToken castResult = (VistaAuthenticationToken) result; assertTrue(VistaUserDetails.class.isAssignableFrom(castResult.getPrincipal().getClass())); assertEquals(TEST_VISTA_ID, castResult.getVistaId()); assertNull(castResult.getAccessCode()); assertNull(castResult.getVerifyCode()); assertEquals(TEST_APP_HANDLE, castResult.getAppHandle()); assertEquals(TEST_REMOTE_ADDRESS, castResult.getRemoteAddress()); assertEquals(TEST_REMOTE_HOSTNAME, castResult.getRemoteHostName()); assertEquals("ROLE_ONE", new ArrayList<GrantedAuthority>(castResult.getAuthorities()).get(0).getAuthority()); assertEquals("ROLE_TWO", new ArrayList<GrantedAuthority>(castResult.getAuthorities()).get(1).getAuthority()); assertEquals(TEST_REMOTE_HOSTNAME + "(" + TEST_REMOTE_ADDRESS + ")", castResult.getDetails()); verify(mockUserDetailService).login(TEST_VISTA_ID, TEST_DIVISION, TEST_APP_HANDLE, TEST_REMOTE_ADDRESS, TEST_REMOTE_HOSTNAME); }