io.robe.auth.token.TokenManager Maven / Gradle / Ivy
package io.robe.auth.token;
import io.robe.auth.token.configuration.TokenBasedAuthConfiguration;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
/**
* A singleton factory class for configuring and creating token implementations.
* It wraps and caches 2 main constructors of token class.
*
* @param
*/
public class TokenManager {
private static TokenManager instance = null;
private static final Logger LOGGER = LoggerFactory.getLogger(TokenManager.class);
private static Class extends Token> tokenClass;
private static Constructor constructorByParameters;
private static Constructor constructorByTokenString;
private static ThreadLocal currentLoginToken = new ThreadLocal();
private TokenManager() {
}
static void setCurrentLoginToken(Token token) {
currentLoginToken.set(token);
}
public static Token getCurrentLoginToken() {
return currentLoginToken.get();
}
/**
* Creates a single instance for factory.
*
* @param Token class
* @return instance
*/
public static TokenManager getInstance() {
if (instance == null)
instance = new TokenManager();
{
}
return instance;
}
/**
* Configure the factory and token. Cache the constructors of token class for future use.
*
* @param tokenClass
* @param configuration
* @param Token Class
*/
public static void configure(Class tokenClass, TokenBasedAuthConfiguration configuration) throws Exception {
TokenManager.tokenClass = tokenClass;
Method configureMethod = null;
LOGGER.info("Configuring token class : " + tokenClass.getName());
configureMethod = tokenClass.getMethod("configure", TokenBasedAuthConfiguration.class);
if (configureMethod != null)
configureMethod.invoke(null, configuration);
else
LOGGER.info("Configuring token class : " + tokenClass.getName() + " : Failed No such method");
LOGGER.debug("Caching constructors");
constructorByParameters = tokenClass.getConstructor(String.class, String.class, DateTime.class, Map.class);
if (constructorByParameters == null) {
LOGGER.error("Constructor (String username, DateTime expireAt, Map attributes): Missing constructor implementation.");
throw new RuntimeException("Missing constructor implementation");
}
LOGGER.debug("Constructor (String username, DateTime expireAt, Map attributes): Loaded");
constructorByTokenString = tokenClass.getConstructor(String.class);
if (constructorByTokenString == null) {
LOGGER.error("Constructor (String tokenString): Missing constructor implementation.");
throw new RuntimeException("Missing constructor implementation");
}
LOGGER.debug("Constructor (String tokenString): Loaded.");
TokenManager.getInstance();
}
/**
* Creates an access token with the given parameters.
*
* @param username Username
* @param expireAt expiration time of token
* @param attributes extra attributes to customize token
* @return
* @throws Exception
*/
public Token createToken(String userId, String username, DateTime expireAt, Map attributes) throws Exception {
return (I) constructorByParameters.newInstance(userId, username, expireAt, attributes);
}
/**
* Creates an access token with the given tokenString.
*
* @param tokenString to parse
* @return
* @throws Exception
*/
public Token createToken(String tokenString) throws IllegalAccessException, InvocationTargetException, InstantiationException {
return (I) constructorByTokenString.newInstance(tokenString);
}
}