@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); }
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); }
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); } }
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); }  }
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(); }
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();  }