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;
37 import edu.internet2.middleware.shibboleth.idp.authn.AuthenticationEngine;
40 * A filter that adds the current users {@link Session} the request, if the user has a session.
42 public class IdPSessionFilter implements Filter {
45 private final Logger log = LoggerFactory.getLogger(IdPSessionFilter.class);
47 /** IdP session manager. */
48 private SessionManager<Session> sessionManager;
51 public void destroy() {
56 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
58 HttpServletRequest httpRequest = (HttpServletRequest) request;
59 HttpServletResponse httpResponse = (HttpServletResponse) response;
61 Session idpSession = null;
62 Cookie idpSessionCookie = getIdPSessionCookie(httpRequest);
63 if (idpSessionCookie != null) {
64 idpSession = sessionManager.getSession(idpSessionCookie.getValue());
65 if (idpSession != null) {
66 log.trace("Updating IdP session activity time and adding session object to the request");
67 idpSession.setLastActivityInstant(new DateTime());
68 httpRequest.setAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE, idpSession);
72 filterChain.doFilter(request, response);
76 public void init(FilterConfig filterConfig) throws ServletException {
77 sessionManager = (SessionManager<Session>) filterConfig.getServletContext().getAttribute("sessionManager");
81 * Gets the IdP session cookie from the current request, if the user currently has a session.
83 * @param request current HTTP request
85 * @return the user's current IdP session cookie, if they have a current session, otherwise null
87 protected Cookie getIdPSessionCookie(HttpServletRequest request) {
88 log.trace("Attempting to retrieve IdP session cookie.");
89 Cookie[] requestCookies = request.getCookies();
91 if (requestCookies != null) {
92 for (Cookie requestCookie : requestCookies) {
93 if (DatatypeHelper.safeEquals(requestCookie.getName(), AuthenticationEngine.IDP_SESSION_COOKIE_NAME)) {
94 log.trace("Found IdP session cookie.");
100 log.trace("No IdP session cookie sent by the client.");