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.common.util.HttpHelper;
38 import edu.internet2.middleware.shibboleth.idp.profile.IdPProfileHandlerManager;
39 import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
40 import edu.internet2.middleware.shibboleth.idp.session.ServiceInformation;
41 import edu.internet2.middleware.shibboleth.idp.session.Session;
42 import edu.internet2.middleware.shibboleth.idp.session.impl.AuthenticationMethodInformationImpl;
43 import edu.internet2.middleware.shibboleth.idp.session.impl.ServiceInformationImpl;
46 * Manager responsible for handling authentication requests.
48 public class AuthenticationEngine extends HttpServlet {
50 /** Serial version UID. */
51 private static final long serialVersionUID = 8494202791991613148L;
54 private static final Logger LOG = Logger.getLogger(AuthenticationEngine.class);
57 * Gets the manager used to retrieve handlers for requests.
59 * @return manager used to retrieve handlers for requests
61 public IdPProfileHandlerManager getProfileHandlerManager() {
62 return (IdPProfileHandlerManager) getServletContext().getAttribute("handlerManager");
66 * Gets the session manager to be used.
68 * @return session manager to be used
70 @SuppressWarnings("unchecked")
71 public SessionManager<Session> getSessionManager() {
72 return (SessionManager<Session>) getServletContext().getAttribute("sessionManager");
76 * Returns control back to the authentication engine.
78 * @param httpRequest current http request
79 * @param httpResponse current http response
81 public static void returnToAuthenticationEngine(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
82 if (LOG.isDebugEnabled()) {
83 LOG.debug("Returning control to authentication engine");
85 HttpSession httpSession = httpRequest.getSession();
86 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
87 forwardRequest(loginContext.getAuthenticationEngineURL(), httpRequest, httpResponse);
91 * Returns control back to the profile handler that invoked the authentication engine.
93 * @param loginContext current login context
94 * @param httpRequest current http request
95 * @param httpResponse current http response
97 public static void returnToProfileHandler(LoginContext loginContext, HttpServletRequest httpRequest,
98 HttpServletResponse httpResponse) {
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("Returning control to profile handler at: " + loginContext.getProfileHandlerURL());
102 forwardRequest(loginContext.getProfileHandlerURL(), httpRequest, httpResponse);
106 * Forwards a request to the given path.
108 * @param forwardPath path to forward the request to
109 * @param httpRequest current HTTP request
110 * @param httpResponse current HTTP response
112 protected static void forwardRequest(String forwardPath, HttpServletRequest httpRequest,
113 HttpServletResponse httpResponse) {
115 RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(forwardPath);
116 dispatcher.forward(httpRequest, httpResponse);
118 } catch (IOException e) {
119 LOG.fatal("Unable to return control back to authentication engine", e);
120 } catch (ServletException e) {
121 LOG.fatal("Unable to return control back to authentication engine", e);
126 @SuppressWarnings("unchecked")
127 protected void service(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException,
129 if (LOG.isDebugEnabled()) {
130 LOG.debug("Processing incoming request");
133 if(httpResponse.isCommitted()){
134 LOG.error("HTTP Response already committed");
137 HttpSession httpSession = httpRequest.getSession();
138 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
139 if (loginContext == null) {
140 LOG.error("Incoming request does not have attached login context");
141 throw new ServletException("Incoming request does not have attached login context");
144 if (!loginContext.getAuthenticationAttempted()) {
145 String shibSessionId = (String) httpSession.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
146 Session shibSession = getSessionManager().getSession(shibSessionId);
148 if (shibSession != null) {
149 AuthenticationMethodInformation authenticationMethod = getUsableExistingAuthenticationMethod(
150 loginContext, shibSession);
151 if (authenticationMethod != null) {
152 if (LOG.isDebugEnabled()) {
153 LOG.debug("An active authentication method is applicable for relying party. "
154 + "Using authentication method " + authenticationMethod.getAuthenticationMethod()
155 + " as authentication method to relying party without re-authenticating user.");
157 authenticateUserWithActiveMethod(httpRequest, httpResponse, authenticationMethod);
161 if (LOG.isDebugEnabled()) {
162 LOG.debug("No active authentication method is applicable for relying party. "
163 + "Authenticating user with to be determined method.");
165 authenticateUserWithoutActiveMethod1(httpRequest, httpResponse);
167 if (LOG.isDebugEnabled()) {
168 LOG.debug("Request returned from authentication handler, completing authentication process.");
170 authenticateUserWithoutActiveMethod2(httpRequest, httpResponse);
177 * Completes the authentication request using an existing, active, authentication method for the current user.
179 * @param httpRequest current HTTP request
180 * @param httpResponse current HTTP response
181 * @param authenticationMethod authentication method to use to complete the request
183 protected void authenticateUserWithActiveMethod(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
184 AuthenticationMethodInformation authenticationMethod) {
185 HttpSession httpSession = httpRequest.getSession();
187 String shibSessionId = (String) httpSession.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
188 Session shibSession = getSessionManager().getSession(shibSessionId);
190 if (LOG.isDebugEnabled()) {
191 LOG.debug("Populating login context with existing session and authentication method information.");
193 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
194 loginContext.setAuthenticationDuration(authenticationMethod.getAuthenticationDuration());
195 loginContext.setAuthenticationInstant(authenticationMethod.getAuthenticationInstant());
196 loginContext.setAuthenticationMethod(authenticationMethod.getAuthenticationMethod());
197 loginContext.setPrincipalAuthenticated(true);
198 loginContext.setPrincipalName(shibSession.getPrincipalName());
200 ServiceInformation serviceInfo = new ServiceInformationImpl(loginContext.getRelyingPartyId(), new DateTime(),
201 authenticationMethod);
202 shibSession.getServicesInformation().put(serviceInfo.getEntityID(), serviceInfo);
204 returnToProfileHandler(loginContext, httpRequest, httpResponse);
208 * Performs the first part of user authentication. An authentication handler is determined, the login context is
209 * populated with some initial information, and control is forward to the selected handler so that it may
210 * authenticate the user.
212 * @param httpRequest current HTTP request
213 * @param httpResponse current HTTP response
215 protected void authenticateUserWithoutActiveMethod1(HttpServletRequest httpRequest,
216 HttpServletResponse httpResponse) {
217 HttpSession httpSession = httpRequest.getSession();
218 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
220 if (LOG.isDebugEnabled()) {
221 LOG.debug("Selecting appropriate authentication method for request.");
223 Pair<String, AuthenticationHandler> handler = getProfileHandlerManager().getAuthenticationHandler(
226 if (handler == null) {
227 loginContext.setPrincipalAuthenticated(false);
228 loginContext.setAuthenticationFailureMessage("No AuthenticationHandler satisfys the request from: "
229 + loginContext.getRelyingPartyId());
230 LOG.error("No AuthenticationHandler satisfys the request from relying party: "
231 + loginContext.getRelyingPartyId());
232 returnToProfileHandler(loginContext, httpRequest, httpResponse);
236 if (LOG.isDebugEnabled()) {
237 LOG.debug("Authentication method " + handler.getFirst() + " will be used to authenticate user.");
239 loginContext.setAuthenticationAttempted();
240 loginContext.setAuthenticationDuration(handler.getSecond().getAuthenticationDuration());
241 loginContext.setAuthenticationMethod(handler.getFirst());
242 loginContext.setAuthenticationEngineURL(HttpHelper.getRequestUriWithoutContext(httpRequest));
244 if (LOG.isDebugEnabled()) {
245 LOG.debug("Transferring control to authentication handler of type: "
246 + handler.getSecond().getClass().getName());
248 handler.getSecond().login(httpRequest, httpResponse);
252 * Performs the second part of user authentication. The principal name set by the authentication handler is
253 * retrieved and pushed in to the login context, a Shibboleth session is created if needed, information indicating
254 * that the user has logged into the service is recorded and finally control is returned back to the profile
257 * @param httpRequest current HTTP request
258 * @param httpResponse current HTTP response
260 protected void authenticateUserWithoutActiveMethod2(HttpServletRequest httpRequest,
261 HttpServletResponse httpResponse) {
262 HttpSession httpSession = httpRequest.getSession();
264 String principalName = (String) httpRequest.getAttribute(AuthenticationHandler.PRINCIPAL_NAME_KEY);
265 LoginContext loginContext = (LoginContext) httpSession.getAttribute(LoginContext.LOGIN_CONTEXT_KEY);
266 if (DatatypeHelper.isEmpty(principalName)) {
267 loginContext.setPrincipalAuthenticated(false);
268 loginContext.setAuthenticationFailureMessage("No principal name returned from authentication handler.");
269 LOG.error("No principal name returned from authentication method: "
270 + loginContext.getAuthenticationMethod());
271 returnToProfileHandler(loginContext, httpRequest, httpResponse);
274 loginContext.setPrincipalName(principalName);
275 loginContext.setAuthenticationInstant(new DateTime());
277 String shibSessionId = (String) httpSession.getAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE);
278 Session shibSession = getSessionManager().getSession(shibSessionId);
280 if (shibSession == null) {
281 if (LOG.isDebugEnabled()) {
282 LOG.debug("Creating shibboleth session for principal " + principalName);
287 addr = InetAddress.getByName(httpRequest.getRemoteAddr());
288 } catch (UnknownHostException ex) {
292 shibSession = (Session) getSessionManager().createSession(addr, loginContext.getPrincipalName());
293 loginContext.setSessionID(shibSession.getSessionID());
294 httpSession.setAttribute(Session.HTTP_SESSION_BINDING_ATTRIBUTE, shibSession.getSessionID());
297 if (LOG.isDebugEnabled()) {
298 LOG.debug("Recording authentication and service information in Shibboleth session for principal: "
301 AuthenticationMethodInformation authnMethodInfo = new AuthenticationMethodInformationImpl(loginContext
302 .getAuthenticationMethod(), new DateTime(), loginContext.getAuthenticationDuration());
303 shibSession.getAuthenticationMethods().put(authnMethodInfo.getAuthenticationMethod(), authnMethodInfo);
305 ServiceInformation serviceInfo = new ServiceInformationImpl(loginContext.getRelyingPartyId(), new DateTime(),
307 shibSession.getServicesInformation().put(serviceInfo.getEntityID(), serviceInfo);
309 shibSession.setLastActivityInstant(new DateTime());
311 returnToProfileHandler(loginContext, httpRequest, httpResponse);
315 * Gets the authentication method, currently active for the user, that also meets the requirements expressed by the
316 * login context. If a method is returned the user does not need to authenticate again, if null is returned then the
317 * user must be authenticated.
319 * @param loginContext user login context
320 * @param shibSession user's shibboleth session
322 * @return active authentication method that meets authentication requirements or null
324 protected AuthenticationMethodInformation getUsableExistingAuthenticationMethod(LoginContext loginContext,
325 Session shibSession) {
326 if (loginContext.getForceAuth() || shibSession == null) {
330 List<String> preferredAuthnMethods = loginContext.getRequestedAuthenticationMethods();
332 if (preferredAuthnMethods == null || preferredAuthnMethods.size() == 0) {
333 for (AuthenticationMethodInformation authnMethod : shibSession.getAuthenticationMethods().values()) {
334 if (!authnMethod.isExpired()) {
339 for (String preferredAuthnMethod : preferredAuthnMethods) {
340 if (shibSession.getAuthenticationMethods().containsKey(preferredAuthnMethod)) {
341 AuthenticationMethodInformation authnMethodInfo = shibSession.getAuthenticationMethods().get(
342 preferredAuthnMethod);
343 if (!authnMethodInfo.isExpired()) {
344 return authnMethodInfo;