de.sekmi.li2b2.services.token.AbstractTokenManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of li2b2-server Show documentation
Show all versions of li2b2-server Show documentation
This project emulates the core components
of an i2b2 server backend. Basic functionality
of PM, CRC, ONT and WORK cells allows the
official i2b2 webclient to connect ot this
emulated server.
The newest version!
package de.sekmi.li2b2.services.token;
import java.security.Principal;
import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
/**
* Abstract token manager to handle simple token authentication.
*
* Expiration defaults to 15 minutes of inactivity. This can be configured
* by overriding {@link #getExpirationMillis()}
*
* @author R.W.Majeed
*
* @param token user payload
*/
public abstract class AbstractTokenManager implements TokenManager {
private static final Logger log = Logger.getLogger(AbstractTokenManager.class.getName());
private Map> tokenMap;
private long maxLifetime;
private long cleanupInterval;
private long lastCleanup;
public AbstractTokenManager(){
this.tokenMap = new HashMap<>();
this.maxLifetime = Long.MAX_VALUE;
// TODO use external configuration
this.cleanupInterval = 1000*60*60; // default is 1 hour
this.lastCleanup = System.currentTimeMillis();
}
public abstract T createPrincipal(String name);
public String registerPrincipal(T data){
Token token = new Token(data);
UUID uuid = UUID.randomUUID();
// try to clean expired tokens
cleanExpiredTokens();
// add new token
synchronized( tokenMap ){
tokenMap.put(uuid, token);
}
log.info("New token for user "+data.getName()+": "+uuid.toString());
return uuid.toString();
}
/* (non-Javadoc)
* @see de.sekmi.li2b2.services.TokenManager#lookupToken(java.lang.String)
*/
@Override
public Token lookupToken(String uuid){
Token token;
UUID key;
try{
key = UUID.fromString(uuid);
synchronized( tokenMap ){
token = tokenMap.get(key);
}
}catch( IllegalArgumentException e ){
token = null;
key = null;
}
if( token != null ){
// check if expired
if( isExpired(token, System.currentTimeMillis()) ){
token = null;
}
}
return token;
}
protected boolean isExpired(Token token, long now){
if( now - token.issued > maxLifetime ){
log.info("Token lifetime exceeded for "+token.getPayload().getName());
return true;
}else if( now - token.renewed > getExpirationMillis() ){
log.info("Token too old ("+Instant.ofEpochMilli(token.renewed)+") for "+token.getPayload().getName());
return true;
}else{
return false;
}
}
private void cleanExpiredTokens(){
long now = System.currentTimeMillis();
if( now - lastCleanup < cleanupInterval ){
return;
}
cleanExpiredTokens(now);
lastCleanup = now;
}
protected void cleanExpiredTokens(long now){
synchronized( tokenMap ){
Iterator> iter = tokenMap.values().iterator();
while( iter.hasNext() ){
Token t = iter.next();
if( isExpired(t, now) ){
iter.remove();
}
}
}
}
public void renew(Token token){
token.renewed = System.currentTimeMillis();
}
/* (non-Javadoc)
* @see de.sekmi.li2b2.services.TokenManager#getTokenCount()
*/
@Override
public int getTokenCount(){
return tokenMap.size();
}
@Override
public String registerPrincipal(String name) {
return registerPrincipal(createPrincipal(name));
}
@Override
public void renew(String uuid) {
Token token = lookupToken(uuid);
if( token != null ){
renew(token);
}
}
@Override
public long getExpirationMillis(){
// default is 15 minutes
return 1000*60*15;
}
}