// pretend that we had session id before
Serializable id = null;
if (sessionId != null && !sessionId.isEmpty()) {
id = sessionId;
}
final Subject subject = new Subject.Builder().sessionId(id).buildSubject();
ThreadContext.bind(subject);
try {
subject.login(new UsernamePasswordToken(createRequest.username, createRequest.password));
final User user = userService.load(createRequest.username);
if (user != null) {
long timeoutInMillis = user.getSessionTimeoutMs();
subject.getSession().setTimeout(timeoutInMillis);
} else {
// set a sane default. really we should be able to load the user from above.
subject.getSession().setTimeout(TimeUnit.HOURS.toMillis(8));
}
subject.getSession().touch();
// save subject in session, otherwise we can't get the username back in subsequent requests.
((DefaultSecurityManager) SecurityUtils.getSecurityManager()).getSubjectDAO().save(subject);
} catch (AuthenticationException e) {
LOG.warn("Unable to log in user " + createRequest.username, e);
} catch (UnknownSessionException e) {
subject.logout();
}
if (subject.isAuthenticated()) {
final org.apache.shiro.session.Session session = subject.getSession();
id = session.getId();
result.sessionId = id.toString();
// TODO is this even used by anyone yet?
result.validUntil = new DateTime(session.getLastAccessTime(), DateTimeZone.UTC).plus(session.getTimeout()).toDate();
return result;