Commit 88352731 authored by Tobias Assmann's avatar Tobias Assmann
Browse files

start adding session stuff from remote_eac branch from renè

parent 01041d42
......@@ -22,6 +22,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import reqesidta.ssa.sa.CertificateAuthorityClient;
import reqesidta.ssa.server.config.SSAConfig;
import reqesidta.ssa.session.SessionStore;
/**
*
......@@ -36,6 +37,8 @@ public class SsaService {
@Inject CertificateAuthorityClient caClient;
@Inject SessionStore sessionStore;
public SsaService() {
JsonbConfig config = new JsonbConfig()
.withBinaryDataStrategy(BinaryDataStrategy.BASE_64);
......
package reqesidta.ssa.session;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import javax.annotation.PreDestroy;
import org.jboss.logging.Logger;
/**
* Taken from remote_eac branch of Renè
* @author Tobias Assmann <tobias.assmann@ecsec.de>
*/
public class SessionStore {
private final static Logger LOG = Logger.getLogger(SessionStore.class);
private final Duration maxAge;
private final Duration checkAgeIntervall;
private final Timer sessionLifeTime = new Timer("SessionExpireChecker", true);
private final ConcurrentNavigableMap<String, Session> storage;
public SessionStore(Duration timeout) {
this.storage = new ConcurrentSkipListMap<>();
this.maxAge = timeout;
this.checkAgeIntervall = Duration.ofMinutes(30);
LOG.debug("Setting up SessionStore ... starting Timer 'SessionExpireChecker'");
this.startTimer();
}
@PreDestroy
public synchronized void shutdown() {
this.stopTimer();
}
public Session getNewSession() {
String id = UUID.randomUUID().toString();
Session s = new Session(id);
this.storage.put(id, s);
return s;
}
public Optional<Session> getSession(String id) {
return Optional.ofNullable(this.storage.get(id));
}
public synchronized void removeSession(String id) {
this.storage.remove(id);
}
private void startTimer() {
sessionLifeTime.schedule(new TimerTask() {
@Override
public void run() {
removeExpired();
}
}, 0, checkAgeIntervall.toMillis());
}
private void stopTimer() {
sessionLifeTime.cancel();
}
private boolean isExpired(Session s) {
return s.getLastAccess().plus(maxAge).isAfter(Instant.now());
}
private synchronized void removeExpired() {
ArrayList<String> toRemove = new ArrayList<>();
storage.forEach((id, session) -> {
if (this.isExpired(session)) {
toRemove.add(id);
}
});
toRemove.forEach((id) -> {
removeSession(id);
});
}
}
package reqesidta.ssa.session;
import java.time.Duration;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
/**
*
* Taken from remote_eac branch of Renè
* @author tobias assmann
*/
@ApplicationScoped
public class SessionStoreProvider {
private final SessionStore store;
public SessionStoreProvider() {
this.store = new SessionStore(Duration.ofHours(1));
}
@Produces
public SessionStore getStore() {
return store;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment