@Test(expected = InvalidCookieException.class) public void testProcessAutoLoginCookieWrongNumberFormat() { final PersistentLoginRepository loginRepo = context .mock(PersistentLoginRepository.class); final UserDetailsService userDetailsService = context .mock(UserDetailsService.class); final HttpServletRequest request = context .mock(HttpServletRequest.class); final HttpServletResponse response = context .mock(HttpServletResponse.class); final String[] cookieTokens = new String[] { "blah", "foo", "whatever" }; PersistentLoginService svc = new PersistentLoginService("key", userDetailsService, loginRepo); svc.processAutoLoginCookie(cookieTokens, request, response); context.assertIsSatisfied(); }
public UserDetails processAutoLoginCookie(final String[] cookieTokens, final HttpServletRequest request, final HttpServletResponse response) { if (cookieTokens.length != 3) { throw new InvalidCookieException("Cookie token did not contain " + 2 + " tokens, but contained '" + Arrays.asList(cookieTokens) + "'"); } long tokenExpiryTime; try { tokenExpiryTime = new Long(cookieTokens[1]).longValue(); } catch (NumberFormatException nfe) { throw new InvalidCookieException( "Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1] + "')"); } if (isTokenExpired(tokenExpiryTime)) { throw new InvalidCookieException( "Cookie token[1] has expired (expired on '" + new Date(tokenExpiryTime) + "'; current time is '" + new Date() + "')"); } ExtendedUserDetails userDetails = (ExtendedUserDetails) (getUserDetailsService().loadUserByUsername(cookieTokens[0])); PersistentLogin login = userDetails.getPersistentLogin(); if (login == null) { throw new InvalidCookieException( "No PersistentLogin record in repository"); } String expectedTokenSignature = login.getTokenValue(); long expectedExpiryDate = login.getTokenExpirationDate(); if (tokenExpiryTime != expectedExpiryDate) { throw new InvalidCookieException( "Cookie token[1] contained expirationDate '" + cookieTokens[2] + "' but expected '" + expectedExpiryDate + "'"); } if (!expectedTokenSignature.equals(cookieTokens[2])) { throw new InvalidCookieException( "Cookie token[2] contained signature '" + cookieTokens[2] + "' but expected '" + expectedTokenSignature + "'"); } return userDetails; }
PersistentLoginService extends TokenBasedRememberMeServices { public UserDetails processAutoLoginCookie(final String[] cookieTokens, final HttpServletRequest request, final HttpServletResponse response) { if (cookieTokens.length != 3) { throw new InvalidCookieException("Cookie token did not contain " + 2 + " tokens, but contained '" + Arrays.asList(cookieTokens) + "'"); } long tokenExpiryTime; try { tokenExpiryTime = new Long(cookieTokens[1]).longValue(); } catch (NumberFormatException nfe) { throw new InvalidCookieException( "Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1] + "')"); } if (isTokenExpired(tokenExpiryTime)) { throw new InvalidCookieException( "Cookie token[1] has expired (expired on '" + new Date(tokenExpiryTime) + "'; current time is '" + new Date() + "')"); } ExtendedUserDetails userDetails = (ExtendedUserDetails) (getUserDetailsService().loadUserByUsername(cookieTokens[0])); PersistentLogin login = userDetails.getPersistentLogin(); if (login == null) { throw new InvalidCookieException( "No PersistentLogin record in repository"); } String expectedTokenSignature = login.getTokenValue(); long expectedExpiryDate = login.getTokenExpirationDate(); if (tokenExpiryTime != expectedExpiryDate) { throw new InvalidCookieException( "Cookie token[1] contained expirationDate '" + cookieTokens[2] + "' but expected '" + expectedExpiryDate + "'"); } if (!expectedTokenSignature.equals(cookieTokens[2])) { throw new InvalidCookieException( "Cookie token[2] contained signature '" + cookieTokens[2] + "' but expected '" + expectedTokenSignature + "'"); } return userDetails; } }
PersistentLoginService extends TokenBasedRememberMeServices { public UserDetails processAutoLoginCookie(final String[] cookieTokens, final HttpServletRequest request, final HttpServletResponse response) { if (cookieTokens.length != 3) { throw new InvalidCookieException("Cookie token did not contain " + 2 + " tokens, but contained '" + Arrays.asList(cookieTokens) + "'"); } long tokenExpiryTime; try { tokenExpiryTime = new Long(cookieTokens[1]).longValue(); } catch (NumberFormatException nfe) { throw new InvalidCookieException( "Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1] + "')"); } if (isTokenExpired(tokenExpiryTime)) { throw new InvalidCookieException( "Cookie token[1] has expired (expired on '" + new Date(tokenExpiryTime) + "'; current time is '" + new Date() + "')"); } ExtendedUserDetails userDetails = (ExtendedUserDetails) (getUserDetailsService().loadUserByUsername(cookieTokens[0])); PersistentLogin login = userDetails.getPersistentLogin(); if (login == null) { throw new InvalidCookieException( "No PersistentLogin record in repository"); } String expectedTokenSignature = login.getTokenValue(); long expectedExpiryDate = login.getTokenExpirationDate(); if (tokenExpiryTime != expectedExpiryDate) { throw new InvalidCookieException( "Cookie token[1] contained expirationDate '" + cookieTokens[2] + "' but expected '" + expectedExpiryDate + "'"); } if (!expectedTokenSignature.equals(cookieTokens[2])) { throw new InvalidCookieException( "Cookie token[2] contained signature '" + cookieTokens[2] + "' but expected '" + expectedTokenSignature + "'"); } return userDetails; } PersistentLoginService(final String key,
            final UserDetailsService userDetailsService,
            final PersistentLoginRepository persistentLoginRepository); }
PersistentLoginService extends TokenBasedRememberMeServices { public UserDetails processAutoLoginCookie(final String[] cookieTokens, final HttpServletRequest request, final HttpServletResponse response) { if (cookieTokens.length != 3) { throw new InvalidCookieException("Cookie token did not contain " + 2 + " tokens, but contained '" + Arrays.asList(cookieTokens) + "'"); } long tokenExpiryTime; try { tokenExpiryTime = new Long(cookieTokens[1]).longValue(); } catch (NumberFormatException nfe) { throw new InvalidCookieException( "Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1] + "')"); } if (isTokenExpired(tokenExpiryTime)) { throw new InvalidCookieException( "Cookie token[1] has expired (expired on '" + new Date(tokenExpiryTime) + "'; current time is '" + new Date() + "')"); } ExtendedUserDetails userDetails = (ExtendedUserDetails) (getUserDetailsService().loadUserByUsername(cookieTokens[0])); PersistentLogin login = userDetails.getPersistentLogin(); if (login == null) { throw new InvalidCookieException( "No PersistentLogin record in repository"); } String expectedTokenSignature = login.getTokenValue(); long expectedExpiryDate = login.getTokenExpirationDate(); if (tokenExpiryTime != expectedExpiryDate) { throw new InvalidCookieException( "Cookie token[1] contained expirationDate '" + cookieTokens[2] + "' but expected '" + expectedExpiryDate + "'"); } if (!expectedTokenSignature.equals(cookieTokens[2])) { throw new InvalidCookieException( "Cookie token[2] contained signature '" + cookieTokens[2] + "' but expected '" + expectedTokenSignature + "'"); } return userDetails; } PersistentLoginService(final String key,
            final UserDetailsService userDetailsService,
            final PersistentLoginRepository persistentLoginRepository); UserDetails processAutoLoginCookie(final String[] cookieTokens,
            final HttpServletRequest request, final HttpServletResponse response); void onLoginSuccess(final HttpServletRequest request,
            final HttpServletResponse response,
            final Authentication successfulAuthentication); void logout(final HttpServletRequest request,
            final HttpServletResponse response, final Authentication authentication); }
PersistentLoginService extends TokenBasedRememberMeServices { public UserDetails processAutoLoginCookie(final String[] cookieTokens, final HttpServletRequest request, final HttpServletResponse response) { if (cookieTokens.length != 3) { throw new InvalidCookieException("Cookie token did not contain " + 2 + " tokens, but contained '" + Arrays.asList(cookieTokens) + "'"); } long tokenExpiryTime; try { tokenExpiryTime = new Long(cookieTokens[1]).longValue(); } catch (NumberFormatException nfe) { throw new InvalidCookieException( "Cookie token[1] did not contain a valid number (contained '" + cookieTokens[1] + "')"); } if (isTokenExpired(tokenExpiryTime)) { throw new InvalidCookieException( "Cookie token[1] has expired (expired on '" + new Date(tokenExpiryTime) + "'; current time is '" + new Date() + "')"); } ExtendedUserDetails userDetails = (ExtendedUserDetails) (getUserDetailsService().loadUserByUsername(cookieTokens[0])); PersistentLogin login = userDetails.getPersistentLogin(); if (login == null) { throw new InvalidCookieException( "No PersistentLogin record in repository"); } String expectedTokenSignature = login.getTokenValue(); long expectedExpiryDate = login.getTokenExpirationDate(); if (tokenExpiryTime != expectedExpiryDate) { throw new InvalidCookieException( "Cookie token[1] contained expirationDate '" + cookieTokens[2] + "' but expected '" + expectedExpiryDate + "'"); } if (!expectedTokenSignature.equals(cookieTokens[2])) { throw new InvalidCookieException( "Cookie token[2] contained signature '" + cookieTokens[2] + "' but expected '" + expectedTokenSignature + "'"); } return userDetails; } PersistentLoginService(final String key,
            final UserDetailsService userDetailsService,
            final PersistentLoginRepository persistentLoginRepository); UserDetails processAutoLoginCookie(final String[] cookieTokens,
            final HttpServletRequest request, final HttpServletResponse response); void onLoginSuccess(final HttpServletRequest request,
            final HttpServletResponse response,
            final Authentication successfulAuthentication); void logout(final HttpServletRequest request,
            final HttpServletResponse response, final Authentication authentication);  }