2 * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.idp.session;
19 import java.io.IOException;
21 import javax.servlet.Filter;
22 import javax.servlet.FilterChain;
23 import javax.servlet.FilterConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.http.Cookie;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.joda.time.DateTime;
32 import org.opensaml.xml.util.DatatypeHelper;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import edu.internet2.middleware.shibboleth.common.session.SessionManager;
39 * A filter that adds the current users {@link Session} the request, if the user has a session.
41 public class IdPSessionFilter implements Filter {
43 /** Name of the IdP Cookie containing the IdP session ID. */
44 public static final String IDP_SESSION_COOKIE_NAME = "_idp_session";
47 private final Logger log = LoggerFactory.getLogger(IdPSessionFilter.class);
49 /** IdP session manager. */
50 private SessionManager<Session> sessionManager;
53 public void destroy() {
58 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
60 HttpServletRequest httpRequest = (HttpServletRequest) request;
61 HttpServletResponse httpResponse = (HttpServletResponse) response;
63 Session idpSession = null;
64 Cookie idpSessionCookie = getIdPSessionCookie(httpRequest);
65 if (idpSessionCookie != null) {
66 idpSession = sessionManager.getSession(idpSessionCookie.getValue());
67 if (idpSession != null) {
68 idpSession.setLastActivityInstant(new DateTime());
69 httpRequest.setAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE, idpSession);
73 addIdPSessionCookieToResponse(httpRequest, httpResponse, idpSession);
75 filterChain.doFilter(request, response);
79 public void init(FilterConfig filterConfig) throws ServletException {
80 sessionManager = (SessionManager<Session>) filterConfig.getServletContext().getAttribute("sessionManager");
84 * Gets the IdP session cookie from the current request, if the user currently has a session.
86 * @param request current HTTP request
88 * @return the user's current IdP session cookie, if they have a current session, otherwise null
90 protected Cookie getIdPSessionCookie(HttpServletRequest request) {
91 log.debug("Attempting to retrieve IdP session cookie.");
92 Cookie[] requestCookies = request.getCookies();
94 if (requestCookies != null) {
95 for (Cookie requestCookie : requestCookies) {
96 if (DatatypeHelper.safeEquals(requestCookie.getDomain(), request.getLocalName())
97 && DatatypeHelper.safeEquals(requestCookie.getPath(), request.getContextPath())
98 && DatatypeHelper.safeEquals(requestCookie.getName(), IDP_SESSION_COOKIE_NAME)) {
99 log.debug("Found IdP session cookie.");
100 return requestCookie;
105 log.debug("No IdP session cookie sent by the client.");
110 * Adds a cookie, containing the user's IdP session ID, to the response.
112 * @param request current HTTP request
113 * @param response current HTTP response
114 * @param userSession user's currentSession
116 protected void addIdPSessionCookieToResponse(HttpServletRequest request, HttpServletResponse response,
117 Session userSession) {
118 log.debug("Adding session cookie to HTTP response.");
119 Session currentSession = userSession;
120 if (currentSession == null) {
121 log.debug("Retrieving IdP session from HTTP request");
122 currentSession = (Session) request.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
123 if (currentSession == null) {
124 log.debug("Retrieving IdP session from HTTP session");
125 currentSession = (Session) request.getSession().getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
129 if (currentSession != null) {
130 Cookie sessionCookie = new Cookie(IDP_SESSION_COOKIE_NAME, userSession.getSessionID());
131 sessionCookie.setDomain(request.getLocalName());
132 sessionCookie.setPath(request.getContextPath());
133 sessionCookie.setSecure(false);
135 int maxAge = (int) (userSession.getInactivityTimeout() / 1000);
136 sessionCookie.setMaxAge(maxAge);
138 response.addCookie(sessionCookie);
139 log.debug("Added IdP session cookie to HTTP response");