All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.textrecruit.ustackserverapi.auth.LogoutUtil Maven / Gradle / Ivy

There is a newer version: 1.0.31
Show newest version
package com.textrecruit.ustackserverapi.auth;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.textrecruit.ustackserverapi.exceptions.UserAuthenticationException;
import com.textrecruit.ustack.data.AccessToken;
import com.textrecruit.ustack.data.UDataCache;
import com.textrecruit.ustack.main.UOpts;
import org.apache.log4j.Logger;

public class LogoutUtil {

    static Logger logger               	= Logger.getLogger(LogoutUtil.class);

    public static void logoutToken(AccessToken.AccessTokenDetails token) {

        long remainingTokenLife = token.getExpirationAge() - System.currentTimeMillis();
        int remainingTokenLifeSec = (int)(remainingTokenLife / 1000L);

        String key = String.format("logout_%s_%d", token.getUserName(), token.getExpirationAge());
        if (UOpts.getCacheEnabled()) {
            UDataCache.getInstance().set(key, remainingTokenLifeSec, "T");
        }
        logger.info("Logging out : " + key + " (" + remainingTokenLifeSec + ")");

    }

    public static void checkTokenLogout(AccessToken.AccessTokenDetails token) throws UserAuthenticationException {

        String key = String.format("logout_%s_%d", token.getUserName(), token.getExpirationAge());
        if (UOpts.getCacheEnabled() && UDataCache.getInstance().get(key) != null) {
            throw new UserAuthenticationException("User Logged Out", "LoggedOut");
        }
    }

    public static void checkTokenLogout(DecodedJWT token) throws UserAuthenticationException {

        String key = String.format("logout_jwt_%s", token.getClaim("id"));
        if (UOpts.getCacheEnabled() && UDataCache.getInstance().get(key) != null) {
            throw new UserAuthenticationException("User Logged Out", "LoggedOut");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy