View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.security.authentication;
20  
21  import javax.servlet.ServletRequest;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpSession;
25  
26  import org.eclipse.jetty.security.Authenticator;
27  import org.eclipse.jetty.security.IdentityService;
28  import org.eclipse.jetty.security.LoginService;
29  import org.eclipse.jetty.server.Request;
30  import org.eclipse.jetty.server.Response;
31  import org.eclipse.jetty.server.UserIdentity;
32  import org.eclipse.jetty.server.session.AbstractSession;
33  import org.eclipse.jetty.util.log.Log;
34  import org.eclipse.jetty.util.log.Logger;
35  
36  public abstract class LoginAuthenticator implements Authenticator
37  {
38      private static final Logger LOG = Log.getLogger(LoginAuthenticator.class);
39  
40      protected LoginService _loginService;
41      protected IdentityService _identityService;
42      private boolean _renewSession;
43  
44      protected LoginAuthenticator()
45      {
46      }
47  
48  
49      /* ------------------------------------------------------------ */
50      public UserIdentity login(String username, Object password, ServletRequest request)
51      {
52          UserIdentity user = _loginService.login(username,password);
53          if (user!=null)
54          {
55              renewSession((HttpServletRequest)request, (request instanceof Request? ((Request)request).getResponse() : null));
56              return user;
57          }
58          return null;
59      }
60  
61  
62      @Override
63      public void setConfiguration(AuthConfiguration configuration)
64      {
65          _loginService=configuration.getLoginService();
66          if (_loginService==null)
67              throw new IllegalStateException("No LoginService for "+this+" in "+configuration);
68          _identityService=configuration.getIdentityService();
69          if (_identityService==null)
70              throw new IllegalStateException("No IdentityService for "+this+" in "+configuration);
71          _renewSession=configuration.isSessionRenewedOnAuthentication();
72      }
73  
74      public LoginService getLoginService()
75      {
76          return _loginService;
77      }
78  
79      /** Change the session id.
80       * The session is changed to a new instance with a new ID if and only if:<ul>
81       * <li>A session exists.
82       * <li>The {@link AuthConfiguration#isSessionRenewedOnAuthentication()} returns true.
83       * <li>The session ID has been given to unauthenticated responses
84       * </ul>
85       * @param request
86       * @param response
87       * @return The new session.
88       */
89      protected HttpSession renewSession(HttpServletRequest request, HttpServletResponse response)
90      {
91          HttpSession httpSession = request.getSession(false);
92  
93          if (_renewSession && httpSession!=null)
94          {
95              synchronized (httpSession)
96              {
97                  //if we should renew sessions, and there is an existing session that may have been seen by non-authenticated users
98                  //(indicated by SESSION_SECURED not being set on the session) then we should change id
99                  if (httpSession.getAttribute(AbstractSession.SESSION_KNOWN_ONLY_TO_AUTHENTICATED)!=Boolean.TRUE)
100                 {
101                     if (httpSession instanceof AbstractSession)
102                     {
103                         AbstractSession abstractSession = (AbstractSession)httpSession;
104                         String oldId = abstractSession.getId();
105                         abstractSession.renewId(request);
106                         abstractSession.setAttribute(AbstractSession.SESSION_KNOWN_ONLY_TO_AUTHENTICATED, Boolean.TRUE);
107                         if (abstractSession.isIdChanged() && response != null && (response instanceof Response))
108                             ((Response)response).addCookie(abstractSession.getSessionManager().getSessionCookie(abstractSession, request.getContextPath(), request.isSecure()));
109                         LOG.debug("renew {}->{}",oldId,abstractSession.getId());
110                     }
111                     else
112                         LOG.warn("Unable to renew session "+httpSession);
113                     
114                     return httpSession;
115                 }
116             }
117         }
118         return httpSession;
119     }
120 }