View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.security.jaspi.modules;
15  
16  import java.io.IOException;
17  import java.util.Map;
18  
19  import javax.security.auth.Subject;
20  import javax.security.auth.callback.CallbackHandler;
21  import javax.security.auth.callback.UnsupportedCallbackException;
22  import javax.security.auth.message.AuthException;
23  import javax.security.auth.message.AuthStatus;
24  import javax.security.auth.message.MessageInfo;
25  import javax.security.auth.message.MessagePolicy;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.eclipse.jetty.http.HttpHeaders;
30  import org.eclipse.jetty.http.security.Constraint;
31  import org.eclipse.jetty.util.log.Log;
32  
33  /**
34   * @deprecated use *ServerAuthentication
35   * @version $Rev: 4660 $ $Date: 2009-02-25 17:29:53 +0100 (Wed, 25 Feb 2009) $
36   */
37  public class BasicAuthModule extends BaseAuthModule
38  {
39  
40      private String realmName;
41  
42      private static final String REALM_KEY = "org.eclipse.jetty.security.jaspi.modules.RealmName";
43  
44      public BasicAuthModule()
45      {
46      }
47  
48      public BasicAuthModule(CallbackHandler callbackHandler, String realmName)
49      {
50          super(callbackHandler);
51          this.realmName = realmName;
52      }
53  
54      @Override
55      public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, 
56                             CallbackHandler handler, Map options) 
57      throws AuthException
58      {
59          super.initialize(requestPolicy, responsePolicy, handler, options);
60          realmName = (String) options.get(REALM_KEY);
61      }
62  
63      @Override
64      public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, 
65                                        Subject serviceSubject) 
66      throws AuthException
67      {
68          HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
69          HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
70          String credentials = request.getHeader(HttpHeaders.AUTHORIZATION);
71  
72          try
73          {
74              if (credentials != null)
75              {
76                  if (Log.isDebugEnabled()) Log.debug("Credentials: " + credentials);
77                  if (login(clientSubject, credentials, Constraint.__BASIC_AUTH, messageInfo)) { return AuthStatus.SUCCESS; }
78  
79              }
80  
81              if (!isMandatory(messageInfo)) { return AuthStatus.SUCCESS; }
82              response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "basic realm=\"" + realmName + '"');
83              response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
84              return AuthStatus.SEND_CONTINUE;
85          }
86          catch (IOException e)
87          {
88              throw new AuthException(e.getMessage());
89          }
90          catch (UnsupportedCallbackException e)
91          {
92              throw new AuthException(e.getMessage());
93          }
94  
95      }
96  }