2 * Copyright [2006] [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.authn;
19 import java.io.IOException;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.util.List;
24 import javax.servlet.RequestDispatcher;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
31 import org.apache.log4j.Logger;
32 import org.joda.time.DateTime;
33 import org.opensaml.xml.util.DatatypeHelper;
34 import org.opensaml.xml.util.Pair;
36 import edu.internet2.middleware.shibboleth.common.session.SessionManager;
37 import edu.internet2.middleware.shibboleth.idp.profile.IdPProfileHandlerManager;
38 import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
39 import edu.internet2.middleware.shibboleth.idp.session.ServiceInformation;
40 import edu.internet2.middleware.shibboleth.idp.session.Session;
41 import edu.internet2.middleware.shibboleth.idp.session.impl.AuthenticationMethodInformationImpl;
42 import edu.internet2.middleware.shibboleth.idp.session.impl.ServiceInformationImpl;
45 * Manager responsible for handling authentication requests.
47 public class AuthenticationEngine extends HttpServlet {
49 /** Serial version UID. */
50 private static final long serialVersionUID = 8494202791991613148L;
53 private static final Logger LOG = Logger.getLogger(AuthenticationEngine.class);
56 * Gets the manager used to retrieve handlers for requests.
58 * @return manager used to retrieve handlers for requests
60 public IdPProfileHandlerManager getProfileHandlerManager() {
61 return (IdPProfileHandlerManager) getServletContext().getAttribute("handlerManager");
65 * Gets the session manager to be used.
67 * @return session manager to be used
69 @SuppressWarnings("unchecked")
70 public SessionManager<Session> getSessionManager() {
71 return (SessionManager<Session>) getServletContext().getAttribute("sessionManager");
75 * Returns control back to the authentication engine.
77 * @param httpRequest current http request
78 * @param httpResponse current http response
80 public static void returnToAuthenticationEngine(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
81 if (LOG.isDebugEnabled()) {
82 LOG.debug("Returning control to authentication engine");
84 HttpSession httpSession = httpRequest.getSession();
85 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
86 forwardRequest(loginContext.getAuthenticationEngineURL(), httpRequest, httpResponse);
90 * Returns control back to the profile handler that invoked the authentication engine.
92 * @param loginContext current login context
93 * @param httpRequest current http request
94 * @param httpResponse current http response
96 public static void returnToProfileHandler(LoginContext loginContext, HttpServletRequest httpRequest,
97 HttpServletResponse httpResponse) {
98 if (LOG.isDebugEnabled()) {
99 LOG.debug("Returning control to profile handler at: " + loginContext.getProfileHandlerURL());
101 forwardRequest(loginContext.getProfileHandlerURL(), httpRequest, httpResponse);
105 * Forwards a request to the given path.
107 * @param forwardPath path to forward the request to
108 * @param httpRequest current HTTP request
109 * @param httpResponse current HTTP response
111 protected static void forwardRequest(String forwardPath, HttpServletRequest httpRequest,
112 HttpServletResponse httpResponse) {
114 RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(forwardPath);
115 dispatcher.forward(httpRequest, httpResponse);
116 } catch (IOException e) {
117 LOG.fatal("Unable to return control back to authentication engine", e);
118 } catch (ServletException e) {
119 LOG.fatal("Unable to return control back to authentication engine", e);
124 @SuppressWarnings("unchecked")
125 protected void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException,
127 if (LOG.isDebugEnabled()) {
128 LOG.debug("Processing incoming request");
131 HttpSession httpSession = httpRequest.getSession();
132 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
133 if (loginContext == null) {
134 LOG.error("Incoming request does not have attached login context");
135 throw new ServletException("Incoming request does not have attached login context");
138 if (!loginContext.getAuthenticationAttempted()) {
139 String shibSessionId = (String) httpSession.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
140 Session shibSession = getSessionManager().getSession(shibSessionId);
142 if (shibSession != null) {
143 AuthenticationMethodInformation authenticationMethod = getUsableExistingAuthenticationMethod(
144 loginContext, shibSession);
145 if (authenticationMethod != null) {
146 if (LOG.isDebugEnabled()) {
147 LOG.debug("An active authentication method is applicable for relying party. "
148 + "Using authentication method " + authenticationMethod.getAuthenticationMethod()
149 + " as authentication method to relying party without re-authenticating user.");
151 authenticateUserWithActiveMethod(httpRequest, httpResponse, authenticationMethod);
155 if (LOG.isDebugEnabled()) {
156 LOG.debug("No active authentication method is applicable for relying party. "
157 + "Authenticating user with to be determined method.");
159 authenticateUserWithoutActiveMethod1(httpRequest, httpResponse);
161 if (LOG.isDebugEnabled()) {
162 LOG.debug("Request returned from authentication handler, completing authentication process.");
164 authenticateUserWithoutActiveMethod2(httpRequest, httpResponse);
169 * Completes the authentication request using an existing, active, authentication method for the current user.
171 * @param httpRequest current HTTP request
172 * @param httpResponse current HTTP response
173 * @param authenticationMethod authentication method to use to complete the request
175 protected void authenticateUserWithActiveMethod(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
176 AuthenticationMethodInformation authenticationMethod) {
177 HttpSession httpSession = httpRequest.getSession();
179 String shibSessionId = (String) httpSession.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
180 Session shibSession = getSessionManager().getSession(shibSessionId);
182 if (LOG.isDebugEnabled()) {
183 LOG.debug("Populating login context with existing session and authentication method information.");
185 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
186 loginContext.setAuthenticationDuration(authenticationMethod.getAuthenticationDuration());
187 loginContext.setAuthenticationInstant(authenticationMethod.getAuthenticationInstant());
188 loginContext.setAuthenticationMethod(authenticationMethod.getAuthenticationMethod());
189 loginContext.setPrincipalAuthenticated(true);
190 loginContext.setPrincipalName(shibSession.getPrincipalName());
192 ServiceInformation serviceInfo = new ServiceInformationImpl(loginContext.getRelyingPartyId(), new DateTime(),
193 authenticationMethod);
194 shibSession.getServicesInformation().put(serviceInfo.getEntityID(), serviceInfo);
196 returnToProfileHandler(loginContext, httpRequest, httpResponse);
200 * Performs the first part of user authentication. An authentication handler is determined, the login context is
201 * populated with some initial information, and control is forward to the selected handler so that it may
202 * authenticate the user.
204 * @param httpRequest current HTTP request
205 * @param httpResponse current HTTP response
207 protected void authenticateUserWithoutActiveMethod1(HttpServletRequest httpRequest,
208 HttpServletResponse httpResponse) {
209 HttpSession httpSession = httpRequest.getSession();
210 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
212 if (LOG.isDebugEnabled()) {
213 LOG.debug("Selecting appropriate authentication method for request.");
215 Pair<String, AuthenticationHandler> handler = getProfileHandlerManager().getAuthenticationHandler(
218 if (handler == null) {
219 loginContext.setPrincipalAuthenticated(false);
220 loginContext.setAuthenticationFailureMessage("No AuthenticationHandler satisfys the request from: "
221 + loginContext.getRelyingPartyId());
222 LOG.error("No AuthenticationHandler satisfys the request from relying party: "
223 + loginContext.getRelyingPartyId());
224 returnToProfileHandler(loginContext, httpRequest, httpResponse);
227 if (LOG.isDebugEnabled()) {
228 LOG.debug("Authentication method " + handler.getFirst() + " will be used to authenticate user.");
230 loginContext.setAuthenticationAttempted();
231 loginContext.setAuthenticationDuration(handler.getSecond().getAuthenticationDuration());
232 loginContext.setAuthenticationMethod(handler.getFirst());
233 loginContext.setAuthenticationEngineURL(httpRequest.getRequestURI());
235 if (LOG.isDebugEnabled()) {
236 LOG.debug("Transferring control to authentication handler of type: "
237 + handler.getSecond().getClass().getName());
239 handler.getSecond().login(httpRequest, httpResponse);
243 * Performs the second part of user authentication. The principal name set by the authentication handler is
244 * retrieved and pushed in to the login context, a Shibboleth session is created if needed, information indicating
245 * that the user has logged into the service is recorded and finally control is returned back to the profile
248 * @param httpRequest current HTTP request
249 * @param httpResponse current HTTP response
251 protected void authenticateUserWithoutActiveMethod2(HttpServletRequest httpRequest,
252 HttpServletResponse httpResponse) {
253 HttpSession httpSession = httpRequest.getSession();
255 String principalName = (String) httpRequest.getAttribute(AuthenticationHandler.PRINCIPAL_NAME_KEY);
256 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
257 if (DatatypeHelper.isEmpty(principalName)) {
258 loginContext.setPrincipalAuthenticated(false);
259 loginContext.setAuthenticationFailureMessage("No principal name returned from authentication handler.");
260 LOG.error("No principal name returned from authentication method: "
261 + loginContext.getAuthenticationMethod());
262 returnToProfileHandler(loginContext, httpRequest, httpResponse);
264 loginContext.setPrincipalName(principalName);
266 String shibSessionId = (String) httpSession.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
267 Session shibSession = getSessionManager().getSession(shibSessionId);
269 if (shibSession == null) {
270 if (LOG.isDebugEnabled()) {
271 LOG.debug("Creating shibboleth session for principal " + principalName);
276 addr = InetAddress.getByName(httpRequest.getRemoteAddr());
277 } catch (UnknownHostException ex) {
281 shibSession = (Session) getSessionManager().createSession(addr, loginContext.getPrincipalName());
282 loginContext.setSessionID(shibSession.getSessionID());
283 httpSession.setAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE, shibSession.getSessionID());
286 if (LOG.isDebugEnabled()) {
287 LOG.debug("Recording authentication and service information in Shibboleth session for principal: "
290 AuthenticationMethodInformation authnMethodInfo = new AuthenticationMethodInformationImpl(loginContext
291 .getAuthenticationMethod(), new DateTime(), loginContext.getAuthenticationDuration());
292 shibSession.getAuthenticationMethods().put(authnMethodInfo.getAuthenticationMethod(), authnMethodInfo);
294 ServiceInformation serviceInfo = new ServiceInformationImpl(loginContext.getRelyingPartyId(), new DateTime(),
296 shibSession.getServicesInformation().put(serviceInfo.getEntityID(), serviceInfo);
298 shibSession.setLastActivityInstant(new DateTime());
300 returnToProfileHandler(loginContext, httpRequest, httpResponse);
304 * Gets the authentication method, currently active for the user, that also meets the requirements expressed by the
305 * login context. If a method is returned the user does not need to authenticate again, if null is returned then the
306 * user must be authenticated.
308 * @param loginContext user login context
309 * @param shibSession user's shibboleth session
311 * @return active authentication method that meets authentication requirements or null
313 protected AuthenticationMethodInformation getUsableExistingAuthenticationMethod(LoginContext loginContext,
314 Session shibSession) {
315 if (loginContext.getForceAuth() || shibSession == null) {
319 List<String> preferredAuthnMethods = loginContext.getRequestedAuthenticationMethods();
321 if (preferredAuthnMethods == null || preferredAuthnMethods.size() == 0) {
322 for (AuthenticationMethodInformation authnMethod : shibSession.getAuthenticationMethods().values()) {
323 if (!authnMethod.isExpired()) {
328 for (String preferredAuthnMethod : preferredAuthnMethods) {
329 if (shibSession.getAuthenticationMethods().containsKey(preferredAuthnMethod)) {
330 AuthenticationMethodInformation authnMethodInfo = shibSession.getAuthenticationMethods().get(
331 preferredAuthnMethod);
332 if (!authnMethodInfo.isExpired()) {
333 return authnMethodInfo;