Skip to content
Snippets Groups Projects
Commit 84b78f9b authored by felmer's avatar felmer
Browse files

LMS-445 expiration time added to BasicSession

SVN: 8237
parent d52b8df2
No related branches found
No related tags found
No related merge requests found
......@@ -35,25 +35,28 @@ public class BasicSession implements Serializable
private final Principal principal;
private final String remoteHost;
private final long sessionStart;
private final int sessionExpirationTime;
/**
* Creates an instance from the specified session token, user name, principal, remoteHost, and
* session start (in milliseconds since start of the epoch).
*/
public BasicSession(String sessionToken, String userName, Principal principal,
String remoteHost, long sessionStart)
String remoteHost, long sessionStart, int sessionExpirationTime)
{
assert sessionToken != null : "Given session token can not be null.";
assert userName != null : "Given user name can not be null.";
assert principal != null : "Given principal can not be null.";
assert sessionStart > 0 : "Given session start must be larger than zero.";
assert remoteHost != null : "Given remote host can not be null";
assert sessionExpirationTime >= 0;
this.sessionToken = sessionToken;
this.userName = userName;
this.principal = principal;
this.remoteHost = remoteHost;
this.sessionStart = sessionStart;
this.sessionExpirationTime = sessionExpirationTime;
}
/**
......@@ -96,7 +99,15 @@ public class BasicSession implements Serializable
{
return sessionStart;
}
/**
* Returns the expiration time of this session in milliseconds.
*/
public final int getSessionExpirationTime()
{
return sessionExpirationTime;
}
@Override
public String toString()
{
......
......@@ -57,24 +57,19 @@ public class DefaultSessionManager<T extends BasicSession> implements ISessionMa
private static final TokenGenerator tokenGenerator = new TokenGenerator();
private static final class FullSession<S>
private static final class FullSession<S extends BasicSession>
{
/** Session data. */
private final S session;
/** The time period of inactivity (in milliseconds) after which the session will expire. */
private final long expirationPeriodMillis;
/** The last time when this session has been used (in milliseconds since 1970-01-01). */
private long lastActiveTime;
FullSession(final S session, final long expirationPeriodMillis)
FullSession(final S session)
{
assert session != null : "Undefined session";
assert expirationPeriodMillis >= 0; // == 0 is for unit tests
this.session = session;
this.expirationPeriodMillis = expirationPeriodMillis;
touch();
}
......@@ -100,7 +95,7 @@ public class DefaultSessionManager<T extends BasicSession> implements ISessionMa
*/
boolean hasExpired()
{
return System.currentTimeMillis() - lastActiveTime > expirationPeriodMillis;
return System.currentTimeMillis() - lastActiveTime > session.getSessionExpirationTime();
}
}
......@@ -119,7 +114,7 @@ public class DefaultSessionManager<T extends BasicSession> implements ISessionMa
private final IRemoteHostProvider remoteHostProvider;
/** The time after which an inactive session will be expired (in milliseconds). */
private final long sessionExpirationPeriodMillis;
private final int sessionExpirationPeriodMillis;
public DefaultSessionManager(final ISessionFactory<T> sessionFactory,
final ILogMessagePrefixGenerator<T> prefixGenerator,
......@@ -138,7 +133,7 @@ public class DefaultSessionManager<T extends BasicSession> implements ISessionMa
this.authenticationService = authenticationService;
this.remoteHostProvider = remoteHostProvider;
sessionExpirationPeriodMillis =
sessionExpirationPeriodMinutes * DateUtils.MILLIS_PER_MINUTE;
(int) (sessionExpirationPeriodMinutes * DateUtils.MILLIS_PER_MINUTE);
operationLog.info(String.format("Authentication service: '%s'", authenticationService
.getClass().getName()));
......@@ -154,9 +149,9 @@ public class DefaultSessionManager<T extends BasicSession> implements ISessionMa
synchronized (sessions)
{
final T session =
sessionFactory.create(sessionToken, user, principal, getRemoteHost(), now);
sessionFactory.create(sessionToken, user, principal, getRemoteHost(), now, sessionExpirationPeriodMillis);
final FullSession<T> createdSession =
new FullSession<T>(session, sessionExpirationPeriodMillis);
new FullSession<T>(session);
sessions.put(user, createdSession);
return session;
}
......
......@@ -24,9 +24,9 @@ package ch.systemsx.cisd.authentication;
public interface ISessionFactory<T extends BasicSession>
{
/**
* Creates a session from the specified session token, user name, principal, remoteHost, and
* session start (in milliseconds since start of the epoch).
* Creates a session from the specified session token, user name, principal, remoteHost, session
* start (in milliseconds since start of the epoch), and expiration time (in milliseconds).
*/
public T create(String sessionToken, String userName, Principal principal,
String remoteHost, long sessionStart);
String remoteHost, long sessionStart, int sessionExpirationTime);
}
......@@ -117,9 +117,9 @@ public class DefaultSessionManagerTest
one(sessionFactory)
.create(with(any(String.class)), with(equal(user)),
with(equal(principal)), with(equal(REMOTE_HOST)),
with(any(Long.class)));
with(any(Long.class)), with(any(Integer.class)));
BasicSession session =
new BasicSession(user + "-1", user, principal, REMOTE_HOST, 42L);
new BasicSession(user + "-1", user, principal, REMOTE_HOST, 42L, 0);
will(returnValue(session));
atLeast(1).of(prefixGenerator).createPrefix(session);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment