Search in sources :

Example 6 with PersistentToken

use of de.tum.in.www1.artemis.domain.PersistentToken in project ArTEMiS by ls1intum.

the class PersistentTokenRememberMeServices method processAutoLoginCookie.

@Override
protected UserDetails processAutoLoginCookie(String[] cookieTokens, HttpServletRequest request, HttpServletResponse response) {
    synchronized (this) {
        // prevent 2 authentication requests from the same user in parallel
        String login = null;
        UpgradedRememberMeToken upgradedToken = upgradedTokenCache.getIfPresent(cookieTokens[0]);
        if (upgradedToken != null) {
            login = upgradedToken.getUserLoginIfValidAndRecentUpgrade(cookieTokens);
            log.debug("Detected previously upgraded login token for user '{}'", login);
        }
        if (login == null) {
            PersistentToken token = getPersistentToken(cookieTokens);
            login = token.getUser().getLogin();
            // Token also matches, so login is valid. Update the token value, keeping the *same* series number.
            log.debug("Refreshing persistent login token for user '{}', series '{}'", login, token.getSeries());
            token.setTokenDate(LocalDate.now());
            token.setTokenValue(RandomUtil.generateTokenData());
            token.setIpAddress(request.getRemoteAddr());
            token.setUserAgent(request.getHeader("User-Agent"));
            try {
                persistentTokenRepository.saveAndFlush(token);
            } catch (DataAccessException e) {
                log.error("Failed to update token: ", e);
                throw new RememberMeAuthenticationException("Autologin failed due to data access problem", e);
            }
            addCookie(token, request, response);
            upgradedTokenCache.put(cookieTokens[0], new UpgradedRememberMeToken(cookieTokens, login));
        }
        return getUserDetailsService().loadUserByUsername(login);
    }
}
Also used : RememberMeAuthenticationException(org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException) PersistentToken(de.tum.in.www1.artemis.domain.PersistentToken) DataAccessException(org.springframework.dao.DataAccessException)

Example 7 with PersistentToken

use of de.tum.in.www1.artemis.domain.PersistentToken in project ArTEMiS by ls1intum.

the class UserServiceIntTest method generateUserToken.

private void generateUserToken(User user, String tokenSeries, LocalDate localDate) {
    PersistentToken token = new PersistentToken();
    token.setSeries(tokenSeries);
    token.setUser(user);
    token.setTokenValue(tokenSeries + "-data");
    token.setTokenDate(localDate);
    token.setIpAddress("127.0.0.1");
    token.setUserAgent("Test agent");
    persistentTokenRepository.saveAndFlush(token);
}
Also used : PersistentToken(de.tum.in.www1.artemis.domain.PersistentToken)

Aggregations

PersistentToken (de.tum.in.www1.artemis.domain.PersistentToken)7 RememberMeAuthenticationException (org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException)4 Transactional (org.springframework.transaction.annotation.Transactional)4 InvalidCookieException (org.springframework.security.web.authentication.rememberme.InvalidCookieException)3 User (de.tum.in.www1.artemis.domain.User)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 DataAccessException (org.springframework.dao.DataAccessException)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 CookieTheftException (org.springframework.security.web.authentication.rememberme.CookieTheftException)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 PersistentTokenRepository (de.tum.in.www1.artemis.repository.PersistentTokenRepository)1 UserRepository (de.tum.in.www1.artemis.repository.UserRepository)1 RandomUtil (de.tum.in.www1.artemis.service.util.RandomUtil)1 JHipsterProperties (io.github.jhipster.config.JHipsterProperties)1 Serializable (java.io.Serializable)1 LocalDate (java.time.LocalDate)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1