edu.ksu.lti.launch.service.LtiSessionService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lti-launch Show documentation
Show all versions of lti-launch Show documentation
Java framework for authenticating LTI launch requests from the Canvas LMS
The newest version!
package edu.ksu.lti.launch.service;
import edu.ksu.lti.launch.exception.NoLtiSessionException;
import edu.ksu.lti.launch.model.LtiSession;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Created by alexanda on 7/27/16.
*/
@Service
public class LtiSessionService {
/**
* Get the LtiSession object from the HTTP session. It is put there up in the ltiLaunch method.
* This should really be done using a SpringSecurityContext to get the authenticated principal
* which could then hold this LTI information. But I'm having serious trouble figuring out how to
* do this correctly and I need *some* kind of session management for right now.
* Another approach would be to create this as a session scoped bean but the problem there is that
* I need to share this session object across controllers (the OauthController to be specific) and
* this breaks for some reason so I'm rolling my own session management here.
*
* @return The current user's LTI session information
* @throws edu.ksu.lti.launch.exception.NoLtiSessionException if the user does not have a valid LTI session.
*/
public LtiSession getLtiSession() throws NoLtiSessionException {
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();
HttpSession session = req.getSession();
LtiSession ltiSession = (LtiSession) session.getAttribute(LtiSession.class.getName());
if (ltiSession == null) {
throw new NoLtiSessionException();
}
return ltiSession;
}
}