Search in sources :

Example 1 with PersistentRememberMeToken

use of org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken in project spring-security by spring-projects.

the class PersistentTokenBasedRememberMeServicesTests method successfulAutoLoginCreatesNewTokenAndCookieWithSameSeries.

@Test
public void successfulAutoLoginCreatesNewTokenAndCookieWithSameSeries() {
    services = create(new PersistentRememberMeToken("joe", "series", "token", new Date()));
    // 12 => b64 length will be 16
    services.setTokenLength(12);
    MockHttpServletResponse response = new MockHttpServletResponse();
    services.processAutoLoginCookie(new String[] { "series", "token" }, new MockHttpServletRequest(), response);
    assertThat(repo.getStoredToken().getSeries()).isEqualTo("series");
    assertThat(repo.getStoredToken().getTokenValue().length()).isEqualTo(16);
    String[] cookie = services.decodeCookie(response.getCookie("mycookiename").getValue());
    assertThat(cookie[0]).isEqualTo("series");
    assertThat(cookie[1]).isEqualTo(repo.getStoredToken().getTokenValue());
}
Also used : PersistentRememberMeToken(org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with PersistentRememberMeToken

use of org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken in project spring-security by spring-projects.

the class PersistentTokenBasedRememberMeServicesTests method logoutClearsUsersTokenAndCookie.

@Test
public void logoutClearsUsersTokenAndCookie() throws Exception {
    Cookie cookie = new Cookie("mycookiename", "somevalue");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    services = create(new PersistentRememberMeToken("joe", "series", "token", new Date()));
    services.logout(request, response, new TestingAuthenticationToken("joe", "somepass", "SOME_AUTH"));
    Cookie returnedCookie = response.getCookie("mycookiename");
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isEqualTo(0);
    // SEC-1280
    services.logout(request, response, null);
}
Also used : Cookie(javax.servlet.http.Cookie) PersistentRememberMeToken(org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 3 with PersistentRememberMeToken

use of org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken in project spring-security by spring-projects.

the class PersistentTokenBasedRememberMeServicesTests method cookieTheftIsDetectedWhenSeriesAndTokenDontMatch.

@Test(expected = CookieTheftException.class)
public void cookieTheftIsDetectedWhenSeriesAndTokenDontMatch() {
    services = create(new PersistentRememberMeToken("joe", "series", "wrongtoken", new Date()));
    services.processAutoLoginCookie(new String[] { "series", "token" }, new MockHttpServletRequest(), new MockHttpServletResponse());
}
Also used : PersistentRememberMeToken(org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with PersistentRememberMeToken

use of org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken in project spring-security by spring-projects.

the class PersistentTokenBasedRememberMeServicesTests method loginIsRejectedWhenTokenIsExpired.

@Test(expected = RememberMeAuthenticationException.class)
public void loginIsRejectedWhenTokenIsExpired() {
    services = create(new PersistentRememberMeToken("joe", "series", "token", new Date(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(1) - 100)));
    services.setTokenValiditySeconds(1);
    services.processAutoLoginCookie(new String[] { "series", "token" }, new MockHttpServletRequest(), new MockHttpServletResponse());
}
Also used : PersistentRememberMeToken(org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 5 with PersistentRememberMeToken

use of org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken in project google-app-engine-jappstart by taylorleese.

the class PersistentTokenRepositoryImpl method getTokenForSeries.

/**
     * Gets the token for the given series.
     *
     * @param series the series
     * @return the remember me token
     */
@Override
public final PersistentRememberMeToken getTokenForSeries(final String series) {
    PersistentLogin persistentLogin = null;
    final Query query = entityManager.createQuery("SELECT p FROM PersistentLogin p WHERE series = :series");
    query.setParameter("series", series);
    try {
        persistentLogin = (PersistentLogin) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
    return new PersistentRememberMeToken(persistentLogin.getUsername(), persistentLogin.getSeries(), persistentLogin.getToken(), persistentLogin.getLastUsed());
}
Also used : PersistentRememberMeToken(org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) PersistentLogin(com.jappstart.model.auth.PersistentLogin)

Aggregations

PersistentRememberMeToken (org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken)5 Date (java.util.Date)4 Test (org.junit.Test)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 PersistentLogin (com.jappstart.model.auth.PersistentLogin)1 NoResultException (javax.persistence.NoResultException)1 Query (javax.persistence.Query)1 Cookie (javax.servlet.http.Cookie)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1